Reference no: EM132387918
Question 1
The following codes define a class named Music, with a constructor that specifies default initial values of the instance variables - the title of music and the number being played, and a toString()method that returns a string containing the value of instance variables.
public class Music
{
private String title; private int numPlay;
public Music( )
{
title = ""; numPlay = 0;
}
public boolean isPopular()
{ //................
}
public String toString()
{
return String.format( "%s is played %d",
title, numPlay );
}
} //end class definition
Your tasks are:
• to define a parametrized constructor for the class Music.
• to define two set methods to set the title and numPlay of the Music.
• to define two get methods to retrieve the title and numPlay of the Music.
• to define the isPopular( ) method. If the numPlay is greater than 1000, the method returns true otherwise returns false.
• to create a Music object with the title "Radetzky March" and the numPlay 5000, and display a string containing the value of the object's instance variables.
Question 2
Based on the following Horse class declaration, define a subclass called
RaceHorse. The RaceHorse class will have additionally:
? A private data member to hold the number of races in which the horse has competed and methods to get and set the new field.
? A constructor that uses the super class constructor.
? A toString method that can be used to display the value of the private data member.
Then write a code segment to demonstrate creating objects of each class.
// Horse class- a super class public class Horse
{
private String name; // Horse name private int yearBirth; // birth year
//Constructor
public Horse(String n, int y)
{
name = n; yearBirth = y;
}
//mutator method
public void setName(String n)
{ name = n;
}
public void setYearBirth(int y)
{ yearBirth = y;
}
//accessor method public String getName()
{ return name;
}
public int getYearBirth()
{ return yearBirth;
}
// toString method public String toString()
{ return "Name: " + name + "\nbirth year: " + yearBirth;
}
} // end of definition of Horse class
Question 3
Briefly describe how you would read lines of text from a text file. Specify the class(es) you would use to accomplish this task. Suppose there is a text file - StudentRecord.txt with data format as below:
s0112929 John Citizen 77.5 s0987621 Peter London 66 s0234412 David Pauls 83.5
..........................................
The following program contains the partial code that reads this data file and displays each line in a console window. This program also displays the number of lines of this text file. Complete the program code for the required programming task.
import java.io.*; import java.util.*;
//definition of read file test class public class ReadFileTest
{
public static void main(String[] args) throws
java.io.IOException
{
int count=0;
FileReader reader=new FileReader("StudentRecord.txt");
Scanner fileIn=new Scanner(reader); while(fileIn.hasNextLine())
{
//write code for required task
}
// write code for required task
}
}
Question 4
The figure as shown below is a graphical user interface (GUI) that is related to a java application on general knowledge quiz. List all the javax.swing components in this GUI. Explain how the layout manager might be used for building this GUI. (Note: The javax.swing components must be written in correct case.)
Question 5
The following program is based on linked list operations. What will be the output when this program is executed?
import java.util.LinkedList; import java.util.ListIterator; import java.util.Collections;
public class LinkedListTest {
public static void main(String[] args)
{
LinkedList<String> list = new LinkedList<String>(); ListIterator<String> iter;
String[] countryStr={"Canada","Italy","Germany","Japan",
"USA","France","Britain"};
for(int i=0;i<countryStr.length;i++) list.addLast(countryStr[i]);
System.out.println("The List: "+list);
list.removeFirst(); System.out.println("The List: "+list); list.set(0,"Australia"); list.addLast("Spain");
iter = list.listIterator(); System.out.print("The List contains: "); while (iter.hasNext())
System.out.print(" "+ iter.next()); System.out.println("\n The List: "+list);
Collections.reverse(list); System.out.println("The List: "+list);
}
}
Question 6
Consider the following Java program:
import java.util.*;
public class UseStack
{
public static void main(String[] args)
{
int n=5;
Stack<Integer> s=new Stack<Integer>(); while(n>0)
{
s.push(n);
n--;
}
int result=1; while(!s.isEmpty())
{
int integer=s.pop(); result=result*integer;
}
System.out.println("result= "+result);
}
}
What value is displayed when this program executes? What mathematical function does this program evaluate? Write your answer in no more than three or four lines.
Question 7
What is the output from the following sequence of queue operations? Assume that the PriorityQueue class here is a class that has implemented standard Queue operations offer, peek and poll.
PriorityQueue<Integer> q = new PriorityQueue<Integer>(); int n1=99, n2=150;
q.offer(80);
q.offer(n1);
q.offer(30);
q.poll(); System.out.println(q.peek()); q.poll(); System.out.println(q.poll()); q.offer(200);
ffer(n2);
System.out.println(q.peek()); while(!q.isEmpty())
System.out.println(q.poll()+ " ");
Question 8
(a) The Big-O estimates for the running times of the three sorting algorithms are as following:
Algorithm 1: O(n2)
Algorithm 2: O(n log2n)
Algorithm 3: O(2n)
Rank the algorithms in order of performance. Explain your answer.
(b) In terms of Big-O notation, explain why the Merge sort algorithm is more efficient than the Insertion sort algorithm?
Question 9
The method rotateLeft() takes an integer array as an argument. It moves each element to the left by one position beginning at the second element and moves the first element to the last, in effect rotating the elements.
rotateLeft():
public static void rotateLeft(int[] arr)
{
int i, n = arr.length; int firstElem = arr[0]; for (i = 0; i < n-1; i++)
arr[i] = arr[i+1]; arr[n-1] = firstElem;
}
Write a generic version of rotateLeft() that uses a generic type T array instead of an integer array. The method then applies to an array of any object type. Write a test program to test this method with the following arrays:
String[ ] strArr = {"red", "green", "black", "blue", "yellow"};
Character [ ] cArr={‘a','b','c','d','e'};
Question 10
The following diagram represents a binary search tree (BST), in which each node stores a name. Your task is to manually provide the inorder, preorder and postorder traversals of this tree. In addition, if the names Kevin and William are required to be inserted into two nodes on the tree so that the tree is still a binary search tree, where these two names should be placed? Draw your diagram.
Question 11
What does the following program do? Justify your answer by showing the calls of the recursive method.
public class RecursiveTest
{
public static int f( int[] array, int size )
{
if ( size == 1 ) return array[ 0 ];
else
return array[ size - 1 ] + f( array, size - 1 );
} // end method f
public static void main( String[] args )
{
int[] a = { 10, 20, 30, 40, 50};
int result = f( a, a.length ); System.out.printf( "Result is: %d\n", result );
} // end method main
} // end class RecursiveTest
Attachment:- EXAMINATION.rar