1. Create a new Java Project. 2. Create a class named Book that contains data fi
ID: 3715821 • Letter: 1
Question
1. Create a new Java Project.
2. Create a class named Book that contains data fields for the title and number of pages, include get and set methods for these fields.
3. Next, create a subclass named Textbook, which contains an additional field that holds a class level for the Textbook and additional methods to get and set the class level field.
4. Write a driver program named DemoBook that demonstrates using objects of each class. All data is in DemoBook.
Output:
Book 1 is Cat and the Hat with 61 pages.
Book 2 is Java Programming with 1084 pages.
Explanation / Answer
Hi Dear,
Please find my code.
public class Book {
private String title;
private int pages;
public Book(String title, int pages) {
super();
this.title = title;
this.pages = pages;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
}
public class TextBook extends Book {
private String gradeLevel;
private String courseName;
public TextBook(String title, int pages, String gradeLevel,
String courseName) {
super(title, pages);
this.gradeLevel = gradeLevel;
this.courseName = courseName;
}
public String getGradeLevel() {
return gradeLevel;
}
public void setGradeLevel(String gradeLevel) {
this.gradeLevel = gradeLevel;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
public class DemoBook {
public static void main(String[] args) {
Book book = new Book("My Notes", 650);
System.out.println("Title: " + book.getTitle() + " " + "Pages: "
+ book.getPages());
TextBook textBook = new TextBook("CPP ", 650, "XYZ", "B.Tech");
System.out.println("Title: " + textBook.getTitle() + " " + "Pages: "
+ textBook.getPages() + " Grade Level: "
+ textBook.getGradeLevel() + " Course: "
+ textBook.getCourseName());
;
}
}
output:
Title: My Notes Pages: 650
Title: CPP Pages: 650 Grade Level: XYZ Course: B.Tech
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.