Question 1

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.
  • Public methods to get and set the instance variables.
  • A public method named calcCost( ) 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 calcCost( ).

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

  • public void setNumPizzas(int numPizzas) - sets the number of pizzas in the order. 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. You must complete them as part of the Programming Project.

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 piza
order.setPizza2(pizza2); // Set second pizza
double total = order.calcTotal(); // Should be 18+20 = 38

Question 2

Write a grading program for a class with the following grading policies:

  • There are three quizzes, each graded on the basis of 10 points.
  • There is one midterm exam, graded on the basis of 100 points.
  • There is one final exam, graded on the basis of 100 points.

The final exam counts for 40% of the grade. The midterm counts for 35% of the grade. The three quizzes together count for a total of 25% of the grade. (Do not forget to convert the quiz scores to percentages before they are averaged in.)

Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D, and any grade below 60 is an F. The program should read in the student's scores and output the students record, which consists of three quiz scores and two exam scores, as well as the students overall numeric score for the entire course and final letter grade.

Define and use a class for the student record. The class should have instance variables for the quizzes, midterm, final, overall numeric score for the course, and final letter grade. The overall numeric score is a number in the range 0 to 100, which represents the weighted average of the student's work. The class should have methods to compute the overall numeric grade and the final letter grade. These last methods should be void methods that set the appropriate instance variables. Your class should have a reasonable set of accessor and mutator methods, an equals method, and a toString method, whether or not your program uses them. You may add other methods if you wish.

Question 3

Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to a string.

Your class should handle denominators that are zero. Fractions should always occur in lowest terms, and the class should be responsible for this requirement. For example, if the user tries to create a fraction such as 4/8, the class should set the fraction to 1/2. Likewise, the results of all arithmetic operations should be in lowest terms. Note that a fraction can be improperthat is, have a numerator that is larger than its denominator. Such a fraction, however, should be in lowest terms.

Design the class Fraction. Begin by writing a CRC card for this class. Then write a Java interface that declares each public method.

To reduce a fraction such as 4/8 to lowest terms, you need to divide both the numerator and the denominator by their greatest common denominator. The greatest common denominator of 4 and 8 is 4, so when you divide the numerator and denominator of 4/8 by 4, you get the fraction 1/2. The following recursive algorithm finds the greatest common denominator of two positive integers:

Algorithm gcd(integerOne, integerTwo)
if (integerOne % integerTwo == 0)
result = integerTwo
else
result = gcd(integerTwo, integerOne % integerTwo)
return result

It will be easier to determine the correct sign of a fraction if you force the fraction's denominator to be positive. However, your implementation must handle negative denominators that the client might provide.

Write a program that adequately demonstrates your class.

Question 4

Suppose you want to design a class that is given numbers one at a time. The class computes the smallest, second smallest, and average of the numbers that have been seen so far. Create an interface for the class. Create a class that implements the interface.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.