Lab 12: Objects

In this lab, we will build a class and create objects using the class. Work in pairs if you wish.

Problem 1

We created Account.java and UseAccount.java in class the other day. See the results in the lecture notes area.

We also saw Point.java and UsePoint.java in class yesterday. See them in the lecture notes area.

Problem 2

We will create a class named Student in Student.java and UseStudent in UseStudent.java in this problem. Do the following, one step at a time, making sure it works before you go on to the next step. Be sure to get help if you have any difficulties.

  1. Add the following attributes: name, major, id, and gpa to the Student class for now. Although we did not use any public attribute in Account and Point, let's make the four attributes in this class (Student) public this time.

  2. Add a constructor that takes four parameters, one for each attribute in the class.

  3. In the main of UseStudent, create one student object named jen and print the value of each attribute of the object. Since each attribute is declared to be public, you can access the value directly without using a getter method, e.g., you can do the following:
       System.out.println(jen.name);
       System.out.println(jen.major);
       // similarly for other attributes.
       
  4. If everything works okay, now revise Student so that each attribute is declared to be private this time. As soon as you do this, your main is not going to work any more.

  5. Add a getter, e.g., getName, for each attribute and revise the main to make it work. This time you will have to use the getters instead of accessing each attribute directly without using a getter for it.

  6. Add a setter, e.g., setName, for name, major, and gpa. We assume that student ids are permanent once assigned, thus not needing a setter for it. Setters are used to change attribute values.

  7. This time call each setter once to change attributes that have a setter defined.

  8. Again print the value of each attribute of the object, but this time using getters since accessing attributes directly without going through the getters would not work (meaning you will get compile-time errors).

  9. Now, add a method named goodStudent to Student that returns true if the student's GPA is greater than 3.0, and false otherwise.

  10. Call goodStudent for jen in the main of UseStudent and print the result.

  11. Create one more student object named yong in the main of UseStudent and print the attributes of this new object, obviously using getters.

  12. Create three more student objects with the names of your choice in the main of UseStudent.

  13. Create an array called students of size 5 and add all those five student objects into the array. We have created an array of String objects before so you should be able to create an array of Students and add Student objects. The array will be an array of Students much like an array of Strings.

  14. This time using a loop in main, print the names of the students, one at a time in the array.

  15. This time count the number of "good" students in the array of students and print the count, all in main.

When you are done,

When you are done with these, feel free to leave the meeting.