The Super-class Write a class called Item. The Item class represents all that is common between various multimedia items. It should have the following attributes (fields): a) a String name to store the name of the item. b) a String comment to store a comment about the item. c) a double value to store how much the item cost. It should have appropriate constructors and at least the following methods a) getters and setters for the attributes. b) a public String toString() method that returns a String representation of the Item. c) a method void depreciate() that decreases the value of the item by 5%.
class Item {
// a) a String name to store the name of the item.
private String name;
// b) a String comment to store a comment about the item.
private String comment;
// c) a double value to store how much the item cost.
private double cost;
// It should have appropriate constructors and at least the following methods
public Item() {
}
public Item(String name, String comment, double cost) {
this.name = name;
this.comment = comment;
this.cost = cost;
}
// a) getters and setters for the attributes.
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the comment
*/
public String getComment() {
return comment;
}
/**
* @param comment the comment to set
*/
public void setComment(String comment) {
this.comment = comment;
}
/**
* @return the cost
*/
public double getCost() {
return cost;
}
/**
* @param cost the cost to set
*/
public void setCost(double cost) {
this.cost = cost;
}
// b) a public String toString() method that returns a String representation of
// the Item.
public String toString() {
return "Name: " + name + "\n" + "Comment: " + comment + "\n" + "Cost: " + cost + "\n";
}
// c) a method void depreciate() that decreases the value of the item by 5%.
public void depreciate() {
this.cost -= cost * 0.05;
}
}
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
Item item = new Item("Item name", "Comments", 100);
System.out.println(item.toString());
item.depreciate();
System.out.println(item.toString());
}
}
Comments
Leave a comment