Thread: Java and OOP design help???

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Java and OOP design help???

    Im just starting to learn Java and its OOP stuff. Im having problems understanding when it should be used.

    Lets say Im making a program to for a teacher to use for there students grades.

    Im guessing id make a student class as a 'template' and each student in the class would become a object when they are entered in.

    What about the assignments? Lets say I have a datafile that has the Assigments Name, Data due and Grade. Could i make that a object? Could i make it good for only one particular student(since it would have their grades)?

    Anything you could point out to me? Would you do anythign different? Do you have a feeling that im missing somethere here like me?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    The way I usually get started:

    Think of the project in ENGLISH! break it down, noun/verb. A simple architecture falls into place when you break your sentences down. For instance:

    John turned in his assignment late and got an F.

    John (Instance of class Student) turned in (verb, method on Student) his assignment (instance of class Assignment contained in Student) late (property of Assignment) and got an F (property of Assignment)

    The basic architecture is there in your project description! Of course you'll have to refine it some, but you can essentially create an architecture by simply recognizing nouns and verbs as being classes and methods.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    But the homework has to be inside the student's class, can that be done?

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    class Assignment
         {
         public:
         private:
         };
    
    class Student
         {
         public:
         private:
              Assignment
                   m_homework;
         };
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    of course, how many students get only ONE homework. You should likely have a list object or a vector containing homeworks.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    of course, how many students get only ONE homework. You should likely have a list object or a vector containing homeworks.
    I ment have a object for every assignment. How could i work with that? How can i create a new name for every one that i can keep track of?

    In C i think i would have to use a linked list, does Java have anything to offer? Something more Java-like i should try instead?

    This is day 2 for me...

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    java has a vector class I believe
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> does Java have anything to offer?

    java has more container classes then youd possibly imagine.

    using the Vector class sounds like a good idea as you dont know how many assignments a student might have.

    In java, only objects can be added to containers (not primitives) so creating an assignment class would also be a good start.

    *note, if you need to iterate through a structure be sure to use an Iterator or you can turn a simple O(n) proceedure into an O(n^2) one.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    could i see some frame work please? Like what FillYourBrain did?

    Im not getting it, my book is very cheesy.

    Thanks

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    sure...
    here is a simple way to keep track of students and their grades. the 'main' class would have an array/vector of Student objects.

    Code:
    /* class Student */
    public class Student {
    
        private Vector grades;
        private String name;
        /* ...etc   */
    
        public Student() {
             init();
        }
        public Student(String n) {
            name = n;
            init();
        }
        public void init() {
            grades = new Vector();
        }
    
        public void addGrade(Double g) {
            grades.add(g);
        }
    
        public double getAverage() {
            Iterator iter = grades.iterator();
            double avg = 0;
            while(iter.hasNext()) {
                avg += ( (Double)iter.next() ).doubleValue();
            }
            return avg;
        }
    
    public void setName(String n) ....
    
    public String getName() ....
    
    // etc...
    note that using an Iterator to traverse containers is important. also, Double != double, Double is a wrapper class (its an object while a double is a primitive type) Only objects can be added to containers like Vectors.

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >John (Instance of class Student) turned in (verb, method on Student) his assignment (instance of class Assignment contained in Student) late (property of Assignment) and got an F (property of Assignment)

    Thats sweet! Im gonna start doing that!
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  2. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM
  3. Beginners guide to OOP from a Java background?!
    By eggsy84 in forum C++ Programming
    Replies: 6
    Last Post: 06-08-2005, 06:13 AM
  4. C or Java as a first language
    By CorJava in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 10-23-2002, 05:12 PM
  5. C/C++ Vs Java
    By Spectrum48k in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-21-2002, 09:06 PM