In: Computer Science
In Java
Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing.
Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create two child classes of Novel: Fiction and NonFiction. Each must include a setPrice() method that sets the price for all Fiction Novel at 89.90 SAR and for all NonFiction Novels at $124.50 SAR. Write a constructor for each subclass, and include a call to setPrice() within each. Write an application demonstrating that you can create both a Fiction and NonFiction Novel and display their fields. Save the fields as Novel.java, Fiction.java, NonFiction.java, and UseNovel.java.
Write an application named NovelArray in which you create an array that holds 8 Novels, some Fiction and some NonFiction. Using a for loop, display details about all 8 novels. Save the file as NovelArray.java.
If you have any problem with the code feel free to comment.
Printing
public interface Printing {
public int getPageNumber();
}
Novel
public abstract class Novel implements Printing {
protected String title;
protected double price;
public Novel(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
abstract public void setPrice();
}
Fiction
public class Fiction extends Novel {
public Fiction(String title) {
super(title);
setPrice();
}
@Override
public int getPageNumber() {
// TODO Auto-generated method
stub
return 0;
}
@Override
public void setPrice() {
price = 89.90;
}
}
NonFiction
public class NonFiction extends Novel {
public NonFiction(String title) {
super(title);
setPrice();
}
@Override
public int getPageNumber() {
// TODO Auto-generated method
stub
return 0;
}
@Override
public void setPrice() {
price = 124.50;
}
}
UseNovel
public class UseNovel {
public static void main(String[] args) {
Fiction fic = new
Fiction("Alone");
NonFiction nonfic = new
NonFiction("Im alive");
System.out.println("Title:
"+fic.getTitle()+", Price: $"+fic.getPrice());
System.out.println("Title:
"+nonfic.getTitle()+", Price: $"+nonfic.getPrice());
}
}
NovelArray
public class NovelArray {
public static void main(String[] args) {
Novel[] novels = {
new Fiction("Somthings Wrong!!"),
new NonFiction("Stupid"),
new Fiction("Love"),
new Fiction("Ghost"),
new NonFiction("Money"),
new Fiction("Freedom"),
new NonFiction("Anger"),
new NonFiction("Greed"),
new NonFiction("Chemistry")
};
for(Novel i: novels) {
System.out.println("Title: "+i.getTitle()+", Price:
$"+i.getPrice());
}
}
}
Outputs