Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 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 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 - DATA

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.

i have the solution can any body fix it and make it work...

/** * @author Srinivas * */ public abstract class Publication {

String title; double price; int year;

/** * @param title * @param price * @param year */ public Publication(String title, double price, int year) {

this.title = title; this.price = price; this.year = year; }

/** * @param percent */ public void raisePrice(int percent) {

price = price + (price * ((double) percent / 100.00));

}

/** * method to print the publication object */ public void print() {

System.out .printf("Title:%s, Price:%.2f, Year:%d ", title, price, year); }

public abstract void use();

}

/** * @author Srinivas * */ public class Book extends Publication { int numberOfPages;

public Book(String title, double price, int year, int numberOfPages) { super(title, price, year); // TODO Auto-generated constructor stub

this.numberOfPages = numberOfPages; }

@Override public void use() { // TODO Auto-generated method stub System.out.println("Turn to corresponding Page");

}

@Override public void print() { // TODO Auto-generated method stub super.print(); System.out.println("Number of Pages :" + numberOfPages);

}

}

/** * @author Srinivas * */ public class CD extends Publication { int minutes;

public CD(String title, double price, int year, int minutes) { super(title, price, year); // TODO Auto-generated constructor stub this.minutes = minutes; }

@Override public void use() { // TODO Auto-generated method stub System.out.println("Look up topic on the cd");

}

@Override public void print() { // TODO Auto-generated method stub super.print(); System.out.println("Minutes:" + minutes); }

}

import java.util.Scanner;

/** * @author Srinivas * */ public class TestPublication {

/** * @param args */ public static void main(String[] args) { Scanner scanner = null; try { scanner = new Scanner(System.in); System.out.print("Enter the number of Publications:"); int noofPub = scanner.nextInt(); Publication publication[] = new Publication[noofPub]; for (int i = 0; i < noofPub; i++) {

System.out.print("Type of Publication(CD/Book)?"); String typeOfPub = scanner.next(); if (typeOfPub.equalsIgnoreCase("CD")) { System.out.print("Enter the Title:"); String title = scanner.next();

System.out.print("Enter the Price:"); double price = scanner.nextDouble();

System.out.print("Enter the Year:"); int year = scanner.nextInt();

System.out.print("Enter the minutes:"); int minutes = scanner.nextInt(); publication[i] = new CD(title, price, year, minutes); } else if (typeOfPub.equalsIgnoreCase("book")) {

System.out.print("Enter the Title:"); String title = scanner.next();

System.out.print("Enter the Price:"); double price = scanner.nextDouble();

System.out.print("Enter the Year:"); int year = scanner.nextInt();

System.out.print("Enter the Pages:"); int pages = scanner.nextInt(); publication[i] = new Book(title, price, year, pages); }

} // calling raise() for (int i = 0; i < noofPub; i++) { publication[i].raisePrice(10);

} // calling print() for (int i = 0; i < noofPub; i++) { publication[i].print();

} // calling use() for (int i = 0; i < noofPub; i++) { publication[i].use();

}

} catch (Exception e) { // TODO: handle exception } finally { scanner.close(); } } }

OUTPUT:

Enter the number of Publications:3 Type of Publication(CD/Book)?CD Enter the Title:Lax Enter the Price:100 Enter the Year:2015 Enter the minutes:5 Type of Publication(CD/Book)?Book Enter the Title:Java Enter the Price:500 Enter the Year:2013 Enter the Pages:500 Type of Publication(CD/Book)?Book Enter the Title:Javascript Enter the Price:600 Enter the Year:2011 Enter the Pages:360 Title:Lax, Price:110.00, Year:2015 Minutes:5 Title:Java, Price:550.00, Year:2013 Number of Pages :500 Title:Javascript, Price:660.00, Year:2011 Number of Pages :360 Look up topic on the cd Turn to corresponding Page Turn to corresponding Page

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions