|
-
Oct 4th, 2005, 06:26 AM
#1
My First Java Program (Tea Anyone)
This is quite a simple one. I hacked this from what i know in C and PHP so was wondering if I any of this looks like bad coding practice?
TeaTime.java
Code:
import java.io.*;
public class TeaTime
{
private static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[]) throws IOException
{
Kettle kipper = new Kettle();
int fillAmount;
int cupSize;
int noCups;
int spilt;
float actualCups;
System.out.print("Kettle Boiling Program\n\n");
fillAmount = TeaTime.getIntegerFromConsole ("How much water do you want to fill the kettle with? " +
"(MAX " + kipper.getMaxFillLevel() + "ml) ");
spilt = kipper.fill(fillAmount);
if (kipper.isOverFlow())
{
System.out.println("You put too much water in.");
System.out.println(spilt + "ml spilt.");
System.out.println();
}
if (! kipper.boil())
{
if (kipper.isEmpty() && kipper.isBroken())
{
System.out.println("You tried to boil an empty Kettle");
System.out.println("You broke the kettle.");
return;
}
}
System.out.println("Your Kettle Has Boiled\n\n");
cupSize = TeaTime.getIntegerFromConsole("How big are your cups in ml? ");
noCups = TeaTime.getIntegerFromConsole("How many cups of tea would you like? ");
actualCups = 0;
do
{
actualCups += ((float) kipper.pour(cupSize) / (float) cupSize);
if (--noCups == 0)
{
break;
}
}
while (! kipper.isEmpty());
if (kipper.isEmpty())
{
System.out.println("Sorry ... not enough water.");
System.out.println("You got " + actualCups + " cups");
}
System.out.println("Tea is served!!!");
}
private static int getIntegerFromConsole(String msg) throws IOException
{
while(true)
{
System.out.print(msg);
try
{
return new Integer(kb.readLine()).intValue();
}
catch (NumberFormatException e)
{
System.out.println("Enter a proper integer please.");
}
}
}
}
Kettle.java
Code:
public class Kettle
{
private boolean boiled = false;
private boolean broken = false;
private boolean empty = true;
private boolean overFlow = false;
private int maxFillLevel = 2000;
private int fillLevel = 0;
public boolean isBoiled()
{
return this.boiled;
}
public boolean isBroken()
{
return this.broken;
}
public boolean isEmpty()
{
return this.empty;
}
public boolean isOverFlow()
{
return this.overFlow;
}
public int getMaxFillLevel()
{
return this.maxFillLevel;
}
public int getFillLevel()
{
return this.fillLevel;
}
public boolean boil()
{
if (this.broken)
{
return false;
}
else if (this.empty)
{
this.broken = true;
return false;
}
else
{
this.boiled = true;
return true;
}
}
public int fill(int amount)
{
int ret;
if (amount == 0) return 0;
this.empty = false;
if (this.fillLevel + amount > this.maxFillLevel)
{
ret = amount - (this.maxFillLevel - this.fillLevel);
this.overFlow = true;
this.fillLevel = this.maxFillLevel;
}
else
{
this.fillLevel += amount;
ret = 0;
}
return ret;
}
public int pour(int cupSize)
{
int ret;
this.overFlow = false;
if (this.empty)
{
ret = 0;
}
else if (this.fillLevel - cupSize <= 0)
{
ret = this.fillLevel;
this.empty = true;
this.fillLevel = 0;
}
else
{
ret = cupSize;
this.fillLevel -= cupSize;
}
return ret;
}
}
It compiles and works
-
Oct 4th, 2005, 09:23 AM
#2
Re: My First Java Program (Tea Anyone)
You don't need to use "this" every time (unlike in PHP - and doesn't that confuse me when I'm alternately working on a Java and a PHP project).
Looks good. You could perhaps use exceptions, in boil() in particular.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 8th, 2005, 09:08 AM
#3
Frenzied Member
Re: My First Java Program (Tea Anyone)
I haven't looked at the code much, but did you create those static members and functions on purpose to share between instances of that type of class object?
-
Oct 9th, 2005, 03:52 PM
#4
Re: My First Java Program (Tea Anyone)
As far as I am aware the main function is meant to be static and any methods it invokes in that class must be static too.
-
Oct 9th, 2005, 04:05 PM
#5
Frenzied Member
Re: My First Java Program (Tea Anyone)
True, but I'd just create an instance of that class and use the dot operator to call the methods.
-
Oct 9th, 2005, 04:42 PM
#6
Re: My First Java Program (Tea Anyone)
What for? It's not like TeaTime has any right to be an object instance.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 9th, 2005, 05:28 PM
#7
Frenzied Member
Re: My First Java Program (Tea Anyone)
Why not? It's not like those methods need to be static.
It's obvious that more than one instance of TeaTime would need seperate instances of those methods, especially the bufferedreader.
-
Oct 9th, 2005, 05:34 PM
#8
Re: My First Java Program (Tea Anyone)
Actually, even more than one instance of TeaTime would do very well to share the buffered reader, because those would share the underlying stream too.
That aside, the method should be static. It also should be in a class ConsoleUtils, but that's a different issue. It should be static because the application driver, main(), is static and needs it, and there's no good reason for main() to create a TeaTime object just to use it.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 9th, 2005, 07:39 PM
#9
Frenzied Member
Re: My First Java Program (Tea Anyone)
So what happens if the code is not synchronized?
-
Oct 10th, 2005, 03:19 AM
#10
Re: My First Java Program (Tea Anyone)
Nothing. The streams are thread safe.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 10th, 2005, 02:35 PM
#11
Frenzied Member
Re: My First Java Program (Tea Anyone)
I don't understand how it can be safe though. Will it just wait untill thread 'one' has finished?
Also, what would happen to the getIntegerFromConsole() ?
-
Oct 10th, 2005, 07:23 PM
#12
Re: My First Java Program (Tea Anyone)
Will it just wait untill thread 'one' has finished?
It will wait until no thread is accessing it.
What would happen to getIntegerFromConsole()? What do you mean?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 10th, 2005, 08:20 PM
#13
Frenzied Member
Re: My First Java Program (Tea Anyone)
Like, would there be any kind of collision with more than one thread accessing it? Maybe I'm confusing myself, but
-
Oct 11th, 2005, 04:18 AM
#14
Re: My First Java Program (Tea Anyone)
No collision within the stream itself. It's thread-safe. What could happen are collisions like:
T1 writes prompt.
T2 writes prompt.
T1 requests input.
T2 requests input.
Making anything instance-specific won't change that, though. You have to synchronize it.
In general, there should only be one thread that interacts with the user anyway. More than that is bad, because a user can't multithread.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|