I have a question using processing code. Write the class definition for StockIte
ID: 3647994 • Letter: I
Question
I have a question using processing code.Write the class definition for StockItem containing title, quantityLeft and
unitPrice, containing -
1. Default constructor
2. Parameterized constructor (convert any negative quantity or unitPrice that is
passed as parameter to positive value before copying into data members)
3.display() method
4.getTotalPrice() method that returns the total price of that StockItem
5.isInStock() method that returns true if the StockItem is in stock, and
false otherwise
Also write a client code that creates an object myItem that has 8 quantities left @ 4.5$
per item, yourItem that has 6 quantities left @ 5.6$ per item, and invoke each of the
methods on myItem, passing yourItem as a parameter to isMoreExpensiveThan method.
Explanation / Answer
class StockItem{ private int quantityLeft; private int unitPrice; public StockItem(){ } public StockItem(int quantityLeft , int unitPrice){ // converting to positive if the passed parameters are negative. if(quantityLeft < 0){ quantityLeft = -quantityLeft; } if(unitPrice < 0){ untiPrice = -untiPrice; } this.quantityLeft = quantityLeft; this.unitPrice = unitPrice; } public void display(){ System.out.println("quantity left : " + quantityLeft); System.out.println("unit price: " + unitPrice); return ; } public int getTotalPrice(){ return unitPrice*quantityLeft; } public boolean isInStock(){ return StockItem; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.