Classes & Objects
in
Programming Questions
•
1 year ago
Hey all,
I was casually doing a practical task for my class until i get a major stone wall (as usual). Although i think i've managed to do all of it, its not 100% complete. Any help would be greatly appreciated :).
Write the class definition for StockItem containing quantityLeft and unitPrice, containing -
1. Default constructor //I believe this is done
2. Parameterized constructor (with validation checks performed) //and this - but without validation checks.
3. display() method //almost there
4. getTotalPrice() method //almost there
5. isMoreExpensiveThan(StockItem other) method (comparison based on unit price) //almost there
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.
-
-
class StockItem {
-
int quantityLeft;
-
float unitPrice;
-
-
//default constructor
-
StockItem() {
-
quantityLeft = 0;
-
unitPrice = 0;
-
}
-
-
//parameterized constructor 1
-
StockItem(int _quantityLeft, float _unitPrice) { //StockItem m =
-
quantityLeft = _quantityLeft;
-
unitPrice = _unitPrice;
-
}
-
//Display method
-
void display() {
-
println();
-
}
-
-
//totalPrice Method
-
int getTotalPrice() {
-
totalPrice = _unitPrice*_quantityLeft;
-
}
-
-
//isMoreExpensiveThan Method
-
boolean isMoreExpensiveThan(int y1, int m1) {
-
if (m1._unitPrice() > y1._unitPrice())
-
return true;
-
else
-
return false;
-
}
-
}
-
-
StockItem m1, y1;
-
//myItem m = new StockItem(); //@ $4.5 per item
-
//yourItem y = new StockItem(); //@ $5.6 per item
-
-
void setup(){
-
size(600,400);
-
background(255);
-
smooth();
-
}
-
-
void draw(){
-
background(255);
-
m1 = new StockItem(8, 4.5);
-
y1 = new StockItem(6, 5.6);
-
m1.display();
-
y1.display();
-
}
-
1