Docsity

Bottled Water Consumption Calculator Program Assignment - Prof. Gregory Shaw, Study notes of Computer Science

Toggle sidebar
bg1
Computer Programming I Instructor: Greg Shaw
COP 2210
Programming Assignment #3
(Creating a Class)
I. Problem Statement
According to a recent TV commercial, Americans consumed so much
bottled water last year that the bottles – if laid end-to-end –
would circle the Earth 190 times at the Equator! One can only
imagine how disruptive this would be for folks actually living
at the Equator! Anyway, approximately how many bottles is that,
and what is the average amount of water consumed by each
American? Let’s write a program to find out! Better yet, let’s
write a program that computes this information for any country.
Given this data (i.e. inputs)
1. the name of the country
2. the population of the country
3. the number of times the bottles would circle the earth
4. the average length of a bottle, in inches
5. the average volume of a bottle, in fluid ounces
your program will compute (i.e. outputs)
a. the total number of bottles used
b. the average amount of water consumed per person, in
gallons
Your program will consist of a BottledWaterCalculator class and
a “tester” class.
II. Your BottledWaterCalculator Class - Specifications
1. Your class will have private instance variables that store
the data listed above (1 - 5)
2. All numeric instance variables are type int except for the
length of a bottle, which is a double
3. Instance variables are to be initialized to values passed
to the class constructor
4. Your class will have accessor (“get”) methods to return
each of the instance variables
pf3
pf4
pf5
pf8

Partial preview of the text

Download Bottled Water Consumption Calculator Program Assignment - Prof. Gregory Shaw and more Study notes Computer Science in PDF only on Docsity!

Computer Programming I Instructor: Greg Shaw COP 2210

Programming Assignment

(Creating a Class)

I. Problem Statement

According to a recent TV commercial, Americans consumed so much bottled water last year that the bottles – if laid end-to-end – would circle the Earth 190 times at the Equator! One can only imagine how disruptive this would be for folks actually living at the Equator! Anyway, approximately how many bottles is that, and what is the average amount of water consumed by each American? Let’s write a program to find out! Better yet, let’s write a program that computes this information for any country. Given this data (i.e. inputs )

  1. the name of the country
  2. the population of the country
  3. the number of times the bottles would circle the earth
  4. the average length of a bottle, in inches
  5. the average volume of a bottle, in fluid ounces your program will compute (i.e. outputs ) a. the total number of bottles used b. the average amount of water consumed per person, in gallons Your program will consist of a BottledWaterCalculator class and a “tester” class.

II. Your BottledWaterCalculator Class - Specifications

1. Your class will have private instance variables that store the data listed above (1 - 5) 2. All numeric instance variables are type int except for the length of a bottle, which is a double 3. Instance variables are to be initialized to values passed to the class constructor 4. Your class will have accessor (“get”) methods to return each of the instance variables

5. Your class will have 2 methods that compute and return the outputs (a and b) listed above 6. Your class will also have a mutator (“set”) method that changes the length and volume of a bottle to new values passed as parameters

 Note that your BottledWaterCalculator class doesn’t do

any output. All output is to be done in your test class.

III. Your Test Class

In the main method of your test class, write statements to accomplish each of the following, in order

  1. Create a BottledWaterCalculator object, using the data below
  2. Call the accessor methods to get each of the data values and then print them
  3. Call the methods that compute and return the 2 outputs and print them
  4. Call the mutator method to change the bottle data. Do not create a new object. Modify the existing one
  5. Call the accessor methods to get the average bottle length and volume, and print them
  6. Call again the methods that compute and return the 2 outputs and print them

 Although your BottledWaterCalculator must work for all

valid input values, use this data in the run you hand in: Country: USA Population: 320 million Number of Circumferences: 190 Length of an Average Bottle: 8.5 inches Volume of an Average Bottle: 12 ounces Modified Bottle Length: 9 inches Modified Bottle Volume: 16 ounces

IV. For Reference

There are 128 ounces in a gallon, and the circumference of the Earth at the Equator is 24,902 miles. Since these values will not change, they should be literals (or defined constants) and not instance variables

// Constructing program; Step 3 public Water (String country,int population,int rounds,double length, int volume) { // Just in case of shadowing... this.country = country; this.population = population; this.rounds = rounds; this.length = length; this.volume = volume; // Now everything's the same, except for the values assigned. } // Creating accessor methods for the instance variables; Step 4 public String getCountry() { return country; // returns country } public int getPopulation() { return population; // returns popluation } public int getRounds() { return rounds; // returns rounds } public double getLength() {

return length; // returns length } public int getVolume() { return volume; // returns volume } // Computing the program outputs; Step 5 public double totalBottles(int rounds,double length) { return (long)rounds * 1577790720 / length; } public double consumption(int population,int rounds,double length,int volume) { double total = (long) rounds * 1577790720 / length; total = (double) total ; double consumption = total * volume /128 / population; return consumption ; } // Adding mutator methods to some instance variables; Step 6 public void resize(double newLength, int newVolume) { // Again, just in case of shadowing... this.length = newLength; this.volume = newVolume; // Now it's reset for future reference. } }

bottles"); System.out.println("Total amount of water consumed per person: "

  • consumed + " gallons"); // Mutating bottle dimensions; Step 4 Cap.resize(9,16); // Retrieving mutated data; Step 5 double newLength = Cap.getLength(); int newVolume = Cap.getVolume(); // Printing mutated methods; Still Step 5 System.out.println("New average length of the bottle: " + newLength
  • " inches"); System.out.println("New average volume of the bottle: " + newVolume
  • " ounces"); // Accessing new outputs; Step 6 double load = Cap.totalBottles(rounds,newLength); double drunk = Cap.consumption(population,rounds,length,newVolume); // Printing new outputs; Still Step 6 System.out.println("New total bottles used: " + load + " bottles"); System.out.println("New total amount of water consumed per person: "
  • drunk + " gallons"); } } /* OUTPUT run: @ Name (§U0#) Country: USA Population: 320000000 people Number of rounds on Equator: 190 rounds Average length of the bottle: 8.5 inches Average volume of the bottle: 12 ounces Total bottles consumed: 3.526826315294118E10 bottles Total amount of water consumed per person: 10. gallons New average length of the bottle: 9.0 inches New average volume of the bottle: 16 ounces New total bottles used: 3.33089152E10 bottles

New total amount of water consumed per person: 13. gallons BUILD SUCCESSFUL (total time: 0 seconds) */