I have written a superclass, main class and 2 subclasses, but I did error in dif
ID: 3539803 • Letter: I
Question
I 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).
This is what I have for the super class:
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 getpublicationinfo();
}
correct if i did something wrong and please guide me into the book and cd subclasses:
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 sublcass:
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
Explanation / Answer
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.print("Title is" + title);
System.out.printf("Price is = %.2f " , price);
System.out.print("Publication Year is " + year);
}
public abstract String getpublicationinfo();
}
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 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 String getpublicationinfo()
{
return ("Title is" + title);
}
}