Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider an abstract Product Class having two subclasses WeighedProduct and CountedProduct. Product class has private instance variables product ID, Name and Price, a parameterized constructor

Consider an abstract Product Class having two subclasses WeighedProduct and CountedProduct. Product class has private instance variables product ID, Name and Price, a parameterized constructor and a toString. WeighedProduct has an additional instance variable weight in Kg while CountedProduct has an additional instance variable quantity (both private). In addition to a parameterized constructor and a toString methods in CountedProduct and WeighedProduct, both classes have getPrice() method that returns the total amount of the purchased products based on its unit price and weight (WeighedItem), or quantity (CountedItem). Following is the driver class for the given scenario public class ProductTest { public static void main(String[] args) { ArrayList bill = new ArrayList(); bill.add(new WeighedProduct(1.37,"Rice",3.00)); bill.add(new CountedProduct(10,"Pens",4.5)); bill.add(new CountedProduct(10,"Pens",20)); System.out.println(billTotal(bill)); ArrayList cp = new ArrayList(); cp.add(new CountedProduct(10,"Pens",4.5)); cp.add(new CountedProduct(10,"Pens",20)); System.out.println(billTotal(cp)); ArrayList wp = new ArrayList(); wp.add(new WeighedProduct(1.37,"Rice",3.00)); System.out.println(billTotal(wp)); writeProduct(bill, args); readProduct(bill, args); } } You are required to do the following: a) Write a generic final static method billTotal that returns the total cost price of products in the ArrayList passed as an argument to the billTotal method. Make sure the method billTotal should be applicable only for the ArrayList of Product and its subclasses. [5 marks] b) Write a generic static method writeProduct that writes objects to the file (whose name is passed as command line argument). Make sure the method writeProduct should be bounded to service ArrayList of products and its subclasses only. [10 marks] c) Write a generic static method readProduct that reads objects from the file (whose name is passed as command line argument). The readProduct method displays the details of all the objects of WeighedProduct and for CountedProduct, the object with product ID 203. Make sure the method readProduct should be applicable for the ArrayList of products and its subclasses only.

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

Database Design Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago