Question

Mike is a consultant for the Department of Labor. One day he is analyzing the midyear...

Mike is a consultant for the Department of Labor. One day he is analyzing the midyear

population of the United States, in thousands, during the years 1950 through 1990. He write

Population Class in java including a main method as the testing client. His next task is to

complete the entire class by adding:

1. A private field that can store a yearly population data as an integer array.

2. A constructor that can instantiate a Population object by using an external integer

array as the parameter.

3. A method that can return the average annual change in population during the time

period.

4. A method that can return the year with the greatest increase in population during the

time period

5. A method that can return the year with the smallest increase in population during the

time period

6. Some testing procedures in the testing client to verify the functionality of the above

methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code in java with test case with screenshot. I have used a very small dataset to test, please feel to add more data and test

========================================================================================

public class Population {

    //assuming it will be a 2D array, each row will
    // have year and population count
    private long[][] yearlyPopulation;

    public Population(long[][] yearlyPopulation) {
        this.yearlyPopulation = yearlyPopulation;
    }

    public double averageAnnualChange() {

        long initialCount = yearlyPopulation[0][1];
        long finalCount = yearlyPopulation[yearlyPopulation.length - 1][1];

        double averageChange = (finalCount - initialCount) / yearlyPopulation.length;
        return averageChange;

    }

    public long yearWithGreatestIncrease() {

        long year = 0;
        long change = Long.MIN_VALUE;

        for (int i = 1; i < yearlyPopulation.length; i++) {
            long previousYear = yearlyPopulation[i - 1][1];
            long currentYear = yearlyPopulation[i][1];
            long diff = currentYear - previousYear;
            if (diff > 0 && diff > change) {
                change = diff;
                year = yearlyPopulation[i][0];
            }
        }
        return year;
    }

    public long yearWithSmallestIncrease() {
        long year = 0;
        long change = Long.MAX_VALUE;

        for (int i = 1; i < yearlyPopulation.length; i++) {
            long previousYear = yearlyPopulation[i - 1][1];
            long currentYear = yearlyPopulation[i][1];
            long diff = currentYear - previousYear;
            if (diff > 0 && diff < change) {
                change = diff;
                year = yearlyPopulation[i][0];
            }
        }
        return year;
    }

    public static void main(String[] args) {

        long[][] population = {{2000, 34000000}, {2001, 34001234},
                {2002, 35000000}, {2003, 36000000}, {2004, 36000001},
                {2005, 3700000}, {2006, 38007000}, {2007, 39000333},
                {2008, 44000000}, {2009, 44005000}, {2010, 45000000}};

        Population sensus = new Population(population);
        System.out.println("Average change: " + sensus.averageAnnualChange());
        System.out.println("Year with greatest increase: " + sensus.yearWithGreatestIncrease());
        System.out.println("Year with smallest increase: " + sensus.yearWithSmallestIncrease());

    }

}

========================================================================================

Add a comment
Know the answer?
Add Answer to:
Mike is a consultant for the Department of Labor. One day he is analyzing the midyear...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# Visual Studio Problem You are given a file named USPopulation.txt. The file contains the midyear...

    C# Visual Studio Problem You are given a file named USPopulation.txt. The file contains the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the population for 1951, and so forth. Create an application that reads the file’s contents into an array or a List. The application should display the following data (statistics) in read-only textboxes. The average population during...

  • CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!)....

    CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!). Can be resubmitted up to two times until Session 6 if your first version is submitted by session 3. In this project, you create two classes for tracking students. One class is the Student class that holds student data; the other is the GradeItem class that holds data about grades that students earned in various courses. Note: the next three projects build on this...

  • Introduction In this final programming exercise, you'll get a chance to put together many of the...

    Introduction In this final programming exercise, you'll get a chance to put together many of the techniques used during this semester while incorporating OOP techniques to develop a simple song playlist class. This playlist class allows a user to add, remove and display songs in a playlist. The Base Class, Derived Class and Test Client Unlike the other PAs you completed in zyLabs, for this PA you will be creating THREE Java files in IntelliJ to submit. You will need...

  • Please use Java to write the program The purpose of this lab is to practice using...

    Please use Java to write the program The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company. Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc. Insurance policies have a lot of data and behavior in common. But they also have differences. You want to build each insurance policy class so that you can reuse as much code as possible. Avoid duplicating code....

  • SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what...

    SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what choices does one make when creating a class that is an example of Abstraction? Encapsulation: What is encapsulation? What is information hiding? What does a public interface to a class consist of (not in the sense of actual java interfaces but what the client uses to manipulate objects of the class) What is an object of a class? What is the constructor? How do...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from...

    In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...

  • Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main()...

    Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main() method. Within the same file, create another class named Employee, and do not declare it public. Create a field for each of the following pieces of information: employee name, employee number, hourly pay rate, and overtime rate. Create accessor and mutator methods for each field. The mutator method for the hourly pay rate should require the value to be greater than zero. If it...

  • IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from...

    IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...

  • SELF-CHECK Why did the first version of method search that passed the first test itemNotFirstElementInSingleElementArray contain...

    SELF-CHECK Why did the first version of method search that passed the first test itemNotFirstElementInSingleElementArray contain only the statement return −1? Assume the first JUnit test for the findLargest method described in Self-Check exercise 2 in section 3.4 is a test that determines whether the first item in a one element array is the largest. What would be the minimal code for a method findLargest that passed this test? PROGRAMMING Write the findLargest method described in self-check exercise 2 in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT