Work on javabat Arrays2 more14 copy and paste the green check marks to submit an
ID: 3823764 • Letter: W
Question
Work on javabat Arrays2 more14 copy and paste the green check marks to submit
and write out the code for the two examples on the sheet.
1. For a listClass storing an array of ints, write the member method search and fill in the missing lines in main
public class listClass
{
private int [ ] list;
private int count;
public int search ( int key)
{
}
class listDemo
{public static void main(String [] args ) throws IOException
{ int number, position;
listClass numberList = ____________________// instantiate the numberList;
System.out.println( “For what number are you searching?”);
Scanner keyboard= new Scanner (System.in);
_____________________________________
_____________________________________
subscript = numberList. search ( _______________);
if ( subscript >= 0 )
System.out.println( _______ + “was found at position “ + ___________);
else
________________________________________
}
Fill in the missing lines
2. An amateur pianist, in training to play Chopin’s “Minute Waltz” in less than 60 seconds practices the piece between 25 and 40 times at a sitting. The following code is a method that reads in first the number of times practicing, and a list of the times in seconds it takes the pianist to play the piece and prints out as output the time for the last 10 performances of the waltz followed by the average time for these 10 performances.
public static void main()
{
int [ ] practice = new int[40];
int times;
int sum;
double average;
System.out.println( “How many times did you practice?”);
Scanner keyboard = new Scanner ( System.in));
__________________
__________________
for (__________________________
{
System.out.println( “Enter seconds for try “ + (i + 1) );
____________________________= keyboard.nextInt();
}
// averaging the last 10 tries
sum = 0;
for (
____________________________________
______________________________
System.out.println ( “The average for the last 10 tries is “ + ____________);
}
Explanation / Answer
public class ListClass
{
private int [ ] list;
private int count;
public int search ( int key)
{
for(int i = 0; i < list.length; i++)
if(list[i] == key)
return i;
return -1;
}
class listDemo
{public static void main(String [] args ) throws IOException
{ int number, subscript;
listClass numberList = new listClass; // instantiate the numberList;
System.out.println( "For what number are you searching?");
Scanner keyboard= new Scanner (System.in);
number = keyboard.nextInt();
_____________________________________
subscript = numberList. search ( number);
if ( subscript >= 0 )
System.out.println( number + " was found at position " + subscript);
else
System.out.println(number + " was not found in the list.");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.