Labsheets of JavaFullStack
Labsheets of JavaFullStack
}
@FunctionalInterface
interface myinterface2 {
void method2();
}
@FunctionalInterface
interface myinterface3 {
void method3();
}
//partial implementation of interface is not allowed before java8
R3=()->{
[Link]("Method3 implemented in anonymous class2");
};
R1.method1();
R2.method2();
R3.method3();
Labsheet 2
import [Link];
Labsheet 3
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
//LIST INTERFACE METHODS WITH ArrayList (IMPLEMENTED CLASS)
//there are 2 kinds of list to keep objects
//1. generic list to hold objects of primitive types-heterogeneous
//2. specific list to hold objects for both type(primitive and custom)-homogeneous
public class TEST3 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
[Link](10);
[Link](7);
[Link](15);
[Link](42);
//how to check List created above
//List allows duplicates , preserves the insertion order
[Link](list);//possible bcz of toString()
//how to print objects inside the list object
[Link]("using for-each");
for(Integer i:list) {
[Link](i);
}
[Link]("using iterator");
ListIterator<Integer> itr=[Link]();
while([Link]()) {
[Link]([Link]());
}
//insert at begin , end and any position
[Link](list);
[Link](0,100);
[Link](list);
[Link](5,200);
[Link](list);
[Link](3,300);
[Link](list);
//delete object from list
[Link](0);
[Link](list);
Integer ob = new Integer(200);
[Link](ob);
[Link](list);
//searching an object
ob=new Integer(7);
if( [Link](ob))
[Link]("Present");
else
[Link]("Not Present");
//find index of an object
[Link]([Link](ob));
[Link]("printing few elements......");
//printing few objects from the list
for (int i=0;i<=2;i++) {
[Link]([Link](i));
}
//LambdaExpression
oddlist= [Link]().filter(e->e%2!=0).collect([Link]());
[Link]("Printing odd list...");
[Link](oddlist);
[Link](list);
boolean res=[Link]().allMatch(e->e%5==0);
[Link](res);
res=[Link]().anyMatch(e->e%5==0);
[Link](res);
res=[Link]().noneMatch(e->e%5==0);
[Link](res);
Labsheet 4
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
//binary search
int p=[Link]
(studentlist,new Student(342,"upendra",8.3),(x,y)-
>[Link]([Link](), [Link]()));
[Link]("is present in the list at "+p+" index");
//binary search returns index of the object from a sorted collection
Student ob1=new Student(101,"amith",7.5);
Student ob2=new Student(100,"nithin",7.8);
Student ob3=new Student(102,"sreekanth",7.4);
Student ob4=new Student(109,"supriya",7.3);
//sort by id
[Link](studentlist,
(x,y)->[Link]([Link](), [Link]()));
[Link]("Descending by id"+studentlist);
//sort by name
[Link](studentlist,
(x,y)->[Link]().compareTo([Link]()));
[Link]("Descending by name"+studentlist);
//sort by cgpa
[Link](studentlist,
(x,y)->[Link]([Link](), [Link]()));
[Link]("Descending by cgpa"+studentlist);
// sort using method reference
}
}
TestSpot1
import [Link];
import [Link];
import [Link];
import [Link];
class PicnicSpot {
String name;
double distance;
boolean hotelAndRestaurantAvailability;
double reviews;
TestSpot2
import [Link];
import [Link];
import [Link];
import [Link];
class Hotel {
private String name;
private double dist_from_location;
private double charges;
private double reviews;
}
class PicnicSpot {
String name;
double distance;
List<Hotel> hotels;
double reviews;
//add constructor with field
public PicnicSpot(String name, double distance, List<Hotel> hotels, double reviews) {
super();
[Link] = name;
[Link] = distance;
[Link] = hotels;
[Link] = reviews;
}
public PicnicSpot() {
super();
}
// Find the best spot as per distance and find the best hotel
// as per distance
Hotel bestHotelByDistance = [Link]()
.min((x,y)->[Link]([Link](),[Link]())).get()
.getHotels().stream()
.min((x,y)->[Link](x.getDist_from_location(), y.getDist_from_location()))
.get();
[Link]("Best Hotel as per distance: " + bestHotelByDistance);
// Find the best spot as per distance and find the best hotel
// as per price
PicnicSpot best_spot_as_per_distance=
[Link]().min((x,y)->[Link]([Link](), [Link]()))
.get();
[Link]("Best spot as per distance "+best_spot_as_per_distance);
}
}
Labsheet 5
/*objective - prepare a table of entries
consists of customerid as key and List of product
selected to purchase are values
Define a Product
Map<String, List<Product>>
sunny123--------------> {hp laptop, }
*/
import [Link].*;
//pojo
class Product {
private String prodId;
private String prodName;
private String prodCategory;
private double prodPrice;
// add getter setter, constructor , toString, equals and hashCode
public String getProdId() {
return prodId;
}
public void setProdId(String prodId) {
[Link] = prodId;
}
public String getProdName() {
return prodName;
}
public void setProdName(String prodName) {
[Link] = prodName;
}
public String getProdCategory() {
return prodCategory;
}
public void setProdCategory(String prodCategory) {
[Link] = prodCategory;
}
public double getProdPrice() {
return prodPrice;
}
public void setProdPrice(double prodPrice) {
[Link] = prodPrice;
}
public Product(String prodId, String prodName, String prodCategory, double prodPrice) {
super();
[Link] = prodId;
[Link] = prodName;
[Link] = prodCategory;
[Link] = prodPrice;
}
public Product() {
super();
}
@Override
public String toString() {
return "Product [prodId=" + prodId + ", prodName=" + prodName + ",
prodCategory=" + prodCategory
+ ", prodPrice=" + prodPrice + "]";
}
@Override
public int hashCode() {
return [Link](prodCategory, prodId, prodName, prodPrice);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != [Link]())
return false;
Product other = (Product) obj;
return [Link](prodCategory, [Link]) &&
[Link](prodId, [Link])
&& [Link](prodName, [Link])
&& [Link](prodPrice) ==
[Link]([Link]);
}
}
public class Test5 {
public static void main(String[] args) {
List<Product> productList1 = new ArrayList<Product>();
[Link](new Product("pres101","Java Complete Reference","book",700));
[Link](new Product("pres109","Python","snake",1000));
[Link](new Product("pres1125","HP Laptop","Electronic",45000));
[Link](new Product("pres1199","Iphone","Electronic",150000));
List<Product> productList2 = new ArrayList<Product>();
[Link](new Product("pres101","Java Complete Reference","book",700));
[Link](new Product("pres1101","water bottles","Acessories",700));
[Link](new Product("pres1230","Jeans","Clothes",3000));
[Link](new Product("pres1199","Ihone","Electronic",150000));
[Link](new Product("pres101","Java Complete Reference","book",700));
List<Product> productList3 = new ArrayList<Product>();
[Link](new Product("pres230","Sotalol","medicine",200));
[Link](new Product("pres238","Carvedilol","medicine",700));
[Link](new Product("pres239","Aciclovir","medicine",70));
[Link](new Product("pres245","Pantoprazol","medicine",240));
[Link](new Product("pres289","Nitroglyserin","medicine",1000));
Map<String, List<Product>> table =
new HashMap<String,List<Product>>();
[Link]("PUNIV00448", productList3);
[Link]("PUNIV01690", productList1);
[Link]("PUNIV00123", productList2);
//Display the details of table
for([Link]<String,List<Product>> e:[Link]()) {
[Link]([Link]()+"->"+[Link]());
}
//Display the details by name of products
for([Link]<String,List<Product>> e:[Link]()) {
[Link]([Link]()+"->");
[Link]().stream().forEach(x->
[Link]([Link]()+","));
[Link]();
}
TestOneToMany
package JDBC;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
}
class Address {
private String HouseNumber;
private String postOffice;
private String district;
private String state;
private int pin;
private String mobileNumber;
private int sid;
public String getHouseNumber() {
return HouseNumber;
}
public void setHouseNumber(String houseNumber) {
HouseNumber = houseNumber;
}
public String getPostOffice() {
return postOffice;
}
public void setPostOffice(String postOffice) {
[Link] = postOffice;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
[Link] = district;
}
public String getState() {
return state;
}
public void setState(String state) {
[Link] = state;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
[Link] = pin;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
[Link] = mobileNumber;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
[Link] = sid;
}
public Address(String houseNumber, String postOffice, String district, String state, int pin,
String mobileNumber,
int sid) {
super();
HouseNumber = houseNumber;
[Link] = postOffice;
[Link] = district;
[Link] = state;
[Link] = pin;
[Link] = mobileNumber;
[Link] = sid;
}
public Address() {
super();
}
@Override
public String toString() {
return "Address [HouseNumber=" + HouseNumber + ", postOffice=" + postOffice + ",
district=" + district
+ ", state=" + state + ", pin=" + pin + ", mobileNumber=" +
mobileNumber + ", sid=" + sid + "]";
}
@Override
public int hashCode() {
return [Link](HouseNumber, district, mobileNumber, pin, postOffice, sid,
state);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != [Link]())
return false;
Address other = (Address) obj;
return [Link](HouseNumber, [Link]) &&
[Link](district, [Link])
&& [Link](mobileNumber, [Link]) && pin == [Link]
&& [Link](postOffice, [Link]) && sid == [Link]
&& [Link](state, [Link]);
}
}
//create a datasource object-singleton
interface StudentDAO {
public boolean create(String dbn,String user,String pwd);
public int insertStudent(Student s);
public int updateStudent(int id, Student s);
public int deleteStudent(int id);
public List<Student> fetchAllStudents();
public Student fetchAStudent(int id);
}
class StudentDAOImpl implements StudentDAO {
private Connection cts=null;
public static StudentDAOImpl ob=null;
public static StudentDAOImpl createObject() {
if(ob==null)
ob=new StudentDAOImpl();
return ob;
}
private StudentDAOImpl() {}
public boolean create(String dbn,String user,String pwd) {
boolean result=true;
try {
cts=[Link](
"jdbc:mysql://localhost:3306/"+dbn,user,pwd);
[Link]("[Link]");
Statement st=[Link]();
// result=[Link]("create table student(id int primary key, name varchar(30), cgpa
double)");
}catch(Exception e) {
[Link](e);
}
if(result==true) {
return true;
}
else {
return false;
}
}
public int insertStudent(Student s) {
int response=0;
try {
String sql="insert into student values(?,?,?)";
PreparedStatement p=[Link](sql);
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
response=[Link]();
}catch(Exception e) {
[Link](e);
}
return response;
}
public int updateStudent(int id, Student s) {
int response=0;
try {
String sql="update student set cgpa=? where id=?";
PreparedStatement p=[Link](sql);
[Link](1, [Link]());
[Link](2, [Link]());
response=[Link]();
}catch(Exception e) {
[Link](e);
}
return response;
}
public int deleteStudent(int id) {
int response=0;
try {
String sql="delete from student where id=?";
PreparedStatement p=[Link](sql);
[Link](1, id);
response=[Link]();
}catch(Exception e) {
[Link](e);
}
return response;
}
public List<Student> fetchAllStudents(){
List<Student> response=new ArrayList<Student>();
try {
String sql="select * from student";
PreparedStatement p=[Link](sql);
ResultSet r=[Link]();
Student s;
while([Link]()) {
s=new Student();
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
[Link]([Link]());
[Link](s);
}
}catch(Exception e) {
[Link](e);
}
return response;
}
public Student fetchAStudent(int id) {
return null;
}
}
interface AddressDAO {
public boolean create(String dbn,String user,String pwd);
public int insertAddress(Address s);
public int updateAddress(String hn, Address s);
public int deleteAddress(String hn);
public List<Address> fetchAllAddresses();
public Address fetchAAddress(String hn);
}
class AddressDAOImpl implements AddressDAO {
public static AddressDAOImpl ob=null;
private Connection cta=null;
public static AddressDAOImpl createObject() {
if(ob==null)
ob=new AddressDAOImpl();
return ob;
}
private AddressDAOImpl() {}
public boolean create(String dbn,String user,String pwd) {
boolean result=false;
try {
cta=[Link](
"jdbc:mysql://localhost:3306/"+dbn,user,pwd);
[Link]("[Link]");
Statement st=[Link]();
String query="create table address(HouseNumber varchar(30) primary key, postofc
varchar(10), district varchar(10), state varchar(10), pin int, mobilenumber varchar(13),sid int, foreign
key(sid) references student(id))";
result=[Link](query);
}catch(Exception e) {
[Link](e);
}
if(result==true) {
return true;
}
else {
return false;
}
}
public int insertAddress(Address s) {
return 1;
}
public int updateAddress(String hn, Address s) {
return 1;
}
public int deleteAddress(String hn) {
return 1;
}
public List<Address> fetchAllAddresses(){
return null;
}
public Address fetchAAddress(String hn) {
return null;
}
}
}
public static int updateStudent(int id,Student s) {
return ([Link](id, s));
}
public static int deleteStudent(int id) {
return([Link](id));
}
public static List<Student> fetchAllStudent() {
return ([Link]());
}
}while(repeat!='n');