0% found this document useful (0 votes)
65 views5 pages

Notes - 190916 - Copying Objects

The document discusses copying objects in Java. It provides examples of a copy method and copy constructor to make a new instance of an object that copies the attribute values from an existing object. The copy method creates a new Person object, copies the attribute values from the calling object, and returns the new object. The copy constructor also creates a new Person object but uses the attribute values passed from another object instead of individual parameter values.

Uploaded by

Jillian Morgan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views5 pages

Notes - 190916 - Copying Objects

The document discusses copying objects in Java. It provides examples of a copy method and copy constructor to make a new instance of an object that copies the attribute values from an existing object. The copy method creates a new Person object, copies the attribute values from the calling object, and returns the new object. The copy constructor also creates a new Person object but uses the attribute values passed from another object instead of individual parameter values.

Uploaded by

Jillian Morgan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Copying Objects

Monday, September 16, 2019

Test Topics
ArrayLists (7.13)
Arrays (chapter 7)
2D arrays (7.12)
toString method (chapter 8.4)
Classes & objects (including constructors) (chapter 6 and 8)
equals method (chapter 8.5)
this keyword (chapter 8.9)
Passing by reference vs passing by value (8.2)
-----------------------------------
Copy method/copy constructor (8.3, 8.6)
Aggregation (8.4)
Static class members* (8.1)

69 //copy method
70 //creates a NEW INSTANCE of a Person object, copies the attribute values of the calling object and return
71 //the new object
72 //modifier returnType methodName (parameters)
73 public Person copy(){
74 //create new instance of a Person object
75 Person other = new Person(this.first, this.last, this.midInit);
76
77 //return the new object
78 return other;
79
80 //or you can do it in one line like this!
81 //create an anonymous object
82 //return new Person(this.first, this.last, this.midInit);
83 }

CSCI 1302 Page 1


85 //copy constructor
86 //creates a new Person object (like a constructor) but uses attribute values passed from object
87 //instead of individual values for attributes
88 public Person(Person other){
89 this.first = other.first;
90 this.last = other.last;
91 this.midInit = other.midInit;
92
93 //this(other.first, other.last, other.midInit);
94 //the above line calls the constructor created on line 24
95 //passing the values of other.last, other.first, and other.midInit as the arguments
96
97 }

CSCI 1302 Page 2


1 public class Car{
2 //attributes
3 private String make;
4 private int year;
5 private double mpg, fuelLevel; //creates two different double variables on one line
6
7 //constituent object from Person class
8 private Person owner;
9
10 //getters & setters
11 public String getMake(){
12 return make;
13 }
14 public void setMake(String make){
15 this.make = make;
16 }
17
18 //year
19 public int getYear(){
20 return year;
21 }
22 //modifier returnType methodName (parameters)
23 public void setYear(int y){
24 year = y;
25 }
26

CSCI 1302 Page 3


26
27 public double getFuelLevel(){ return fuelLevel; }
28 //constructor(s)
29 //specify initial values for an object's attributes
30 //it essentially CONSTRUCTS the object, hence the name
31 //only called when an object is created
32 //has. no. return. type
33 //must have the same name as the class
34 //java provides a constructor if we don't
35 //called the default constructor
36
37 //no-args constructor
38 public Car(){
39 make = "Honda";
40 year = 1992;
41 mpg = 37;
42 fuelLevel = 0;
43 }
44
45 public Car(String make, int year, double mpg){
46 fuelLevel = 2; //will stay zero if I comment this out
47 this.make = make;
48 this.year = year;
49 this.mpg = mpg;
50 }
51
52 public Car(String make, int year, double mpg, double fuelLevel, Person owner){
53 this.make = make;
54 this.year = year;
55 this.mpg = mpg;
56 this.fuelLevel = fuelLevel;
57 this.owner = owner;
58 }
59
60 //other methods
61 public void drive(double distance){
62 fuelLevel = fuelLevel - (distance/mpg);
63 }
64
65 public String toString(){
66 return year + " " + make + " with " + mpg +
67 " mpg and a " + fuelLevel + " gallon tank owned by " + owner.getFirst();
68 }
69 }

Person bob = new Person("Bob", "Random", 'Q');


Car bobsCar = new Car("Chevy", 1990, 20, 30, bob);
System.out.println(bobsCar);
1990 Chevy with 20.0 mpg and a 30.0 gallon tank owned by Hello, my name is Bob Q Random
--------------------------------------
Person bob = new Person("Bob", "Random", 'Q');
Car bobsCar = new Car("Chevy", 1990, 20, 30, bob);
System.out.println(bobsCar);

CSCI 1302 Page 4


System.out.println(bobsCar);
1990 Chevy with 20.0 mpg and a 30.0 gallon tank owned by Bob
bob.setFirst("John");
System.out.println(bobsCar);
1990 Chevy with 20.0 mpg and a 30.0 gallon tank owned by John


CSCI 1302 Page 5

You might also like