Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Program that takes in family members calculate family's average age and prints out ai

  1. #1
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Program that takes in family members calculate family's average age and prints out ai

    i am to design a program in java that will allow a user to Input a list of your family members along with their age and state where they reside. Determine and print the average age of your family and print the names of anyone who live in Texas. I have tried but my results are wrong. please help. thanks

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Program that takes in family members calculate family's average age and prints ou

    Can you post the code that you have tried so far?

  3. #3
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    here is the code
    Java Code:
    import java.util.Scanner;
    
    public class State
    {
       public static void main(String[] args)
       {
          Scanner input = new Scanner (System.in);
          
          int age;
          String name;
          String state;
          int averageAge;
          
          
          System.out.print("Enter family member's name (or 'quit' to exit)");
          name = input.next();
          
          while (!name.equals("quit"))
          {
             System.out.print("Enter family member's age");
             age = input.nextInt();
             System.out.print("Enter family member's state");
             state = input.next();
             
             averageAge =age + age;//My problem at this level, im unable to calculate the aver
             System.out.printf("the familys' average age is %d\n", averageAge);
             if (state.equals ("texas"))
               {System.out.printf ("%s lives in texas\n", name);}
               
    		   System.out.print("Enter family member's name (or 'quit' to exit)");
               name = input.next();
          }
       }
       
    }

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Program that takes in family members calculate family's average age and prints ou

    You'd want to keep a tally of the number of family members and their total age. Use a variable for each of these; declare them outside the while loop so that you don't overwrite them with each iteration in the loop. Update these variables in the while loop with data received from the user's input, and then calculate the average using values from the variables.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Program that takes in family members calculate family's average age and prints ou

    To make your problem easier to solve you can divide your problem into a smaller peace.

    1. Create a Person class that have attribute such as name, age and state.
    2. Create an array / List that enables to stores many of this Person object.
    3. Create a loop that will ask you for all information of the family members (name, age and state). With this information you can create a Person object and store it in an array / Array.
    4. After all information gathered you can analyze the array to calculate the average of the age and find someone who lives in Texas.
    bennzk4 likes this.

  6. #6
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    please how can i do that?

  7. #7
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    i will appreciate it if you give an example code. thanks

  8. #8
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    this is what i have so far but i still get errors
    Java Code:
    public class State1
    {
    	public static void main(String[] args)
    	{
    		int x = 0;
    		int count=0;
    		float sum;
    		float average;
    		int age;
    		string name;
    		string state;
    		
    		System.out.print("Enter the family member's name. Type 'done' when complete.");
    		name = input.next();
    		do
    		{
    			System.out.print( "Enter family member's age");
    			age = input.nextInt();
    			System.out.print("Enter state where family member resides");
    			state = input.next();
    			
    			System.out.print("Enter the family member's name. Type 'done' when complete.");
    			name = input.next();
    			count++;
    			
    		} while (!name.equals("done") && count != 10);
    
    		sum = 0.0;
    		average = 0.0;
    		for ( x=0;x<count;x++)
    		{
    			sum = sum + age[x];
    		}
    		average = sum/count;
    		System.out.printf("The average age of your family is %d", average);
    		for ( x=0; x < count ;x++)
    		{
    			if (state.equals("texas"))
    			System.out.printf("%s lives in texas", name);
    		}
    	}
    }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Program that takes in family members calculate family's average age and prints ou

    i still get errors
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    this is an image of the 7 errors i get when i try compiling
    Program that takes in family members calculate family's average age and prints out ai-untitled.png

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Program that takes in family members calculate family's average age and prints ou

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    ok here is what it says

    Microsoft Windows [Version 6.3.9600]
    (c) 2013 Microsoft Corporation. All rights reserved.

    C:\Users\K4-BENZ>cd desktop

    C:\Users\K4-BENZ\Desktop>javac State1.java
    State1.java:15: error: cannot find symbol
    name = input.next();
    ^
    symbol: variable input
    location: class State1
    State1.java:19: error: cannot find symbol
    age = input.nextInt();
    ^
    symbol: variable input
    location: class State1
    State1.java:21: error: cannot find symbol
    state = input.next();
    ^
    symbol: variable input
    location: class State1
    State1.java:24: error: cannot find symbol
    name = input.next();
    ^
    symbol: variable input
    location: class State1
    State1.java:29: error: possible loss of precision
    sum = 0.0;
    ^
    required: float
    found: double
    State1.java:30: error: possible loss of precision
    average = 0.0;
    ^
    required: float
    found: double
    State1.java:33: error: array required, but int found
    sum = sum + age[x];
    ^
    7 errors

    C:\Users\K4-BENZ\Desktop>

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Program that takes in family members calculate family's average age and prints ou

    cannot find symbol
    That error means that the compiler can not find a definition for the variable(symbol) named in the error message.
    Look at the error message, get the name of the variable and make sure that it is defined in scope with where it is being used.
    In scope means the definition and usage are within the same pair of { }s
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    updated my code but still get the following errors

    C:\Users\K4-BENZ\Desktop>javac State1.java
    State1.java:19: error: incompatible types
    name = input.next();
    ^
    required: String[]
    found: String
    State1.java:25: error: incompatible types
    state = input.next();
    ^
    required: String[]
    found: String
    State1.java:28: error: incompatible types
    name = input.next();
    ^
    required: String[]
    found: String
    State1.java:37: error: array required, but int found
    sum = sum + age[x];
    ^
    State1.java:44: warning: non-varargs call of varargs method with inexact argumen
    t type for last parameter;
    System.out.printf("%s lives in texas", name);
    ^
    cast to Object for a varargs call
    cast to Object[] for a non-varargs call and to suppress this warning
    4 errors
    1 warning

    C:\Users\K4-BENZ\Desktop>

    here is the code
    Java Code:
    import java.util.Scanner;
    
    public class State1
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner (System.in);
    		
    		int x = 0;
    		int found = 0;
    		int count=0;
    		float sum;
    		float average;
    		int age;
    		String[] name = new String[10];
    		String[] state = new String[10];
    		
    		System.out.print("Enter the family member's name. Type 'done' when complete.");
    		name = input.next();
    		do
    		{
    			System.out.print( "Enter family member's age");
    			age = input.nextInt();
    			System.out.print("Enter state where family member resides");
    			state = input.next();
    			
    			System.out.print("Enter the family member's name. Type 'done' when complete.");
    			name = input.next();
    			count++;
    			
    		} while (!name.equals("done") && count != 10);
    
    		sum = 0.0f;
    		average = 0.0f;
    		for ( x=0;x<count;x++)
    		{
    			sum = sum + age[x];
    		}
    		average = sum/count;
    		System.out.printf("The average age of your family is %d", average);
    		for ( x=0; x < count ;x++)
    		{
    			if (state.equals("texas"))
    			System.out.printf("%s lives in texas", name);
    		}
    	}
    }

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Program that takes in family members calculate family's average age and prints ou

    error: incompatible types
    The type of the value on the righthand side of the = can NOT be converted to the type of the variable on the lefthand side of the =. Read the API doc for the class and method on the righthand side, get the type that the method returns and make sure the variable on the left of the = is that type.

    array required, but int found
    The compiler expects the name of an array to be used with array notation: [i]
    It found an int variable: age, not the name of an array.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    please can you give an example code?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Program that takes in family members calculate family's average age and prints ou

    What code are you asking about? Please be specific and describe what line of code you are trying to write.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    bennzk4 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Program that takes in family members calculate family's average age and prints ou

    the corrections that you stated please

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Program that takes in family members calculate family's average age and prints ou

    Please post the source line here and my description of what was wrong with it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 10-09-2013, 11:11 PM
  2. Replies: 4
    Last Post: 02-15-2011, 11:05 PM

Tags for this Thread

Posting Permissions

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