A kidsMagazine is a magazine that has a recommended age range. When you subscrib
ID: 3633779 • Letter: A
Question
A kidsMagazine is a magazine that has a recommended age range. When you subscribe to kids magazine, you must provide the age of the subscriber. The subscription is accepted only if the age is in the proper range.
Define a publication hierarchy.
Write a test class that creates a $ 14.00 book java by java Javison. A magazine called Bicycling that is published monthly for $ 4 an issue, and a kid’s magazine called Ranger Rick for ages 6-11 that is published weekly and coasts $ 2.00 an issue. Simulate the following transactions with the appropriate method calls.
. Shai subscribes to Bicycling magazine and pays $ 45.
. java Javison owns his own book then sells it to Ralph for $ 35
. Another copy of java Javsion’s book is created and owned by the author.
. Emily, an 11-yr old girl, subscribes to Range Rick and receives four issues.
. Emily adds an extra two years to here subscription and then sells it to Charlie, who is 10 years old, who
pays $ 250.
. Charlie receives 10 issues and tries to sell it for $ 200 to java Javison, who is 27 years old.
Explanation / Answer
I'm still going with the same assumptions as the pervious exercise about the publishing class. We can make anu necessary modifications later.
Here's the hierarchy that makes sense to me according to the examples provided, if anything else is overlooked, then please provide more context :)
Publishing class ( the Ancestor )
Variables: Owner, Price, Title, Pages
Methods: Sell() sets owner, returns change from money
Book class (1st generation inheritance) extends Publishing
Variables: Author + those inherited from Publishing
Methods: the one inherited from Publishing (Sell)
Magazine class (also 1st generation inheritance) extends Publishing
Variables: period String (mothly, weekly, etc) + issues integer (number of issues subscribed for) + those inherited from Publishing
Methods: overloaded sell() sets owner, and issues number from subscription period input, returns change for amout given issues
KidsMagazine class (2nd gen inheritance) extends Magazine duh
Variables: recommended age range as 2 integers ageMin and ageMax
Methods: overloaded sell() method that also takes the age range to check it
deciding to count issues persubscription for a given owner, and overloading the sell method instead of using a new method subscribe is a jugment call+my interpretation of your examples. Also, I decided to count subscription time by month, so even for a weekly, the minimum subscription period would be 1 month. Again, your call unless there are explicit requirements.
Also, some of the numbers don't seem to add, I think charlie should have 100 isssues by the end up OR I did'tn get it, and everytime using one means or another of setting up a subscription is making me think using a separate subscribe method is probably a good idea... but still making it work with overridden and overloaded versions of the sell method. So here it goes
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 = "";
}
/* getters & setters I might need */
public String getOwner()
{
return owner;
}
public void setOwner(String o)
{
owner = o;
}
public double getPrice()
{
return price;
}
/* 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 [" + title + "] pages [" + pages +
"] price [" + price + "] owner [" + owner + "]";
}
}
public class PublishingTest {
public static void main(String[] args) {
double change = 0;
System.out.println("Creating a book");
Book java = new Book(390, 14, "Java", "Java Javison");
System.out.println(java.toString());
System.out.println("Creating a magazine");
Magazine bicycling = new Magazine(30, 4 , "Bicycling", "Monthly");
System.out.println(bicycling.toString());
System.out.println("Creating a kids magazine");
KidsMagazine rangerRick = new KidsMagazine(25, 2 , "Ranger Rick",
"Weekly", 6, 11);
System.out.println(rangerRick.toString());
System.out.println("Simulating various transactions");
System.out.println("Shai subscribes to Bicycling magazine and pays $ 45");
change = bicycling.sell("Shai", 45.0);
System.out.println("Sold! Change is: " + change);
System.out.println(bicycling.toString());
System.out.println("Java Javison owns his own book then sells it to Ralph for $ 35");
change = java.sell("Ralph", 35.0);
System.out.println("Sold! Change is: " + change);
System.out.println(java.toString());
System.out.println("Another copy of java Javsion’s book is created and owned by the author");
Book java2 = new Book(390, 14.0, "Java", "Java Javison");
java2.setOwner("Java Javison");
System.out.println(java2.toString());
System.out.println("Emily, an 11-yr old girl, subscribes to Range Rick and receives four issues");
change = rangerRick.sell("Emily",1, 11);
System.out.println(rangerRick.toString());
System.out.println("Emily adds an extra two years to here subscription");
change = rangerRick.sell("Emily", 12*2, 11);
System.out.println(rangerRick.toString());
System.out.println("and then sells it to Charlie, who is 10 years old, who pays $ 250");
change = rangerRick.sell("Charlie", 250.0, 10);
System.out.println("Change is: " + change);
System.out.println(rangerRick.toString());
System.out.println("Charlie receives 10 issues and tries to sell it for $200 to java Javison, who is 27 years old");
change = rangerRick.sell("Java Javison", 200.0, 27);
System.out.println("Change is: " + change);
System.out.println(rangerRick.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.