You need to submit the solution to this UML (in Java). The driver must demonstrate, from all classes, all the methods at least once and the driver must create at least 100,000 items and display the time it took to create them. You are required to create the structured logic for each methods and your code must be properly documented using Java docstrings.
> Retailtem +getRetailPrice():double Movie -title:String -runningTime:int -retailPrice:double +Movie(dvdTitle:String, I r runTime:int dvdPrice:double +getRunnigTime():int +getRetailPrice():double Disc title:String artist:String retailPrice:double +Disc(cdTitle:String, RetailltemDemno cdArtist:String cdPrice:double) +main(String args[):void showPrice(item Retailltem):void +getTitle(O:String +getArtist():String +getRetailPrice():double
Expert Answer
Given below is the code for the question along with output. Please do rate the answer if it helped. Thank you.
RetailItem.java
public interface RetailItem {
/**
* Returns the retail price of this RetailItem object
* @return
*/
public double getRetailPrice();
}
Disc.java
/**
* The Disc represents any album by an artist and implements the RetailItem interface.
*
*/
public class Disc implements RetailItem {
private String title; //title of this disc
private String artist; //the artist of the album
private double retailPrice; //the retail price of the disc
/**
* Constructor to create the Disc object with the specified parameters
* @param title – the title of the album
* @param artist – the artist of the album
* @param retailPrice – the retail price of the disc
*/
public Disc(String title, String artist, double retailPrice)
{
this.title = title;
this.artist = artist;
this.retailPrice = retailPrice;
}
/**
* Returns the title of the disc
* @return the disc’s title
*/
public String getTitle()
{
return title;
}
/**
* Returns the artist of the album on disc
* @return – the name of the artist
*/
public String getArtist()
{
return artist;
}
@Override
public double getRetailPrice() {
return retailPrice;
}
}
Movie.java
/**
*
* A Movie class represent a movie on a DVD. The running time and price are stored
* along with the movie title. The running time is in mins.
* The Movie implements RetailItem interface
*
*/
public class Movie implements RetailItem {
private String title; //the title of the movie
private int runningTime; //the running time of movie in mins
private double retailPrice; //the retail price of the DVD
/**
* Constructor to initialize the Movie object
* @param dvdTitle – The title of the movie
* @param runTime – The running time of the movie in mins
* @param dvdPrice – The price of the DVD
*/
public Movie(String dvdTitle, int runTime, double dvdPrice)
{
this.title = dvdTitle;
this.runningTime = runTime;
this.retailPrice = dvdPrice;
}
/**
* Returns the title of the movie
* @return – movie title
*/
public String getTitle()
{
return title;
}
/**
* Returns the running time for the movie on DVD
* @return – running time in mins
*/
public int getRunningTime()
{
return runningTime;
}
@Override
public double getRetailPrice() {
return retailPrice;
}
}
RetailItemDemo.java
import java.util.Random;
public class RetailItemDemo {
/**
* Method to show the price of a RetailItem
* @param item – the retailItem object whose price should be displayed
*/
private static void showPrice(RetailItem item)
{
System.out.println(“Retail Price: $” + item.getRetailPrice());
}
public static void main(String[] args) {
Movie movie1 = new Movie(“Real Steel”, 127, 2.99);
Disc disc1 = new Disc(“Thriller”, “Michael Jackson”, 100);
System.out.println(“Displaying movie details-“);
System.out.println(“Movie Title: ” + movie1.getTitle());
showPrice(movie1);//since Movie implements RetailItem, we pass Movie object
System.out.println(“nDisplaying disc details-“);
System.out.println(“Disc Title: ” + disc1.getTitle());
System.out.println(“Artist: ” + disc1.getArtist());
showPrice(disc1);//since Disc implements RetailItem, we pass Disc object
//create 100,000 objects and time it
RetailItem[] items = new RetailItem[100000]; //RetailItem can store both Movie and Disc objects
Random random = new Random();
long startTime = System.currentTimeMillis();
//create 50000 movie objects
for(int i = 0; i < 50000; i++)
{
//create movies with running time between 60 – 120 mins and price 1.0 – 2.0
items[i] = new Movie(“Movie ” + i , 60 + random.nextInt(60), 1 + random.nextDouble());
}
//create 50000 movie objects
for(int i = 50000; i < 100000; i++)
{
//create discs with Artists0 – Artist9, and price between 1.0 – 2.0
items[i] = new Disc(“Disc ” + i , “Artist ” + random.nextInt(10), 1 + random.nextDouble());
}
long endTime = System.currentTimeMillis();
long elapsedTime = (endTime – startTime) ; //calculate elapsed time in milli secs
System.out.println(“nIt took ” + elapsedTime + ” milli secs to create 100,000 objects”);
}
}
output
Displaying movie details-
Movie Title: Real Steel
Retail Price: $2.99
Displaying disc details-
Disc Title: Thriller
Artist: Michael Jackson
Retail Price: $100.0
It took 84 milli secs to create 100,000 objects