Answer to Question #265637 in Java | JSP | JSF for nothando

Question #265637

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%.


1
Expert's answer
2021-11-13T12:04:04-0500


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());


	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS