Вы находитесь на странице: 1из 6

Printing and Comparing Objects

Wednesday, September 11, 2019

1 public class Person{


2 //attributes
3 private String first;
4 private char midInit; //only a single character, right?
5 private String last;
6
7 //getters & setters
8 public String getFirst(){ return first; }
9 public void setFirst(String first){
10 this.first = first;
11 }
12
13 public char getMidInit(){ return midInit; }
14 public void setMidInit(char midInit){
15 this.midInit = midInit;
16 }
17
18 public String getLast(){ return last; }
19 public void setLast(String last){
20 this.last = last;
21 }
22
23 //constructors
24 public Person(String first, String last, char midInit){
25 this.first = first;
26 this.midInit = midInit;
27 this.last = last;
28 }
29
30 public Person(String first, String last){
31 this(first, last, '\u0000');
32 //what do you think the above line does?
33 //I could also just do this, right?
34 //this.first = first;
35 //this.last = last;
36 }
37
38 //toString method
39 //basic toString method for a Person
40 // public String toString(){
41 // return "Hello, my name is " + first + " "
42 // + midInit + " " + last;
43 // }
44
45 //how to make toString handle the scenario when the Person
46 //does not have a middle initial?
47 public String toString(){
48 if(midInit == '\u0000'){
49 return "Hello, my name is " + first + " " + last;
50 }
51 else{

CSCI 1302 Page 1


52 return "Hello, my name is " + first + " "
53 + midInit + " " + last;
54 }
55 }
56 }

1 public class PersonDriver{


2 public static void main(String[] args){
3 Person bob = new Person("Bob", "Random", 'Q');
4 Person grace = new Person ("Grace", "Hopper");
5
6 String str = "Person: " + bob;
7
8
9 System.out.println(bob);
10 System.out.print(grace + "\n");
11 System.out.println(str);
12 }
13 }

CSCI 1302 Page 2


1 public class Person{
2 //attributes
3 private String first;
4 private char midInit; //only a single character, right?
5 private String last;
6
7 //getters & setters
8 public String getFirst(){ return first; }
9 public void setFirst(String first){
10 this.first = first;
11 }
12
13 public char getMidInit(){ return midInit; }
14 public void setMidInit(char midInit){
15 this.midInit = midInit;
16 }
17
18 public String getLast(){ return last; }
19 public void setLast(String last){

CSCI 1302 Page 3


19 public void setLast(String last){
20 this.last = last;
21 }
22
23 //constructors
24 public Person(String first, String last, char midInit){
25 this.first = first;
26 this.midInit = midInit;
27 this.last = last;
28 }
29
30 public Person(String first, String last){
31 this(first, last, '\u0000');
32 //what do you think the above line does?
33 //I could also just do this, right?
34 //this.first = first;
35 //this.last = last;
36 }
37
38 //toString method
39 //basic toString method for a Person
40 // public String toString(){
41 // return "Hello, my name is " + first + " "
42 // + midInit + " " + last;
43 // }
44
45 //how to make toString handle the scenario when the Person
46 //does not have a middle initial?
47 public String toString(){
48 if(midInit == '\u0000'){
49 return "Hello, my name is " + first + " " + last;
50 }
51 else{
52 return "Hello, my name is " + first + " "
53 + midInit + " " + last;
54 }
55 }
56
57 //equals method for Person
58 public boolean equals (Person other){
59 // if(this.first.equals(other.first) && this.last.equals(other.last) &&
60 // this.midInit == other.midInit){
61 // return true;
62 // }
63 // else{
64 // return false;
65 // }
66 return this.first.equals(other.first) && this.last.equals(other.last) && this.midInit ==
other.midInit;
67 }
68 }

1 public class PersonDriver{


2 public static void main(String[] args){

CSCI 1302 Page 4


3 Person bob = new Person("Bob", "Random", 'Q');
4 Person grace = new Person ("Grace", "Hopper");
5
6 // String str = "Person: " + bob;
7 //
8 //
9 // System.out.println(bob);
10 // System.out.print(grace + "\n");
11 // System.out.println(str);
12
13 System.out.println(bob.equals(grace);
14 Person goat = new Person("Bob", "Random", 'Q');
15 System.out.println(bob.equals(goat));
16 }
17 }

1 public class PersonDriver{


2 public static void main(String[] args){
3 Person bob = new Person("Bob", "Random", 'Q');
4 Person grace = new Person ("Grace", "Hopper");
5
6 // String str = "Person: " + bob;
7 //
8 //
9 // System.out.println(bob);
10 // System.out.print(grace + "\n");
11 // System.out.println(str);
12
13 // System.out.println(bob.equals(grace);
14 // Person goat = new Person("Bob", "Random", 'Q');
15 // System.out.println(bob.equals(goat));
16
17 //copying objects
18 Person p = bob;
19 System.out.println(p);
20 System.out.println(bob);
21
22 p.setFirst("Bruce");
23 System.out.println(p);
24 System.out.println(bob);
25 }
26 }

CSCI 1302 Page 5


1 public class PassingArguments{
2 public static void main(String[] args){
3 // int x;
4 // System.out.println("x = " + x + " before calling changePrimitive.");
5 //
6 // changePrimitive(x);
7 // System.out.println("x = " + x + " after calling changePrimitive.");
8
9 Person p = new Person("Bruce", "Banner");
10 System.out.println("p = " + p + " before calling changeObject.");
11
12 changeObject(p);
13 System.out.println("p = " + p + " after calling changeObject.");
14 }
15
16 public static void changePrimitive(int x){
17 x = 42;
18 }
19
20 public static void changeObject(Person goat){
21 goat.setFirst("Goat");
22 }
23 }

CSCI 1302 Page 6

Вам также может понравиться