Help needed with class assignment.
I have a class assignment that for some reason I just can't work out...If I could get help to get this done...it would be much appreciated.
The problem is this:
Create a class names Eggs. it's main() method holds an integer variable named numberOfEggs to which you will assign a value entered by a user at he keyboard. Create a method to which you pass numberOfEggs. The method displays the eggs in dozens.
import java.util.Scanner;
Code:
public class eggs {
public static void main(String[] args) {
// TODO Auto-generated method stub
int numberOfEggs;
Scanner input = new Scanner(System.in);
System.out.print("How Many Eggs were collected? ");
numberOfEggs= input.nextInt();
int total = numberOfEggs/12;
int leftOver = numberOfEggs%12;
}
public void totalEggs(int numberOfEggs) {
System.out.print("You collected " +
numOfEggs + "Which makes " + total + "egg cartons" + "with" + leftOver + "extra eggs");
}
I have errors on the total and the leftOver lines and on the last line. What am I missing?
Re: Help needed with class assignment.
Quote:
Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
http://docs.oracle.com/javase/tutori...variables.html
Re: Help needed with class assignment.
Please copy and paste here the full text of the error messages.
Re: Help needed with class assignment.
Quote:
I have errors on the total and the leftOver lines and on the last line.
Could you post the full compiler messages?
Also. notice that your assignment asks you to "Create a method to which you pass numberOfEggs. The method displays the eggs in dozens". At the moment you are trying to figure out the dozens in the main() method which is the wrong place: let the totalEggs() method do the calculation.
Re: Help needed with class assignment.
Quote:
Originally Posted by
DigDug
I have a class assignment that for some reason I just can't work out...If I could get help to get this done...it would be much appreciated.
The problem is this:
Create a class names Eggs. it's main() method holds an integer variable named numberOfEggs to which you will assign a value entered by a user at he keyboard. Create a method to which you pass numberOfEggs. The method displays the eggs in dozens.
I have errors on the total and the leftOver lines and on the last line. What am I missing?
This:
Code:
int total = numberOfEggs/12;
int leftOver = numberOfEggs%12;
should be in method totalEggs(int numberOfEggs).
Otherwise, total and leftOver are local variables of class egg main method.
So, when You want result, You have to call method totalEggs(int numberOfEggs) from main method.
And on line 21, You have numOfEggs and there is no such variable.
3 Attachment(s)
Re: Help needed with class assignment.
pbrockway and diamondragon...thanks...I tried it there earlier...but thought I was doing it wrong there as well...I'll try that. As for the errors...I'm using Eclipse and it just gives me these flags:
Attachment 2731Attachment 2732Attachment 2733
I am working on changing the code I have to what you sugested...all of you...thanks SOOO much!
1 Attachment(s)
Re: Help needed with class assignment.
ohh...that cleared up a lot....but for some reason it isn't running my second method:
Attachment 2734
Did I miss something else?
Re: Help needed with class assignment.
Where do you call your second method?
Re: Help needed with class assignment.
Quote:
Originally Posted by
Norm
Where do you call your second method?
Yeah...I guess I did forget that....I am so new at this...to be honest...I am really not too sure of what I'm doing let alone if I'm doing it right. :S
Re: Help needed with class assignment.
You're doing OK. Don't worry too much about not doing things right to begin with.
In main() "call" the method you have written. Ask if you are unsure what this means. (Or consult your textbook!;)
-----
Those "errors" you got before in main() weren't errors at all. What was happening was that Eclipse noticed that you had declared total and leftOver and assigned values to them, but... you never used them anywhere. The IDE was suggesting things like removing them. It's good that Eclipse picks this sort of thing up as it indicates a problem of some sort (in your case that you were attempting to do the calculation in the wrong place.)