-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
140 lines (108 loc) · 4.43 KB
/
Copy pathBook.java
File metadata and controls
140 lines (108 loc) · 4.43 KB
1
2
3
4
5
6
7
8
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// CS 272 Intro to Data Structures
// Program Name: Book.java
// Author: Voltflip
// Date: 01/28/2019
// Purpose: The purpose of this program is to create many methods in order to manipulate various instance variables
public class Book {
// declare variables
private String title;
private int numAuthors;
private String[] authors;
private String ISBN;
public Book() {
title = null;
ISBN = null;
numAuthors = 0;
authors = new String[3];
} // end Book constructor with no argument
public Book (String _title) {
title = _title;
ISBN = null;
numAuthors = 0;
authors = new String[3];
} // end Book constructor with String argument
public Book (Object obj) {
} // end Book method with Object argument
public String getTitle() {
return title;
} // end getTitle method
public int getAuthorNumber() {
return numAuthors;
} // end getAuthorNumber method
public void setTitle (String _title) {
title = _title;
} // end setTitle method
public void setISBN (String _isbn) {
ISBN = _isbn;
} // end setISBN method
public boolean addAuthor (String _author) {
for (int i=0; i < authors.length; i++) {
if (authors[i] == null) {
authors[i] = _author;
numAuthors += 1;
return true;
} // end if statement
} // end for loop
return false;
} // end addAuthor method
public boolean equals (Book obj) {
if (obj.ISBN == ISBN) {
return true;
}
return false;
} // end equals method
// I'm pretty sure an Object is an instance of a class you create with
// a constructor such as Book1 and Book2 that I create below in the main method. Otherwise I'm not sure
// what to put in for the argument. Not sure how literally I was supposed to take the method listed in the lab instructions for this section.
// I hope I did it correctly.
public static String[] getAllAuthors(Book b1, Book b2) {
String[] allAuthors = new String[6];
int a = 0;
for (int i = 0; i < b1.authors.length; i++) {
if (b1.authors[i] != null) {
allAuthors[a] = b1.authors[i];
a += 1;
} // end if
} // end for
for (int j = 0; j < b2.authors.length; j++) {
if (b2.authors[j] != null) {
allAuthors[a] = b2.authors[j];
a += 1;
} // end if
}// end for
return allAuthors;
} // end getAllAuthors method
public String toString() {
return String.format("%s %s %d %s", title, ISBN, numAuthors, java.util.Arrays.deepToString(authors));
} // end toString method
public static void main (String[] args) {
// test above methods
// create a new book class and print it
Book Book1 = new Book();
System.out.println(Book1);
// create a 2nd book class using the method that takes a String parameter which gives it a title. Then print it
Book Book2 = new Book("The Hobbit");
System.out.println(Book2);
// give Book2 a new title with setTitle method
Book2.setTitle("Harry Potter");
// print Book2's new title
System.out.println(Book2.getTitle());
// give Book1 an author
Book1.addAuthor("JRR Tolkien");
// give Book2 an author
Book2.addAuthor("JK Rowling");
// print Book2's number of author's using the getAuthorNumber method
System.out.println(Book2.getAuthorNumber());
// set Book2's ISBN using the setISBN method
Book2.setISBN("0-7475-3269-9");
// get authors of both books using the getAllAuthors method and print both of them
System.out.println(getAllAuthors(Book1, Book2));
// print Book2 using toString method
System.out.println(Book2.toString());
// compare the 2 books' ISBNs using the equals method
if (Book1.equals(Book2)) {
System.out.println("The ISBNs are the same");
}
else System.out.println("The ISBNs are different.");
} // end main method
} // end Book class