Results 1 to 4 of 4
  1. #1
    vassilianna is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Question 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?

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    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:
    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]);
    		}
    }
    Object:
    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 ~-'

  3. #3
    vassilianna is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    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

  1. Using an Instance method
    By Thumper in forum New To Java
    Replies: 4
    Last Post: 12-09-2009, 06:43 PM
  2. Accessing instance outside of creation method
    By meringue in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 01:36 AM
  3. public instance method
    By steve123 in forum New To Java
    Replies: 5
    Last Post: 06-20-2008, 08:45 PM
  4. Instantiation using an instance factory method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 12:35 PM
  5. Instantiation using an instance factory method
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •