Using the Dot Operator
Lab 4. 2: Writing Classes In this lab, you will write a class for each of the classes you described in
Lab 4.1.
1. Write a Java class for each of the classes you came up with in Lab 4.1. When writing your classes, keep the following in mind:
■■ Each class you write should be public; therefore, each class needs
to appear in a separate source code file.
■■ We have not discussed the details of writing methods. (Methods
are discussed in the next chapter.) Focus on which methods each class should have, but don’t worry about how they should be implemented. Within each method, use the System.out.println() method to display the name of the method.
■■ Be sure to save all your classes in the same directory on your
hard drive. 102 Chapter 4
2. Writing a program to tie the classes together to make everything functional is not feasible without implementing all the methods. For now, just write and compile each class to match your design from Lab 4.1.
3. Write a program named VideoStore that creates an instance of each of your classes, initializes their fields, and invokes the methods to practice accessing the fields and methods of objects.
4. Compile and run your VideoStore program.
You should see the output of all the methods you invoked within your classes.
Summary
■■ Procedural programming involves designing a program around the
tasks that the program needs to accomplish. Object-oriented program- ming involves designing a program around the objects in the problem domain.
■■ Object-oriented analysis and design is the process of determining the
objects in a problem domain, determining any relationships between these objects, and also determining the attributes and behaviors of each object.
■■ A class is a description of an object, and an object is an instance of a
class.
■■ The class keyword is used to declare a class in Java. A class consists of
fields and methods.
■■ The new keyword instantiates an object. The new operator returns a
reference to the newly created object. The object remains in memory until it can no longer be reached by any reference, at which point the object becomes eligible for garbage collection.
■■ The garbage collector is a low-priority thread of a JVM that is constantly
looking for unreachable objects and freeing them from memory.
■■ The dot operator is used to access the fields and methods of an object
using a reference to the object.
■■ Every object has a reference to itself referred to as the this reference.
Review Questions
1. A class is a description of a(n) _______________. 2. An object is an instance of a(n) _______________.
3. When designing a class to describe an object, how are the attributes of the object rep- resented in the class?
4. When designing a class to describe an object, how are the behaviors of the object rep- resented in the class? Use the following BaseballGame and Team classes to answer the ensuing questions.
public class Team {
public String name; public String city;
public int numberOfWins, numberOfLosses; }
public class BaseballGame {
public Team home, visitor;
public int homeScore, visitorScore;
public void homeTeamScored(int numberOfRuns) {
homeScore += numberOfRuns; }
public void visitorTeamScored(int numberOfRuns) {
visitorScore += numberOfRuns; }
public void gameOver() { if(homeScore > visitorScore) { home.numberOfWins++; visitor.numberOfLosses++; } else { visitor.numberOfWins++; home.numberOfLosses++; } 104 Chapter 4
Classes and Objects 105
}
public void setHomeTeam(Team t) {
home = t; }
public void setVisitingTeam(Team visitor) {
visitor = visitor; }
}
5. How many fields are in the Team class? Name them.
6. How many fields are in the BaseballGame class? Name them. 7. How many methods are in the Team class? Name them.
8. How many methods are in the BaseballGame class? Name them.
9. After the following statement, what is the value of each field of the game object?
BaseballGame game = new BaseballGame();
10. After the following statement, what is the value of each field of the giants object?
Team giants = new Team();
Consider the following statements when answering questions 11 and 12:
Team angels;
BaseballGame worldSeries;
11. How many objects are there in memory after the previous two statements are done executing?
12. Which consumes more memory: angels or worldSeries?
13. How many Team objects are there in memory after the following two statements?
Team a = new Team(); Team b = a;
14. In the previous question, how many Team references are there in memory after the two statements execute?
15. After some testing, it has been determined that the setVisitingTeam() method of the BaseballGame class does not work successfully. After it is invoked, the visitor field does not change. What is the problem, and how can it be fixed?
Answers to Review Questions
1. Object. 2. Class.
3. Attributes are represented as fields in the class. 4. Behaviors are represented as methods in the class. 5. Four: name, city, numberOfWins, numberOfLosses. 6. Four: home, visitor, homeScore, visitorScore. 7. Zero. The Team class has only fields.
8. Five: homeTeamScored, visitorTeamScored, gameOver, setHomeTeam, and setVisitingTeam.
9. Home and visitor are references, so they will both be null. homeScore and visitorScore are of type int, so they will both be zero.
10. Name and city are references and will both be null, whereas numberOfWins and numberOfLosses will be zero because they are ints.
11. There are no objects in memory because the new keyword was not used. 12. Angels and worldSeries are references, and even though they are of different data
types, they consume the same amount of memory.
13. The new keyword was used once, so there is one Team object in memory. Both a and b refer to this one object.
14. There are two Team references, a and b.
15. The parameter visitor is the same name as the field visitor. Setting the following assigns the visitor parameter equal to itself and does not change the field visitor:
visitor = visitor;
To distinguish the parameter from the field, the field needs to explicitly use the this
reference. To fix the problem, change the body of the method to the following:
this.visitor = visitor;
107
The behaviors of an object are represented as methods in a class. In this chapter, we will discuss the details of methods in Java, including the signature of a method, invoking a method, and how data is passed back and forth between