A publishing has a publishing, number of pages, a price, an owner, and a title.
ID: 3633778 • Letter: A
Question
A publishing has a publishing, number of pages, a price, an owner, and a title. When
a publication object is created using a constructor the number of pages , price, and
The title must be supplied. A default constructor uses blank and zero values. When
a publication is created, it no owner. An owner can be set, and the publication explicitly sold, using The:
Double sell(String owner, double amount)
Method. The method call
p.sell(String owner, double amount)
sells publication p to owner and returns the change, from amount if there is any . For example, if the price of publication p is $ 5.89, then p.sell(“shai”, 6.0); sets “shai” as the owner of publication p and return 0.11. The sell(…) method can be called numerous times, as the publication is sold and resold.
Explanation / Answer
I sent you a PM for clarification, don't you if you missed that or...
I'm not sure if a class publishing containing an object of type publishing in it represents a good example of composition, I don't know. I may be reading the requirements wrong, or there's a typo somewhere, or some already existing classes and/or context needs to be included here.
Anyway, I'll go ahead and do what makes sense to me, without composition, and then you can explain what's missing/wrong and we can edit it later.
--------------------------------------------------------------------------------
the class Publishing.java
public class Publishing {
/* Private variables declaration */
private int pages;
private double price;
private String title;
private String owner;
/* Constructors */
public Publishing()
{
pages = 0;
price = 0;
title = "";
owner = "";
}
public Publishing(int pgs, double pr, String titl )
{
pages = pgs;
price = pr;
title = titl;
owner = "";
}
/* sell method implemented */
public double sell(String own, double amount)
{
owner = own;
return amount - price;
}
/* toString method just to display stuff */
public String toString()
{
return title + " pages: " + pages +
" price: " + price +
" owner: " + owner;
}
}
--------------------------------------------------------------------------------
the test class for it PublishingTest.java
public class PublishingTest {
public static void main(String[] args) {
System.out.println("Initialzing a publication");
Publishing p = new Publishing(120, 5.89, "An Interesting Read");
System.out.println(p.toString());
System.out.println("Selling it to Shai, change for $6.00...");
double change = p.sell("Shai", 6);
System.out.println("Sold! Change is: " + change);
System.out.println(p.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.