You will create an abstract publication class that will define a general publica
ID: 3684381 • Letter: Y
Question
You will create an abstract publication class that will define a general publication. It will be too general for specific publications.
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)
< tell the user how to use the publication to look up a specific topic (but since a publication is too general, it will be abstract because we could not tell how to use it unless we have a specific kind of publication).
You will then create two subclasses that "is 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 use method will be to tell the user to look up their topic in the table of contents and turn to the corresponding page.
< 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 use method will be to tell the user to look up their topic on the cd and skip to the corresponding section.
< The print method will be similar to the book print.
You will then create a main class that will:
< Prompt the user for how many publications there are.
< Declare an array of abstract super references of publications of the size the user requested.
< Use a for loop.
< 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 and assign the object into the next reference in the array.
< Start a separate for loop.
< Call the method to raise each of the publication references by 10% (as a decimal amount argument).
< Call the print method (with dynamic method binding!) for each of the publication references.
< Call the use method (with dynamic method binding!) for each of the publication references.
Document all java files with at least 4 lines of comments at the top and at least 5 comments throughout the code for all of the not easily understandable lines of code. Run your program with the data below. Submit your three class definitions, main program, and output .txt saved.
PUBLICATION INHERITANCE - ARRAY - DAT
Input:
Enter number of publications: 2
Enter title: Java Reader
Enter price: 17.50
Enter publication year: 2015
Is publication a Book (b) or CD (c): b
Enter pages: 250
Enter title: Java Listener
Enter price: 24.95
Enter publication year: 2016
Is publication a Book (b) or CD (c): C
Enter minutes: 330
Output:
Book
Title is Java Reader
Price is 19.25
Publication Year is 2015
Number of pages is 250
Open book to Table of Contents.
Look up topic and turn to corresponding page.
CD
Title is Java Listener
Price is 27.45
Publication Year is 2016
Number of minutes is 330
Look on CD to find topic number.
Skip to corresponding section.
Explanation / Answer
Hello there ,
Kindly find below all three classes ,main app and output.
==========
import java.text.DecimalFormat;
/**
* This is an abstract class containing publication information. It holds title
* price publication year
*
* @author dipal.prajapati
*
*/
public abstract class Publication {
String title;
double price;
int year;
public Publication(String title, double price, int year) {
super();
this.title = title;
this.price = price;
this.year = year;
}
/**
* It prints publication info.
*/
public void print() {
System.out.println("Title is " + title);
System.out.println("Price is " + price);
System.out.println("Publication Year is " + year);
}
/**
* It gives usage guidance.
*/
public void use() {
}
/**
* It raise price by perc.
*
* @param perc
*/
public void raisPrice(double perc) {
DecimalFormat df = new DecimalFormat("#.##");
price = (double) Math.round(Double.parseDouble(df.format(price
+ ((price * perc) / 100))) * 100) / 100;
}
}
==========
/**
* This is a child class of Publication,add book specific information. It holds
* noOfPages
*
* @author dipal.prajapati
*
*/
public class Book extends Publication {
int noOfPages;
public Book(String title, double price, int year, int noOfPages) {
super(title, price, year);
this.noOfPages = noOfPages;
}
/**
* It prints Book information.
*/
@Override
public void print() {
System.out.println("Book");
super.print();
System.out.println("Number of pages is " + noOfPages);
}
/**
* It gives Book usage guidance.
*/
@Override
public void use() {
System.out.println("Open book to Table of Contents.");
System.out.println("Look up topic and turn to corresponding page. ");
}
}
=========
/**
* This is a child class of Publication,add cd specific information. It holds
* noOfMinutes
*
* @author dipal.prajapati
*
*/
public class CD extends Publication {
int noOfMinutes;
public CD(String title, double price, int year, int noOfMinutes) {
super(title, price, year);
this.noOfMinutes = noOfMinutes;
}
/**
* It prints CD information.
*/
@Override
public void print() {
System.out.println("CD");
super.print();
System.out.println("Number of minutes is " + noOfMinutes);
}
/**
* It gives CD usage guidance.
*/
@Override
public void use() {
System.out.println("Look on CD to find topic number.");
System.out.println("Skip to corresponding section. ");
}
}
======
import java.util.Scanner;
public class PublicationApp {
static Scanner reader = new Scanner(System.in); // Reading from System.in
public static void main(String args[]) {
System.out.println("Input:");
System.out.println("Enter number of publications: ");
int numOfPubs = reader.nextInt();
Publication[] publicationsArray = new Publication[numOfPubs];
for (int index = 0; index < numOfPubs; index++) {
Publication pubObject = null;
// Eat the new line
reader.nextLine();
System.out.println("Enter title:");
String title = reader.nextLine();
System.out.println("Enter price:");
double price = reader.nextDouble();
System.out.println("Enter publication year:");
int year = reader.nextInt();
System.out.println("Is publication a Book (b) or CD (c):");
String isBookOrCD = reader.next();
if (isBookOrCD.equals("b")) {
System.out.println("Enter pages:");
int noOfPages = reader.nextInt();
pubObject = new Book(title, price, year, noOfPages);
} else if (isBookOrCD.equals("c")) {
System.out.println("Enter minutes:");
int noOfMinutes = reader.nextInt();
pubObject = new CD(title, price, year, noOfMinutes);
}
publicationsArray[index]=pubObject;
}
System.out.println(" Output: ");
for (int index = 0; index < publicationsArray.length; index++) {
publicationsArray[index].raisPrice(10);
publicationsArray[index].print();
publicationsArray[index].use();
}
}
}
======
Let me know if you have any queries.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.