Results 1 to 4 of 4
Thread: instance method problem
- 07-23-2010, 06:59 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 2
- Rep Power
- 0
instance method problem
hi, i'm tottaly new to the forum and also to java. i have a question i wish to be solved...I made an application by myself. Some simple classes, creating and reading objects.Code for one of 'em is:
public class mainDish{
public mainDish(int a){
String[] foodname= new String[3];
foodname[0]= "meat";
foodname[1]= "fish";
foodname[2]= "pasta";
System.out.println("Menu includes "+foodname[0]+" "+foodname[1]+" "+foodname[2]);
System.out.println("\nYou have chosen "+foodname[a]);
}
}
This gets compiled normally (gladly). Now the problematic one is:
public class prices{
public prices(int a){
int meatprice, fishprice, pastaprice;
meatprice=8;
fishprice=12;
pastaprice=4;
public int calculator() {
if (foodname[a]== "meat"){
return meatprice;
}else if (foodname[a]=="fish"){
return fishprice;
}else if (foodname[a]=="pasta"){
return pastaprice;
}
}
}
}
The problem is on compilation, line7: says public is wrong declaration; also in the same line, says it expects ";"...what is wrong? Is it about the reference to the object from the other class or to the construction of the method?
- 07-23-2010, 07:30 PM #2
Your passing your variables totally wrong. Your not even passing the array. This program is very backwards.
Actually None of your code makes any sense :/
I would suggest reading some tutorials.
Basically you should have
1. Driver class (usually main)
2. Object Classes
Here
Driver:
Object:Java Code:public class mainDish { public mainDish(int a){ Object[] foodname= new Object[3]; fish Salmon = new fish("Salmon",12.00); foodname[0]= "meat"; foodname[1]= Salmon; foodname[2]= "pasta"; System.out.println("Menu includes "+foodname[0]+" "+foodname[1]+" "+foodname[2]); System.out.println("\nYou have chosen "+foodname[a]); } }
Java Code:public class fish { String name = ""; double price = 0.00; public fish(String Name, double Price){ name = Name; price = Price; } public void setFishPrice(double Price){ price = Price; } public void setFishName(String Name){ name = Name; } public double getFishPrice(){ return price; } public String getFishName(){ return name; } }Last edited by Sno; 07-23-2010 at 07:46 PM.
:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 07-24-2010, 09:22 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 2
- Rep Power
- 0
i actually have one main class i didn't write it down..nevermind, since it's wrong, i'll try this one..but actually i'm reading the tutorial provided by sun.. i don't know why i'm learning java the wrong way
- 07-24-2010, 06:30 PM #4
The compiler error is provided by the fact that you didn't close the bracket for the prices(int a) constructor. (Which should have been just before the calculator() declaration).
Additionally, Sno is right in that you are passing variables wrong; the foodname array is local to the mainDish class constructor. This means that it's outside of the scope of the calculator() function.
If I were you, I'd read up on scope:
Wikipedia: Scope (programming)
Variable Scope : Variable Scope*«*Language*«*Java Tutorial
Variables in Java by Richard G Baldwin
Or, alternately, you could go with Sno's method. It's your call!
Similar Threads
-
Using an Instance method
By Thumper in forum New To JavaReplies: 4Last Post: 12-09-2009, 06:43 PM -
Accessing instance outside of creation method
By meringue in forum New To JavaReplies: 6Last Post: 03-22-2009, 01:36 AM -
public instance method
By steve123 in forum New To JavaReplies: 5Last Post: 06-20-2008, 08:45 PM -
Instantiation using an instance factory method
By Java Tip in forum Java TipReplies: 0Last Post: 03-29-2008, 12:35 PM -
Instantiation using an instance factory method
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks