Results 1 to 7 of 7
Thread: can someone validate my code?
- 08-07-2010, 08:10 PM #1
can someone validate my code?
Hi i basically wanna know if im going about this the right way and if theres a more efficient way to do this.
I havent yet looked into databases or anything but i plan to in a few weeks time.
heres my code, any suggestions would be nice. (btw this isnt a full app or anything, ive just started it yesterday and want to know if its ok to start building on or can be improved. Ill be later adding a GUI, a database, events, etc etc.
main class
PHP Code:import java.util.*; public class Main { static food foodOb[] = new food[6]; static private int id = 0; private static void fillDB(){ foodOb[0] = new catFood(++id,"felix",'S',4.99); foodOb[1] = new catFood(++id,"felix",'M',5.99); foodOb[2] = new catFood(++id,"felix",'L',7.99); foodOb[3] = new dogFood(++id,"butchers",'S',7.99); foodOb[4] = new dogFood(++id,"butchers",'M',10.99); foodOb[5] = new dogFood(++id,"butchers",'L',14.99); } public static void main(String[] args) { Main.fillDB(); List lstFood = Arrays.asList(foodOb); //lstFood.add(); << need work out syntax } }
base class
sub classPHP Code:public class food { private int id; private double price; private char size; private String name; public void setStats(int _id,String _name,char _size,double _price){ setID(_id); setName(_name); setSize(_size); setPrice(_price); } public void setName(String _name){ name = _name; } public String getName(){ return name; } public void setPrice(double _price){ price = _price; } public double getPrice(){ return price; } public void setSize(char _size){ size = _size; } public char getSize(){ return size; } public void setID(int _id){ id = _id; } public int getID(){ return id; } }
PHP Code:public class catFood extends food { public catFood(int _id,String _name,char _size,double _price){ super.setStats(_id, _name, _size, _price); //additional things unique to catFood added here later } }PHP Code:public class dogFood extends food { public dogFood(int _id,String _name,char _size,double _price){ super.setStats(_id, _name, _size, _price); //additional things unique to dogFood added here later } }Teaching myself java so that i can eventually join the industry! Started in June 2010
- 08-07-2010, 08:17 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Why do you have a setStats( ... ) method in your food (Food?) class? i.e. why don't you have an appropriate constructor in that class as in the subclasses thereof?
kind regards,
Jos
- 08-07-2010, 11:18 PM #3
-
No, constructors aren't inherited. Note however that a constructor of the super class is always called in any subclass constructor as the first call, be it by default or by explicit call to super(....). If you haven't done so, you should read the Sun/Oracle tutorial on constructors.
http://download.oracle.com/javase/tu...structors.htmlLast edited by Fubarable; 08-08-2010 at 12:25 AM.
- 08-08-2010, 12:32 AM #5
i tried doing what u said but i got an error. Basically what i did was put the parenthesis in the contructor for the food class and also the same parenthesis in the catFood and dogFood class and it came back as "cannot find constructor food(), so i made a second contructor called food() so now and the errors went away, but now the values all come back as 0. ive made a mistake, please help.
PHP Code:public class food { private int id; private double price; private char size; private String name; public food(){ } public food(int _id,String _name,char _size,double _price){ setID(_id); setName(_name); setSize(_size); setPrice(_price); } public void setStats(){ } public void setName(String _name){ name = _name; } public String getName(){ return name; } public void setPrice(double _price){ price = _price; } public double getPrice(){ return price; } public void setSize(char _size){ size = _size; } public char getSize(){ return size; } public void setID(int _id){ id = _id; } public int getID(){ return id; } }
PHP Code:public class catFood extends food { public catFood(int _id,String _name,char _size,double _price){ // super.setStats(_id, _name, _size, _price); } }PHP Code:public class dogFood extends food { public dogFood(int _id,String _name,char _size,double _price){ } }Teaching myself java so that i can eventually join the industry! Started in June 2010
- 08-08-2010, 12:53 AM #6
As mentioned by Fubarable:
For example:by explicit call to super(....)
super(_id, _name, _size, _price);
- 08-08-2010, 03:18 AM #7
thanks guys i got it working and thanks norm for the source code. i know you guys prefer teaching people by making them do their own research but i prity much study this stuff hours a day and sometimes i need specific code because thats how i learn quickest, and i remember it and use it for other programs.
I've made the modifications to my code, is there anything else which can be improved?
thanks for taking the time to help
edit
the bit that im most concerned about is the code below. can this be improved any how?
PHP Code:foodOb[0] = new catFood(++id,"felix",'S',4.99); foodOb[1] = new catFood(++id,"felix",'M',5.99); foodOb[2] = new catFood(++id,"felix",'L',7.99); foodOb[3] = new dogFood(++id,"butchers",'S',7.99); foodOb[4] = new dogFood(++id,"butchers",'M',10.99); foodOb[5] = new dogFood(++id,"butchers",'L',14.99);Last edited by alacn; 08-08-2010 at 03:22 AM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
Similar Threads
-
Validate url in a JTextField
By knickerbocker in forum New To JavaReplies: 16Last Post: 05-30-2010, 03:09 PM -
Validate XML against XSD during generation
By mandarkatdare in forum XMLReplies: 0Last Post: 05-29-2010, 10:57 AM -
unable to validate
By sanser in forum Java ServletReplies: 4Last Post: 10-28-2009, 09:41 AM -
validate between int and double
By heartysnowy in forum New To JavaReplies: 4Last Post: 10-05-2009, 07:15 PM -
validate hex character
By paul in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks