1st Java class assignment, and having some difficulties. Little help please.
So I started my first Java class last week, and Ia m having problems. The question I have in my assignment is;
"Create an application named TestMethods whose main() methods holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayIt(), displayItTimesTwo(), and displatItPlusOneHundred(). Create each method to perform the task its name implies. Save the application as TestMethods.java."
So this is what I have so far...
public class TestMethods
{
public static void main(String[] args)
{
int myVariable1 = 10;
int myVariable2 = 27;
System.out.println(displayIt (myVariable1, myVariable2));
}
public static void displayIt(int myVariable1, int myVariable2)
{
System.out.println("My two variables are " + myVariable1 + " and " + myVariable2);
}
}
I keep getting "void type not allowed here' on line 16 which is bold. What am I doing wrong here? This is my first assignment, and it has been frustrating. I know it's an easy question, but I cannot get it in to code for the very first part. Any help is greatly appreciated.
Re: 1st Java class assignment, and having some difficulties. Little help please.
Hello, flpanthers1. The reason you are getting this error is because the System.out.println method has a parameter of type 'String', and therefore a method declared as type void cannot be invoked. Try changing the method 'displayIt' so that it is declared as a String. (this will require a return statement)
Also, while using variables that don't have a value over 128, or below -127 it is recommend to use the 'byte' keyword instead of the 'int' keyword. This is to save memory as an integer is a 32-bit integer, and a byte is an 8-bit integer.
You can learn more about primitive data types here:
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
You can learn more about methods and parameters here:
http://download.oracle.com/javase/tu...O/methods.html
Re: 1st Java class assignment, and having some difficulties. Little help please.
Instead of passing the call to displayIt to println, call displayIt directly. The displayIt method calls println to do the printing.
Don't work about saving a few bytes of memory. Using int can be more efficient and you'll likely have fewer headaches when you change the values in the numbers to be too large for bytes.
Re: 1st Java class assignment, and having some difficulties. Little help please.
Ok, so I changed this up a little, but I cannot figure out why I am getting this...
My first variable is 10
10
My second variable is 27
27
Process completed.
I realize it has something to do with the return I put in, but I really stumbled across the solution. to be honest, when I saw "Process Completed" I was happy it had no errors. But of course you see the extra '10' and '27'. not sure why this is either. I appreciate your patience.
public class TestMethods
{
public static void main(String[] args)
{
int myVariable1 = 10;
int myVariable2 = 27;
System.out.println(displayIt (myVariable1, myVariable2));
System.out.println(displayIt2 (myVariable1, myVariable2));
}
public static int displayIt(int myVariable1, int myVariable2)
{
System.out.println("My first variable is " + myVariable1);
int newAmount;
newAmount = myVariable1;
return newAmount;
}
public static int displayIt2(int myVariable1, int myVariable2)
{
System.out.println("My second variable is " + myVariable2);
int newAmount;
newAmount = myVariable2;
return newAmount;
}
}
Re: 1st Java class assignment, and having some difficulties. Little help please.
You code does double printing. The displayIt methods print things AND return data that is printed by the caller. You need to figure where you want the data printed and only print it there. Only do the print in one place! If you return a value to be printed, do NOT print it before returning it.
Re: 1st Java class assignment, and having some difficulties. Little help please.
I see what you mean, but every time I remove something, or add it I still get errors. It's making me nuts, and I know this is simple. I see the two println's in there, but I delete one, and I get errors.... Delete the other, and it still does not work. This is what I have now with the result at the bottom. I am only supposed to have the one displayIt method, so I had to delete the displayIt2
public class TestMethods
{
public static void main(String[] args)
{
int myVariable1 = 10;
int myVariable2 = 27;
System.out.println(displayIt (myVariable1, myVariable2));
}
public static int displayIt(int myVariable1, int myVariable2)
{
System.out.println("My first variable is " + myVariable1);
System.out.println("My second variable is " + myVariable2);
int newAmount;
newAmount = myVariable2;
return newAmount;
}
}
Result:
My first variable is 10
My second variable is 27
27
Process completed.
Re: 1st Java class assignment, and having some difficulties. Little help please.
Quote:
Originally Posted by
Karl
Also, while using variables that don't have a value over 128, or below -127 it is recommend to use the 'byte' keyword instead of the 'int' keyword. This is to save memory as an integer is a 32-bit integer, and a byte is an 8-bit integer.
With todays modern hardware (and the excessive amounts of RAM available) you won't be saving much memory by doing that. I suggest staying away from bytes and shorts (unless you have a very good reason to use them) and only use ints and longs to represent integers.
Re: 1st Java class assignment, and having some difficulties. Little help please.
Quote:
Originally Posted by
Karl
Also, while using variables that don't have a value over 128, or below -127 it is recommend to use the 'byte' keyword instead of the 'int' keyword. This is to save memory as an integer is a 32-bit integer, and a byte is an 8-bit integer.
This is only really relevant where very large arrays are involved - and on retrieval from a byte array, all bytes are sign-extended to integers and any manipulation is done with these integer values (see VM Spec: baload).
Re: 1st Java class assignment, and having some difficulties. Little help please.
Quote:
I still get errors.
Step thru your code line by line and play computer. See when each println is called and with what values. When a method is called as an argument to another method, the called method executes first and then can return a value that will be used by the calling method.
Re: 1st Java class assignment, and having some difficulties. Little help please.
You might want to seek clarification but I interpret the instructions as each method only accepts a single parameter, the value to be displayed. So you create 2 variables and then call displayIt with var 1 and then call displayIt with var 2.
Re: 1st Java class assignment, and having some difficulties. Little help please.
The requirements imply that the printing should be done inside the displayIt.. methods (as the method names also suggest), so these methods do not need to return a value - after all, the only value they have is the one passed to them, so there's no point returning it. In other words, declare the displayIt.. methods as 'void' and just call them directly, don't call them from inside a System.out.println(..) call.