0

So I'm doing my homework right now, but I stuck in the constructor parts. This is my homework assignment. 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

  2. Create Instance Variables (attributes) (1 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 2 Constructors Create a default constructor (no parameters) that assigns all your instance variables to default values (1 point) Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters (1 point)

  6. Testing your program (1 point) Create a class called Demo.java. This class will contain your main method Create an instance of your class by using the default constructor. Call all your objects set methods to assign values to your object Call the objects display method, to print out it's values Create another instance of your class by using the parameterized constructor Call the objects display method, to print out it's value

public class Pizza {

private double pizza;

private double Price;

public Pizza(double Piz, double Pri)
{
    pizza = Piz;
    Price = Pri;
}



public void setPizza(double piz)
{
    pizza = piz;
}
public void setPrice(double Pri)
{
    Price = Pri;

}
public double getPizza()
{
    return pizza;
}

public double getPrice()
{
    return Price;
}

public double getTotal()
{
    return pizza * Price;
}
}

import javax.swing.JOptionPane;
public class PizzaPlace {

    public static void main(String[] args)
    {
        double number;
        double totalcost;
        String input;


        Pizza Domino = new Pizza();
        Pizza Pizzahut = new Pizza();
        Pizza Papa = new Pizza();

          input = JOptionPane.showInputDialog("How many " +
                  "Pizza Box you order?");
number = Double.parseDouble(input);
Domino.setPizza(number);

input = JOptionPane.showInputDialog("How much " +
        "Domino pizza cost?");
number = Double.parseDouble(input);
Domino.setPrice(number);

input = JOptionPane.showInputDialog("How many " +
        "Pizza Box you order?");
number = Double.parseDouble(input);
Pizzahut.setPizza(number);

input = JOptionPane.showInputDialog("How much " +
"Pizzahut pizza cost?");
number = Double.parseDouble(input);
Pizzahut.setPrice(number);

input = JOptionPane.showInputDialog("How many " +
        "Pizza Box you order?");
number = Double.parseDouble(input);
Papa.setPizza(number);

input = JOptionPane.showInputDialog("How much " +
"PapaJ pizza cost?");
number = Double.parseDouble(input);
Papa.setPrice(number);

double totalPrice = Domino.getTotal() + Pizzahut.getTotal() + Papa.getTotal();

JOptionPane.showMessageDialog(null, "The total price " +
        "of Pizzas will be " + totalPrice);


System.exit(0);

    }
}

I finish to set up method and finish to create getter and setter method. And this will be my instance methods

which is my constructor parts. The real problem is I have no idea how to create a two constructor which Default and Parameterized Constructor. How I can create the constructor that fit into the assignment? I create several constructors but It always gives error. I only have a problem with the constructors right now.

Sara Kim
  • 3
  • 2

0 Answers0