Russell Havik's Web, where all things started :)
 
home | for fun | graphics | me | portfolio | links | CELTICS.COM | CELTICS SCHEDULES & RESULTS
home / for fun / my java / Exception
 
 
Below links are designed for those who doesn't have Flash Plugins installed :)
 
Articles | My Celtics | My Java | My Prayers | Time to Brag | My win32 Applications
 

my JAVA - Exception
Please note that coding were created by me, if you want to use them, please state a little credit for me :-)
Most important. please use them for learning purpose only. No cheating and copying code for cut and paste, lots of practice will help to improve your knowledge...remember, no lemons no melons...

Please note: after you copy the codes below, save the file as file_name.JAVA and make sure the CLASS name is EXACTLY the same as the filename that you saved before...
Example: MyFirstJava.JAVA---contains class MyFirstJava{......}; easy peasy...:-)

The 'tab rules' suck when it comes to html, so don't be shock to see evry codes are on the same left side..sowie...

 

Coffee: Introducing the use of "exception"

// CupOfCoffee C = new cupOfCoffee;
// C.addCup(1)
// C.addMilk(100);
// C,addCup(1) - Error, u got a cup already
// C.addWater(200)
// C.drink error, cause havent stir
// C.Stir() - println stir stir
// C.Drink() - ok
// C.Drink - cannot drink, empty

public class Coffee
{
public static void main(String s[])
{
CupOfCoffee C = new CupOfCoffee();
C.addCup(1);
C.addCoffee(1);
C.addSugar(3);
C.addMilk(30);
C.addMilk(20);
C.addMilk(50);
C.addWater(150);
C.Stir(true);
C.Drink(true);

}
};


class CupOfCoffee


{
private int coffee,
sugar,
milk,
water,
cup;

private boolean stir,
drink;

private static int maxMilk = 100;


void addCup(int cup) // max of 1 (size 200ml)
{
int maxCup;

maxCup = 1;

this.cup = cup;

if (cup > maxCup)
{
System.out.println ("Cannot have more than 1 cup");
}
else
{
System.out.println ("Add " + cup + " cup");
}
}

void addCoffee(int coffee)
{
this.coffee = coffee;
}


void addMilk(int milk) // max 100ml
{

try
{
Exception e;
if (cup == 0)
{
e = new Exception("You have no cup so there is a mess on the bench");
throw(e);
}
if ((milk + this.milk) > maxMilk)
{
e = new Exception("Cannot have more 100 ml");
throw(e);
// add what we can and spill the rest
}
else
{
this.milk += milk;
System.out.println ("Now have " + this.milk + " ml of milk");
}
}
catch (Exception e)
{
System.out.println (e.toString());
}
}


void addSugar(int sugar) // max 5 spoon
{
int maxSugar = 5;

this.sugar = sugar;

if (sugar > maxSugar)
{
System.out.println ("Cannot have more 5 spoon");
}

}


void addWater(int water) // max 200ml
{
int maxWater = 200;

this.water = water;

if (water > maxWater)
{
System.out.println ("Cannot have more than 200 ml");
}
}

void Stir(boolean stir) // must be stir before drinking
{
this.stir = stir;

if (stir == true)
{
System.out.println ("Stir Stir");
}
}

void Drink(boolean drink) // glug glug
{
this.drink = drink;

if (drink == true)
{
System.out.println ("Drink glug glug");
}
}

};

 

 

 
 
+------------+
ICQ-93893008
[View] / [Sign]
G-Book 1
[View] / [Sign]
G-Book 2
 
ChangingLINKS
owned by russ
 
 
all.at/russellhavik
I got it from
v3.com
 
 
hosted for free by Geocities.com
 
link, link, link
 
russ' blog
 
css 2 official!!
home | for fun | graphics | me | portfolio | links © Celticslopedia - Russell Havik, 1998 - 2005
 
my blogs
Hosted by www.Geocities.ws

1