have written a superclass, main class and 2 subclasses, but I did error in diffe
ID: 3539806 • Letter: H
Question
have written a superclass, main class and 2 subclasses, but I did error in different parts so I want know where my errors are.
1st subclass = book subclass
2nd subclass = CD subclass
The superclass consists of: title,price, year
The super class should :initialize title,price, year
raise the price to 10%
tell the user how to use the publication to look up a specific topic.(it will be bstract).
That's what I have for superclass so far:
public abstract class superclass
{
protected String title;
protected double price;
protected int year;
public superclass(String t, int y)
{
title = t;
price *= 0.1;
year = y;
}
public void print()
{
System.out.print("Title is" + title);
System.out.printf("Price is = %.2f " , price);
System.out.print("Publication Year is " + year);
}
public abstract String getsuperclassinfo();
}
HELP ME IF I DID ANY ERRO AND GUIDE ME THROUGH BOOK,CD SUBCLASSES AND MAIN CLASS:
BOOK SUBCLASS:
use what inherited
and has a number of pages
use method to tell user to look up their topic in the table of contents
print method will label the output as a book and print all labeled data fields (use publication's print method for title,price,year)
CD SUBCLASS
use what inherited: title,price,year
also has a number of minutes
use method to tell the user to look up their topic on the cd
print method ==label output as CD and print all labeled data fields
MAIN CLASS:
ask user for how many publications there are.
declare array of abstract super references of publication of the # of publications.
use for loop
prompt and input all data fields (ask user what kind of publication for what added data to input and take upper or lowercase)
create object and assign it into the next reference in the array
start another for loop
call method to raise each of publication ref. by 10%
print method with dynamic method bindin for each of the publication references
use method with dynamic method binding for each of the publication references.
The Data should be entered as follows:
Input:
Enter number of publications
Enter title
Enter price
Enter publication year
Is publication a boook or cd
enter pages
enter title
enter price
enterpublication year
is publication a book or cd
enter minutes
output:
Book
Title is...
Price is
publication year is
number of pages is
open book to table of contents
look up topic and turn to corresponding page
CD
title is
price is
publication year
number of minutes
look on cd to find topic number
skip to correspondin section
Explanation / Answer
import java.util.*;
abstract class superclass
{
protected String title;
protected double price;
protected int year;
public superclass(String t, int y,double p)
{
title = t;
price = p;
year = y;
}
public void print()
{
System.out.println(" Title is " + title);
System.out.printf("Price is = %.2f " , price);
System.out.println("Publication Year is" + year);
}
public abstract String getpublicationinfo();
public abstract void look_into_contents();
}
class book extends superclass
{
private int no_of_pages;
public book(String t,int y,double p,int no)
{
super(t,y,p);
no_of_pages = no;
}
public void print()
{
super.print();
System.out.println("This book has number of pages " + no_of_pages);
}
public void look_into_contents()
{
System.out.println("look up their topic in the table of contents");
}
public void raise_price()
{
price*=1.1;
}
public String getpublicationinfo()
{
return ("Title is" + title);
}
}
class CD extends superclass
{
private int no_of_minutes;
public CD(String t,int y,double p,int no)
{
super(t,y,p);
no_of_minutes = no;
}
public void print()
{
super.print();
System.out.println("This CD has number of minutes " + no_of_minutes);
}
public void look_into_contents()
{
System.out.println("look up their topic on CD ");
}
public void raise_price()
{
price*=1.1;
}
public String getpublicationinfo()
{
return ("Title is" + title);
}
}
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println(" How many total publication u have? ");
int no = in.nextInt();
superclass[] array = new superclass[no];
for(int i=0; i<no; i++)
{
System.out.println("Enter title");
String title = in.next();
System.out.println("Enter price");
double price = in.nextDouble();
System.out.println("Enter publication year");
int year = in.nextInt();
System.out.println("Is publication a boook or cd");
String book_or_cd = in.next();
if(book_or_cd.equalsIgnoreCase("book"))
{
System.out.println("enter pages");
array[i] = new book(title,year,price,in.nextInt());
}
else if(book_or_cd.equalsIgnoreCase("CD"))
{
System.out.println("enter minutes");
array[i] = new CD(title,year,price,in.nextInt());
}
}
for(int i=0; i<no; i++)
{
array[i].print();
array[i].look_into_contents();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.