In this lab, we will build a class and create objects using the class. Work in pairs if you wish.
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.
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.
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.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.
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.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.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.goodStudent
to Student
that returns true
if the student's GPA is greater than
3.0, and false
otherwise.goodStudent
for jen
in the main
of UseStudent
and print the result.yong
in
the main
of UseStudent
and print the attributes of
this new object, obviously using getters.main
of UseStudent
.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 Student
s much like an array of String
s.main
, print the names of the
students, one at a time in the array.main
.When you are done with these, feel free to leave the meeting.