Provide the inorder - preorder and postorder traversals

Assignment Help Data Structure & Algorithms
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.)

1948_graphical user interface.jpg

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.

778_graphical user interface1.jpg

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

Reference no: EM132387918

Questions Cloud

Market price and quantity of sandals : An increase in both the market price and quantity of sandals following an increase in consumer incomes proves that demand curves do not always have a negative
Explain why ford and gm purchase tires : Explain why Ford and GM purchase tires for their automobiles from independent suppliers but produce their own engines.
Maximizing the revenue and profits : Is this policy consistent with maximizing the revenue and profits from these sites? Explain why.
Piece rates or a percentage of firm revenues : Why should you pay your administrative assistant an hourly wage instead of piece rates or a percentage of your firm's revenues/profits? Explain.
Provide the inorder - preorder and postorder traversals : 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
Explain how each of the three barriers to entry : Choose a firm that has monopoly power. Explain how each of the three barriers to entry would apply to that firm.
What are the two critical questions a firm must answer : (a) What are the two critical questions a firm must answer? (b) What is the main economic objective of every firm?
Find the consumers demand cuntions for goods : Find the consumers demand cuntions for goods 1 and 2. [Hint: use condition for consumer equilibrium to derive these.
Who would you have voted for and why : Who won the election? Did you feel that candidate had the better, more convincing commercials? Why or why not?

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Implement an open hash table

In this programming assignment you will implement an open hash table and compare the performance of four hash functions using various prime table sizes.

  Use a search tree to find the solution

Explain how will use a search tree to find the solution.

  How to access virtualised applications through unicore

How to access virtualised applications through UNICORE

  Recursive tree algorithms

Write a recursive function to determine if a binary tree is a binary search tree.

  Determine the mean salary as well as the number of salaries

Determine the mean salary as well as the number of salaries.

  Currency conversion development

Currency Conversion Development

  Cloud computing assignment

WSDL service that receives a request for a stock market quote and returns the quote

  Design a gui and implement tic tac toe game in java

Design a GUI and implement Tic Tac Toe game in java

  Recursive implementation of euclids algorithm

Write a recursive implementation of Euclid's algorithm for finding the greatest common divisor (GCD) of two integers

  Data structures for a single algorithm

Data structures for a single algorithm

  Write the selection sort algorithm

Write the selection sort algorithm

  Design of sample and hold amplifiers for 100 msps by using n

The report is divided into four main parts. The introduction about sample, hold amplifier and design, bootstrap switch design followed by simulation results.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd