Results 1 to 10 of 10
- 01-24-2012, 02:17 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
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;
Java 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"); }
- 01-24-2012, 02:22 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
Re: Help needed with class assignment.
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.
- 01-24-2012, 02:24 AM #3
Re: Help needed with class assignment.
I have errors
- 01-24-2012, 02:27 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Help needed with class assignment.
I have errors on the total and the leftOver lines and on the last line.
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.
- 01-24-2012, 02:43 AM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 10
Re: Help needed with class assignment.
This:
Java Code:int total = numberOfEggs/12; int leftOver = numberOfEggs%12;
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.
- 01-24-2012, 02:43 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Re: Help needed with class assignment.
Last edited by DigDug; 01-24-2012 at 02:52 AM.
- 01-24-2012, 02:50 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
- 01-24-2012, 02:52 AM #8
Re: Help needed with class assignment.
Where do you call your second method?
- 01-24-2012, 02:59 AM #9
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
- 01-24-2012, 05:52 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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.)
Similar Threads
-
Java Programming University Assignment - Guidance needed
By jay89 in forum New To JavaReplies: 2Last Post: 01-19-2012, 08:43 PM -
Need help with Instrument Class assignment
By Kinney.j in forum New To JavaReplies: 0Last Post: 11-02-2011, 05:42 AM -
Help with implement class programming assignment
By ALH813 in forum EclipseReplies: 1Last Post: 10-02-2009, 02:35 AM -
Calculator Program HELP NEEDED FAST! Homework assignment
By SteroidalPsycho in forum New To JavaReplies: 3Last Post: 03-05-2009, 05:02 AM -
assignment problem help needed
By tiggz1980 in forum New To JavaReplies: 2Last Post: 02-07-2008, 12:14 AM
Bookmarks