Create a computer object with the computer name

Assignment Help Data Structure & Algorithms
Reference no: EM132387908

DATA STRUCTURES AND ALGORITHMS

Question 1

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

public class EquilateralTriangle

{

private double base; // the base length of the triangle private double angle; // the angle of the triangle


// constructor without parameters public EquilateralTriangle ()

{

base = 2.0;

angle = 60;

}

// constructor with parameters

public EquilateralTriangle( double b, double a )

{

base = b; angle = a;

}

public double getBase() //method to get base length of the triangle

{

return base;

}

public double getArea() //method to calculate area

{

return (0.4 * base * base );

}

public boolean isTriangle() //method to check if it is an equilateral triangle

{

return (angle == 60);

}


public static void main( String[] args )

{

EquilateralTriangle myTriang1 = new EquilateralTriangle();

System.out.println("Base is: " + myTriang1.getBase());

System.out.println("Area is: " + myTriang1.getArea());

System.out.println("Is it a EquilateralTriangle? " + myTriang1.isTriangle());

EquilateralTriangle myTriang2 = new EquilateralTriangle (1, 60);

System.out.println("Base is: " + myTriang2.getBase());

System.out.println("Area is: " + myTriang2.getArea());

}

}

Question 2

The following code defines class Computer, with a constructor that specifies the Computer's name and cost, and a toString method that returns a String containing the Computer's name and cost.

public class Computer

{

private String computerName; private double computerCost;

public Computer(String name, double cost)

{

computerName = name; computerCost = cost;

}

public String toString()

{

return String.format( "%s cost is %.2f", computerName, computerCost );

}

}

Your tasks are to:

define two set methods to set the computer's name and cost.
define two get methods to retrieve the computer's name and cost.
create a Computer object with the computer name "Dell" and the cost 689, 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 hWg, double hWk )

{

super( first, last); wage = hWg; hours =hWk;

}

public double earnings()

{

System.out.println( "the method calculates earnings based on hours" ); if ( hours <= 50 )

return wage *hours; else

return 50 * wage + (hours - 50 ) * wage;

}

}
public class PayrollSystemTest

{

public static void main( String[] args )

{

Employee currentEmployee = new HourlyEmployee("Jim", "Son", 10, 51 ); System.out.println( "Calculate Employee Earnings \n" ); System.out.printf("Earnings $%,.2f\n\n", currentEmployee.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 MyTimetable extends JFrame

{

private JPanel panel; private JButton exitButton;

private JComboBox dataBox;


public MyTimetable()

{

super("My Timetable for Programming Courses");

setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

dataBox = new JComboBox(); dataBox.addItem("Programming Course COIT20245");

dataBox.addItem("Programming Course 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, "Mon 8-10am"); break; case 1: JOptionPane.showMessageDialog(null, "Fri 9-11am"); break;

}

}

}

private class ExitButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

public static void main(String[] args)

{

MyTimetable myFrame= new MyTimetable();

}

}

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;


class LinkedListTest

{

public static void main(String[] args)

{

LinkedList<String> myList = new LinkedList<String>(); ListIterator<String> iter;

myList.add("Apple"); System.out.println("List: "+myList); myList.addLast("Mango"); myList.addLast("Orange"); System.out.println("List: "+myList); iter = myList.listIterator(); iter.next(); iter.next();

iter.add("Grapes");

iter.add("Banana"); System.out.println("List: "+myList);

while (iter.hasNext()) System.out.println("List: "+ iter.next()); iter.add("Apricot");

System.out.println("List: "+myList);

}

}

Question 6

Assume that each of the following operators =, <, &&, [ ], ==, ++ counts as an operation. What will be the worst-case running time for the following code? Justify your answer (write maximum four lines).

int maxVal=3, searchKeyFound=1; for (int i=1; i<N+1; i++)

{

if ( (i<N+1) && (myValue[i] == maxVal) ) searchKeyFound = 0;


else


}


searchKeyFound = 1;

Question 7

Suppose there is a text file (movies.txt) with data format (movie name, download cost) as below:

Jurassic World 10

The Sting 10

The Godfather 5

The following program contains the partial code. Complete the program code so that it can read the data file (movies.txt) and display the following information on the screen.

movie name.
total number of movies.
all movie names with download cost equal to 5. import java.io.*;

import java.util.*; public class FileTest

{

public static void main(String[] args) throws java.io.IOException

{

int countMovies=0; try

{

File movies=new File("movies.txt "); Scanner input=new Scanner(movies); while (input.hasNext())

{

String name1 = input.next(); String name2 = input.next(); int cost = input.nextInt();


//Display the movie name and increment countMovies
/*Student to COMPLETE*/


//Display the movie names which have download cost equal to 5
/*Student to COMPLETE*/

}

//Print the total number of movies
/*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 = 100, num2 = 102;

ffer(89); q.offer(num1); q.offer(33);

oll(); System.out.println(q.peek()); q.poll(); System.out.println(q.poll()); q.offer(303);

q.offer(num2); System.out.println(q.peek());

while (!q.isEmpty()) System.out.print(q.poll() + " ");

Question 9

The method findMax() takes an integer array as an argument. It returns the maximum value stored in the array.

public static int findMax(int [] myNumbers)

{

int max= 0;

for (int i = 0; i < myNumbers.length; i++) {

if (myNumbers[i]>max) max= myNumbers[i];

}

return max;

}

Write a generic version of findMax() method that uses a generic type T array instead of an integer array, so that this method can work with an array of integer and double types. Write a demonstration program to verify this method with the following arrays:

Integer [] intArray={4, 10, 2, 15};

Double [] doubleArray={3.2, 2,4, 3.4, 4.6};

Question 10

What does the following program do (e.g. power, square root, greatest common divisor, factorial, etc.)? Justify your answer (write maximum 3 lines). What will be the output of this program?

public class Recursion

{

public static int guessProg( int num1, int num2 )

{

if ( num2 > 0 )

return guessProg(num1, num2-1)*num1; else

return 1;

}

public static void main(String[] args)

{

System.out.println(guessProg (2,3));

System.out.println(guessProg (4,2));

}

}

Question 11

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.

1899_DATA STRUCTURES AND ALGORITHMS.jpg


Attachment:- DATA STRUCTURES AND ALGORITHMS.rar

Reference no: EM132387908

Questions Cloud

Constant and decreasing returns to scale of a firm : Use equation, diagrams and economic theory to explain increasing, constant and decreasing returns to scale of a firm.
Determining a borrower creditworthiness : Throughout history, money-lenders and banks have established different criteria for determining a borrower's creditworthiness.
Describe the demand curve for a monopolist : Describe the demand curve for a monopolist. Why does the monopolist's demand curve look different than the demand curve of a perfectly competitive firm?
Improving the design of the perceptual map study : What recommendations do you have for improving the design of the perceptual map study (i.e. the products, attributes and segmentation questions)?
Create a computer object with the computer name : Create a Computer object with the computer name "Dell" and the cost 689, and display a String containing the value of the object's instance variables
What is the equilibrium price and quantity : Industry supply and demand are given by: QD = 1000 - 2P and QS = 3P a. What is the equilibrium price and quantity?
Estimate the price where sheetz would sell : Estimate the price where Sheetz would sell 12,000 gallons of gasoline
Equilibrium price and output for gasoline : Assuming a competitive market, what effect would this change have on the equilibrium price and output for gasoline?
Measure a country economic welfare : What factors does GDP ignore? And what other measures can we use to measure a country's economic welfare?

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  A function call being bound to different functions

Inheritance: Deriving one class from another class so that the new class inherits all the members of the original class.

  Will the user interface consist of one or more screens

Will the user interface consist of one or more screens? How will the user enter the status of the each maintenance task? How will that status be displayed?

  Create a flowchart to show the process

Create a flowchart to show the process that will allow the implementation of Stack, Push, and Pop operations.

  What data structures should be used to play game efficiently

A group of children want to play a game, called Unmonopoly, where in each turn player. What data structure(s) should be used to play this game efficiently? Why?

  Determine the activity precedence relationships

Two brothers have purchased a small lot, in the center of town, where they intend to build a gas station. The station will have two pumps, a service area.

  Write the search algorithm for a b-pluse tree

Write the search algorithm for a B+tree. Rewrite the B-tree deletion algorithm using a stack instead of recursion. Rewrite the B-tree insertion algorithm using a stack instead of recursion.

  Create a class whose main method creates three arrays

Create a class whose main method creates three arrays. The first array will contain five kinds of flowers - petunia, pansy, rose, violet, and carnation.

  Eliminate every other integer beginning with the integer

the Collections class which has an algorithm called rotate(List list, int distance) which can be used to rotate a list left or right. use to eliminate every other Integer beginning with the Integer in the second position. Remember that if you rem..

  Illustrate influence of virtual memory management

Design a program that illustrates the influence of virtual memory management on execution. Specifically, for a computer platform that uses VMM, determine the size of the active memory set and the access characteristics of the components involved i..

  What is the machine run time in second for sorting array

Write computer program to implement this algorithm and demonstrate the results and what is the machine run time in second for sorting array A?

  Write the implementation of a data structure

Write an implementation of a data structure S that supports the following operations: Insert(S, x): insert the key x into S only if it is not already there.

  Linear-time algorithm to find odd-length cycle in graph

Give a linear-time algorithm to find an odd-length cycle in a directed graph. You may not suppose that graph is strongly connected.

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