Spaces:
Running
Running
Create 03B. Thesis inherits book
Browse files
Week 5: Class hierarchies/03B. Thesis inherits book
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program defines the class 'Book'.
|
| 2 |
+
|
| 3 |
+
An object can be created from a class by giving it a name and an author as follows:
|
| 4 |
+
Book b = new Book("Old Man and the Sea", "Ernest Hemingway");
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
Write the class 'Thesis', which inherits the class Book.
|
| 9 |
+
|
| 10 |
+
The class has the following properties:
|
| 11 |
+
A constructor that takes as parameters the name, the author, and the grade (integer).
|
| 12 |
+
The constructor sets the name and author by calling the parent class constructor.
|
| 13 |
+
The grade is an attribute of the Thesis class.
|
| 14 |
+
Set and get methods for grade
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
import java.util.Random;
|
| 20 |
+
|
| 21 |
+
public class Test{
|
| 22 |
+
public static void main(String[] args){
|
| 23 |
+
final Random r = new Random();
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
String[] authors = {"Sam Student", "Sally Student", "Alex Average",
|
| 27 |
+
"Ethan Excellent", "Fiona Flunked"};
|
| 28 |
+
|
| 29 |
+
String[] names = {"Thesis", "Term Paper", "Masters thesis"};
|
| 30 |
+
|
| 31 |
+
for (int testi=1; testi<=3; testi++) {
|
| 32 |
+
String author = authors[r.nextInt(authors.length)];
|
| 33 |
+
String name = names[r.nextInt(names.length)];
|
| 34 |
+
|
| 35 |
+
Thesis lt = new Thesis(name, author, r.nextInt(5) + 1);
|
| 36 |
+
System.out.println("Thesis object created!");
|
| 37 |
+
Book k = (Book) lt;
|
| 38 |
+
System.out.println("Author: " + k.getAuthor());
|
| 39 |
+
System.out.println("Name: " + k.getName());
|
| 40 |
+
System.out.println("Grade:" + lt.getGrade());
|
| 41 |
+
System.out.println("Changing grade...");
|
| 42 |
+
if (r.nextInt(2) == 0) {
|
| 43 |
+
lt.setGrade(lt.getGrade() - 1);
|
| 44 |
+
} else {
|
| 45 |
+
lt.setGrade(lt.getGrade() + 1);
|
| 46 |
+
}
|
| 47 |
+
System.out.println("Grade:" + lt.getGrade());
|
| 48 |
+
System.out.println("");
|
| 49 |
+
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class Book {
|
| 56 |
+
private String name;
|
| 57 |
+
private String author;
|
| 58 |
+
|
| 59 |
+
public Book(String name, String author) {
|
| 60 |
+
this.name = name;
|
| 61 |
+
this.author = author;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
public String getName() {
|
| 65 |
+
return name;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
public void setName(String name) {
|
| 69 |
+
this.name = name;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
public String getAuthor() {
|
| 73 |
+
return author;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
public void setAuthor(String author) {
|
| 77 |
+
this.author = author;
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class Thesis extends Book {
|
| 83 |
+
//subclass attributes
|
| 84 |
+
private int grade;
|
| 85 |
+
|
| 86 |
+
//constructor
|
| 87 |
+
public Thesis(String name, String author, int grade) {
|
| 88 |
+
super(name, author);
|
| 89 |
+
this.grade = grade;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
public int getGrade() {
|
| 93 |
+
return this.grade;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
public void setGrade(int grade) {
|
| 97 |
+
this.grade = grade;
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
Thesis object created!
|
| 105 |
+
Author: Sam Student
|
| 106 |
+
Name: Masters thesis
|
| 107 |
+
Grade:1
|
| 108 |
+
Changing grade...
|
| 109 |
+
Grade:2
|
| 110 |
+
|
| 111 |
+
Thesis object created!
|
| 112 |
+
Author: Sam Student
|
| 113 |
+
Name: Masters thesis
|
| 114 |
+
Grade:1
|
| 115 |
+
Changing grade...
|
| 116 |
+
Grade:2
|
| 117 |
+
|
| 118 |
+
Thesis object created!
|
| 119 |
+
Author: Alex Average
|
| 120 |
+
Name: Term Paper
|
| 121 |
+
Grade:1
|
| 122 |
+
Changing grade...
|
| 123 |
+
Grade:0
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|