Question

In: Computer Science

Instructions 1. Create a class named after your favorite object in the whole world. For example,...

Instructions

1. Create a class named after your favorite object in the whole world. For example, if you love pizza, then create a class called Pizza.java. This class should not have a main method (1 point)

2. Create Instance Variables (attributes) (2 point)

  • Create at least 3 private instance fields (attributes) for your class
  • You must use at least 3 different data types for your fields

3. Create getter (accessor) and setter (mutator) methods  

  • Create a getter (accessor) method for each of your instance variables (1 point)
  • Create a setter (mutator) method for each of your instance variables  (1 point)

4. Create a Method to display your data (1 point)

  • Create a method called display, that simply prints out the values of all instance variables of your object

5. Create any other method of your choice that increments or decrements any one of the objects instance fields. Test out the method for at least one of your objects. (2 point)

6. Create 2 Constructors

  • Create a default constructor (no parameters) that assigns all your instance variables to default values  (1 points)
  • Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters (1 points)

7. Testing your program (2 points)

  • Create a class called Driver.java. This class will contain your main method
  • Create 2 instances of your class by using the default constructor.
    • Call all your objects set methods to assign values to your object
  • Create 2 instances of your class by using the parameterized constructor


8. Put all of your objects in an array, loop through the array, and call the display method on each object (2 points)

9. Add JavaDoc style comments above all methods and classes   (1 point)

Solutions

Expert Solution

/**
* @author pyari
* Defines class Pizza
*
*/
class Pizza
{
   // Instance variables to store pizza information
   private String topping;
   private String size;
   private double price;
   private int quantity;
  
   /**
   * Default constructor to assign "Unknown" to string instance variables
   * 0 to double and integer instance variables
   */
   public Pizza()
   {
       topping = "Unknown";
       size = "Unknown";
       price = 0.0;
       quantity = 0;      
   }
  
   /**
   * Parameterized constructor to assign parameter values to instance variables
   * @param topping - type of topping for pizza
   * @param size - size of the pizza
   * @param quantity - number of pizza ordered
   * @param price - price of each pizza
   */
   public Pizza(String topping, String size, int quantity, double price)
   {
       this.topping = topping;
       this.size = size;
       this.price = price;
       this.quantity = quantity;
   }
  
   /**
   * Method to increase the price of the pizza by given parameter percentage
   * @param percent - price percentage to increase
   */
   public void increasePriceByPercent(double percent)
   {
       price += (price * percent) / 100.0;
   }
  
   // Getter methods
   public String getTopping()
   {
       return topping;
   }
   public String getSize()
   {
       return size;
   }
   public double getPrice()
   {
       return price;
   }
   public int getQuantity()
   {
       return quantity;
   }
  
   // Setter methods
   public void setTopping(String topping)
   {
       this.topping = topping;
   }
   public void setSize(String size)
   {
       this.size = size;
   }
   public void setPrice(double price)
   {
       this.price = price;
   }
   public void setQuantity(int quantity)
   {
       this.quantity = quantity;
   }
  
   /**
   * Method to display pizza information
   */
   public void showPizza()
   {
       System.out.println("\n Topping: " + topping + "\n Size: " + size +
               "\n Quantity: " + quantity + "\n Price: $" + price +
               "\n Amount to pay: $" + (quantity * price));
   }
}// End of class Pizza

/**
* @author pyari
* Driver class to test the pizza class methods
*
*/
public class PizzaDriver
{
   // main method definition
   public static void main(String []s)
   {
       // Declares an array of object of class Pizza of size 4
       Pizza [] pizza = new Pizza[4];
      
       // Creates a pizza object using default constructor
       pizza[0] = new Pizza();
       // Calls the setter method to set data
       pizza[0].setTopping("Mushrooms");
       pizza[0].setSize("Small");
       pizza[0].setQuantity(10);
       pizza[0].setPrice(50.63);
      
       // Creates a pizza object using default constructor
       pizza[1] = new Pizza();
       // Calls the setter method to set data
       pizza[1].setTopping("Cheese");
       pizza[1].setSize("Large");
       pizza[1].setQuantity(4);
       pizza[1].setPrice(90.12);
      
       // Creates pizza object using parameterized constructor
       pizza[2] = new Pizza("Onions", "Medium", 8, 61.12);
       pizza[3] = new Pizza("Pepperoni", "extraLarge", 6, 78.33);
      
       // Loops till number of pizza ordered      
       for(int c = 0; c < pizza.length; c++)
           // Calls the method to display each pizza information
           pizza[c].showPizza();
      
       // Calls the method to increase the pizza price by 10%
       pizza[1].increasePriceByPercent(10.0);
       System.out.println("\n After increasing the price of pizza One by 10%");
       // Calls the method to display pizza information
       pizza[1].showPizza();
   }// End of main method
}// End of driver class

Sample Output:


Topping: Mushrooms
Size: Small
Quantity: 10
Price: $50.63
Amount to pay: $506.3

Topping: Cheese
Size: Large
Quantity: 4
Price: $90.12
Amount to pay: $360.48

Topping: Onions
Size: Medium
Quantity: 8
Price: $61.12
Amount to pay: $488.96

Topping: Pepperoni
Size: extraLarge
Quantity: 6
Price: $78.33
Amount to pay: $469.98

After increasing the price of pizza One by 10%

Topping: Cheese
Size: Large
Quantity: 4
Price: $99.132
Amount to pay: $396.528


Related Solutions

Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
Create java class with name BaseballPlayer Instructions for BaseballPlayer class: The BaseballPlayer class is modeled after...
Create java class with name BaseballPlayer Instructions for BaseballPlayer class: The BaseballPlayer class is modeled after a BaseballPlayer and will contain methods to calculate various statistics based on the stats of a player. For this class, you will want to use a static variable for storing a DecimalFormat object. See the API document for the BaseballPlayer class for a list of methods you will write. API Document Constructors: Identifier: BaseballPlayer(String name, int number, int singles, int doubles, int triples, int...
Write the code to create an array named movies and store three of your favorite movies...
Write the code to create an array named movies and store three of your favorite movies in the array. Only provide the array and code needed to put the movie names in the array. Do not include additional code
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problem : The Fraction class (Filename: TestFraction.java) Design...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT