AP CS Unit 4: Classes and Objects Programs 1. Copy the Bucket class. Make sure it compiles (but you won t be able to run it because it does not have a main method). public class Bucket { private double amt; private final double CAPACITY; Add a second class (named BucketRunner) that does contain a main method. The main method should do the following: create a Bucket object that can hold 10 gallons. add a random amount using Math.random. The amount should be a decimal between 5 (inc) and 12 (exc). display how full the bucket is (e.g. 74%) empty the bucket call the percentfull method again and display the returned value ( it should be zero). 2. Copy the Walker method and complete the walk method. Then make sure it compiles. Add a second class (named Runner) and enter this code: public class Runner { public static void main(string[] args) { Walker w1 = new Walker(); w1.walk( 5 ); w1.walk( 10 ); w1.walk( 8 ); System.out.println( "w1 is now at " + w1.getx() ); Run it and make sure it works. 3. Add a third class that contains a main method. In this main method create another walker and keep calling the walk method (with an argument of 10) until its x value is 100 or greater. After it reaches 100, display its x value and the number of times the walk method was called. public Bucket( double c ) { CAPACITY = c; // gallons amt = 0; public void emptybucket() { amt = 0; public void add ( double stuff ) { amt += stuff; if ( amt > CAPACITY ) amt = CAPACITY; public double percentfull( ){ return 100 * amt / CAPACITY; public class Walker { private int x; public Walker() { x = 0; public void walk( int max ){ /* If max is less than one this method does nothing. If max is positive, then this method generates a random number between zero and max (inclusive) and increases x by that amount. */ public int getx(){ return x;
4. Copy and complete the CoinTosser class. An object of this class is someone who really likes tossing a coin and seeing how many times they get heads. Create another class with a main method. In that method: Instantiate two CoinTosser objects using these two names. Have each CossTosser toss his/her coin 100 times. (In other words, call the toss method). Determine who had the most heads and print out something like: #1 wins, 54.0% to 47.0% If it s a tie, say so. 5. Create another class with a main method. This is similar to the above exercise. You will again enter 2 names and instantiate 2 CoinTosser objects. But this time they will play 1,000 contests (where each contest consists of 100 tosses). This means that the main method should contain a for-loop. At the end of each iteration you need to call the reset method. Print out the results of each contest and at the end print out the number of wins for each. The results should look something like this: #1 wins, 60.0% to 47.0% #1 wins, 50.0% to 41.0% It is a tie! Both had 47.0%... #1 wins, 52.0% to 45.0% #2 wins, 56.00000000000001% to 46.0% #1 wins, 51.0% to 48.0% public class CoinTosser { private int numtosses; private int numheads; public CoinTosser() { numtosses = 0; numheads = 0; public void toss(int n){ numtosses += n; for ( int k = 1; k <= n; k++ ){ if ( heads() ) numheads++; public void reset() { numtosses = 0; numheads = 0; private boolean heads() { // half the time it returns true public double getpercentheads() { /* returns percentage of heads. For example, if 3 heads out of 4 tosses, returns 0.75 */ #1 had 546 wins #2 had 452 wins #1 wins!
6. First, complete the Star class. public class Star{ private int x, y, z; public Star(){ // assign the instance variables random values between 0 and 20 // write accessor methods for the instance variables public double distance( int x1, int y1, int z1 ){ // returns the distance from (x, y, z) to (x1, y1, z1) public void display(){ System.out.println( "coordinates: " + x + ", " + y + ", " + z ); Second, write a runner class that creates three Star objects and calls their display methods. Then it calculates (using the distance method) and displays the distance between the three stars. If the stars create an isosceles or equilateral triangle, it displays a message to that effect. Otherwise it determines and displays which two stars are closest to each other. Here s a sample output: Star 1 coordinates: 15, 20, 18 Star 2 coordinates: 7, 14, 15 Star 3 coordinates: 14, 18, 20 The distance from star 1 to star 2 is 10.44030650891055 The distance from star 2 to star 3 is 9.486832980505138 The distance from star 3 to star 1 is 3.0 Star 3 and Star 1 are the closest 7. Complete any three exercises from the String1 category at codingbat.com. 8. Complete any five exercises from the String2 category at codingbat.com. 9. Complete any three exercises from the String3 category at codingbat.com.
10. First, complete the code below to create a Plant class. public class Plant { private String name; private boolean living; //create a constructor (no parameters), alive = true, name =??? // write a mutator method for name // write an accessor method for name // write an addwater method that has an int parameter that represents the water added. If the amount of water added is less than 20 or greater than 80, then set living equal to false. Otherwise do nothing. Note: if a Plant is dead, then adding any amount of water to it cannot bring it back to life. public String tostring() { // returns the name and whether the plant is alive or not. Second, write a class that has a main method. The main method should: Instantiate three Plant objects. Adds a random amount of water (between 0 and 100 units) to each plant. After watering the plants, call the tostring method for each plant. 11. First, complete the code below to create a Sentence class. Note, consider any series nonwhite characters as a word. For example, the following count as words: mom, 34T, and?$_q. public class Sentence{ private String stringy; // constructor with a String parameter // write an accessor method for stringy public String trim(){ /* return a string that is identical to stringy except that there is exactly one space between each word in stringy. For example, if stringy is cat dog then this method returns cat dog (with one space between cat and dog). */ public int count(){ // returns the number of words in stringy. May return zero. // this is a lot easier if you call the above trim method within this method Create a main method that asks the user to enter three sentences. Create three Sentence objects and call the trim and count methods. Display the return values.
12. Complete the Name class public class Name { private String first, middle, last; public Name( String str ) { check_name( str ); private void check_name( String nm ){ nm contains a name such as John Jacob Jingleheimer Schmidt or Tuhin Gupta. You may assume that there is at least a first and last name in nm. You need to break nm into first, middle, and last names and correctly assign them to the instance variables. You may assume that nm has at least two parts. If there are 2 parts then middle should be a blank String. If there are 3 parts, then you know what to do. If there are more than 3 parts, then assign part 1 to first, part 2 to middle, and everything else to last. The first thing this method should do is call the remove_spaces method. private String remove_spaces( String s ){ This returns a String that is identical to s except that all leading and trailing spaces have been removed. In addition, if there are two or more adjacent spaces then all but one are removed. For example: if s is " that's my name too " then it returns "that's my name too" public String full_name(){ Returns first + middle + last but there should be exactly one space between each part. If there is no middle name then there should be only one space between first and last public String tostring(){ return last + ", " + first + " " + middle; Write a second class that has a main method. Have the user enter their name. Instantiate a Name object and then call the full_name and tostring methods and display the results.