Results 1 to 7 of 7
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default 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
    PHP 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;
        }
    
    }
    sub class
    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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    does the subclasses inherit the contructor from the super class?
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.html
    Last edited by Fubarable; 08-08-2010 at 12:25 AM.

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default

    As mentioned by Fubarable:
    by explicit call to super(....)
    For example:
    super(_id, _name, _size, _price);

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    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

  1. Validate url in a JTextField
    By knickerbocker in forum New To Java
    Replies: 16
    Last Post: 05-30-2010, 03:09 PM
  2. Validate XML against XSD during generation
    By mandarkatdare in forum XML
    Replies: 0
    Last Post: 05-29-2010, 10:57 AM
  3. unable to validate
    By sanser in forum Java Servlet
    Replies: 4
    Last Post: 10-28-2009, 09:41 AM
  4. validate between int and double
    By heartysnowy in forum New To Java
    Replies: 4
    Last Post: 10-05-2009, 07:15 PM
  5. validate hex character
    By paul in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:08 PM

Posting Permissions

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