Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default How do you decide what classes to make in a project?

    Hi guys I manage to finish writing this programme in java and it ticks everything in my project specification. However, when I discussed it with my friend he did 8+ classes and yet to finish. I only made 4 classes including the Main menu. How do you decide what and how many classes to make in a project?
    Last edited by coopc; 04-15-2011 at 06:18 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    Quote Originally Posted by coopc View Post
    Hi guys I manage to finish writing this programme in java and it ticks everything in my project specification. However, when I discussed it with my friend he did 8+ classes and yet to finish. I only made 4 classes including the Main menu. How do you decide what and how many classes to make in a project?
    It's not an exact science. It depends how the program fits inside your head. If you see it requiring four classes, then that's how many you should use. Just make sure that your classes aren't doing too much- small is generally better with methods and classes.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    There are some useful rules of thumb - the main one I use is that a class should have a single, clearly defined task or role.

    It sometimes helps to think of the classes in a program as a team of specialists that cooperate to do the job.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I agree with dlorde. Yet you should maintain polymorphism. Don't make your class so specific is can only be called in one situation. If you haven't gotten that far in your class then don't worry about it yet. Any good book will cover it.

    What I do is I write out the program in a flowchart. Every block will determine what I need. It really comes down to a preference thing. You just have to feel it out, kind of like the hippies.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    So say the program's task is for a cupcake company where it records the price and time it takes for it to be made.
    would the classes be main menu, price and time? :confused:

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by coopc View Post
    So say the program's task is for a cupcake company where it records the price and time it takes for it to be made.
    would the classes be main menu, price and time? :confused:
    You'll have to be a bit clearer about the program - where does 'main menu' come from?

    Generally, data items like price and time would be part of the data in a class. For example, if different types of cupcake cost different amounts and took different times to make, the price and time would be part of the data in the abstract (have you done 'abstract' yet?) Cupcake class. The different types of Cupcake could be separate subclasses. Each Cupcake subclass (e.g. ChocolateCupcake, PlainCupcake, RaisinCupcake) would have a different price, time, description, (plus recipe and ingredients too, perhaps).

    I realise I may be answering a different question to the one you asked, but it's hard to be more helpful without knowing what Java you've been taught so far and what this particular exercise is.

    I guess the simplest rule of thumb for beginners is that classes typically correspond roughly to the real-world objects (e.g. cupcake) or the nouns in the requirements description. Simple descriptive values (like price, time, name, etc) are data items that belong to a class.
    Last edited by dlorde; 04-15-2011 at 07:45 PM.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    For a cupcake program I think the easiest way to manage it would be a class file that looked like this:
    Java Code:
    class CupCake
    {
        private String cupCakeFlavor;
        private double price;
        private String cupCakeDescrip;
    
        public void setcupCakeFlavor(String name)
       {
            cupCakeFlavor = name;
        }
    
        public String getcupCakeFlavor()
        {
             return cupCakeFlavor;
        }
        //Repeat for all instance variables
    }
    Then every time you make a new cupcake, create a new object and place it in an array. Whenever you were trying to look at a cupcake type you just loop through the array until you find the name of the cupcake you're trying to display.

    I hope that made sense, but what I'm trying to get across here is that you would have one class, with 6 methods(Getters and Setters, not sure if you covered that). Then of course your main class to run it. Now if you haven't covered getters and setters you would just have instance variables to that class. Then you would directly edit your instance variables via the object reference.
    Last edited by Dark; 04-15-2011 at 07:56 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well thats ok, everyone starts somewhere. When I started with VisualBasic and Python I have no idea what OOP even was.

    Basically when you make a new object.
    Java Code:
    CupCake c1 = new CupCake();
    It is independant from everything else. I'll break it down like this in a class.
    Java Code:
    class CupCake
    {
        String name;
    }
    Very simple CupCake class right? All it does is define the object with a name, starting with null.

    So in your main class you would have this snippet somewhere.
    Java Code:
    CupCake c1 = new CupCake();
    c1.name = "Chocolate Chip";
    System.out.println(c1.name);
    Would then output:
    Java Code:
    Chocolate Chip
    However if your main class looked like this
    Java Code:
    CupCake c1 = new CupCake();
    c1.name = "Chocolate Chip";
    CupCake c2 = new CupCake();
    c2.name = "Marble Swirl";
    System.out.println(c1.name);
    System.out.println(c2.name);
    That output would be
    Java Code:
    Chocolate Chip
    Marble Swirl
    Do you understand why?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Yes, I understand. There are now 2 things (is it called objects?) stored in the CupCake class.

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Yes they are objects, have you learned about the heap yet? If not I won't go into it yet, your book should cover it later.

    As I was demonstrating earlier, the class file only has one instance variable. So you can edit that class file to contain two more instance variables.
    Java Code:
    String cupCakeDescrip;
    double price;
    Do you understand why to accomplish your program you only need this simple class? You wanted to store some information about different cupcakes for your "business".

    If you understand, try taking the class and main snippet I gave you. Add the two new instance variables and set c1 and c2 to have a price and a description. Then output it to the console.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    So it goes:
    Java Code:
    class CupCake
    {
        String name;
        String cupCakeDescrip;
        double price;
    }
    then in main class:
    Java Code:
    CupCake c1 = new CupCake();
    c1.name = "Chocolate Chip";
    c1.cupCakeDescrip="blahblah";
    c1.price="2.00";
    CupCake c2 = new CupCake();
    c2.name = "Marble Swirl";
    c2.cupCakeDescrip="blahblahblah";
    c2.price="2.50";
    System.out.println(c1.name+" has "+c1.cupCakeDescrip+" costing:"+c1.price);
    System.out.println(c2.name+" has "+c2.cupCakeDescrip+" costing:"+c2.price);
    which then outputs
    Java Code:
    Chocolate Chip has blahblah costing:2.00
    Marble Swirl has blahblahblah costing:2.50

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Indeed it does. Can you explain why though? I only ask because this is something fundamental of Java. If you don't understand this concept you cannot truely understand Java.

    EDIT: Maybe that was a little to broad of a question. Can you explain why you only need one class to accomplish your program instead of what you were originally thinking?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  13. #13
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    From what I understand the CupCake class deals with the storing of different types of cup cakes. Im not too sure why I only need one class tbh. What if the program has 20 different types of cupcakes already and is set so the user can enter the name of the cupcake they wish to see the price on? Surely, there would be an array somewhere in the class?

  14. #14
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    The array would exist in main.
    Java Code:
    CupCake[] cupCakeArray = new CupCake[20];//Creating a new array
    for(int i=0;i<20;i++)//Running through a loop to intialize each array slot
    {
       cupCakeArray[i] = new CupCake();//Initiliazing cupCakeArray[i]
    }
    cupCakeArray[0].name = "Chocolate Chip";//Setting cupCakeArray[0] object to Chocolate Chip
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  15. #15
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Ah I see, Does that mean I have to set each objects like this:
    Java Code:
    cupCakeArray[0].name="Chocolate Chip";
    cupCakeArray[0].price=3.50;
    cupCakeArray[1].name="Marble Swirl";
    cupCakeArray[1].price=2.00;
    cupCakeArray[2].name="Strawberry Delight";
    cupCakeArray[2].price=3.00;
    ..
    and so on until I get to cupCakeArray[20]?

  16. #16
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Yes, but you could make other arrays to effectively loop them all. Theres a lot of things you could do, but as you progress through the language you'll discover them. Right now I'm staying in the basic area, that way I'm not trying to explain things that you haven't covered yet.

    However you can declare an array and initialize the variables in one line. You're still hardcoding everything and its rather inefficient but if you made an array for each Instance Variable you could just loop it through something like.

    Java Code:
    for(int i=0;i<20;i++)
    {
        cupCakeArray[i].name = cupCakeName[i];
        cupCakeArray[i].price = cupCakePrice[i];
        cupCakeArray[i].cupCakeDescrip = cupCakeDescrip[i];
    }
    It would save you typing and possibly make your code more readable, a little harder to predict the outcome due to a massive amount of information intialized on one line, but more of less easier to accomplish using basic tools.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  17. #17
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    So cupCakeName[], cupCakePrice[] and cupCakeDescrip[] are all separate arrays?
    So before that for loop I could have
    Java Code:
    String[] cupCakeName={"Chocolate Chip","Marble Swirl","Strawberry Delight"...} //etc.
    ?
    So I think I get it. What about say a customer gets a discount if the cupcake is bought at the start of the month does that mean there will be a new class called discount where it will work out if the user entered month entitles him/her for a discount and if it does calculates it?

  18. #18
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    No, you could simply add a method into your CupCake class that gets the date from Main and if date = whatever day you want then you would return price-discount. Else return price.

    Thats something you would incorporate into getters and setters, and I don't know if you covered that yet. You could even calculate the entire discount part in the main, though that wouldn't be too OO.

    Have you covered getters and setters?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  19. #19
    coopc is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Is that types of methods? so getters returns stuff and setters puts what the user entered in the program?

  20. #20
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    A setter a way of setting instance variables so you can check whether or not the input is a valid input if you want, and then a getter is just a method that returns a value. This is called Encapsulation.

    Its much safer to use this than allow direct changes to Instance Variables.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Page 1 of 2 12 LastLast

Similar Threads

  1. difficulty understanding writing classes
    By elecleoalune in forum New To Java
    Replies: 11
    Last Post: 04-18-2011, 04:06 PM
  2. Writing classes???
    By Bgreen7887 in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 09:06 AM
  3. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  4. Writing importable classes
    By Katsu in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 10:10 PM
  5. Writing classes in graphics
    By CyberFrog in forum Java Applets
    Replies: 2
    Last Post: 04-05-2008, 06:37 PM

Tags for this Thread

Posting Permissions

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