1. Create a class definition that meets these requirements. Class name of Tools.
ID: 3578274 • Letter: 1
Question
1. Create a class definition that meets these requirements. Class name of Tools. Has data members for the quantity (int), price (double) and name (string). Accessors and Mutators for the data members. A default constructor that has 3 inputs for the quantity, price and name. There should be NO code.
2. Create an array of 3 Tools (from previous question) objects. It should use the consturctor three differnet ways, one as a default, then two other ways. It should all be done when declaring the array variable.
Explanation / Answer
1)
public class Tools {
int quantity;
double price;
String name;
public Tools() {
super();
}
public Tools(int quantity, double price, String name) {
this.quantity = quantity;
this.price = price;
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Tools [quantity=" + quantity + ", price=" + price + ", name=" + name + "]";
}
}
2)
public class TestTools {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Tools t1 = new Tools();
// Tools t2 = new Tools(2, 400, "Hammer");
// Tools t3 = new Tools(3, 500,"");
Tools t1[] = { new Tools(1, 400, "Hammer"), new Tools(2, 500, "Axe"),
new Tools() };
for(Object result : t1){
System.out.println(result);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.