8 Replies - 1223 Views - Last Post: 02 March 2010 - 07:29 AM Rate Topic: -----

#1 Guest_Ola*


Reputation:

Pizza object sent to a double

Posted 01 March 2010 - 03:33 AM

public static void main (String[] args) {
Pizza myPizza = new Pizza ("small", 0, 0, 2);
double calcCost = (myPizza);
getDesc(myPizza);
}

not compiling, can anyone help by fixing this for me.

This post has been edited by pbl: 01 March 2010 - 08:58 PM

Is This A Good Question/Topic? 0

Replies To: Pizza object sent to a double

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Pizza object sent to a double

Posted 01 March 2010 - 04:27 AM

Quote

double calcCost = (myPizza);


You can't assign a type of Pizza to a double
Was This Post Helpful? 0
  • +
  • -

#3 Simple_Condolences   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 180
  • Joined: 10-January 10

Re: Pizza object sent to a double

Posted 01 March 2010 - 06:53 AM

double pizzaCost = calcCost(myPizza);



Also, where you have get description, we have what we call a toString() method. Basically, when you say System.out.println(Object o); it takes the information stored in object o and turns it into MEANINGFUL output. Basically, myPizza would print...

System.out.print(myPizza);
/*
This is a small pizza with 0... and 0... and 2...
*/


Was This Post Helpful? 0
  • +
  • -

#4 Guest_Ola*


Reputation:

Re: Pizza object sent to a double

Posted 01 March 2010 - 07:05 AM

i try what you said but still doesnt compile, i have attached the whole programme, if u can please have a look at it.

public class Pizza {

private String pi_size;
private int che_topping;
private int peper_topping;
private int ham_topping;
private int prize;


//constructor
public Pizza(String pi_size,int che_topping, int peper_topping, int ham_topping) {
this.pi_size = pi_size;
this.che_topping = che_topping;
this.peper_topping = peper_topping;
this.ham_topping = ham_topping;
}


public double calcCost (String pi_size, int che_topping, int peper_topping, int ham_topping) {

if (pi_size == "small") {
double prize = 10;
} else if (pi_size == "medium") {
double prize = 12;
} else if (pi_size == "small"){
double prize = 14;
} else {
double prize = 0;
}
double prize = prize + (che_topping *2) + (peper_topping*2) + (ham_topping*2);
return prize;

}

public String getDesc (String pi_size, int che_topping, int peper_topping, int ham_topping) {

System.out.println("The pizza is of size "+pi_size+ "with "+che_topping+" Chesse toppings, "+peper_topping+" Peperoni toppings and "
+ham_topping+" Ham toppings");
}



public static void main (String[] args) {
Pizza myPizza = new Pizza ("small", 0, 0, 2);
double calcCost = (myPizza);
double getDesc = myPizza.getDesc();
}
}
Was This Post Helpful? 0

#5 Simple_Condolences   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 180
  • Joined: 10-January 10

Re: Pizza object sent to a double

Posted 01 March 2010 - 07:56 AM

Please edit your post and :code: so that we may be helped to better help you!

But...

alright, to start with, lets look at your method headers.

public double calcCost (String pi_size, int che_topping, int peper_topping, int ham_topping)

public String getDesc (String pi_size, int che_topping, int peper_topping, int ham_topping)

public Pizza(String pi_size, int che_topping, int peper_topping, int ham_topping)



I'll post what I've changed your code to after I go through everything. Please though, don't
For each and every method header you have, you are asking for four separate parameters, however, you are only calling it with an OBJECT. The best way to fix what your biggest errors is to get rid of all your parameters.

On a side note, for your parameter names, you should also call your variables names that are a little easier to keep up with and don't require quite so much typing. So... int che_topping would become int cheese.
Get what I'm saying?

Next...

if (pi_size == "small") {
double prize = 10;
} else if (pi_size == "medium") {
double prize = 12;
} else if (pi_size == "small"){
double prize = 14;
} else {
double prize = 0;
}[/code

You've already declared the variable prize up top, so declaring it again is just extra typing and compile time errors. Your main problem, though, is you're trying to compare a string and another string using[il]==[/il]. You've got your head in the right place, however, you should be using the [il].equals(String s)[/il] method. So... [il]if(size.equals("small"))[/il], etc.

[code]public String getDesc (String pi_size, int che_topping, int peper_topping, int ham_topping) {

System.out.println("The pizza is of size "+pi_size+ "with "+che_topping+" Chesse toppings, "+peper_topping+" Peperoni toppings and "
+ham_topping+" Ham toppings");
}



Next, we have your makeshift to string method. If you look at your header(remember, I've removed the parameters), you'll see that this is supposed to return a String. This method lacks a return statement at all... If you want to do that, then change String to void. Else, make a string out of your output and return the string. However, a public String toString() method is better to use. You don't have to call the toString and you can just print it like you would a variable!

Lastly, we have your main method...

public static void main (String[] args) {
Pizza myPizza = new Pizza ("small", 0, 0, 2);
double calcCost = (myPizza);
double getDesc = myPizza.getDesc();
}



There are a couple things wrong here. To start with, you declare your pizza properly. That's perfect and you have a working constructor as well!
Next though, you try and call a method. Since calcCost is not static, you need to use an object (myPizza) to call it and you need to assign it to a VARIABLE( such as double cost =.... To call your method, use the '.' operator. So.... double cost = myPizza.calcCost(); will do what you want.
Next, you're trying to assign a type String to a type double. Do for this what I said above and assign it to a String variable and then your method would work perfectly. However, I changed that to a toString... so, instead of having to reassign it your output... you can just use call it in a print statement...

Now, as a whole, I understand you're new. This program gets a D based on organization and indention... proper indenting is actually pretty important. It helps you follow your program's chain of command and if needed, helps you debug your program. Remember, if your IDE isn't doing it for you, every time you hit enter, press tab as well!

Please read through what I said and look at your code so you won't make the same mistake twice! You've done it once and we forgive you, but next time... we'll charge you you life! The working code is below.

- Zach

public class Pizza {

    private String size;
    private int cheese, pepper, Ham, prize;
    
    
    //constructor
    public Pizza(String piSize, int che, int peper, int ham){
        size = piSize;
        cheese = che;
        pepper = peper;
        Ham = ham;
    }
    
    
    public double calcCost (){
        if (this.size.equals("small"))
            prize = 10;
        else if (this.size.equals("medium"))
            prize = 12;
        else if (this.size.equals("large"))
            prize = 14;
        else 
            prize = 0;
        return (prize + (this.cheese *2) + (this.pepper*2) + (this.Ham*2));
    
    }
    
    public String toString(){
        String str = "This is a "+size+ " pizza with "+cheese+" Cheese toppings,\n"+pepper+" Pepperoni toppings and \n"+Ham+" Ham toppings."+"\nIt will cost $"+this.calcCost()+"";
        return str;
    }
    
    public static void main (String[] args) {
        Pizza myPizza = new Pizza ("small", 0, 0, 2);
        double pizzaCost = myPizza.calcCost();
        System.out.print(myPizza);
    }
}



EDIT: Looking back at the code briefly... does anyone else notice that Ham seems to be a reserved word? Anyone care to shed some light on this for me?

This post has been edited by Simple_Condolences: 01 March 2010 - 08:00 AM

Was This Post Helpful? 1
  • +
  • -

#6 Guest_Guest*


Reputation:

Re: Pizza object sent to a double

Posted 01 March 2010 - 08:49 AM

a big thanks to you guys, u r a life saver on this... thanks alot
Was This Post Helpful? 0

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Pizza object sent to a double

Posted 01 March 2010 - 06:21 PM

Please respect rule #5 of this forum and give your topic a descriptive title
"Java: is kind of completly useless in this forum as it might apply to all posts (or at least 99.9% of them)
Was This Post Helpful? 0
  • +
  • -

#8 nano-gilmour   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 39
  • Joined: 07-February 10

Re: Pizza object sent to a double

Posted 01 March 2010 - 06:32 PM

well, instead of
public double calcCost (String pi_size, int che_topping, int peper_topping, int ham_topping) 

you can make the method's parameter a pizza and access the variables u need with
this.instance_variable

but you'll need to make your variables public to do that or by defining get methods to return the variable you want from your pizza and using them.
hope this helps :D
Was This Post Helpful? 0
  • +
  • -

#9 Simple_Condolences   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 180
  • Joined: 10-January 10

Re: Pizza object sent to a double

Posted 02 March 2010 - 07:29 AM

View Postnano-gilmour, on 01 March 2010 - 05:32 PM, said:

well, instead of
public double calcCost (String pi_size, int che_topping, int peper_topping, int ham_topping) 

you can make the method's parameter a pizza and access the variables u need with
this.instance_variable

but you'll need to make your variables public to do that or by defining get methods to return the variable you want from your pizza and using them.
hope this helps :D

I think you're confused a little.

public double calcCost(Pizza a)
{
}


isn't the same as
public double calcCost()
{
}
[/cost]

This is used to reference the object used to invoke a method.
So...
[code]
blah blah blah
public static void main(String[] args){
    {
        Pizza a = new Pizza(params);
        double price = a.calcCost();
    }

    public double calcCost()
    {
        //method body omitted
    }
}



If I sent the pizza as a parameter, I wouldn't use this. That would be redundant and poor coding.

- Zach
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1