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();
}
}
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.
Explain this java code, please. import java.util.Scanner; public class Program11 { 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) { 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) { 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...
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 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...
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...
10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...
import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables int creditScore; double loanAmount,interestRate,interestAmount; final double I_a = 5.56,I_b = 6.38,I_c = 7.12,I_d = 9.34,I_e = 12.45,I_f = 0; String instructions = "This program calculates annual interest\n"+"based on a credit score.\n\n"; String output; Scanner input = new Scanner(System.in);// for receiving input from keyboard // get input from user System.out.println(instructions ); System.out.println("Enter the loan amount: $"); loanAmount = input.nextDouble(); System.out.println("Enter the credit score: "); creditScore...
import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...
public static void main(String[] args) { System.out.println("Welcome to the Future Value Calculator\n"); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // get the input from the user System.out.println("DATA ENTRY"); double monthlyInvestment = getDoubleWithinRange(sc, "Enter monthly investment: ", 0, 1000); double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 30); int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); System.out.println(); ...