Results 1 to 5 of 5
  1. #1
    dchaan is offline Member
    Join Date
    Jul 2015
    Posts
    2
    Rep Power
    0

    Default HELP! Java noob!

    This program is broken down into phases for your convenience only. Please turn in only the final phase. Before turning in your program, please make sure that it does something reasonable if the user enters a negative number the first time.

    Don't use arrays in this program.

    Important Hint: You will save yourself a lot of time and anguish if you follow the patterns and examples given in the lesson closely! Almost everything you need to do for this assignment can be found in an example in the lesson. Your primary job is figuring out how to put them together.

    Phase 1: Write a program for a theater that will keep track of how many people in each of 5 age categories attended a particular movie. Use the 5 age categories listed below in the sample screen output. The user will enter a number of ages, entering a negative number when there are no more ages to enter. Your program will then report on how many people in each age group attended. Sample screen output:

    Enter age of attendee (negative number to quit): 34
    Enter age of attendee (negative number to quit): 16
    Enter age of attendee (negative number to quit): 68
    Enter age of attendee (negative number to quit): 53
    Enter age of attendee (negative number to quit): 39
    Enter age of attendee (negative number to quit): 23
    Enter age of attendee (negative number to quit): 21
    Enter age of attendee (negative number to quit): -1

    age 0 to 18: 1
    age 19 to 30: 2
    age 31 to 40: 2
    age 41 to 60: 1
    over 60: 1
    Be sure to use final variables for the boundaries of the age groups.

    Phase 2: Modify your program so that, in addition to the report that the program currently produces, it also gives the average age of the people in attendance, the age of the oldest person in attendance, and the age of the youngest person in attendance. You may not assume any maximum age of attendees!

    Don't forget to look at the examples in lesson 4 for help with this!

    Sample screen output:

    Enter age of attendee (negative number to quit): 34
    Enter age of attendee (negative number to quit): 16
    Enter age of attendee (negative number to quit): 68
    Enter age of attendee (negative number to quit): 53
    Enter age of attendee (negative number to quit): 39
    Enter age of attendee (negative number to quit): 23
    Enter age of attendee (negative number to quit): 21
    Enter age of attendee (negative number to quit): -5

    age 0 to 18: 1
    age 19 to 30: 2
    age 31 to 40: 2
    age 41 to 60: 1
    over 60: 1

    The average age was 36.
    The youngest person in attendance was 16.
    The oldest person in attendance was 68.
    Phase 3: Modify your program so that it also asks for each attendee's food preference ('p' for popcorn, 's' for soda, 'b' for both). Your program should then report this data as well.

    Hint: The lessons contain an example of a loop like this where two questions are asked. Follow the pattern in that example.

    Sample screen output:

    Enter age of attendee (negative number to quit): 34
    Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): 's'
    Enter age of attendee (negative number to quit): 16
    Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): 'b'
    Enter age of attendee (negative number to quit): 68
    Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): 'b'
    Enter age of attendee (negative number to quit): 53
    Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): 's'
    Enter age of attendee (negative number to quit): 39
    Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): 'p'
    Enter age of attendee (negative number to quit): 23
    Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): 's'
    Enter age of attendee (negative number to quit): 21
    Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): 's'
    Enter age of attendee (negative number to quit): -1

    age 0 to 18: 1
    age 19 to 30: 2
    age 31 to 40: 2
    age 41 to 60: 1
    over 60: 1

    food preference popcorn: 1
    food preference soda: 4
    food preference both: 2
    The average age was 36.
    The youngest person in attendance was 16.
    The oldest person in attendance was 68.
    Your program should do something reasonable if no ages are entered (i.e. the user enters a negative number the first time).

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: HELP! Java noob!

    Ok, so you dumped your homework here; are you happy now?

    kind regards,

    Jos

    ps. I deleted your other (identical) thread; don't double post.
    Last edited by JosAH; 07-04-2015 at 11:29 AM.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    dchaan is offline Member
    Join Date
    Jul 2015
    Posts
    2
    Rep Power
    0

    Default Re: HELP! Java noob!

    I have started it but I am stuck with phase 2. It does not output what it supposed to be outputted

    import java.util.Scanner;

    public class Theater {
    public static void main(String[] args)
    {
    Scanner input = new Scanner( System.in);

    int age = 0;
    int age1 = 0; // age group 0-18
    int age2 = 0; // age group 19-30
    int age3 = 0; // age group 31-40
    int age4 = 0; // age group 41-60
    int age5 = 0; // age group over 60
    int count;
    int sumSoFar;
    int smallest;

    sumSoFar = 0;
    count = 0;
    System.out.print("Enter age of attendee (negative number to quit): ");
    age = input.nextInt();
    smallest = age;

    while( age >= 0)
    {
    sumSoFar += age;
    System.out.print("Enter age of attendee (negative number to quit): ");
    age = input.nextInt();
    count++;

    if (age >= 0 && age <= 18)
    age1 += 1;
    if (age >= 19 && age <= 30)
    age2 += 1;
    if (age >= 31 && age <= 40)
    age3 += 1;
    if (age >= 41 && age <= 60)
    age4 += 1;
    if (age > 60)
    age5 += 1;
    }
    System.out.printf("\nage 0 to 18: %d\nage 19 to 30: %d\nage 31 to 40: %d\nage 41 to 60: %d\nover 60: %d\n", age1, age2, age3, age4, age5);

    if (count == 0){
    System.out.println("There is no average, because you did not "
    + "enter any numbers!");
    } else {
    System.out.println("The average is " + sumSoFar/count);
    }
    while ( age >= 0){
    if (age < smallest){
    smallest = age;
    }
    System.out.println("Enter age of attendee (negative number to quit): ");
    age = input.nextInt();
    }
    if (smallest < 0){
    System.out.println("There is no smallest because you "
    + "did not enter any numbers!");
    } else {
    System.out.println("The smallest number you entered was "
    + smallest);
    }
    }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: HELP! Java noob!

    Please edit your post and wrap the code in code tags to make it more readable.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: HELP! Java noob!

    And explain what it is outputting now and what you think is wrong with that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Another noob to Java
    By roggoblue in forum New To Java
    Replies: 3
    Last Post: 02-06-2013, 09:52 AM
  2. Help with GUI - Java NOOB :(
    By bdelarosa7 in forum New To Java
    Replies: 3
    Last Post: 11-22-2011, 09:54 PM
  3. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 AM
  4. Hello, Java Noob
    By furry in forum Introductions
    Replies: 0
    Last Post: 10-07-2008, 07:39 PM
  5. Ah! Help a Java Noob
    By Snejana in forum New To Java
    Replies: 4
    Last Post: 01-24-2008, 03:52 AM

Posting Permissions

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