You will be creating a driver class for this assignment, in addition to a collec
ID: 3711476 • Letter: Y
Question
You will be creating a driver class for this assignment, in addition to a collection class (DVDList.java) and an object class (DVD.java). The object (DVD) only has a title as instance data.
Within the main method, please complete the following tasks:
Print a menu of options to the screen that complete different tasks related to management of the DVDList. The tasks the program must be able to complete are:
Add to back
Add to front
Remove the last node
Remove the first node
Print the list
Print the number of nodes in the list
Quit
Remember that many of the methods need to first determine if the list is empty.
Include clear comments throughout the program that explain the well named variables and what functionality the various lines/small sections of code are completing as related to the execution of the program.
Explanation / Answer
//DVD.java
public class DVD {
private String title;
/**
* Takes title as parameter and created a new DVD object
* @param title
*/
public DVD(String title) {
this.title = title;
}
/**
* Takes the title as parameter and sets to the instance variable
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return the title of the DVD
*/
public String getTitle() {
return title;
}
}
//DVDList.java
import java.util.ArrayList;
public class DVDList {
// ArrayList of DVD which holds the list of DVDs at runtime
private ArrayList<DVD> dvdList;
/**
* Instantiates a DVDList class
*/
public DVDList() {
dvdList = new ArrayList<>();
}
/**
* Takes a DVD parameter and adds it to the back of the list
* @param dvd
*/
public void addToBack(DVD dvd) {
dvdList.add(dvd);
}
/**
* Takes a DVD parameter and adds it to the front of the list
* @param dvd
*/
public void addToFront(DVD dvd) {
if (dvdList.isEmpty()) {
dvdList.add(dvd);
} else {
dvdList.add(0, dvd);
}
}
/**
* Removes the last item from the list, if the list is not empty
*/
public void removeLastNode() {
if (dvdList.isEmpty()) {
return;
}
dvdList.remove(dvdList.size() - 1);
}
/**
* Removes the first item from the list, if the list is not empty
*/
public void removeFirstNode() {
if (dvdList.isEmpty()) {
return;
}
dvdList.remove(0);
}
/**
* Prints the titles of the DVDs in the list, if the list is not empty
*/
public void printList() {
if(dvdList.isEmpty()){
System.out.println("The list is empty.");
return;
}
System.out.println("DVD List:");
dvdList.stream().forEach((dvd) -> {
System.out.println(dvd.getTitle());
});
}
/**
* Prints the number of nodes in the list
*/
public void printNumberOfNodes() {
System.out.println("Total no of DVDs in the list: " + dvdList.size());
}
}
//DVDListDriver.java
import java.util.Scanner;
public class DVDListDriver {
/**
* This method ask the user to enter a title for the DVD
* and returns a new DVD for that title
* @param sc
* @return
*/
public static DVD getDVD(Scanner sc) {
String title;
System.out.println("Enter name of the title");
title = sc.next();
return new DVD(title);
}
public static void main(String[] args) {
DVDList dvdDist = new DVDList();
Scanner sc = new Scanner(System.in);
int input = 0;
while (true) {
System.out.println("1. Add to back "
+ "2. Add to front "
+ "3. Remove the last node "
+ "4. Remove the first node "
+ "5. Print the list "
+ "6. Print the number of nodes in the list "
+ "7. Quit ");
input = sc.nextInt();
switch (input) {
case 1:
dvdDist.addToBack(getDVD(sc));
break;
case 2:
dvdDist.addToFront(getDVD(sc));
break;
case 3:
dvdDist.removeLastNode();
break;
case 4:
dvdDist.removeFirstNode();
break;
case 5:
dvdDist.printList();
break;
case 6:
dvdDist.printNumberOfNodes();
break;
case 7:
System.exit(0);
default:
System.out.println("Invalid input. Please try again.. ");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.