Page 284 - Introduction to Programming with Java: A Problem Solving Approach
P. 284

                250 Chapter 7 Object-Oriented Programming—Additional Details
 1 /***************************************************************
2 *
3 *
4*
5
Car.java
Dean & Dean
This class implements copy functionality for a car.
***************************************************************/
6
7
8
public class Car
9{
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
private String make;
private int year;
private String color;
*
// car's make
// car's manufacturing year
// car's primary color
//************************************************************
public void setMake(String make)
{
}
this.make = make;
public void setYear(int year)
{
this.year = year;
       Apago PDF Enhancer
public void setColor(String color)
}
{
}
this.color = color;
//************************************************************
public Car makeCopy()
 {
  Car car = new Car();
car.make = this.make;
car.year = this.year;
car.color = this.color;
return car;
// end makeCopy
This instantiates a new object.
This returns a reference to the new object.
   50
}
// end class Car
}
//************************************************************
public void display()
{
}
System.out.printf("make= %s\nyear= %s\ncolor= %s\n",
this.make, this.year, this.color);
// end display
Figure 7.2 Car class with makeCopy method that returns a reference to copy of calling object
   282   283   284   285   286