Lab #1: Write, Run, Print - University of Arizona



C Sc 127A Lab 2: The Input Process Output Pattern

1) Develop test cases, an algorithm, and a computer program

Work on this together as a section. It is written like the Programming Projects

The US Census webpage gives information about the current US population as well as approximate rates of change for it (and lots of other cool stuff!). Complete a program in Population.java that asks the user for a year greater than 2012 and then displays the new estimated total population since 2012.

If you look at the webpage, you’ll likely see a different population change! For this program assume the current US population is 310,198,453. And use these estimated rates of change:

A. A birth every 7 seconds

B. A death every 13 seconds

C. A new immigrant every 36 seconds

Assume that every year is exactly 365 days (no leap year for us!) and note that the rates of change units are in seconds. After getting the target year (2013 below), print the number of births, immigrants, and deaths. Follow these predictions with the total increase in population and the new estimate for the target year. Sample output from running your program twice with the given year (2012) and population (310198453) with a user's input of 2013, your dialog should look precisely like this:

Enter a year after 2012 for the estimated US population since 2012: 2013

Births: 4505143

Immigrants: 876000

Deaths: 2425846

Total increase: 2955297

In the year 2012 the new estimated US population: 313153750

Notes

• Assume the current year is 2012 and the starting population is 310,198,453

• The number of seconds in one year = 365 * 24 * 60 * 60

• Round births, deaths, and immigrants to the nearest integer (it is an approximation)

Two Test Cases (using the sample dialog above as one test case)

| |1 year ahead |5 years ahead |

|Births |4505143 | |

|Immigrants |876000 | |

|Deaths |2425846 | |

|Total increase |2955297 | |

|Population |313153750 | |

As a section, help your section leader write a complete Java Program that meets the above specification.

When complete, login to a computer in 930 Gould Simpson. See the next page.

Two programs in teams of two

Get ready for the first programming project by completing two programs that are similar to problems to be solved next week. All programming projects including the first one must be completed solo with no collaboration with anyone such as a student or professionional cheater. This lab encourages you to work with one other person.

Program 1 When it is monsoon season in Tucson the weather can be a little cooler when storms are in the area. On a cloudy day you decided to go hiking but you can see lightning in the distance and hear thunder. How many miles away is the lightning? (“How fast can you run to your car?” is a question for another assignment!).

Complete a program in the file Lightning.java that calculates the distance to a lightning strike based on the time elapsed between the flash and the sound of thunder. The speed of sound is approximately 1100 ft/sec and 1 mile is 5280 ft. A sample execution of your program where the user enters 10 for seconds should look like this (the distance in miles is rounded):

This program finds the distance to a lightning strike.

How many seconds between seeing the lightning flash and hearing the thunder? 10

For 10 seconds, lightning flashed 2 miles away.

Three Test Cases (using the sample dialog above as one test case)

|seconds |10 |5 | |

|Distance in miles | 2 | | |

Hints:

double distanceInMiles = (seconds * 1100.0) / 5280.0

long roundedDistance = Math.round(distanceInMiles)

Program 2 Shocking evidence has been uncovered. Most college students do not know how to compute their GPA. When asked, the typical response is “Well, I never had to compute it, someone else did that for me”. Write a Java program named GPA.java that computes a student’s cumulative grade point average (GPA) for three courses. Credits range from 0.5 to 5.0. Grades can be 0.0 (F), 1.0 (D), 2.0 (C), 3.0 (B), or 4.0 (A).

GPA is computed by dividing the total quality points by the total number of credits. For example, if a student gets a 4 unit A, a 3 unit C, and a 1 unit E, the quality points are computed as follows:

qualityPoints = 4.0 * 4.0 + 3 * 2.0 + 1 * 0.0 = 22.0

totalCredits = 4.0 + 3.0 + 1.0 = 8.0

GPA = 22.0 / 8.0 = 2.75

Test your program by running it with several different inputs where you know the expected results. The dialog should look like this:

This program computes a GPA for three courses where 4.0 is an A

Credits for course 1: 2.0

  Grade for course 1: 2.0

Credits for course 2: 3.0

  Grade for course 2: 4.0

Credits for course 3: 3.0

  Grade for course 3: 4.0

GPA: 3.5

The above test case shows two As and a C for a GPA of 3.5. Write at least one more test case!

credits 1: ___

grade 1: ___

credits 2: ___

grade 2: ___

credits 3: ___

grade 3: ___

GPA: ___

When both programs are complete, raise your hand to let our section leader watch you run the programs.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download