Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Download the classes that are provided to you: Currency, Coin, Paper Money, Book, and SportCard. Make sure you understand them. Context: You like to collect

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Download the classes that are provided to you: Currency, Coin, Paper Money, Book, and SportCard. Make sure you understand them. Context: You like to collect rare coins, books, and sport cards. You would like to keep your collection of items into a single ArrayList. Because those objects are of different type, you will have to use polymorphism. However, those objects do not have much in common, other than having a monetary value and the amount you paid for them (and the related methods). Also, the Coin class already inherits from another class, so inheritance cannot be used here. Make the appropriate modifications to the classes provided (but not removing the inheritance links) so that you could have an ArrayList of all such objects. Then create a driver class (i.e., main method in a separate class) that performs the following: 1. Create some objects for your collection. You should have at least one Book, at least one Sport Card, and at least 3 Coins. Note: you do not have to initialize them with values that really exist, but try to have something that still make sense in the context (e.g., when a name of a person is required, put a string that looks like a name, even if that name does not exist). 2. Create an ArrayList and put the objects created above in it. 3. Use a "for each" loop (i.e., not a loop on indexes) to go over each element of the list, and perform the following: a. Calculate the total value of all items (sum) b. Calculate the total profit on all items (sum) c. When the object is a book, call the printReference method on it d. When the object is a coin, make the necessary calculations so that you will be able to print the average quality level of all coins at the end 4. After you loop, print all results: sum of values, sum of profits, and average quality level of the coins. Use the Number Format class to format the sums properly (as currency). Use the DecimalFormat class to format the average quality level with 2 decimal places. If there are no coins in the list, print a message indicating that rather than attempting to calculate the average, otherwise your program could crash (divide by zero). Important: your code above should work for any list of items, of any size. It should not use any knowledge of what you put inside. Hint: find the true class of the object using "instanceof". 2 class SportCard 000 NOU AWNP private String athlete; private String sport; private String team; private double value; private double amount Paid; public SportCard (String athlete, String sport, String team, double value, double amount Paid) this.athlete = athlete; this.sport = sport; this.team = team; this.value = value; this. amountPaid = amount Paid: ca public String getAthlete(), { return athlete; N w 24 25 public double getValue(); 26 27 return value; public void setValue( double value) 31 this.value = value; public double calcProfit() return value - amount Paid; 38 39 w class Book covou AWN private String title; private String[] authors; private String editor; private int year; private double value; private double amount Paid; public Book(String title, String[] authors, String editor, int year, double value, double amountPaid), 14 15 16 this.title = title; this.authors = authors this.editor = editor; this.year = year; this.value = value; this.amountPaid = amount Paid; 20 21 public void printReference (), 23 24 25 26 for (String author : authors) System.out.print(author + ", "); System.out.print("\" + title + "\", "); System.out.print(editor + ", "); System.out.println(year + "."); 27 28 29 30 31 public double getValue() 32 33 return value; 34 35 36 public void setValue( double value) 37 38 this.value = value; public double calcProfit(); return value - amountPaid; } class PaperMoney extends Currency Ovou W NP private int faceValue; // e.g., 10 private String name; // e.g., dollars public PaperMoney(String country, int faceValue, String name) 10 super(country); this.faceValue = faceValue; this.name = name; 14 public String getFullName(), 15 return faceValue + " " + name; 17 18 } class Coin extends Currency private String denomination; // e.g., 5 cents private int year; // e.g., 1970 private int qualityLevel; // a number between 1 and 5, 1 = bad, 5 = excellent private double value; // e.g., $8.33 private double amount Paid; // e.g., $2.50 public Coin(String country, String denomination, int year, int qualityLevel, double value, double amount Paid), super(country); this.denomination = denomination; this.year = year; this.qualityLevel = qualityLevel; this.value = value; this.amountPaid = amount Paid; public String getFullName() return denomination; public int getQualityLevel() return qualityLevel; public double getValue() return value; public void setValue( double value) this.value = value; public double calcProfit() { return value - amountPaid; abstract class Currency protected String country; // e.g., Canada ovou WNE public Currency (String country) this.country = country; 10 11 public String getCountry(), 12 13 return country; 14 15 16 abstract public String getFullName(); 17 18 19 Download the classes that are provided to you: Currency, Coin, Paper Money, Book, and SportCard. Make sure you understand them. Context: You like to collect rare coins, books, and sport cards. You would like to keep your collection of items into a single ArrayList. Because those objects are of different type, you will have to use polymorphism. However, those objects do not have much in common, other than having a monetary value and the amount you paid for them (and the related methods). Also, the Coin class already inherits from another class, so inheritance cannot be used here. Make the appropriate modifications to the classes provided (but not removing the inheritance links) so that you could have an ArrayList of all such objects. Then create a driver class (i.e., main method in a separate class) that performs the following: 1. Create some objects for your collection. You should have at least one Book, at least one Sport Card, and at least 3 Coins. Note: you do not have to initialize them with values that really exist, but try to have something that still make sense in the context (e.g., when a name of a person is required, put a string that looks like a name, even if that name does not exist). 2. Create an ArrayList and put the objects created above in it. 3. Use a "for each" loop (i.e., not a loop on indexes) to go over each element of the list, and perform the following: a. Calculate the total value of all items (sum) b. Calculate the total profit on all items (sum) c. When the object is a book, call the printReference method on it d. When the object is a coin, make the necessary calculations so that you will be able to print the average quality level of all coins at the end 4. After you loop, print all results: sum of values, sum of profits, and average quality level of the coins. Use the Number Format class to format the sums properly (as currency). Use the DecimalFormat class to format the average quality level with 2 decimal places. If there are no coins in the list, print a message indicating that rather than attempting to calculate the average, otherwise your program could crash (divide by zero). Important: your code above should work for any list of items, of any size. It should not use any knowledge of what you put inside. Hint: find the true class of the object using "instanceof". 2 class SportCard 000 NOU AWNP private String athlete; private String sport; private String team; private double value; private double amount Paid; public SportCard (String athlete, String sport, String team, double value, double amount Paid) this.athlete = athlete; this.sport = sport; this.team = team; this.value = value; this. amountPaid = amount Paid: ca public String getAthlete(), { return athlete; N w 24 25 public double getValue(); 26 27 return value; public void setValue( double value) 31 this.value = value; public double calcProfit() return value - amount Paid; 38 39 w class Book covou AWN private String title; private String[] authors; private String editor; private int year; private double value; private double amount Paid; public Book(String title, String[] authors, String editor, int year, double value, double amountPaid), 14 15 16 this.title = title; this.authors = authors this.editor = editor; this.year = year; this.value = value; this.amountPaid = amount Paid; 20 21 public void printReference (), 23 24 25 26 for (String author : authors) System.out.print(author + ", "); System.out.print("\" + title + "\", "); System.out.print(editor + ", "); System.out.println(year + "."); 27 28 29 30 31 public double getValue() 32 33 return value; 34 35 36 public void setValue( double value) 37 38 this.value = value; public double calcProfit(); return value - amountPaid; } class PaperMoney extends Currency Ovou W NP private int faceValue; // e.g., 10 private String name; // e.g., dollars public PaperMoney(String country, int faceValue, String name) 10 super(country); this.faceValue = faceValue; this.name = name; 14 public String getFullName(), 15 return faceValue + " " + name; 17 18 } class Coin extends Currency private String denomination; // e.g., 5 cents private int year; // e.g., 1970 private int qualityLevel; // a number between 1 and 5, 1 = bad, 5 = excellent private double value; // e.g., $8.33 private double amount Paid; // e.g., $2.50 public Coin(String country, String denomination, int year, int qualityLevel, double value, double amount Paid), super(country); this.denomination = denomination; this.year = year; this.qualityLevel = qualityLevel; this.value = value; this.amountPaid = amount Paid; public String getFullName() return denomination; public int getQualityLevel() return qualityLevel; public double getValue() return value; public void setValue( double value) this.value = value; public double calcProfit() { return value - amountPaid; abstract class Currency protected String country; // e.g., Canada ovou WNE public Currency (String country) this.country = country; 10 11 public String getCountry(), 12 13 return country; 14 15 16 abstract public String getFullName(); 17 18 19

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

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

What are all the ways you count or measure customer complaints?

Answered: 1 week ago

Question

Do your staff and customers know these examples?

Answered: 1 week ago