You are on page 1of 4

Lab 9 Classes

Program 66: Shirts Lab Shirts Lab In this lab you will do the following: 1: Write a Shirt class as follows: Instance variables int size representing the shirt size, boolean sleeves indicating whether the shirt has sleeves or not, String color defining the shirt color, String pattern storing any particular fabric pattern, and double cost storing the shirt cost. Write no args Shirt constructor and one that fully initializes all instance variables using parameters passed in. Write accessor and modifier methods, including a toString method. Write an equals method that accepts a Shirt object as an argument. Two shirts will be considered equal if all of their attributes match. (same color, same pattern, ..) Write a compareTo method such that when two shirts have the same cost, zero is returned; when shirt passed in for comparison is less, return negative value if this shirt is less than one passed in; and returns positive value if cost is greater. (add the words implements Comparable to your class header public class Shirt implements Comparable 2: Write a ShirtTester class as follows: When the program starts, it creates an array of size 6 to hold references to Shirts. Create two shirts and add them to the first two elements of the array. For the remaining positions, ask the user for each attribute, create a Shirt object using those attributes, and place a reference in the array. Because your class declares that it implements the Comparable interface and you have written a compareTo method, you can sort your array using Arrays.sort(arrayname). Sort your array of Shirt references and print out each. (Try using a foreach loop and placing the object reference inside println().

3.

Program 67: Stamp Machine STAMP MACHINE CLASS A customer inserts money into the vending machine. Then the customer pushes a First Class Stamps button. The vending machine gives out as many first-class stamps as the customer paid for. (A first class stamp costs $0.44) Any change is dispersed in the form of penny stamps. Create a class that simulates this machine. You will input the amount of money the customer inserts and output the number of First Class Stamps and the number of Penny Stamps. Your class will have one instance variable that holds the current amount of money and 1 default constructor that sets the amount of money to zero. Use the main method below to test your class. import java.io.*; public class StampMachineTest { public static void main(String args[]) throws Exception { Scanner in = new Scanner(System.in);

double amount; StampMachine machine = new StampMachine(); System.out.println("Enter the amount you are spending on stamps: "); input = in.nexDouble();

machine.insert(amount); System.out.println("Number of First Class Stamps: "+machine.getFirstClassStamps()); System.out.println("Number of Penney Stamps: "+machine.getPenneyStamps());

} }

Program 68: Gas Milage


Gas Mileage In Class Exercise Create a class called Automobile in which you pass a gas mileage (miles per gallon) parameter to the constructor which in turn passes it to the instance variable, mpg. The constructor should also set the instance variable gallons (gas in tank) to 0. A method called fillUP adds gas to the tank. Another method, takeTrip, removes gas from the tank as the result of driving a specified number of miles. The toString method returns a String describing current state of Automobile. Finally, the method reportFuel returns how much gas is left in the car. Create a Tester class as follows: public class Tester { public static void main(String args[]) { // Create a new object called myBmw of type Automobile. Pass the constructor an // argument of 24 miles per gallon // Use the myBmw object to call the fillUp method. Pass it an argument // of 20 gallons. //Use the myBmw object to call the takeTrip method. Pass it an argument // of 100 miles. Driving 100 miles of course uses fuel and we would now // find less fuel in the tank. //Use the myBmw object to call the reportFuel method. It returns a double // value of the amount of gas left in the tank and this is assigned to the //variable fuel_left. //Print the fuel_left variable } }

Program 69: Purse PURSE CLASS Create a Purse Class with the following specifications. The Purse class will have three private integer attributes: the number of nickels, the number of dimes, and the number of quarters. The Purse class will have two constructors: a default constructor that sets the number of nickels , dimes and quarters to zero and an overloaded constructor that set the number of nickels, dimes and quarters to values received as parameters.

Other methods are needed to increase the number of each coin, to access the number of each coin and to return the total value of the coins. Use the following main method to check your class. public class PurseTest { public static void main(String args[]) { Purse p1 = new Purse(); p1.addNickels(13); p1.addDimes(1); p1.addQuarters(2); double totalValue = p1.getTotal(); System.out.println("The total of p1 is "+totalValue); Purse p2 = new Purse(5, 4, 8); p2.addNickels(8); p2.addDimes(3); p2.addQuarters(6); totalValue = p2.getTotal(); System.out.println("The total of p2 is "+totalValue); System.out.println("The number of nickels in p1 is "+p1.getNickels()); System.out.println("The number of dimes in p1 is "+p1.getDimes()); System.out.println("The number of quarters in p1 is "+p1.getQuarters()); System.out.println("The number of nickels in p2 is "+p2.getNickels()); System.out.println("The number of dimes in p2 is "+p2.getDimes()); System.out.println("The number of quarters in p2 is "+p2.getQuarters());

} }

You might also like