Sorted values of the given array using lambdas

Assignment Help JAVA Programming
Reference no: EM132387933

Question 1
The following program is based on linked list operations. What will be the output when the program given below is executed?

import java.util.LinkedList; import java.util.ListIterator;
public class GraduandsList {
public static void main(String[] args)
{
LinkedList<String> graduands = new LinkedList<String>();
ListIterator<String> iter; graduands.add("William Bhullar"); System.out.println("List: "+ graduands);

graduands.addLast("Andrew Zhang"); System.out.println("List: "+ graduands);

int index = graduands.indexOf("William Bhullar"); graduands.set(index,"Sajan Tabang" ); graduands.addFirst("William Bhullar"); System.out.println("List: "+ graduands);

iter = graduands.listIterator(); iter.next();
iter.next(); iter.add("Prasad Patel");
System.out.println("List: "+ graduands);
}
}

Question 2
Write the missing lines of code and the output of the following program.

import java.util.HashSet; import java.util.Set;

public class Q2SetUnion {
static Set<Integer> setOne = new HashSet<>(); static Set<Integer> setTwo = new HashSet<>();

public static void main(String[] args) {
final Integer groupOne [] = { 2, 5, 17, 27, 7,37 };
final Integer groupTwo [] = { 3, 8, 15, 25, 5, 17};

//initialise sets
for (Integer temp: groupOne)
{
setOne.add(temp);
}
for (Integer temp: groupTwo)
{
setTwo.add(temp);
}
// Complete the following lines of code using the comments as a clue.
// Create a new set named union to store the values of the resulting union
// of the given two sets.

// implement union of the two sets.

// display the values in set named union.

Question 3
Complete the code given below to enable the following functionalities:
(a) To add all the numbers from the array ‘values' to the priority queue ‘q1'.
(b) Display the content of q1 (Hint: create a copy of the queue and use poll on the copy).
(c) Replace the number ‘2' with ‘5' in ‘q1'.

import java.util.PriorityQueue; public class PriorityQueue {
public static void main (String [] args)
{
PriorityQueue<Integer> q1 = new PriorityQueue<>();
Integer [] values = { 9, 2, 12, 7, 15};
//fill the queue q1 with elements from the array values.

// Write code to display the contents of queue q1.

// write code to replace the element ‘2' with ‘5' in q1.

} // end main
} //end class

Question 4
(a) By using the definition of Big-O, show that the running time term T(n)=100+2n+
n2 is O(n2).
(b) For the following running time expressions, what would be the order of the growth rate of each in terms of Big-O notation?
i) 2n+10n
ii) n2 + 10n log2n
iii) n+ 10√n +5

Question 5

Complete the missing lines of code in the program given below to display:
(a) Sorted values of the given array using Lambdas and streams.
(b) Display the String names in ascending order after filtering the values starting with g or less.

public class Q5LambdasAndStreams {
public static void main( String[] args )
{
int [] numbers = {14, 3, 27, 12, 7, 18, 57, 15, 8};
List<Integer> values = new LinkedList<>(); String[] names = {"Lily", "Lin", "Ann", "Bill",
"Michael", "Kay", "Sam"};
for (int n: numbers)
values.add(n);

//Dsiplay the sorted values.
// write your code

//Display names in ascending order after filtering the names strating with ‘g' or less.
// write your code

} //end main
} //end class

Question 6
Assume that a SQL server connection exists to a database named Restaurant. You are given the query to create a Table named Customers that stores customer name and customerId as primary key as shown in the incomplete code given below.
(a) Complete code to execute the given query to create the table.
(b) Use PreparedStatement to search a customer with a given name.

import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager;

import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.Statement;
public class Q6Database {
public static void main(String[] args) {
final String MYSQL_URL ="jdbc:mysql://localhost:3306"; final String DB_URL = MYSQL_URL +"/Restaurant";
//initialise MySql usename and password final String USER_NAME ="tomm"; final String PASSWORD = "mtom0001"; Connection sqlConnection;
Statement statement; PreparedStatement searchOneCustomer;

// query string to create Customers table
final String TABLE_CUSTOMERS_QRY = "CREATE TABLE" +
"CUSTOMERS (CustomerId INTEGER not NULL" + "AUTO_INCREMENT,Name VARCHAR(30)," +
"PRIMARY KEY (CustomerId) )";

// sql driver is registered and connected to the database.
sqlConnection = DriverManager.getConnection(DB_URL,
USER_NAME, PASSWORD);
statement = sqlConnection.createStatement();

//complete the missing code to execute the query to create Customers table
If (!tblCustomersExist)
// write your code

//Write PreparedStatement to execute a query to searchOneCustomer
searchOneCustomer = // write your code
} //end main
} //end class

Question 7
The following program creates a class named GenericStack and then uses it to create a Stack of Strings. Complete the code for the method push() which takes a value of type T and adds to the elements in the GenericStack. Write code to populate the GenericStack with the values from the Array of Strings, cities given below using your push method.

import java.util.LinkedList; public class GenericStack <T> {
private LinkedList <T> elements; public GenericStack() {
elements = new LinkedList <T> ();
}

//Write code for the push method
//Use appropriate LinkedList method to add elements to the stack.
public static void main(String [] args)
{
String [] cities = {"bne", "mel", "syd", "crns" };
//Write code to create an object of type GenericStack

// Use the push method you have written to populate it with values from cities.
for (String s: cities)
// write missing code

} // end main

}// end class

Question 8
Write a recursive method, public int findSum(int n) which will recrusively calculate and return the sum of numbers 0 to n.

Question 9
What is the output from the following sequence of operations?

Queue<Integer> q = new LinkedList<>(); int a =10;
q.offer(5);
q.offer(a);
q.offer(15); System.out.println (q.peek()); q.poll();
q.offer(50);
Iterator iter = q.iterator(); while(iter.hasNext())
System.out.print(q.poll()+ " ");

Question 10
In the binary search tree shown below certain values are misplaced by not following the binary search tree rules. Correct the misplaced values.
List the values of A and B in the tree shown below so that it is a binary search tree. Manually provide the inorder, preorder and postorder traversals of the binary search tree with values of A, B, and all values correctly placed.

981_figure1.jpg

Attachment:- Term 1 Standard Examination 2018.rar

Reference no: EM132387933

Questions Cloud

Replacing an existing piece of equipment : A company is considering replacing an existing piece of equipment. The company uses an interest rate of 7%
Determine the equivalent annual cost : Determine the equivalent annual cost if the equipment is kept for 1 year, 2 years, 3 years, 4 years, 5 years or 6 years.
Find info to this group-national organization for women : Find info to this group,National Organization for Women. Research the web and other resources for information about your chosen interest group.
Experimenters wanted to know if the moon at the horizon : The experimenters wanted to know if the Moon at the horizon looked bigger than the Moon at the zenith.
Sorted values of the given array using lambdas : Sorted values of the given array using Lambdas and streams and Display the String names in ascending order after filtering the values starting with g or less.
What are the some of the problems : What are the some of the problems in trying to use fiscal and monetary policies under different exchange rate regimes?
What is the law of demand and supply : What is the law of demand and supply? What is equilibrium price?
What does the given program do : Explain how the layout manager might be used for building this GUI - What value is displayed when this program executes? What mathematical function
Construct a table showing norita : (i) Construct a table showing Norita's marginal sales per day in each city.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Development of a graphical user interface

To understand actual communication with an SMTP server can be achieved using TCP Sockets and understand the development of a graphical user interface (GUI) and use of event handling.

  Create a java gui app implement inside a main method

Create a Java GUI app, implement the following criteria inside a main method 1) set up a visible frame with width 500, height 300; 2) frame title is "CSE205 Frame app"; and 3) setDefaultCloseOperation to EXIT_ON_CLOSE.

  Write a java program that asks the user to enter two times

This program should have two classes, substing, parsing . Write a Java program that asks the user to enter two times.

  Java socket hello i need to this assignment done in net

hello i need to this assignment done in net beans . and i want comment in code .also screen shots of running program

  Which statement is true about a non-static inner class

Which statement is true about a non-static inner class? Clicking the closing button on the upper-right corner of a frame generates a(n) _____ event.

  How can an applet get information about the url

How can an applet get information about the URL it was called from? What is an applet? What API changes are there for applets with JDK 1.4?

  Create a class with so-called boundarycells

You are to create a class with so-called BoundaryCells. Methods in the class throw exceptions under certain conditions - The constructor has two logical

  Design an applet in java

Design an applet in Java. The applet should be a snack machine. The user can then click on a button to select the products to dispense.

  What do you think the advantages of method overloading

What side are you on? What do you think the advantages of method overloading are? How would you use method overloading when writing a program? Critique or defend your classmates' positions

  Explain what is local variable in java programming

Explain What is local variable in java programming?

  Purchasing type of mobile device

Mobile devices use touch screens to interact with the individual. Select a mobile device and identify three (3) important usability features that are important to you when purchasing this type of mobile device. Provide a rationale for your respons..

  Create a javafx application named runningman.java

Create a JavaFX application named RunningMan.java to animate a man running laps. The man should run from left to right until the edge of the window is reached. The man should then begin running in the opposite direction.

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