Starting from:
$30

$24

Homework Assignment 4 Solution




Problem 1 [20pts]. Create a class Employee that includes three instance variables—a first name (String), a last name (String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each employee a 10% raise and display each employee’s yearly salary again.




Submission format: You must define two classes. One is Employee which must be declared as a non-public class. Two is EmployeeTest which is public and contains the main() method. So, you must submit one file EmployeeTest.java containing the above two classes. The tests must be performed in the main() method.




Problem 2 [40pts]. Create a class named Pizza that stores information about a single pizza. It should contain the following:




Private instance variables to store the size of the pizza (either small, medium, or large), the number of cheese toppings, the number of pepperoni toppings, and the number of ham toppings.



Constructor(s) that set all of the instance variables.



1
Public methods to get and set the instance variables.



A public method named calCost that returns a double that is the cost of the pizza. Pizza cost is determined by:



– Small: $10 + $2 per topping




– Medium: $12 + $2 per topping




– Large: $14 + $2 per topping




A public method named getDescription that returns a String containing the pizza size, quantity of each topping, and the pizza cost as calculated by calCost.



Write test code to create several pizzas and output their descriptions. For example, a large pizza with one cheese, one pepperoni and two ham toppings should cost a total of $22. Submission format: You must must two classes. One is Pizza which must be declared as a non-public class. Two is PizzaTest which is public and contains the main() method. So, you must submit one file PizzaTest.java containing the above two classes. The tests must be performed in the main() method




Problem 3 [60pts]. Create a PizzaOrder class that allows up to three pizzas to be saved in an order. Each pizza saved should be a Pizza object as described in Problem 3. In addition to appropriate instance variables and constructors, add the following methods:




public void setNumPizzas(int numPizzas)—sets the number of pizzas in the or-der. numPizzas must be between 1 and 3.



public void setPizza1(Pizza pizza1)—sets the first pizza in the order.



public void setPizza2(Pizza pizza2)—sets the second pizza in the order.



public void setPizza3(Pizza pizza3)—sets the third pizza in the order.



public double calcTotal()—returns the total cost of the order.



Write a main method to test the class. The setPizza2 and setPizza3 methods will be used only if there are two or three pizzas in the order, respectively. Sample code illustrating the methods is shown below. Note that first three lines are incomplete.




Pizza pizza1 = // Code to create a large pizza, 1 cheese, 1 ham




Pizza pizza2 = // Code to create a medium pizza, 2 cheese, 2 pepperoni PizzaOrder order = // Code to create an order order.setNumPizzas(2); // 2 pizzas in the order order.setPizza1(pizza1); // Set first pizza order.setPizza2(pizza2); // Set second pizza




double total = order.calcTotal(); // Should be 18+20 = 38




Submission format: You must define three classes: Pizza, PizzaOrder which are non-public classes, and PizzaOrderTest which is public and contains the main() method. So, you must submit one file PizzaOrderTest.java containing the above classes. The tests must be performed in the main() method.







2

More products