Results 1 to 14 of 14

Thread: My First Java Program (Tea Anyone)

  1. #1

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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?

  4. #4

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  9. #9
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: My First Java Program (Tea Anyone)

    So what happens if the code is not synchronized?

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  11. #11
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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() ?

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  13. #13
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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
  •  



Click Here to Expand Forum to Full Width