Q4 120 pts) (classes) Design a class named Item that contains the following inst
ID: 3792875 • Letter: Q
Question
Q4 120 pts) (classes) Design a class named Item that contains the following instance variables: A string data field named name for the item name. An int data field named quantity for the item quantity. A double data field named previousPrice that stores the item price for the previous day A double data field named currentPrice that stores the item price for the current time. It contains the following methods: A constructor that creates an item with specified name, quantity and previousPrice. A set and get method for each instance variable. A method named getChangePercent0 that returns the percentage changed from previous Price to currentPrice. Implement the class then write a test program that creates an Item object with the item name Desktop, the quantity 3, and the previous price of 200 KD. Do the following: Set a new current price to 250 KD. Display the price change percentage. Print the total current price. [Use the get method to print the quantity, name and current price of the item] Sample output Price change percentage 25.0% Total price of 3 Desktop 750.0 KDExplanation / Answer
1) Item Class
public class Item {
String Name;
int Quantity;
double previousDayPrice;
double currentDayPrice;
Item(String name, int quant, double Previousdayprice){
this.Name = name;
this.Quantity = quant;
this.previousDayPrice = Previousdayprice;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getQuantity() {
return Quantity;
}
public void setQuantity(int quantity) {
Quantity = quantity;
}
public double getPreviousDayPrice() {
return previousDayPrice;
}
public void setPreviousDayPrice(double previousDayPrice) {
this.previousDayPrice = previousDayPrice;
}
public double getCurrentDayPrice() {
return currentDayPrice;
}
public void setCurrentDayPrice(double currentDayPrice) {
this.currentDayPrice = currentDayPrice;
}
}
2) class to print Item class data
public class PrintIteminfomration {
public static void main(String args[]){
Item item1 = new Item("Desktop",3,200);
item1.setCurrentDayPrice(250);
int pricechangePercentage = (int) ((int)((item1.getCurrentDayPrice()-item1.getPreviousDayPrice())* 100)/item1.getPreviousDayPrice());
System.out.println("Price change percentage = "+pricechangePercentage+"%");
System.out.println("Total price od "+item1.getQuantity()+" "+item1.getName() + " = "+( item1.getCurrentDayPrice()*item1.getQuantity()));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.