Question
Main topics: Menu driven programming Programmer defined methods, Arrays, Parallel Arrays Searching + Sorting Program Specification: You are to modify the program that you wrote
Main topics: Menu driven programming Programmer defined methods, Arrays, Parallel Arrays Searching + Sorting
Program Specification:
You are to modify the program that you wrote for the previous program. Here is the previous program:
import java.util.Scanner;
public class Program10
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String titles[] = new String [128];
int lengths [] = new int [128];
int numDVDs = 0;
String menuChoice;
do
{
menuChoice= menu(stdIn);
if(menuChoice.equalsIgnoreCase("a"))
{
numDVDs += addDVD(titles, lengths, numDVDs, stdIn);
}
else if(menuChoice.equalsIgnoreCase("t"))
{
searchByTitle(titles, lengths, numDVDs, stdIn);
}
else if(menuChoice.equalsIgnoreCase("l"))
{
searchByLength(titles, lengths, numDVDs, stdIn);
}
else
{
System.out.println("Good Bye!");
}
}while(!menuChoice.equalsIgnoreCase("q"));
stdIn.close();
}
public static String menu(Scanner stdIn)
{
String search;
System.out.println("***************************");
System.out.println("A Add a DVD *");
System.out.println("T Search by Title *");
System.out.println("L Search by Length *");
System.out.println("Q Quit *");
System.out.println("***************************");
do
{
System.out.print("Please enter an option : ");
search = stdIn.next();
System.out.println("***************************");
}while(!search.equalsIgnoreCase("a") && !search.equalsIgnoreCase("t") && !search.equalsIgnoreCase("l") && !search.equalsIgnoreCase("q"));
return search;
}
public static int addDVD(String[] titles, int[] lengths, int numDVDs, Scanner stdIn)
{
stdIn.nextLine();
System.out.print("Please enter DVD title : ");
titles[numDVDs] = stdIn.nextLine();
System.out.print("Please enter DVD length : ");
lengths[numDVDs] = stdIn.nextInt();
return 1;
}
public static void searchByTitle(String titles[], int lengths[], int numDVDs, Scanner stdIn)
{
stdIn.nextLine();
String temp;
System.out.print("Please enter DVD title (post * allowed) : ");
String tSStr = stdIn.nextLine();
System.out.println("Title, Length");
System.out.println("***************************");
if(tSStr.charAt(tSStr.length() - 1)!= '*')
{
for(int x = 0; x < numDVDs; ++ x )
if(tSStr.equals(titles[x]))
System.out.println(titles[x] + ", " + lengths[x]);
}
else
{
temp = tSStr.substring(0, tSStr.length() - 1);
for(int i = 0; i < numDVDs; ++i)
for(int n = 0; n <= (titles[i].length() - tSStr.length()); ++ n)
if(temp.equalsIgnoreCase(titles[i].substring(n, (tSStr.length() - 1 + n))))
{
System.out.println(titles[i] + ", " + lengths[i]);
n = titles[i].length();
}
}
}
public static void searchByLength(String[] titles, int[] lengths, int numDVDs, Scanner stdIn)
{
String lSStr;
int temp;
do
{
System.out.print("Please enter DVD length (pre < = > manditory) : ");
lSStr = stdIn.next();
}while(lSStr.charAt(0)!= '<' && lSStr.charAt(0)!= '=' && lSStr.charAt(0)!= '>');
System.out.println("Title, Length");
System.out.println("***************************");
temp = Integer.parseInt(lSStr.substring(1, lSStr.length() - 1));
for(int x = 0; x < numDVDs; ++x)
if(lSStr.charAt(0) == '>')
{
if(lengths[x] > temp)
System.out.println(titles[x] + ", " + lengths[x]);
}
else if(lSStr.charAt(0) == '<')
{
if (lengths[x] < temp)
System.out.println(titles[x] + ", " + lengths[x]);
}
else
{
if(lengths[x] < temp)
System.out.println(titles[x] + ", " + lengths[x]);
}
}
}
The specific modifications are outlined below:
Modifications:
Each piece of a DVD / Movies information must be stored in the two parallel arrays, such that logi- cally they are sorted (based on the title). This requires that your public static int addDVD(String[] titles, int[] lengths, int numDVDs, Scanner stdIn) method be modified.
All title searches must then be done using a Binary Search - since your data will at all times be sorted, via your new addDVD method. This requires that your public static void searchByTitle(String titles[], int lengths[], int numDVDs, Scanner stdIn) method be modified, but NOT your public static void searchByLength(String titles[], int lengths[], int numDVDs, Scanner stdIn) Since your data will NOT be sorted based on length, but instead titles.
Notes and Hints:
Be very careful when re-ordering the values in the title array that you keep the length array in sync (parallel).
The effective use of additional methods may help to simplify the understanding as well as implemen- tation of this assignment.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started