Results 1 to 10 of 10
  1. #1
    DigDug is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default 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");
    		
    	}
    I have errors on the total and the leftOver lines and on the last line. What am I missing?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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.
    http://docs.oracle.com/javase/tutori...variables.html

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help needed with class assignment.

    I have errors
    Please copy and paste here the full text of the error messages.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help needed with class assignment.

    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.

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Help needed with class assignment.

    Quote Originally Posted by DigDug View Post
    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:

    Java 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.

  6. #6
    DigDug is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default 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:

    Help needed with class assignment.-1.jpgHelp needed with class assignment.-2.jpgHelp needed with class assignment.-3.jpg

    I am working on changing the code I have to what you sugested...all of you...thanks SOOO much!
    Last edited by DigDug; 01-24-2012 at 01:52 AM.

  7. #7
    DigDug is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help needed with class assignment.

    ohh...that cleared up a lot....but for some reason it isn't running my second method:

    Help needed with class assignment.-4.jpg

    Did I miss something else?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help needed with class assignment.

    Where do you call your second method?

  9. #9
    DigDug is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help needed with class assignment.

    Quote Originally Posted by Norm View Post
    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

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  1. Replies: 2
    Last Post: 01-19-2012, 07:43 PM
  2. Need help with Instrument Class assignment
    By Kinney.j in forum New To Java
    Replies: 0
    Last Post: 11-02-2011, 04:42 AM
  3. Replies: 1
    Last Post: 10-02-2009, 01:35 AM
  4. Calculator Program HELP NEEDED FAST! Homework assignment
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 03-05-2009, 04:02 AM
  5. assignment problem help needed
    By tiggz1980 in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 11:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •