Question

Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

Explain this java code, please.

import java.util.Scanner;

public class Program11
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);

final int maxSize = 128;
String[] titles = new String[maxSize];
int[] lengths = new int[maxSize];
int numDVDs = 0;
String op;

op = menu(stdIn);
System.out.println();
while (!op.equalsIgnoreCase("q"))
{
if (op.equalsIgnoreCase("a"))
{
if (numDVDs < maxSize)
numDVDs = addDVD(titles, lengths, numDVDs, stdIn);
}
else if (op.equalsIgnoreCase("t"))
searchByTitle(titles, lengths, numDVDs, stdIn);   
else if (op.equalsIgnoreCase("l"))
searchByLength(titles, lengths, numDVDs, stdIn);

System.out.println('\n');
op = menu(stdIn);
System.out.println();
}
}


public static String menu(Scanner stdIn)
{
String op;

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 : ");
op = stdIn.next();
} while (!(op.equalsIgnoreCase("a") || op.equalsIgnoreCase("t") || op.equalsIgnoreCase("l")
|| op.equalsIgnoreCase("q")));
System.out.println("***************************");

return op;
}

public static int addDVD(String[] titles, int[] lengths, int numDVDs, Scanner stdIn)
{
String title;
int length;

System.out.print("Please enter DVD title : ");
// last input used .next()
title = stdIn.nextLine();
title = stdIn.nextLine();
System.out.print("Please enter DVD length : ");
length = stdIn.nextInt();

// add in-order this time

// find index to add new item
int addLoc = numDVDs;
for (int i = 0; i < numDVDs; ++i)
if (titles[i].compareTo(title) >= 0)
{
addLoc = i;
i = numDVDs;
}

// make room for new item
for (int i = numDVDs; i > addLoc; --i)
{
titles[i] = titles[i-1];
lengths[i] = lengths[i-1];
}

// add new item
titles[addLoc] = title;
lengths[addLoc] = length;

return numDVDs + 1;
}

public static int bSearch(String[] titles, int numDVDs, String title, boolean trim)
{
int start = 0;
int end = numDVDs - 1;
while ( start <= end )
{
int mid = (start + end) / 2;

// exact match or trimmed match
String thisTitle = titles[mid];
if (trim && thisTitle.length() >= title.length())
thisTitle = thisTitle.substring(0, title.length());
//
  
if (thisTitle.compareTo(title) < 0)
start = mid + 1;
else if (thisTitle.compareTo(title) > 0)
end = mid - 1;
else
return mid;
}

return -1;
}
  
public static boolean startsWith(String str, String prefix)
{
if (str.length() < prefix.length())
return false;
else
return prefix.equals(str.substring(0, prefix.length()));
}

public static void searchByTitle(String[] titles, int[] lengths, int numDVDs, Scanner stdIn)
{
String title;

System.out.print("Please enter DVD title (post * allowed) : ");
// last input used .next()
title = stdIn.nextLine();
title = stdIn.nextLine();

System.out.println();
System.out.println("Title\t\tLength");
System.out.println("-----------------------------");
  
// wild card search
int tl = title.length();
if (tl > 0 && title.charAt(tl-1) == '*')
{
String tss = title.substring(0, tl-1);
// try to find an index where there is a match
int aLoc = bSearch(titles, numDVDs, tss, true);
// if at least one
if (aLoc != -1)
{
// probe at and left
int d = aLoc;
while (d >= 0 && startsWith(titles[d], tss))
{
System.out.println(titles[d] + "\t" + lengths[d] + "min");
--d;
}
// probe right
d = aLoc + 1;
while (d < numDVDs && startsWith(titles[d], tss))
{
System.out.println(titles[d] + "\t" + lengths[d] + "min");
++d;
}
}
}
// standard search
else
{
int loc = bSearch(titles, numDVDs, title, false);
if (loc != -1)
System.out.println(titles[loc] + "\t" + lengths[loc] + "min");
}
System.out.println();
}

public static void searchByLength(String[] titles, int[] lengths, int numDVDs, Scanner stdIn)
{
String sLength;
char lengthMod;
int length;

System.out.print("Please enter DVD length (pre < = > manditory) : ");
sLength = stdIn.next();
lengthMod = sLength.charAt(0);

System.out.println();
System.out.println("Title\t\tLength");
System.out.println("---------------------------------------");
if (lengthMod == '<')
{
length = Integer.parseInt(sLength.substring(1, sLength.length()));
for (int d = 0; d < numDVDs; ++d)
if (lengths[d] < length)
System.out.println(titles[d] + "\t" + lengths[d] + "min");
}
else if (lengthMod == '=')
{
length = Integer.parseInt(sLength.substring(1, sLength.length()));
for (int d = 0; d < numDVDs; ++d)
if (lengths[d] == length)
System.out.println(titles[d] + "\t" + lengths[d] + "min");
}
else if (lengthMod == '>')
{
length = Integer.parseInt(sLength.substring(1, sLength.length()));
for (int d = 0; d < numDVDs; ++d)
if (lengths[d] > length)
System.out.println(titles[d] + "\t" + lengths[d] + "min");
}
else
System.out.println("Format Error!");

System.out.println();
}
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

  • import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); }    if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...

  • import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new...

    import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc...

    import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");       int num = sc.nextInt();       boolean[] bool = new boolean[num];            for (int i = 0; i< bool.length; i++) {          bool[i] = true;       }       for (int i = 2; i< Math.sqrt(num); i++) {          if(bool[i] == true) {             for(int j = (i*i); j<num; j = j+i) {                bool[j] = false;...

  • please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){...

    please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double radius; double length; System.out.println("Enter radius:"); double r = scan.nextDouble(); System.out.println("Enter length:"); double l = scan.nextDouble(); System.out.println("Enter sides:"); int s = scan.nextInt(); Circle c = Circle(radius); p = RegularPolygon(l , s); System.out.println(c); System.out.println(p); } } Instructions Debug the code provided in the starter file so it does the following: • creates two Double objects named radius and length creates...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT