Question

In: Computer Science

Following is hw14. Save the demo filename as lastname_hw14.java and the class name as lastname_people.java. Following...

Following is hw14. Save the demo filename as lastname_hw14.java and the class name as lastname_people.java.

Following are three arrays each containing 50 values. Store all three arrays in the main method. Write an object to transfer the three arrays.

int []age[] = {70, 68, 52, 69, 40, 59, 61, 34, 45, 50, 43, 22, 35, 50, 67, 33, 36, 22, 63, 65, 56, 31, 55, 28, 30, 24, 55, 35, 39, 59, 68, 50, 33, 45, 26, 54, 44, 56, 58, 24, 69, 56, 65, 52, 20, 23, 37, 27, 69, 35};

int []num_children = {2, 4, 3, 3, 4, 2, 3, 5, 5, 4, 4, 1, 5, 1, 4, 5, 4, 2, 2, 1, 2, 4, 3, 2, 3, 0, 5, 0, 0, 1, 3, 1, 4, 1, 0, 4, 0, 2, 2, 1, 3, 5, 1, 0, 0, 1, 1, 2, 1, 4};

int []weekly_pay = {1600, 950, 400, 1450, 1250, 1350, 2450, 750, 1700, 700, 1200, 1550, 1450, 450, 2600, 1550, 400, 850, 400, 600, 700, 1300, 2050, 900, 2250, 450, 1900, 800, 2500, 1500, 1200, 2900, 1100, 1700, 900, 1000, 750, 2200, 2600, 1300, 2150, 450, 1250, 1750, 800, 2850, 2150, 750, 2350, 1100};

1) In the class file, write a method called method1 to compute the average age.

2) In the class file, write a method called method2 to compute the average age of those who have exactly 3 children.

3) In the class file, write a method called metho3 to compute the lowest age of those making $1501 to $2500 per week.

Print the answers to all three parts from within the main method (lastname_hw14.java).

java

Solutions

Expert Solution

Please find your solution below and if any doubt comment and do upvote.

CODE:

public class lastname_people
{
    //method1 to compute the average age.
    public  static double method1(int age[]){
        int sum=0;
        for(int i=0;i<50;i++)
        {
            sum+=age[i];
        }
        return sum/50;
        
    }
    //method2 to compute the average age of those who have exactly 3 children.
    public static double method2(int age[],int num_children[]){
        int sum=0;
        int count=0;
        for(int i=0;i<50;i++)
        {
            if(num_children[i]==3)
            {
                sum+=age[i];
                count++;
            }
        }
        return sum/count;
    }
    //method3 to compute the lowest age of those making $1501 to $2500 per week.
    public  static int  method3(int []age,int weekly_pay[]){
        int lowestAge=100000;
        for(int i=0;i<50;i++)
        {
            if(weekly_pay[i]>=1501&&weekly_pay[i]<=2500){
                if(lowestAge>age[i])
                {
                    lowestAge=age[i];
                }
            }
        }
        return lowestAge;
    }
        public static void main(String[] args) {
            int []age = {70, 68, 52, 69, 40, 59, 61, 34, 45, 50, 43, 22, 35, 50, 67, 33, 36, 22, 63, 65, 56, 31, 55, 28, 30, 24, 55, 35, 39, 59, 68, 50, 33, 45, 26, 54, 44, 56, 58, 24, 69, 56, 65, 52, 20, 23, 37, 27, 69, 35};
            int []num_children = {2, 4, 3, 3, 4, 2, 3, 5, 5, 4, 4, 1, 5, 1, 4, 5, 4, 2, 2, 1, 2, 4, 3, 2, 3, 0, 5, 0, 0, 1, 3, 1, 4, 1, 0, 4, 0, 2, 2, 1, 3, 5, 1, 0, 0, 1, 1, 2, 1, 4};
            int []weekly_pay = {1600, 950, 400, 1450, 1250, 1350, 2450, 750, 1700, 700, 1200, 1550, 1450, 450, 2600, 1550, 400, 850, 400, 600, 700, 1300, 2050, 900, 2250, 450, 1900, 800, 2500, 1500, 1200, 2900, 1100, 1700, 900, 1000, 750, 2200, 2600, 1300, 2150, 450, 1250, 1750, 800, 2850, 2150, 750, 2350, 1100};
            //prints the output return from the functions
            double average=method1(age);
            System.out.println("The average age is "+ average);
                double avgOf3Child=method2(age,num_children);
                System.out.println("The average age of exactly 3 children is "+avgOf3Child);
                int lowest=method3(age,weekly_pay);
                System.out.println("The lowest age is "+ lowest);
        }
}

OUTPUT:


Related Solutions

Requirements1. Name the java class (and filename) Week3_StringLab .2. Print a welcoming message. 3. Ask user...
Requirements1. Name the java class (and filename) Week3_StringLab .2. Print a welcoming message. 3. Ask user to enter a string. Use the nextLine() input method, 4. Use that string to test out at least 6 String methods, from the String class. 5. Of those 6, you must use these 3 methods: .split(), .indexOf (int ch), .subString (int beginningIndex). 6. Use 3+ other methods that you wish to try. 7. Print out the string BEFORE using each method .8. Have the...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
Required Names: Main class: StoreProgram Java filename: StoreProgram.java StoreProgram This project will be a program that...
Required Names: Main class: StoreProgram Java filename: StoreProgram.java StoreProgram This project will be a program that provides information about inventory in a grocery store. Use this codeas your starting point. This code already has a main method written for you. DO NOT modify the main method. If you modify the main method, you will lose points. In the main method, you will notice three arrays: A String array representing the items in the store by name. An integer array representing...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Write a Java class. The class name must be ShapeMetrics, which means the file name must...
Write a Java class. The class name must be ShapeMetrics, which means the file name must be ShapeMetrics.java. Provide the following functions: 1. getAreaOfRectangle(), with two float arguments, width and height, in that order, returning a float value which is the area of the rectangle with that width and height. 2. getSpaceDiagonalOfRectangularCuboid (), with three float arguments, width, height, and depth, in that order, returning a float value which is the length of the diagonal line which bisects the cuboid....
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
Define a JAVA class ISP. The class consists of the name of the subscription plan and...
Define a JAVA class ISP. The class consists of the name of the subscription plan and a method that display the plan. Derive a class DPlan from ISP. The DPlan will charge RM10 per Mbps subscribe and RM0.20 per GB used. Derive a class MPlan from ISP. The MPlan will charge RM5 per Mbps subscribe and RM0.80 per GB used. Display the plan and select the best plan.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT