-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
65 lines (56 loc) · 1.73 KB
/
Book.java
File metadata and controls
65 lines (56 loc) · 1.73 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
/**
* Created: January 13, 2021
* Instructions: Create a class named Book that contains data fields for the title and number of pages.
* Include get and constructor methods for these fields. Next, create a subclass named Textbook,
* which contains an additional field that holds a grade level for the Textbook and additional methods
* to get and set the grade level field. Write an application (Main class) that demonstrates constructors,
* accessors and mutators using objects of each class.
*/
public class Book {
public static void main(String args[]){
Books penelope = new Books();
penelope.setTitle("The Stars");
penelope.setNumberOfPages(456);
System.out.println("Book Title: "+penelope.getTitle()+
"\nNumber of Pages: "+penelope.getNumberOfPages());
Textbook penelope2 = new Textbook();
penelope2.setGradeLevel(3);
System.out.print("Grade Level: "+penelope2.getGradeLevel());
}
}
class Books{
protected String title;
protected int numberOfPages;
public Books(){
}
public Books(String title, int numberOfPages){
this.title = "";
this.numberOfPages = 0;
}
// accessors
public String getTitle(){
return title;
}
public int getNumberOfPages(){
return numberOfPages;
}
// mutator
public void setTitle(String t){
title = t;
}
public void setNumberOfPages(int nop){
numberOfPages = nop;
}
}
class Textbook extends Book{
private int gradeLevel;
public Textbook(){
this.gradeLevel = 0;
}
public int getGradeLevel(){
return gradeLevel;
}
public void setGradeLevel(int gradeLevel){
this.gradeLevel = gradeLevel;
}
}