Results 1 to 2 of 2
Thread: Better coding
- 04-09-2009, 07:09 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
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(); } }
- 04-09-2009, 07:19 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
-
swing coding
By priya_gurnani166 in forum AWT / SwingReplies: 8Last Post: 01-30-2009, 07:18 AM -
Help with really simple coding
By tigertomas in forum New To JavaReplies: 10Last Post: 01-24-2009, 04:47 AM -
coding help
By accies76 in forum New To JavaReplies: 5Last Post: 11-12-2008, 08:15 PM -
Coding Java on a Mac
By Menre in forum New To JavaReplies: 5Last Post: 05-06-2008, 05:49 AM -
Help with program coding
By cachi in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks