Reputation: 39
I'm a noob to Java & I got stuck on a problem.
So the assignment is like this
There is one method required: getMax, which takes two integer variables as input, returns >the bigger one of the two. Your main method must look like the following (except the comment.
main( String[] args)
{
int num1, num2;
num1 = Integer.parseInt(args[0]);
num2 = Integer.parseInt(args[1]);
System.out.println(“the bigger value of the two is : “ + getMax(num1, num2));
}
You program may work like:
java Assign5 23 67
the bigger value of the two is 67
After a little struggle here is the little program I wrote.
import java.util.Scanner;
//use getMax to takes two numbers and returns bigger of the two
public class assignment5 {
private static void getMax(int a, int b ){
Scanner kevin = new Scanner(System.in);
System.out.println("Enter First Number:");
a = kevin.nextInt();
System.out.println("Enter Second Number:");
b = kevin.nextInt();
getMax(a, b); if (a > b) {
System.out.println(a + " is bigger"); }
else {
System.out.println(b + " is bigger");
}
}
}
but this is somehow very different from the actual assignment. I feel a bit stupid. Can someone help me with this?
Upvotes: 3
Views: 30536
Reputation: 14051
Well there are a lot of things to fix. First your getMax method is not well written (the way your main method is written you'll need to return the number and not just print a string):
public static int getMax(a, b){
if (a > b) {
return a;
}
return b;
}
Next thing you need to declare your main method:
public static void main(String[] args){
int num1, num2;
num1 = Integer.parseInt(args[0]);
num2 = Integer.parseInt(args[1]);
System.out.println(“the bigger value of the two is : “ + getMax(num1, num2));
}
args[0]
et args[1]
means that you have to give the numbers when you start your program and not wait for the user to enter them. When you'll launch your program, for example from your command line, it will look like this:
java -jar programname 23 54
or you can also specify your number in the run arguments if you use eclipse
Upvotes: 0
Reputation: 15683
Look at the example you have been given. The code to read the input is already in the main(String[] args)
method. Since your instructor has been kind enough to give you this part of the solution, use it. Programmers are always looking for ways to avoid re-inventing the wheel. Copying other people's working code is a good way to do that.
Now look at the line in your instructor's code where the actual work is done:
System.out.println("the bigger value of the two is : " + getMax(num1, num2));
This code is calling a method, getMax(num1, num2)
.
Your instructor should have told you how to write your own methods. You will need to do that here. Think about what type value your method will return. Think about what it will have to do internally to determine which of its two parameters, num1
and num2
is larger.
Write that method, put it into your Assignment5
class in the right place and test it. Always test your code. Untested code will fail and get lower marks.
When you have tested it and it is working correctly you can hand it in.
Upvotes: 2
Reputation: 10115
main
method in your assignment5
class such that it can be executed as requiredgetMax
so that it only returns a
or b
(the larger one) which it gets as input from your main methodUpvotes: 0
Reputation: 7074
You went in a wrong direction.
You tried to change the read mecanism of the two int whereas this part was given.
When you look at the exercise source, you see that the code calls "getMax(num1, num2)", which is not implemented. So, you have to implement a method with this signature:
public int getMax(int num1, int num2) {
// your code here
}
Upvotes: 5
Reputation: 3899
public static int getMax(int a, int b) {
return (a>b?a:b);
}
What happens if the numbers are equal?
Upvotes: 2