Author Topic: Need Help With Java  (Read 918 times)

Code: [Select]
public class Pizza
{
    private String size ;
    private int cheeseTops;
    private int pepperoniTops;
    private int hamTops;
    private int price;
   
    public void Pizza(String s, int c, int p, int h) //initilize pizza class with variables
    {
        size = s;
        cheeseTops = c;
        pepperoniTops = p;
        hamTops = h;
    }
   
    public void calcCost() //Gets total price of the pizza
    {
        int totalTops = getCheeseTops() + getPepperoniTops() + getHamTops();
       
        if(size == "small")
        {
            price = 10 + 2 * totalTops;
        }

        if(size == "medium")
        {
            price = 12 + 2 * totalTops;
        }

        if(size == "large")
        {
            price = 14 + 2 * totalTops;
        }
    }
   
    public void display() //Displays size, toppings and total price of the pizza
    {
        System.out.println("Pizza Size: " + getSize());
        System.out.println("Toppings: " + getCheeseTops() + " cheese, " + getPepperoniTops() + " pepperoni, "
        + getHamTops() + " ham");
        System.out.println("Total cost: $" + getPrice());
    }

    public void setSize(String s) //Set size of pizza class
    {
        size = s;
    }

    public void setCheeseTops(int c) //Set amount of cheese toppings
    {
        cheeseTops = c;
    }

    public void setPepperoniTops(int p) //Set amount of pepperoni toppings
    {
        pepperoniTops = p;
    }

    public void setHamTops(int h) //Set amount of ham toppings
    {
        hamTops = h;
    }   

    public double getPrice() //Dispalys total price of pizzza
    {
        return(price);
    }

    public String getSize() //Displays size of pizza
    {
        return(size);
    }

    public int getCheeseTops() //Displays amount of cheese toppings
    {
        return(cheeseTops);
    }

    public int getPepperoniTops() //Displays amount of pepperoni toppings
    {
        return(pepperoniTops);
    }

    public int getHamTops() //Displays amount ham toppings
    {
        return(hamTops);
    }

}


Code: [Select]
public class PizzaOrderDriver
{
    public static void main (String[] args)
    {
        Pizza p1 = new Pizza("small", 1, 0, 1);  // cost=10+2+2=14
        Pizza p2 = new Pizza("medium", 1, 1, 1); // cost = 12+2+2+2=18
        Pizza p3 = new Pizza("large", 1, 1, 2); // cost = 14+2+2+4 = 22
   
       
        // create one PizzaOrder object
        PizzaOrder po1 = new PizzaOrder(3, p1, p2, p3);
        System.out.println("Pizza Order #1:");
        po1.displayOrder();  // total cost = 14+ 18+22 = 54
       
       
        // create another PizzaOrder object with copy constructor
        PizzaOrder po2 = new PizzaOrder(po1);
        po2.getPizza1().setCheeseToppings(3); // p1 cost=10+6+0+2=18
        System.out.println("Pizza Order #2:");
        po2.displayOrder(); // total cost = 18 + 18 + 22 = 58

    }
}

For some reason the Pizza constructor won't work. It  keeps telling me it doesn't need variables. I really don't understand why either. If anyone could help me I would really appreciate it.

You defined your constructor as a method.

You have this:
Code: [Select]
public void Pizza(String s, int c, int p, int h)
It should just be this:
Code: [Select]
Pizza(String s, int c, int p, int h)

Thanks a lot, I feel like an idiot now. We're working on a previous project and my professor never said anything about it.

No problem, I got stuck on the same thing when I was learning

I might have one more quesiton, my friend was telling me one part was kind of difficult.

One more question, how do I make a copy constructor?

Heres an example, enjoy

Code: [Select]
class Complex {
 
    private double re, im;
     
    // A normal parametrized constructor
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
     
    // copy constructor
    Complex(Complex c) {
        System.out.println("Copy constructor called");
        re = c.re;
        im = c.im;
    }
     
    // Overriding the toString of Object class
    @Override
    public String toString() {
        return "(" + re + " + " + im + "i)";
    }
}
 
public class Main {
 
    public static void main(String[] args) {
        Complex c1 = new Complex(10, 15);
         
        // Following involves a copy constructor call
        Complex c2 = new Complex(c1);   
 
        // Note that following doesn't involve a copy constructor call as
        // non-primitive variables are just references.
        Complex c3 = c2;   
 
        System.out.println(c2); // toString() of c2 is called here
 

Code: [Select]
public class PizzaOrder

    public int NumPizzas;
    public double TotalCost;
    public Pizza Pizza1;
    public Pizza Pizza2;
    public Pizza Pizza3;

    public void PizzaOrder()
    {
    }

    PizzaOrder(int orderLength, Pizza p1, Pizza p2, Pizza p3)
    {
        Pizza[] pizzaList = new Pizza[orderLength];
        pizzaList[0] = p1;
        pizzaList[1] = p2;
        pizzaList[2] = p3;
    }     
   
    PizzaOrder(PizzaOrder po)
    {
    }
   
    public int getNumPizzas()
    {
        return(NumPizzas);
    }
   
    public void setPizza1(Pizza p1)
    {
        Pizza1 = p1;
    }

    public void setPizza2(Pizza p2)
    {
        Pizza2 = p2;
    }

    public void setPizza3(Pizza p3)
    {
        Pizza3 = p3;
    }

    public Pizza getPizza1()
    {
        return(Pizza1);
    }

    public Pizza getPizza2()
    {
        return(Pizza2);
    }

    public Pizza getPizza3()
    {
        return(Pizza3);
    }

    public double calcTotal()
    {
        TotalCost = Pizza1.getPrice() + Pizza2.getPrice() + Pizza3.getPrice();
        return(TotalCost);
    }

    public void displayOrder()
    {
        getPizza1().display();
    }
}

Ok this is the actual new program I have to make. I'm super lost on this one. I mean I'm pretty sure I've got it all right so far, but I'm having trouble. It's supposed to make an order and output all that information. Then it can be copied for the next order.