Results 1 to 3 of 3
  1. #1
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default Help with program class?

    Create a class named Eggs. Its main() method holds an integer variable named eggs to which you will assign a value entered by a user at the keyboard. Create a method to which you pass eggs. The method displays the eggs in dozens; for example, 50 eggs is 4 full dozen(with 2 eggs remaining).


    Am I close with this? Can you point me in the right direction please?


    //Import necessary packages
    import java.util.Scanner;

    public class Eggs
    {
    public static void main (String args[])
    {

    //Declare variables
    int eggs;
    Scanner input = new Scanner(System.in);

    //Get user input
    System.out.print("How many eggs do you have? ");
    eggs = input.nextInt();
    int full = eggs/12;
    remaining = eggs%12;

    }
    public class calculateEggs(int eggs);
    {
    System.out.print("You have " + full + "eggs" + "with" + remaining + "eggs remaining");

    }
    }

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

    Default

    Does that compile? If not and you cannot understand the compiler message, post it and maybe someone can explain it.

    Read the question closely. You are asked to "Create a method to which you pass eggs" you weren't asked to create any more classes. So

    Java Code:
    public class calculateEggs(int eggs);
    {
        System.out.print(" // etc
    should probably be

    Java Code:
    static void displayCount(int eggs)
    {
        System.out.print(" // etc

  3. #3
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Default

    Hi,
    That program will not compile. When you attempt to run it you will get a compilation error message:
    Java Code:
    '{' expected
    Java Code:
    //Import necessary packages
    import java.util.Scanner;
    
    public class Eggs
    {
    public static void main (String args[])
    {
    
    //Declare variables
    int eggs;
    Scanner input = new Scanner(System.in);
    
    //Get user input
    System.out.print("How many eggs do you have? ");
    eggs = input.nextInt();
    int full = eggs/12;
    remaining = eggs%12;
    
    }
    public class calculateEggs(int eggs);
    {
    System.out.print("You have " + full + "eggs" + "with" + remaining + "eggs remaining");
    
    }
    }
    As someone has already mentioned, the line of code 'public class calculateEggs(int eggs);' is not necessary. When you comment that line out and run your code again, you get another error message 'cannot find symbol - variable remaining'. Which means you do not really need to create another class for calculateEggs();

    I think a bit of modification to you code will do the task.

Similar Threads

  1. Replies: 6
    Last Post: 05-18-2010, 05:11 PM
  2. Could not find the main class, program will exit.
    By aryubi in forum New To Java
    Replies: 39
    Last Post: 02-19-2010, 10:02 AM
  3. Replies: 3
    Last Post: 10-06-2009, 08:14 PM
  4. Replies: 4
    Last Post: 12-29-2008, 02:20 PM
  5. clock class program
    By jvasilj1 in forum New To Java
    Replies: 12
    Last Post: 02-01-2008, 08:27 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
  •