Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Help with CokeMachine code for school

    This post is going to seem long because of the coding in it, but the solutions I need are simple.

    BASICALLY
    I need to know:

    1) where to use the if's and else's
    2)how to call the boolean method isEmpty() from the CanOfCoke class
    3)how to apply methods from the other classes to the machine, bin, and can variables
    4) how to check to see if an ArrayList has any content (ie if the student has any coins or not)

    This is complicated for me to complain because I'm a complete n00b and this is my first Java class, but I hope someone can help me because my teacher isn't all that great at explaining things. and I'm stuck on the LAST method of the LAST class of our FINAL project lol.

    The project has 5 classes:

    Coin
    DrinksMachine
    CanOfCoke
    GarbageDisposalUnit
    Student

    I am working on the Student class and here is the code I have so far:

    Code:
    import java.util.ArrayList;
    /**
     * Write a description of class Student here.
     * 
     * @author xxx
     * @version Project part 5
     */
    public class Student
    {
        private String name;
        private ArrayList purse;
        private boolean sobbing;
        private DrinksMachine machine;
        private GarbageDisposalUnit bin;
        private CanOfCoke can;
    
        /**
         * Constructor for objects of class Student
         * 
         * @ param pName The name of the studen
         * @ param nCoins The number of coins the student has
         */
        public Student(String pName, int nCoins)
        {
            name = pName;
            purse = new ArrayList<Coin>(nCoins);
            sobbing = false;
            machine = null;
            bin = null;
            can = null;
            
        }
    
        /**
         * This method will set the students machine variable
         * 
         * @param  name of the machine
         */
        public void setMachine(DrinksMachine aMachine)
        {
            machine = aMachine;
        }
        
        /**
         * This method will set the students garbage disposal unit
         * 
         * @param name of the garbage disposal unit
         */
        public void setBin(GarbageDisposalUnit aBin)
        {
            bin = aBin;
        }
        
        /**
         * This method will print out information about the student
         * 
         */
        public void displaySelf()
        {
            if (can == null)
            {
                System.out.println(name + " does not have a can of Coke.");
            }
            else
            {
                System.out.println(name + " has a can of Coke.");
            }
            if (sobbing == true)
            {
                System.out.println(name + " is sobbing.");
            }
            else
            {
                System.out.println(name + " is not sobbing.");
            }
            System.out.println(name + " has" + purse.size() + " coins in their purse.");
            
        }
        
        /**
         * This method will perform the actions of:
         * 
         * If they have a can of coke:
         * The student takes a single sip from a can of Coke and puts it in the trash bin when empty.
         * 
         * If they don't have a can of coke:
         * The student inserts a coin and obtain the can of coke and take a sip from it.
         * 
         * If they can't get a can of coke:
         * The student sobs.
         * 
         */
        public void doAction()
        {
        if (can != null)
        {
            can.giveSip();
        }
            
        else if (can.isEmpty())
        {
                bin.addCan(bi);
                can = null;
        }
        
        else
        {
            if (purse != null && can != isEmpty())
            {
                machine.insertCoin();
                machine.deliverCan();
                can.open();
                can.giveSip();
            }
            else
            {
                sobbing = true;
            }
        }  
        }
    }
    This is the part I am having troubles with:

    [CODE

    public void doAction()
    {
    if (can != null)
    {
    can.giveSip();
    }

    else if (can.isEmpty())
    {
    bin.addCan(bi);
    can = null;
    }

    else
    {
    if (purse != null && can != isEmpty())
    {
    machine.insertCoin();
    machine.deliverCan();
    can.open();
    can.giveSip();
    }
    else
    {
    sobbing = true;
    }
    }
    }
    }
    [/CODE]

    Here are his horrible instructions:

    a method with signature public void doAction() which performs the following actions:
    i. if the Student has a can of Coke then they take a single sip from it. If it becomes empty they put it in the bin, and cease to hold it (i.e. can is set to null). The doAction() method then exits.
    ii. if the Student does not have a can of coke then they insert a Coin(if they have one) into the coke machine (if it isn't empty), take the can of Coke they have paid for, and take a sip from it (after opening the can). If for any reason they are unable to get a can of Coke (machine empty, or no Coins) they begin to sob.
    iii. Pseudo-code for this is shown below:
    IF the student has a can THEN
    take a sip
    IF the can is now empty THEN
    add it to the bin
    OTHERWISE (i.e. the student does not have a can to start with)
    IF the student has a Coin AND the machine is not empty THEN
    insert a Coin into the machine
    reduce Coins by one
    get the can from the machine
    open the can and take a sip
    OTHERWISE
    student starts sobbing
    Last edited by msl27620; 11-12-2010 at 12:14 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    Virginia
    Posts
    112
    Thanks
    11
    Thanked 7 Times in 7 Posts
    Code:
    import java.util.ArrayList;
    /**
     * Write a description of class Student here.
     * 
     * @author xxx
     * @version Project part 5
     */
    public class Student
    {
        private String name;
        private ArrayList purse;
        private boolean sobbing;
        private DrinksMachine machine;
        private GarbageDisposalUnit bin;
        private CanOfCoke can;
    
        /**
         * Constructor for objects of class Student
         * 
         * @ param pName The name of the studen
         * @ param nCoins The number of coins the student has
         */
        public Student(String pName, int nCoins)
        {
            name = pName;
            purse = new ArrayList<Coin>(nCoins);
            sobbing = false;
            machine = null;
            bin = null;
            can = null;
            
        }
    
        /**
         * This method will set the students machine variable
         * 
         * @param  name of the machine
         */
        public void setMachine(DrinksMachine aMachine)
        {
            machine = aMachine;
        }
        
        /**
         * This method will set the students garbage disposal unit
         * 
         * @param name of the garbage disposal unit
         */
        public void setBin(GarbageDisposalUnit aBin)
        {
            bin = aBin;
        }
        
        /**
         * This method will print out information about the student
         * 
         */
        public void displaySelf()
        {
            if (can == null)
            {
                System.out.println(name + " does not have a can of Coke.");
            }
            else
            {
                System.out.println(name + " has a can of Coke.");
            }
            if (sobbing == true)
            {
                System.out.println(name + " is sobbing.");
            }
            else
            {
                System.out.println(name + " is not sobbing.");
            }
            System.out.println(name + " has" + purse.size() + " coins in their purse.");
            
        }
        
        /**
         * This method will perform the actions of:
         * 
         * If they have a can of coke:
         * The student takes a single sip from a can of Coke and puts it in the trash bin when empty.
         * 
         * If they don't have a can of coke:
         * The student inserts a coin and obtain the can of coke and take a sip from it.
         * 
         * If they can't get a can of coke:
         * The student sobs.
         * 
         */
        public void doAction()
        {
        if (can != null)
        {
            can.giveSip();
        }
            
        else if (can.isEmpty())
        {
                bin.addCan(bi);
                can = null;
        }
        
        else
        {
            if (purse != null && can != isEmpty())
            {
                machine.insertCoin();
                machine.deliverCan();
                can.open();
                can.giveSip();
            }
            else
            {
                sobbing = true;
            }
        }  
        }
    }
    If the can object is not null,
    then that means they successfully got a soda from the machine.
    So take a sip, then throw away.

    Code:
    if (can != null)
    {
    takesip()
    throwaway()
    }
    else if (can == null)
    {
    if ((purse() == false) || (machine.isEmpty()))
    {
    sob();
    }
    else
    {
    getSodaPop()
    takeSip()
    }
    }
    If can is null,
    check the purse for money and machine for soda,
    if they don't have any then sob,
    else get a soda and take a sip

    Btw this is a javascript forum.
    Last edited by Afro_Programmer; 11-10-2010 at 04:04 PM.

  • Users who have thanked Afro_Programmer for this post:

    msl27620 (11-11-2010)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Afro should have written that last line thus:
    Btw this is a javascript forum.
    Hint: JavaScript and Java share the first 4 letters of their names...and not a whole bunch else. (Oh, okay, except some superficial syntax similarities.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Afro_Programmer View Post
    Code:
    import java.util.ArrayList;
    /**
     * Write a description of class Student here.
     * 
     * @author xxx
     * @version Project part 5
     */
    public class Student
    {
        private String name;
        private ArrayList purse;
        private boolean sobbing;
        private DrinksMachine machine;
        private GarbageDisposalUnit bin;
        private CanOfCoke can;
    
        /**
         * Constructor for objects of class Student
         * 
         * @ param pName The name of the studen
         * @ param nCoins The number of coins the student has
         */
        public Student(String pName, int nCoins)
        {
            name = pName;
            purse = new ArrayList<Coin>(nCoins);
            sobbing = false;
            machine = null;
            bin = null;
            can = null;
            
        }
    
        /**
         * This method will set the students machine variable
         * 
         * @param  name of the machine
         */
        public void setMachine(DrinksMachine aMachine)
        {
            machine = aMachine;
        }
        
        /**
         * This method will set the students garbage disposal unit
         * 
         * @param name of the garbage disposal unit
         */
        public void setBin(GarbageDisposalUnit aBin)
        {
            bin = aBin;
        }
        
        /**
         * This method will print out information about the student
         * 
         */
        public void displaySelf()
        {
            if (can == null)
            {
                System.out.println(name + " does not have a can of Coke.");
            }
            else
            {
                System.out.println(name + " has a can of Coke.");
            }
            if (sobbing == true)
            {
                System.out.println(name + " is sobbing.");
            }
            else
            {
                System.out.println(name + " is not sobbing.");
            }
            System.out.println(name + " has" + purse.size() + " coins in their purse.");
            
        }
        
        /**
         * This method will perform the actions of:
         * 
         * If they have a can of coke:
         * The student takes a single sip from a can of Coke and puts it in the trash bin when empty.
         * 
         * If they don't have a can of coke:
         * The student inserts a coin and obtain the can of coke and take a sip from it.
         * 
         * If they can't get a can of coke:
         * The student sobs.
         * 
         */
        public void doAction()
        {
        if (can != null)
        {
            can.giveSip();
        }
            
        else if (can.isEmpty())
        {
                bin.addCan(bi);
                can = null;
        }
        
        else
        {
            if (purse != null && can != isEmpty())
            {
                machine.insertCoin();
                machine.deliverCan();
                can.open();
                can.giveSip();
            }
            else
            {
                sobbing = true;
            }
        }  
        }
    }
    If the can object is not null,
    then that means they successfully got a soda from the machine.
    So take a sip, then throw away.

    Code:
    if (can != null)
    {
    takesip()
    throwaway()
    }
    else if (can == null)
    {
    if ((purse() == false) || (machine.isEmpty()))
    {
    sob();
    }
    else
    {
    getSodaPop()
    takeSip()
    }
    }
    If can is null,
    check the purse for money and machine for soda,
    if they don't have any then sob,
    else get a soda and take a sip

    Btw this is a javascript forum.
    This helps, but I'm still missing a link. It's my fault because I forgot to mention that each can has a total of 5 sips until it is empty. It should not be recycled in the bin until it is empty. In another class in the program called CanOfCoke is a boolean method called isEmpty() ... I can't right an if statement that is (isEmpty() == true) because a method can't be true or false can it? So how do I do the equivalent of that?

  • #5
    New Coder
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    8
    Thanked 0 Times in 0 Posts
    okay, now I have this

    Code:
    public void doAction()
        {
        if (can != null)
        {
            can.giveSip();
        }
            
        else if (can.isEmpty())
        {
                bin.addCan(can);
                can = null;
        }
        
        else
        {
            if (purse != null || !can.isEmpty())
            {
                machine.insertCoin(purse.get(purse.lastIndexOf(Coin coin)));
                machine.deliverCan();
                can.open();
                can.giveSip();
            }
            else
            {
                sobbing = true;
            }
        }  
        }
    }
    machine.insertCoin(purse.get(purse.lastIndexOf(Coin coin)));

    is the only thing I can't get to work

    it keeps saying ')' expected and I don't know why

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    *PLEASE* do *NOT* continue this thread in this forum.

    You already asked the question in the Java forum.

    Having said that: You can't specify the TYPE of an argument when *CALLING* a method! You specify it when *declarling* the method.

    machine.insertCoin(purse.get(purse.lastIndexOf(Coin coin)));

    Coin is BOGUS.

    Now, NO MORE POSTS IN THIS THREAD.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Post: Grow Up YOUNG Pedant and learn some tact and manners.


  •  

    Posting Permissions

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