You will create a publication class that will define a general publication. A pu
ID: 3857654 • Letter: Y
Question
You will create a publication class that will define a general publication.
A publication properties will consist of:
< title
< price
< year
The publication should be able to:
< initialize all data fields (arguments from the user input) when created
< raise the price by a certain percentage (that will be sent as a decimal argument)
< print all the labeled data fields (price to two decimal places)
You will then create two subclasses that Ais a@ publication, for a book and for a cd (audio book). Use what you inherit whenever possible.
A book "is a" publication
< Also has a number of pages.
< Initialize all data fields (to the user input) by using the publication constructor for inherited fields.
< The print method will label the output as a book and print all the labeled data fields (but use the publication's print method for the three inherited data fields).
A cd "is a" publication
< Also has a number of minutes.
< Initialize all data fields (to the user input) by using the publication constructor for inherited fields.
< The print method will be similar to the book print.
You will then create a main class that will:
< You will prompt and input all the data fields (asking the user what kind of publication for what added data to input and take upper or lowercase).
< Create the object.
< Call the method to raise the publication references by 10% (as a decimal amount argument).
< Call the print method.
Explanation / Answer
The answer is as follows:
The code is as follows:
import java.io.*;
import java.util.*;
public class Publication {
private String title;
private double price;
private int year;
public Publication(String ti,double pr, int yr){
title = ti;
price = pr;
year = yr;
}
public void raise(double a){
price = price + price * a;
}
public void print(){
System.out.println("Title:" + title);
System.out.println("Price:" + String.format("%.2f",price));
System.out.println("Year:" + year);
}
}
public class Book extends Publication {
private int num_pages;
public Book(string ti,double pr, int yr, int n){
super(ti,pr,yr);
num_pages = n;
}
public void print(){
super.print();
System.out.println("No of Pages:" + num_pages);
}
}
public class CD extends Publication {
private int minutes;
public Book(string ti,double pr, int yr, int m){
super(ti,pr,yr);
minutes = m;
}
public void print(){
super.print();
System.out.println("Minutes:" + minutes);
}
}
public class Main {
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
String title;
double price;
int year;
int num_pages;
int minutes;
int choice;
do {
System.out.println("1.Create Book");
System.out.println("2.Create CD");
System.out.println("3.Quit");
choice = reader.nextInt();
switch (choice) {
case 1:
System.out.println("Enter title:");
title = reader.next();
System.out.println("Enter Price:");
price = reader.nextDouble();
System.out.println("Enter year:");
year = reader.nextInt();
System.out.println("Enter No of Pages:");
num_pages = reader.nextInt();
Book bk = new Book(title,price,year,num_pages);
bk.print();
bk.raise(0.1) // raise by 10%
bk.print();
case 2:
System.out.println("Enter title:");
title = reader.next();
System.out.println("Enter Price:");
price = reader.nextDouble();
System.out.println("Enter year:");
year = reader.nextInt();
System.out.println("Enter Minutes:");
minutes = reader.nextInt();
CD cd = new CD(title,price,year,minutes);
cd.print();
cd.raise(0.1) // raise by 10%
cd.print();
}
} while (choice != 3)
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.