Results 1 to 2 of 2

Thread: Better coding

  1. #1
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default Better coding

    Hi,

    i wrotte a small code wich does exactly what i want(for now)...but i would like some critics and alternatives.. i know i can use set???+get??? methods too(but the code is too long with the methods)... what would be ur alternative...i just wanted to writte a program that shows the number of parked cars(car enters the park place,car leaves the park place)..thx in advance for the critics

    Java Code:
    public class Parking1
    {
    	private static String name;
    	private static int capacity;
    	private static int normal;
    
    	static int parked=0;
    	static int countIn=0;
    	static int countOut=0;
    	
    	
    	public Parking1()
    	{
    	 String name="";
    	 int capacity=0;
    	 int normal=0;
    
    	}
    	
    	public void autoParks()
    	{
    		
    		if(this.parked < this.capacity)
    			{
    			this.countIn++;
    			System.out.println("Auto enters the Parking");
    		    this.parked++;
    			Parking1.print1();
    			}
    		else
    		 System.out.println("Park place is full");
    	}
    	
    	public void autoLeaves()
    	{
    		if(this.parked > 0)
    			{
    			this.countIn--;
    			this.countOut++;
    			System.out.println("Auto leaved the Parking");
    			this.parked--;
    			Parking1.print1();
    			}
    		else
    			System.out.println("Park place is empty");
    	}
    
      static void print()
    	{
    		System.out.println("Name: "+name+"\nCapacity: "+capacity+"\nHandicapped place: "+handicapped
    							+"\nNormal: "+normal);
    	}
    	
    	static void print1()
    	{
    		System.out.println("There are "+parked+" parked");
    	}
    	
    	public static void main(String[] args)
    	{
    	
    		Parking1 park1 = new Parking1();
    		park1.name="Auto Park 1";
    		park1.capacity=4;
    		
    		park1.normal=4;
    		
    		park1.print();
    		
    		System.out.println();
    		park1.autoParks();
    		System.out.println();
    		
    		park1.autoParks();
    		System.out.println();
    		
    		park1.autoParks();
    		System.out.println();
    		
    
    		
    		park1.autoLeaves();
    		System.out.println();
    		
    		park1.autoParks();
    		System.out.println();
    		
    		
    	}
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    for starters, get it to work without having everything as static, apart from your main method. then make your instance variables private. then create the get/set methods you didn't make for whatever reason (making the code too long is no reason for anything). while you may feel inclined to directly modify values within the object, it's standard practice in java and oop in general to keep its actual variables hidden.

Similar Threads

  1. swing coding
    By priya_gurnani166 in forum AWT / Swing
    Replies: 8
    Last Post: 01-30-2009, 07:18 AM
  2. Help with really simple coding
    By tigertomas in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 04:47 AM
  3. coding help
    By accies76 in forum New To Java
    Replies: 5
    Last Post: 11-12-2008, 08:15 PM
  4. Coding Java on a Mac
    By Menre in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 05:49 AM
  5. Help with program coding
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07:16 AM

Posting Permissions

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