JAVA2 Merged
JAVA2 Merged
Question
Develop a Java program to connect with database and retrieve following details:
• Write a Java code to insert records by reading input from user.
• Display all the details which are stored in Nobel Prize table.
• Display the details of the Nobel Prize winners who got for the subject Peace.
• Delete the NobelPrize winners who got NobelPrize before 1930.
Aim
To write a java program to connect database and perform CRUD operations in NobelPrize table.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class noble_prize {
public static Connection getConnect() throws Exception
{
String url="jdbc:mysql://localhost:3306/noble";
String username="root";
String password="root";
Connection con=DriverManager.getConnection(url,username,password);
return con;
}
public static void create_table() throws Exception
{
Connection con=getConnect();
String query="create table noble_prize(Name varchar(50),Year int,Subject
varchar(50))";
Statement s=con.createStatement();
s.execute(query);
con.close();
}
public static void insert(String name,int year,String subject) throws Exception
{
Connection con=getConnect();
String query="insert into noble_prize values(?,?,?)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1,name);
ps.setInt(2,year);
ps.setString(3,subject);
ps.execute();
con.close();
System.out.println("===============================================");
System.out.println("Name\t\t\tYear\t\tSubject");
System.out.println("===============================================");
Statement s=con.createStatement();
ResultSet rs=s.executeQuery(query);
while(rs.next())
{
System.out.println(rs.getString(1)+"\t\t"+rs.getInt(2)+"\t\t"+rs.getString(3));
}
System.out.println("===============================================");
con.close();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try {
create_table();
for(int i=0;i<5;i++) {
System.out.print("Enter name: ");
String s=sc.nextLine();
System.out.print("Enter year: ");
int n=sc.nextInt();
sc.nextLine();
System.out.print("Enter subject: ");
String sub=sc.nextLine();
insert(s,n,sub);
}
insert("Rabindhranath Tagore",1917,"Literature");
display();
Connection con=getConnect();
String query1="select * from noble_prize where Subject='Peace'";
Statement s=con.createStatement();
ResultSet rs=s.executeQuery(query1);
rs.next();
System.out.println("\nPeace winners: "+rs.getString(1)+" "+rs.getInt(2));
String query2="delete from noble_prize where Year<1930";
s.executeUpdate(query2);
display();
}
catch(Exception e)
{
e.printStackTrace();
}
}}
Result
Thus, the java program was executed successfully and the output was verified.
Question
Given the following table containing information about employees of an organization,
develop a small java application, using JDBC, which displays a menu for the user consisting
of following options: (Use Prepared Statement for Executing Queries)
1. Add Record 2. Modify Record 3. Delete Record
4. Display one Record 5. Display All 6. Exit
Aim
To write a java program to connect database and perform CRUD operations using prepared
statement.
Result
Thus, the java program was executed successfully and the output was verified.
Question
1) Develop a Java program to connect with database and retrieve following details: 1) Add a new book
‘HTML, CSS & JavaScript’ by Laura Lemay, Prentice Hall, Rs.250.00 using Prepared Statement.
2) Display all the details which are stored in Books table. 3) Create a Procedure to increase the price
of books by Rs.200. 4) Create a Procedure to add new record into table. 5) Execute procedures using
Callable Statement.
Aim
To write a java program to connect jdbc and perform CRUD operations using callable statement.
Result
Thus, the java program was executed successfully and the output was verified.
Question
Write a Java program to test the function using JUnit & check whether it is finding the Nth Fibonacci
number.Try to fix the logical errors if any.
Aim
To write a java program that checks Fibonacci number is found correctly.
Output
Result
Thus, the java program was executed successfully and the output was verified.
Question
Create the class and implement the method to check given string is a palindrome and return the
result. Create a Junit test class to test the above class.
Aim
To write a java program that checks palindrome string is found correctly..
Output
Result
Thus, the java program was executed successfully and the output was verified.
Question
Create the class and implement the method to find number of digits in a given string. Create a Junit
test class to test the above class.
Aim
To write a java program that checks number of digits is found correctly.
Result
Thus, the java program was executed successfully and the output was verified.
Question
Test the functions of Calculate class using JUnit. Print binary function is used to print the binary
equivalent of the given number. Note: Try to fix the logical errors in these functions.
Aim
To write a java program that checks the binary form of a number is found correctly.
import java.util.Arrays;
public class Calculate {
public byte add(byte b1, byte b2) {
return (byte)(b1+b2);
}public short add(short b1,short b2) {
return (short)(b1+b2);
}public int[] printBinary(byte number) {
int[] result;
if (number < 16) result = new int[4];
else if (number < 32) result = new int[5];
else if (number < 64) result = new int[6];
else result = new int[7];
for(int i = result.length - 1; number > 0; i--) {
result[i] = number % 2;
number = (byte) (number / 2);
}
return Arrays.copyOfRange(result, i + 1, result.length);
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
@RunWith(Parameterized.class)
public class CalculateTest {
byte input;
int[] expected;
Output:
Result
Thus, the java program was executed successfully and the output was verified.
Question
Create a test suite class for the above exercises 2 & 3 and test the methods.
Aim
To write a java program that test the program using suite method.
Output
Result
Thus, the java program was executed successfully and the output was verified.
Question:
A function interface ArrayProcess is defined as follows
public interface ArrayProcess {
int apply( int[] array );
}
Write a lambda expression for the following tasks
• find the maximum value in the array,
• find the minimum value in an array,
• counts the occurrence of a given value occurs in an array
• find the sum of the values in the array, and
• find the average of the values in the array.
Aim:
To write a java program to create functional interface to perform some tasks in the given array.
Program/Source Code:
package Module1;
import java.util.*;
@FunctionalInterface
interface ArrayProcess{
int apply(int[] array);
}
class process implements ArrayProcess{
//Finding Maximum Value.
Scanner sc = new Scanner(System.in);
public int apply(int[] arr) {
ArrayProcess findmax = array->{int max=array[0];
for(int j=1;j<array.length;j++)
{
if(max<array[j])
{
max=array[j];
}
}
return max;
};
//Minimum Element
ArrayProcess findmin=array->{
int min=array[0];
for(int j=1;j<array.length;j++)
{
RESULT:
Thus the java program using functional interface executed successfully and output was
verified.
Question:
Write a Java program to demonstrate working of Consumer interface based on the
following design requirements
Aim:
To write a java program to create functional interface to perform some tasks in the given array.
Program/Source Code:
package Module1;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
class Employee {
private int employeeId;
private String name;
private double salary;
private String designation;
private List<String> skills;
private String email;
private Address address{
public Employee(int employeeId, String name, String email, String designation, Address address,
List<String> skills,double salary) {
this.employeeId = employeeId;
this.name = name;
this.email = email;
this.designation = designation;
this.address = address;
this.skills = skills;
this.salary=salary;
}
OUTPUT:
RESULT:
Thus the java program using functional interface executed successfully and output was
verified.
Question:
Write a Java program to demonstrate working of Predicate interface based on the following
design requirements
Program/Source Code:
package Module3;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
class Address {
private int doorNo;
private String street;
private String city;
private int pinCode;
private Set<Long> phoneNumber;
public Address(int doorNo, String street, String city, int pinCode, Set<Long> phoneNumber)
{ this.doorNo = doorNo;
this.street = street;
this.city = city;
this.pinCode = pinCode;
this.phoneNumber = phoneNumber;
}
public void setdoorNo(int doorNo)
{ this.doorNo = doorNo;
}
public void setStreet(String street)
{ this.street = street;
}
// Print address details which have more than one phone number
System.out.println("\nAddress details which have more than one phone Number:");
addList.forEach(address -> {
if (p3.test(address)) System.out.println(address);
});
}
}
OUTPUT:
RESULT:
Thus the java program using functional interface executed successfully and output was
verified.
Question:
Write a Java program to print Fibonacci series for integer value n. Use Function<T>
functional interface and lambda expression that accepts integer value n and print
Fibonacci series.
Aim:
To write a java program that generates Fibonacci series using lambda expression.
Program/Source Code:
package Module1;
import java.util.Scanner;
import java.util.function.Function;
public class Fibonacci {
public static void main(String
args[]){ Function<Integer,Integer>
f=(n)->{
int t1=0;
int t2=1;
int i;
System.out.print(t1 + " " +t2+" ");
for(i=3;i<=n;i++){
int t3=t1+t2;
System.out.print(t3+" ");
t1=t2;
t2=t3;}
return 0;
};
Scanner sc = new Scanner(System.in);
System.out.print("Enter the integer n: ");
int n=sc.nextInt();
f.apply(n);
}}
OUTPUT:
RESULT:
Thus the java program using lambda expression was executed successfully and output was
verified.
Question:
Write a Java program for the generation of OTP (One Time Password) as 6-digit number using
Supplier interface. Use random () method to generate the OTP.
Aim:
To write a java program that generates OTP using Supplier Interface
Program/Source Code:
package Module1;
import java.util.function.Supplier;
import java.util.Random;
public class OTP {
public static void main(String[] args)
{
Supplier<Integer> OTP=()->new Random().nextInt(900000)+100000;
System.out.println("OTP(One Time PassWord) : "+OTP.get());
}
}
OUTPUT:
RESULT:
Thus the java program using lambda expression was executed successfully and output was
verified.
Question:
Generate a secure random password in Java using Supplier interface.
The criteria for a valid password:
• The length of the password should be eight characters.
• The first and last character should be capital letters.
Aim:
To write a java program that generates password using Supplier Interface.
Program/Source Code:
package Module3;
import java.util.Random;
import java.util.function.Supplier;
import java.util.*;
public class generatePassword{
public static void main(String[] args)
{ Supplier<String>passwordSupplier=()-
>{
String upp="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower=upp.toLowerCase();
String alp=upp+lower;
Random random=new Random();
StringBuilder password=new StringBuilder();
char first=alp.charAt(random.nextInt(alp.length()));
char last=alp.charAt(random.nextInt(alp.length()));
password.append(first);
for(int i=1;i<=6;i++) {
char c=alp.charAt(random.nextInt(alp.length()));
password.append(c);}
password.append(last);
return password.toString();};
String password=passwordSupplier.get();
System.out.println("Generated Password:"+password);}}
OUTPUT:
RESULT:
Thus the java program using lambda expression was executed successfully and output was
verified.
Question:
Write a Java program to create a list of strings and convert lowercase letters to uppercase letters
using stream map () function.
Aim:
To write a java program to create list of strings and convert them to uppercase letters.
Program/Source Code:
package Module1;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class StringListConversion
{ public static void main(String args[])
{
List<String> list =Arrays.asList("shinchan","doremon","apple","java");
List<String> upper = list.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("Original List : "+list);
System.out.println("Uppercase List : "+upper);
}
}
OUTPUT:
RESULT:
Thus the java program using lambda expression was executed successfully and output was
verified.
Question:
Write a Java program to demonstrate working of Stream.
• Create ArrayList emp that will store list of employee objects.
• Display the employee objects whose salary is less than 10000. (Hint: Use stream,
filter concepts).
• Increment 2000 for the employee who is getting the salary less than 5000 using map.
• Sort the employee based on the salary in descending order (i.e highest salary should
come on top).
Aim:
To write java program to create arraylist employee and display the list using filters.
Program/Source Code:
package Module1;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Arrays;
import java.util.stream.Collectors;
class Emp{
private int empId;
private String empName;
private int salary;
RESULT:
Thus the java program using lambda expression was executed successfully and output was
verified.
Question:
Implement following UML diagram and Write a Java program for Comparison Based Stream
Operations using below requirements.
Aim:
To write a java program to create Student list and sort the list based on cgpa .
Program/Source Code:
package Module1;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;
class Student
{ private int id;
private String name;
private double cgpa;
public Student(int id,String name,double cgpa)
{ this.id=id;
this.name=name;
this.cgpa=cgpa;}
public int getId()
{ return this.id;}
public void setId(int id) { this.id=id;}
public String getName()
{ return this.name;}
public void setString(String name)
{ this.name=name;}
public double getCgpa()
{ return this.cgpa;}
public void setCgpa(double cgpa)
{ this.cgpa=cgpa;}
@Override
RESULT:
Thus the java program using lambda expression was executed successfully and output was
verified.
Question:
Create a LinkedList and add 100 random numbers. Find and display number which is
divisible by 8 from LinkedList. Count the numbers which is divisible by 11. (Hint: Use
stream, filter, count concepts).
Aim:
To write a java program that generates list of 100 numbers and display the number divisible by 8.
Program/Source Code:
package Module1;
import java.util.List;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class StreamRandom{
public static void main(String args[]){
List<Integer> ls = newArrayList<>();
int i;
Random r = new Random();
for(i=0;i<100;i++){
ls.add(r.nextInt());}
List<Integer> divide8 = ls.stream().filter(val -> val%8==0).toList();
divide8.forEach(System.out::println);
long divide11 = ls.stream().filter(val -> val%11==0).count();
System.out.println("Total Numbers divided by 11 : "+divide11);}}
OUTPUT:
RESULT:
Thus the java program using lambda expression was executed successfully and output was
verified.
Capability of
Entire procedures are Most of procedures are Some of Few of procedures No procedures are
writing Procedure /
5 written clearly. written clearly. procedures are are written clearly. written.
Drawing Flow
written
Chart undoubtedly.
Some experiments
Capability of All experiments are Some experiments are are written Major errors in Experiments are not
writing program / written precisely written accurately. accurately and experiment writing. written/wrong
5
Constructing based on the question. minor errors in the experiment written.
circuits program.
Represent all data
Validation of 5 appropriately. Finding Partly outputs are found. Minor parts of Major part of output Wrong output/no
Output of results are clear. output are found. not found. output found.
Overall Completion 100% target has been 75% target has been 50% target has been 25% target has been Less than 25% target
of experiments in 5 completed. completed. completed. completed. has been completed.
Lab
Submission of Lab
Completion of Proper Time (i.e.) in next Very poor
Record in proper time
Record 5 lab session, with proper Poor presentation. presentation. Late Submission.
but not with proper
documentation.
documentation.
Exp No 10
Criterion
Validation of Output
Overall Completion of
experiments in Lab
Completion of Record
Faculty Signature