Results 1 to 11 of 11
  1. #1
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default Why am I getting an error when calling this method?

    Hello again everyone,

    I have a class named MachineCabinet that contains the following method

    public void unloadBevarage(int row, int col) {
    }

    I also have a class called KeyPadDisplay. Within this class I have a method called unLoadBeverage that calls up the unloadBeverage method in MachineCabinet.
    Here is the method from the KeyPadDisplay and the code under it I use to call the method in MachineCabinet

    public void unLoadBeverage() {
    int row;
    int col;

    MachineCabinet machineCabinet = new MachineCabinet.unloadBevarage(row, col);
    machineCabinet.unloadBevarage(row, col);

    I am gettting a red sqwiggle in the first line under unLoadBeverage that says

    cannotfindsymbol
    symbol:class unLoadBeverage
    location coldbeveragejava.MachineCabinet

    With my newbie experience I believe these two lines of code should call the method, but I must be mistaken. Any suggestions?

    Morgan

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    do you change the spelling of beverage to bevarage? You need to be consistent with spelling and capitalization.

  3. #3
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    I know they spell it both ways depending on the method, let me go back and make sure I did it the same as the original code writers

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you show your full code? Are both these classes in the same package? If not you have to import them to a common place.

  5. #5
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Both are in the same package yes, There are about 12 classes total in the package but here are the MachineCabinet and the KeyPadDisplay classes


    package coldbeveragejava;

    /**
    *
    * @author dan
    */
    public class MachineCabinet {


    private final static double N_Ounces = 16.0;
    //do not make private so that other objects in package can access directly

    final static int N_ROWS = 5; //Will always be 5 rows
    final static int N_COLS = 6; //Will always be 6 columns

    //Creates beverageInventory
    static Beverage[][] beverageInventory = new Beverage[N_ROWS][N_COLS];


    //BeverageNames will be the same because it's final
    //one beverage per row
    private static final String[] beverageNames =
    {"Regular Coca-Cola", "Diet Coca-Cola",
    "Snapple Ice Tea Lemon","Diet Snapple Ice Tea Lemon",
    "Slice","Diet Slice"};

    //Creates instance variables for the MachineCabinet class
    private static int[] prices = {80, 80, 90, 90, 80, 80}; //one price per beverage
    private static final int N_Bottles = 1;
    private static String imagePath=null;
    private static AccountRepository repository=null;
    private static KeyPadDisplay keyPadDisplay=null; //contains the menus to drive program
    private static DebitCardReader debitCardReader =null;

    //Beginning of the MachineCabinet main method
    public static final void main(String[] args){


    //Constructor: create the beverage inventory and populate with beverages
    for(int r=0; r< N_ROWS; r++){
    for(int c=0; c<N_COLS; c++){

    beverageInventory[r][c] =
    new Beverage(beverageNames[r], N_Ounces,prices[r],
    N_Bottles,imagePath);
    }
    }

    //create the AccountRepository by calling AccountRepository.getInstance()
    System.out.println("Entering AccountRepository getInstance");
    repository = AccountRepository.getInstance();

    //and call the addAccount method 2 times with the values 1 and 100
    //2 and 200 (go look at the code in AccountRepository)
    System.out.println("Entering AccountRepository addAccount");
    repository.addAccount(1, 100);
    repository.addAccount(2, 200);


    //create the DebitCard and KeyPadDisplay reader objects
    //by calling the getInstance() method in each. Use AccountRepository
    //as an example.
    System.out.println("Entering DebitCardReader getInstance");
    debitCardReader = DebitCardReader.getInstance();

    //NOTE KeypadDisplay.getInstance() must be called last or the program will hang, since
    // the Constructor of the KeyPadDisplay, invokes a while loop that prompts for input from
    // the user.
    System.out.println("Entering KeyPadDisplay getInstance");
    keyPadDisplay = KeyPadDisplay.getInstance(); //launches do-forever while loop


    }//End of Main Method

    public Beverage[][] getBeverageInventory() {
    return beverageInventory;
    }

    public static int reserveBeverage(int row, int col) {
    System.out.println("Entering MachineCabinet reserveBeverage");
    return prices[row]; //same beverage for row for IT170
    }

    public int getN_Bottles() {
    return N_Bottles;
    }

    public String getImagePath() {
    return imagePath;
    }

    public void setImagePath(String val) {
    this.imagePath = val;
    }

    public AccountRepository getRepository() {
    return repository;
    }

    public void setRepository(AccountRepository val) {
    this.repository = val;
    }

    public KeyPadDisplay getKeyPadDisplay() {
    return keyPadDisplay;
    }

    public void setKeyPadDisplay(KeyPadDisplay val) {
    this.keyPadDisplay = val;
    }

    public DebitCardReader getDebitCardReader() {
    return debitCardReader;
    }

    public void setDebitCardReader(DebitCardReader val) {
    this.debitCardReader = val;
    }


    public int[] getPrices() {
    return prices;
    }

    public void unloadBevarage(int row, int col) {
    }

    }//MachineCabinet.java


    package coldbeveragejava;
    import java.io.*;
    import java.util.Scanner;


    /**
    *
    * @author dan
    */
    public class KeyPadDisplay {
    private static AccountRepository accountCheck;
    private static KeyPadDisplay instance;
    private static KeyPadDisplay keyPadDisplay;

    //price of beverage
    int price;

    //Make an instance variable to share across method
    //by making the constructor private and calling getInstance, we ensure
    //that only 1 KeyPadDisplay will be created.
    public static KeyPadDisplay getInstance(){
    if(instance==null)
    instance = new KeyPadDisplay();
    return instance;
    }
    /** Creates a new instance of KeyPadDisplay use Singleton "private"
    * Constructor to be called from getInstance()
    */
    private KeyPadDisplay() {

    //begin while loop for getting input here to drive the program
    boolean done = false;

    while(done==false){

    System.out.println("Enter 'q' to quit menu");

    //Write a prompt to enter a 'd' to exit while loop
    System.out.println("Enter 'd' to insert debitcard and accountID: ");

    //Write a prompt to enter an 's' to call displayBottles()
    System.out.println("Enter 's' to call displayBottles(): ");

    //Write a prompt to enter a ‘r’ to call reserveBeverage()
    // in the MachineCabinet class
    System.out.println("Enter 'r' to call reserveBeverage(): ");

    int theChar = 0;
    try{
    theChar = System.in.read();
    }catch(java.io.IOException io){
    io.printStackTrace();
    System.exit(1);
    }
    switch(theChar){
    case 'q':
    done = true;
    break;
    case 'd': //remember that characters are numbers too
    askUserToInsertDebitCard();
    break;
    case 's':
    displayBottles();
    break;
    case 'r':
    Scanner input = new Scanner(System.in);
    int row;
    int column;

    //Ask user to enter row number
    System.out.println("Please enter a row number: ");
    row = input.nextInt();

    //Ask user to enter a column number
    System.out.println("Please enter a column number: ");
    column = input.nextInt();

    // Call reserveBeverage(row,colum) in MachineCabinet
    // Set the instance variable 'price' to the result of
    // MachineCabinet.reserveBeverage
    // Note: Machine.Beverage(row, col) is a static method.
    price = MachineCabinet.reserveBeverage(row, column);

    // if (price<0) that means that there are no bottles left in the column
    // just print an error message and the program will loop again asking for input
    if (price<0){
    System.out.println("There are no bottles left in the column" );
    }
    else{
    System.out.println("Price of beverage = " + price + " cents");
    }

    break;

    }//End of Switch
    }//End of while loop
    }//End of KeyPadDisplay()



    public void displayBottles() {

    //get the array of bottles from MachineCabinet: getBeverageInventory()
    MachineCabinet bevInvent = new MachineCabinet();
    bevInvent.getBeverageInventory();

    //copy and paste the nested for loop from MachineCabinet.main, change it
    //to print the name of each beverage
    //Hint: N_ROWS can be accessed as MachineCabinet.N_ROWS

    for(int r=0; r<= MachineCabinet.N_ROWS; r++){
    if (r==0){
    System.out.println ("Coca Cola Classic");
    }
    else if (r==1){
    System.out.println("Diet Coke");
    }
    else if (r==2){
    System.out.println("Snapple Lemon Iced Tea");
    }
    else if (r==3){
    System.out.println("Diet Snapple Lemon Iced Tea");
    }
    else if (r==4){
    System.out.println("Slice");
    }
    else if (r==5){
    System.out.println("Diet Slice");
    }
    }//end for loop
    }//displayBottles

    public void reserveBevarage(int row, int col) {
    }
    public void showBeverages() {
    }

    public void askUserToInsertDebitCard() {

    int currentAccountID;
    int amountPaid;

    //Creates the two scanner objects that will to hold the
    //accountID and the amount deposited by the user
    Scanner input = new Scanner(System.in);
    Scanner userCentInput = new Scanner(System.in);

    //displays a message and sets the user output to the ID variable
    displayMessage("Please insert debit card" +
    " - Enter account ID (e.g., 1 or 2)");
    currentAccountID = input.nextInt();

    //Calls getAcount() in AccountRepository to check the value with
    //the array of pre-existing accountID's
    accountCheck = AccountRepository.getInstance();
    accountCheck.getAcount(currentAccountID);

    //saves a reference to this fetched account
    // so it can be used to create a new instance of DebitCard.
    Account account = accountCheck.getAcount(currentAccountID);

    //generates a new DebitCard class with the account
    //object created earlier
    DebitCard debitCard = new DebitCard(account);

    //Asks user to insert the amount to be Paid in cents
    displayMessage("Please enter the amount to be deposited in cents");
    amountPaid = userCentInput.nextInt();

    //Creates a debitCardReader object and then calls the
    //insertDebitCard() method
    DebitCardReader debitCardReader = DebitCardReader.getInstance();
    debitCardReader.insertDebitCard(keyPadDisplay, debitCard,price);

    //calls the DebitAccount method while also calling the
    //getAccountonDebitCard() method within a parameter
    debitCard.debitAccount(debitCard.getAccountOnDebit Card(),amountPaid);

    keyPadDisplay.displayMessage("Debiting account " +
    debitCard.getAccountOnDebitCard().getAccountNumber ());

    keyPadDisplay.unLoadBeverage();

    }

    public void inserDebitCard(int accountID) {
    }

    public void displayMessage(String msg) {
    System.out.println(msg);
    }

    public int getPrice() {
    return price;
    }

    public void setPrice(int val) {
    this.price = val;
    }

    public void unLoadBeverage() {
    int row;
    int col;

    MachineCabinet machineCabinet = new MachineCabinet.unloadBevarage(row, col);
    machineCabinet.unloadBevarage(row, col);


    }


    }

  6. #6
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Oh ya
    If it helps, I am supposed to call the method in MachineCabinet with the row and col int vartiables that already exist in the KeyPadDisplay class (In the while loop) but I do not know how to do this so I just made new variables as you can see at the bottom of the KeyPadDisplay class

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    So any idea on why it wont call the method?

  9. #9
    anil.bharadia is offline Member
    Join Date
    Aug 2008
    Location
    india
    Posts
    3
    Rep Power
    0

    Default try this

    you have wrote these lines:

    1) MachineCabinet machineCabinet = new MachineCabinet.unloadBevarage(row,col);

    2) machineCabinet.unloadBevarage(row, col);

    ok.

    now try this:

    MachineCabinet machineCabinet = new MachineCabinet();
    machineCabinet.unloadBevarage(row, col);

    bcoz line 1) MachineCabinet.unloadBevarage(row,col); is the instance of method, not an object of class..
    then also u giving this instance of method to the object of class to refer
    thats why it considers the MachineCabinet.unloadBevarage as the class
    and it do not finds the class def.

    it might work i have not even try this but its simple and tell me the result

  10. #10
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    hey thanks,
    that worked, i see what i did wrong, problem solved!

  11. #11
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by CirKuT View Post

    MachineCabinet machineCabinet = new MachineCabinet.unloadBevarage(row, col);
    machineCabinet.unloadBevarage(row, col);


    Morgan
    unloadBevarage is not the inner class
    i think u should write

    MachineCabinet machineCabinet = new MachineCabinet()
    machineCabinet.unloadBevarage(row, col);

Similar Threads

  1. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  2. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM
  3. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM
  4. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 PM

Posting Permissions

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