In ToyTester add 1. Create a Product array that will hold 2 items 2. Assign the
ID: 3810720 • Letter: I
Question
In ToyTester add
1. Create a Product array that will hold 2 items
2. Assign the watch at index 0 and the rocket at index 1.
3. Will it compile? Why or why not?
4. Use an enhanced for loop to print the description of each item
Run it
5. What do you notice?
6. Which method is executed is determined at run time - not compile time
import java.util.Arrays;
/**
* Write a description of class ProductTester here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ToyTester
{
public static void main(String[] args)
{
Toy rocket = new Toy("Duplo Rocketship", 19.95, 2);
System.out.println(rocket.getDescription());
}
}
/**
* Describes a toy
*/
public class Toy extends Product
{
private int appropriateAge;
public Toy(String description, double price, int age)
{
super(description, price);
appropriateAge = age;
}
public int getAppropriateAge()
{
return appropriateAge;
}
@Override
public String getDescription()
{
return super.getDescription() + " " + appropriateAge;
}
}
/**
* Models a Product that can increase and decrease in price
*/
public class Product
{
private double price;
private String description;
/**
* Constructs a Product with a price and a description
* @param thePrice the price of this Product
* @param theDescription - the description of this product
*/
public Product(String theDescription, double thePrice)
{
price = thePrice;
description = theDescription;
}
/**
* Gets the price
* @return the price of this Product object
*/
public double getPrice()
{
return price;
}
/**
* Gets the description
* @return the description of the Product object
*/
public String getDescription()
{
return description;
}
/**
* Reduces the price of this product by the give percentage
* @param percent the percentage to reduce the priice by
*/
public void reducePrice(double percent)
{
double reduction = price * percent / 100;
price = price - reduction;
}
/**
* Increases the price by the given percent
* @param percent the percent to increase the price by
*/
public void increasePrice(double percent)
{
double increase = price * percent / 100;
price = price + increase;
}
}
Explanation / Answer
1.code below
2.code below
3.wil compile we are creating the object for a class product.its doesn't have any issues.
4.code below
5.the job runs and the ingerientance and we use objects array of class too .
class ToyTester
{
public static void main(String[] args)
{
Toy rocket = new Toy("Duplo Rocketship", 19.95, 2);
Product watch=new Product("Apple watch",300.00);
//System.out.println(rocket.getDescription());
Product[] productArray = new Product[2];//que1
productArray[0]=rocket;//que2
// System.out.println( "HI"+productArray[0].getDescription());
productArray[1]=watch;
// System.out.println("HeLOO"+ productArray[1].getDescription());
for(int i=0;i<2;i++){//forloop
System.out.println(productArray[i].getDescription());
}
}
}
during compile time toytester class is and ryuntime the product and toy
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.