What will be the worst-case running time

Assignment Help Data Structure & Algorithms
Reference no: EM132387942

Question 1
The following program compiles and runs. What is the output of this program?

public class Rectangle
{
private double length; private double width;

public Rectangle()
{
length = 1.5;
width = 2.5;
}
public Rectangle( double theLength, double theWidth )
{
length = theLength; width = theWidth;
}
public double getLength()
{
return length;
}
public double getArea()
{
return (length * width);
}
public boolean isSquare()
{
return (length == width);
}

public static void main( String[] args )
{
Rectangle myRec1 = new Rectangle(2.0, 3.0); System.out.println("The length is: " + myRec1.getLength()); System.out.println("The area is: " + myRec1.getArea()); System.out.println("Is it a square? " + myRec1.isSquare()); Rectangle myRec2 = new Rectangle(); System.out.println("The area is: " + myRec2.getArea()); System.out.println("Is it a square? " + myRec2.isSquare());
}
}

Question 2
The following code defines class Car, with a constructor that specifies the car's model (carModel) and price (carPrice), and a toString method that returns a String containing the car's model and price.
public class Car
{
private String carModel; private double carPrice;
public Car(String model, double price)
{
carModel = model; carPrice = price;
}
public String toString()
{
return String.format( "%s price %.2f", carModel, carPrice );
}
}

Your tasks are to:
• define two set methods to set the carModel and carPrice.
• define two get methods to retrieve the carModel and carPrice.
• create a Car object with the model name "Ford" and the price 20000.00, and display a String containing the value of the object's instance variables.

Write java code for the above mentioned tasks.

Question 3
Based on the following Java programs, what will be the output when the
PayrollSystemTest is executed?
public abstract class Employee
{
private String firstName; private String lastName;
public Employee( String first, String last)
{
firstName = first; lastName = last;
System.out.println(firstName +" "+lastName);
}
public abstract double earnings();
}

public class HourlyEmployee extends Employee
{
private double wage; private double hours;
public HourlyEmployee(String first, String last, double hWage, double hWrk)
{
super( first, last); wage = hWage; hours =hWrk;
}
public double earnings()
{
System.out.println( "Earnings method starts..." ); if ( hours <= 50 )
return wage *hours; else
return 50 * wage + (hours - 50 ) * wage;
}
}

public class PayrollSystemTest
{
public static void main( String[] args )
{
Employee cEmployee = new HourlyEmployee("Mik", "Ton", 10, 51 ); System.out.println( "Calculate Employee Earnings \n" ); System.out.printf("Employee earned $%,.2f\n\n", cEmployee.earnings() );
}
}

Question 4
What will be the output when the following program is executed? Show all GUI components including frames, combo boxes, buttons and message dialog boxes.

import javax.swing.*; import java.awt.*; import java.awt.event.*;

public class UnitTimetable extends JFrame
{
private JPanel panel; private JButton exitButton;
private JComboBox dataBox;

public UnitTimetable()
{
super("Timetable for programming units..."); setSize(500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); dataBox = new JComboBox();
dataBox.addItem("Unit COIT20245"); dataBox.addItem("Unit COIT20256"); exitButton = new JButton("EXIT");
dataBox.addActionListener(new DataComboBoxListener()); exitButton.addActionListener(new ExitButtonListener());
panel = new JPanel(); panel.add(dataBox); panel.add(exitButton); add(panel);
setVisible(true);
}
private class DataComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int index = dataBox.getSelectedIndex(); switch (index)
{
case 0: JOptionPane.showMessageDialog(null, "Thur 4-6pm"); break; case 1: JOptionPane.showMessageDialog(null, "Mon 1-3pm"); break;
}
}
}
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
UnitTimetable mFrame= new UnitTimetable();
}
}

Question 5

The following program is based on linked list operations. What will be the output when this program is executed?

import java.util.List; import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListTest
{
public static void main(String[] args)
{
LinkedList<String> myList = new LinkedList<String>(); ListIterator<String> iter;
myList.add("Brisbane"); myList.addLast("Rocky"); System.out.println("List: "+myList); myList.addLast("Sydney"); myList.addLast("Perth"); System.out.println("List: "+myList); iter = myList.listIterator(); iter.next(); iter.next();
iter.add("Mackay"); iter.add("Melbourne"); System.out.println("List: "+myList); iter.next();
while (iter.hasNext()) System.out.println("List: "+ iter.next()); iter.add("Canberra");
System.out.println("List: "+myList);
}
}

Question 6
(a) By using the definition of Big-O, show that the running time of term T(n)=112+3n+n2 is O(n2). Write your answer in no more than four lines.

(b) Assume that each of the following operators =, <, &&, [ ], ==, -- counts as an operation. What will be the worst-case running time in terms of total number of operations for the following code?
for (int i=N; i>0; i--) {
if ( (i>0) && (data[i] == 3) ) keyFound = 1;
}

Suppose there is a text file (pub.txt) with data format (publication name, publication type, ranking) as below:
Publication name Type Ranking
Pattern-recognition journal 1
Neurocomputing journal 2
International-conference-neural-networks conference 1

Question 7

The following program contains the partial code. Complete the program code so that it can read the data file (pub.txt) and display the following information on the screen.
• publications with ranking 2.
• total number of journal publications.
• total number of conference publications.

import java.io.*; import java.util.*; public class FileTest
{
public static void main(String[] args) throws java.io.IOException
{
int countJournal=0, countConference=0; try
{
File pubs=new File("pub.txt"); Scanner input=new Scanner(pubs); while (input.hasNext())
{
String pubName = input.next(); String pubType = input.next(); int pubRank = input.nextInt();

//Display publication (name, type, ranking) if ranking is 2
/*Student to COMPLETE*/
//Count journal publications and conference publications
/*Student to COMPLETE*/
}
//Display total number of journals and total number of conferences
/*Student to COMPLETE*/
//Close the file
/*Student to COMPLETE*/
}
catch (FileNotFoundException fileNotFoundException)
{
/*Student to COMPLETE*/
}
}
}

Question 8
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 num1 = 50, num2 = 100;
q.offer(num1); q.offer(num2); q.poll();
System.out.println(q.peek()); q.poll();
q.offer(120); System.out.println(q.poll()); q.offer(num1); q.offer(num2); q.offer(num1); System.out.println(q.peek());
while (!q.isEmpty()) System.out.print(q.poll() + " ");

Question 9
The following code is a recursive method. Show how you work out the result of call calcValue(6,1).

public static int calcValue(int num1, int num2)
{
if (num1 <= num2)
return 0;

else

}

return calcValue(num1-1, num2) + 1;

Question 10
What are the values of A and B in the tree shown below so that it is a binary search tree? List the values of A and B. Manually provide the inorder, preorder and postorder traversals of the search tree shown below.

1419_figure.jpg

Attachment:- Examination.rar

Reference no: EM132387942

Questions Cloud

What would you tell him about future revenue : If you were working in a gas station and the manager wants to increase the price of gasoline 5%,
Human development index : In 2015, the Human Development index (HDI) of South Africa was 0.668 (ranked 119 in the world) and that of Tonga was 0.776(ranked 101 in the world).
What is statistics and parameter : What is statistics and parameter for this example?
Propose a plan for how a company : Propose a plan for how a company can use 3D printing to increase sales and customer satisfaction.
What will be the worst-case running time : Show all GUI components including frames, combo boxes, buttons and message dialog boxes - What will be the output when this program is executed
Define negative rights and positive rights : Define negative rights and positive rights. Give an example of negative right and positive right that are in conflict.
Select a country other than the us : Select a country other than the US and comment on its rate. How has it changed over time? What is it now?
Current accounts of all countries in the world : Which of the following quantities should be equal zero always? Please circle or otherwise indicate all that apply out of (1), (2) and (3) below:
Months of equipment operation and parts replacement : Assume parts were new at the beginning of year one and base your analysis on 24 months of equipment operation and parts replacement.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Draw a sequence of trees like those in the text

Draw a sequence of trees like those in the text to illustrate the actions of split to and qui court () while sorting the given list.

  Write an algorithm that displays the squares of the number

Using a FOR loop,I need to write an algorithm that displays the squares of the number 1 to 10to console out put

  Show that arc consistency can be enforced in total time

Explain how to update these numbers efficiently and hence show that arc consistency can be enforced in total time O(n2d2).

  Implement a prototype that uses a linked list

Develop a prototype solution to test the performance of different data structures and algorithms - develop some software to manage student enrolments. You decide to develop a prototype solution to test the performance of different data structures a..

  What are entity-relationship diagrams

What are entity-relationship diagrams, and how are they used? Discuss the ethical issues to consider when planning a database.

  Give a recursive definition of a singly linked list

Give a C++ code fragment that, given an×n matrix M of type float, replaces M with its transpose. Try to do this without the use of a temporary matrix.

  Provide polynomial-time algorithm to decide in graph

Provide a polynomial-time algorithm to decide whether G has unique minimum s - t cut (i.e., an s - t cut of capacity strictly less than that of all other s - t cuts).

  How it would execute on a computer

We are going to trace the following program, i.e. simulate in your head how it would execute on a computer. To help you, a trace table is provided for you to fill. Unlike exam E1, our focus here is not only on keeping track of the values of each v..

  What strategy have you selected

Describe in pseudo-code a two potential parallel implementations for this algorithm: What strategy have you selected? What other options you could use?

  You and your eight-year-old nephew elmo decide to play a

you and your eight-year-old nephew elmo decide to play a simple card game. atthe beginning of the game the cards are

  Explain why hashing is not used more often

Discuss the advantages of hashing. Explain why hashing is not used more often, given these advantages. Post your response and respond to the posts below.

  Discuss the charecteristics of good algorithm

Discuss the charecteristics of good algorithm

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