Data Structures And Algorithms
TASK1 The following program compiles and runs. What is the output of this program? public class MyRectangle { private int theLength; private int theWidth; public MyRectangle() //default constructor { theLength = 0; theWidth = 0; } public MyRectangle(int length, int width) // constructor { theLength = length; theWidth = width; } public int getArea() //method to calculate area { return (theLength * theWidth); } public boolean isSquare() //method to check if it is a square { return (theLength == theWidth); } public static void main(String[] args) // main method { MyRectangle m1 = new MyRectangle(4,4); System.out.println("The area = "+ m1.getArea()); System.out.println("Is it a square? " + m1.isSquare()); MyRectangle m2 = new MyRectangle(20,35); System.out.println("The area = "+ m2.getArea()); System.out.println("Is it a square? " + m2.isSquare()); } } TASK2 The following code define class Product, with a constructor that specifies the product’s name and price, and a toString method that returns a String containing the product’s name and its price. public class Product { private String name; private double price; public Product(String n, double p) { name = n; price = p; } public String toString() { return String.format( “%s costs %f”, name, price ); } } //end class definition Your tasks are to:
- define two set methods to set the name and price of the
- define two get methods to retrieve the name and price of the
- create a Product object with the name is “Milk” and the price is 6.5, and display a String containing the value of the object’s instance
TASK3 Based on the following Java programs, create a subclass called House. The House will have additionally
- Two private data members of type int to store the number of bedrooms,and number of
- A constructor that uses the superclass
- A toString() that can be used to display the data member
public class Building { private String address; public Building(String add) { address = add; } public String getAddress() { return address; } public String toString() { return String.format(“
%s is a building", address); } } TASK4 What will be the output when the following program is executed? Show all GUI components. Assume the user has completed entries and clicked the button. import java.awt.* import javax.swing.* import java.awt.BorderLayout; public class ComboBoxFrame extends JFrame { private final JComboBox coursesJComboBox; // holds course names private JLabel nameLabel; private JLabel choiceLabel; private JTextField nameTextField; private final JButton displayJButton; private JTextField displayfield; private String choice; private static final String[] courseNames = {"Software Design", "Distributed Systems", "Enterprise Computing", "SD Project"}; // ComboBoxFrame constructor adds JComboBox to JFrame public ComboBoxFrame() { super("display course selection"); displayJButton = new JButton("Display choices"); JPanel topPanel = new JPanel(new GridLayout(3,1)); courses J Combo Box = new JComboBox(courseNames); coursesJComboBox.setMaximumRowCount(4); // display four rows buildTopPanel(topPanel); add(topPanel, BorderLayout.NORTH); coursesJComboBox.addItemListener( new ItemListener() // anonymous inner class { // handle JComboBox event public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { choice = courseNames[coursesJComboBox.getSelectedIndex()]; } } // end anonymous inner class displayJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { displayfield.setText(choice); choiceLabel.setText(nameTextField.getText() + “ , Your Choice" ); } } ); // end call to addActionListener } //end constructor public final void buildTopPanel(JPanel aPanel) { nameLabel = new JLabel("Student Name"); nameTextField = new JTextField(18); displayfield = new JTextField(18); choiceLabel = new JLabel("Your Choice"); JPanel RowOnePanel = new JPanel(new FlowLayout()); JPanel RowTwoPanel = new JPanel(new FlowLayout()); JPanel RowThreePanel = new JPanel(new FlowLayout()); RowOnePanel.add(nameLabel) ; RowOnePanel.add(nameTextField); RowTwoPanel.add(coursesJComboBox); RowTwoPanel.add(displayJButton); RowThreePanel.add(choiceLabel); RowThreePanel.add(displayfield); aPanel.add(RowOnePanel); aPanel.add(RowTwoPanel); aPanel.add(RowThreePanel); } public static void main(String args[]) { JFrame testFrame = new ComboBoxFrame(); testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); testFrame.setSize(400, 400); testFrame.setVisible(true); } } // end class ComboBoxFrame TASK5 What is the output from the following sequence of linked list operations? LinkedList myList = new LinkedList(); ListIterator iter; myList.addFirst("John"); myList.addFirst("David"); iter = list.listIterator(); iter.next(); iter.add("Peter"); iter.next(); iter.add("Joshua"); TASK6 What is Big-O notation? By using the definition of Big-O, show that the running time for term T(n)=100+2n+ n2 is O(n2). 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.*; //defintion of ReadFileTest 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()) { //students to complete this part } //students to complete this part } } TASK7 The following Java code segment uses the data structure - queue. It contains some operations such as enqueue( ) and dequeue( ). Please write down the output when the code segment is executed. Queue class has implemented standard Queue interface. Queue q = new Queue(); int a = 10; q.enqueue(5); q.enqueue(a); q.enqueue(20); System.out.println(q.peek()); q.dequeue(); q.enqueue(30); while (!q.isEmpty()) System.out.print(q.dequeue() + " "); TASK8 Provide a declaration and implementation of the generic method findMinimum( ) that takes an array of generic type T as the argument and returns the minimum value of the array. Test this generic method with the Integer and String type data. TASK9 Manually provide the inorder, preorder and postorder traversals of the binary search tree as shown below.
TASK10 Write a method to calculate the sum of integers 1 to n recursively.
The above question has been solved by our Data Structures And Algorithms experts at onlineassignmentbank. Our Assignment Writing Experts are efficient to provide a fresh solution to this question. We are serving more than 10000+ Students in Australia, UK & US by helping them to score HD in their academics. Our Experts are well trained to follow all marking rubrics & referencing style.