Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-11-2010, 11:21 AM   PM User | #1
msl27620
New Coder

 
Join Date: Nov 2010
Posts: 14
Thanks: 8
Thanked 0 Times in 0 Posts
msl27620 is an unknown quantity at this point
CokeMachine complicated IF statement for class

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;
}
}
}
}
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-11-2010 at 11:08 PM..
msl27620 is offline   Reply With Quote
Old 11-11-2010, 04:22 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Ok, this is close, so I don't consider it plagiarism for you to get an alteration in the code structure.
Lets look at the pseudocode:
Code:
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
Without the endif statements, the above appears correct, but it will not match what you intend to do. The correct pseudo code appears to be:
Code:
IF the student has a can THEN
    take a sip
    IF the can is now empty THEN
        add it to the bin
    ENDIF
OTHERWISE 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
Mapping the otherwise to else:
Code:
IF (condition)
    dostuff
    IF (condition)
        dostuff
    ENDIF
ELSE IF (condition)
    dostuff
ELSE
    dostuff
ENDIF
Now, lets alter the structure of your current code.
Code:
public void doAction()
{
    if (can != null) // First condition
    {
        can.giveSip();
        if (can.isEmpty()) // Second condition
        {
            bin.addCan(bi);
            can = null;
        }
    }
    else if (purse != null && can != isEmpty())
    {
        machine.insertCoin();
        machine.deliverCan();
        can.open();
        can.giveSip();
    }
    else
    {
        sobbing = true;
    }
}
Now what we have is if the can is not null, take a sip. If its now empty, we dispose of the can and remove it from this class and the method is satisfied.
If we don't have a can, we check to see if purse is null (this is wrong, we'll get into that) and that the can is not empty (also wrong, we'll get into that too). If we have money and the can is not empty, we then purchase a new can, open it, and sip it. Otherwise, we start to sob.
The changes you will need to still do are here:
PHP Code:
    else if (purse != null && can != isEmpty())
    {
        
machine.insertCoin();
        
machine.deliverCan();
        
can.open();
        
can.giveSip();
    } 
purse will not be null, and you don't want to check if the can is empty since you don't care if it is by this point. As you've asked, the ArrayList is a typeof Collection<T>, so you retrieve the number of items with the size() method. If its > 0, you have items within your collection. Alternatively, isEmpty will return true if the ArrayList has no items. The in action checks are if (list.size() > 0) or if (!list.isEmpty()).

The check for can is also wrong, isEmpty is unlikely to return any results of type CanOfCoke (I don't see it defined in Student, so it actually won't compile), but its not the can you want to check - its the machine (defined here: and the coke machine (if it isn't empty)). If the CokeMachine is also a collection, it will also have an isEmpty method. Using checks against size() or isEmpty() on your purse and machine variables need to result in if (true && true) in order to process. That should give you enough information to get that one fixed up.

And finally, within that elseif block, the current Student needs to obtain the can itself. I assume that the machine and can take care of themselves since I don't know the methods on them. If the current Student does not obtain the can, they will repeatably purchase a can and take a sip, but never discard it.

Try those changes and post back if you run into any more trouble!
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
msl27620 (11-11-2010)
Old 11-11-2010, 11:07 PM   PM User | #3
msl27620
New Coder

 
Join Date: Nov 2010
Posts: 14
Thanks: 8
Thanked 0 Times in 0 Posts
msl27620 is an unknown quantity at this point
That was great help. Need a job teaching at a University? You can replace this guy haha. Thanks again for the help!
msl27620 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:28 PM.


Advertisement
Log in to turn off these ads.