Use the following file: ProductRunner.java Complete the following file: Product.
ID: 3774142 • Letter: U
Question
Use the following file:
ProductRunner.java
Complete the following file:
Product.java
/**
* Models a Product that can increase and decrease in price
*/
public class Product
{
private double price;
private String name;
/**
* Constructs a Product with a price and a description
* @param thePrice the price of this Product
* @param name - the name of this product
*/
public Product( String name,double price)
{
this.price = price;
this.name = name;
}
/**
* Gets the price
* @return the price of this Ptoduct object
*/
public double getPrice()
{
return price;
}
/**
* Gets the name
* @return the name of the Product object
*/
public String getName()
{
return name;
}
/**
* 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;
}
}
Modify the Product class we wrote together to implement the Comparable interface. Products are ordered alphabetically by name. If two products have the same name, the Products are ordered by price from smallest to largest.. If this Product should come before the other Product, compareTo returns -1. If this Product should come after the other Product return -1. Otherwise returns 0.Explanation / Answer
ProductRunner.java
import java.util.Arrays;
/**
* Write a description of class ProductTester here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ProductRunner
{
public static void main(String[] args)
{
Product[] things =
{
new Product("iPad", 766.65 ),
new Product("Apple Watch", 315.55 ),
new Product("Apple Watch", 290.92 ),
new Product("Macbook Air", 795.00 ),
new Product("Macbook Air", 1199.00 ),
new Product("Macbook Air", 949.00 ),
new Product("Macbook Air", 579.99 )
};
Arrays.sort(things);
for (Product p : things)
{
System.out.println(p.getName() + " " + p.getPrice());
}
}
}
Product.java
public class Product implements Comparable<Product>
{
private double price;
private String name;
/**
* Constructs a Product with a price and a description
* @param thePrice the price of this Product
* @param name - the name of this product
*/
public Product( String name,double price)
{
this.price = price;
this.name = name;
}
/**
* Gets the price
* @return the price of this Ptoduct object
*/
public double getPrice()
{
return price;
}
/**
* Gets the name
* @return the name of the Product object
*/
public String getName()
{
return name;
}
/**
* 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;
}
public int compareTo(Product p)
{
if(getName().compareTo(p.getName()) == 0){
return (int)getPrice()-(int)p.getPrice();
}
else{
return getName().compareTo(p.getName());
}
}
}
Output:
Apple Watch 290.92
Apple Watch 315.55
Macbook Air 579.99
Macbook Air 795.0
Macbook Air 949.0
Macbook Air 1199.0
iPad 766.65
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.