Skip to main content
changed "is right.." to "compiles"
Source Link
radarbob
  • 7.9k
  • 20
  • 34

Talk yourself through your "problem domain" and write it as you go. Say a little, write a little. The details will come along. Don't worry about them all at once. Make sure the little you write each time is right firstcompiles.

Talk yourself through your "problem domain" and write it as you go. Say a little, write a little. The details will come along. Don't worry about them all at once. Make sure the little you write each time is right first.

Talk yourself through your "problem domain" and write it as you go. Say a little, write a little. The details will come along. Don't worry about them all at once. Make sure the little you write each time compiles.

Source Link
radarbob
  • 7.9k
  • 20
  • 34

Talk yourself through your "problem domain" and write it as you go. Say a little, write a little. The details will come along. Don't worry about them all at once. Make sure the little you write each time is right first.

For example

Well, there's a Person

public Class Person {}

Is a Student a Person? Yes. Is a Person a student? Well, sometimes.

public Class Student extends Person {}

What's the basic stuff of a Person/Student? name, age, address, studentID, semester....

public class Student extends Person () {
    public string Name;
    public string Age;
    public string Address;
    public string ID;
    public string semester;
}

Oh, some of those belong to all Persons, some are students only

public class Person () {
        public string Name;
        public string Age;
        public string Address;
}

public class Student extends Person () {
        public string ID;
        public string semester;
}

Students take courses. I need a course class. Is a course a student? no. vice versa? no. Oh - a student takes a course. Or a student has courses - has means composition not inheritance

public class Student extends Person () {
            public string ID;
            public string semester;
            ArrayList courses;
 }
 public class Course {} // don't know what's inside this yet.

OK, You get the idea.

Displaying

Your basic idea of making strings of "label: value" is the right idea, but implemented wrong. And once implemented - use it!!

All objects have a toString() method. Override it. Then all you have to "say" to use it is System.out.println(someStudent.toString()) BAM! object oriented programming. Also, DO NOT use println in your toString(). toString() should build all the object information into a single formatted string and pass that. Then you use it as shown.

// in the Person class
public override string toString(){
   string me = "";
   me += "Name: " + this.Name + "\n"  //new line
   me += "Age: " + this.Age + "\n";
   me += "Address: " + this.Address + "\n";
   return me;
}

Now, what class would this go into? Person. What about Student? The above already does most of what we need for Student too. Do we duplicate it in Student class? NO! Object Oriented Design to the rescue!

// in the Student class
public string toString(){
    string me = "";
    me += base.toString();
    me += "semester: " + this.semester + "\n";
    ......
    return me;
}