




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Computer Programming I Instructor: Greg Shaw COP 2210
(Creating a Class)
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. 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
any output. All output is to be done in your test class.
In the main method of your test class, write statements to accomplish each of the following, in order
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
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: "
New total amount of water consumed per person: 13. gallons BUILD SUCCESSFUL (total time: 0 seconds) */