-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay13-Abstract-Classes
More file actions
64 lines (56 loc) · 1.97 KB
/
Copy pathDay13-Abstract-Classes
File metadata and controls
64 lines (56 loc) · 1.97 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
Problem:
Objective
Today, we're taking what we learned yesterday about Inheritance and extending it to Abstract Classes.
Because this is a very specific Object-Oriented concept, submissions are limited to the few languages that use this construct.
Check out the Tutorial tab for learning materials and an instructional video!
Task
Given a Book class and a Solution class, write a MyBook class that does the following:
Inherits from Book
Has a parameterized constructor taking these 3 parameters:
1. string title
2. string author
3. int price
Implements the Book class' abstract display() method so it prints these 3 lines:
Title:, a space, and then the current instance's title.
Author:, a space, and then the current instance's author.
Price:, a space, and then the current instance's price.
Note: Because these classes are being written in the same file, you must not use an access modifier (e.g.: public)
when declaring MyBook or your code will not execute.
Solution:
// Declare your class here. Do not use the 'public' access modifier.
class MyBook extends Book {
// Declare the price instance variable
private int price;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
/**
* Class Constructor
*
* @param title The book's title.
* @param author The book's author.
* @param price The book's price.
**/
// Write your constructor here
MyBook(String title, String author, int price) {
super(title, author);
setPrice(price);
}
/**
* Method Name: display
*
* Print the title, author, and price in the specified format.
**/
// Write your method here
@Override
void display() {
// TODO Auto-generated method stub
System.out.println("Title: " + this.title);
System.out.println("Author: " + this.author);
System.out.println("Price: " + this.price);
}
}
// End class