0% found this document useful (0 votes)
25 views35 pages

Pra Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views35 pages

Pra Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Exception :

import java.util.*;

class InvalidIdException extends Exception{


public InvalidIdException(String message){
super(message);
}
}

class InvalidSalaryException extends Exception{


public InvalidSalaryException(String message){
super(message);
}
}

class InvalidNameException extends Exception{


public InvalidNameException(String message){
super(message);
}
}

class Employee{
int id;
String name;
double salary;

public Employee(int id, String name, double salary) {


// if(id<=0){
// throw new InvalidIdException("Enter id greater than 0");
// }
this.id=id;
this.name=name;

// if(salary<1000){
// throw new InvalidSalaryException("Salary is greater not greater than
1000");
// }
this.salary=salary;
}

public int getId(){


return id;
}

public String getName(){


return name;
}

public double getSalary(){


return salary;
}

public class Main


{
public static void IdError(int id) throws InvalidIdException{
if(id<=0){
throw new InvalidIdException("Enter id greater than 0");
}
}

public static void SalaryError(double salary) throws InvalidSalaryException{


if(salary<1000){
throw new InvalidSalaryException("Salary is greater not greater than
1000");
}
}

public static void NameError(String name) throws InvalidNameException{


if(name.length() < 4){
throw new InvalidNameException("Insufficient Character in name");
}
}

public static void main(String[] args) {

// System.out.println("Hello World");
Scanner sc = new Scanner(System.in);

Employee[] employees= new Employee[3];

try{
for(int i=0;i<3;i++){
int id=sc.nextInt();sc.nextLine();
IdError(id);
String name = sc.next();
NameError(name);
Double salary = sc.nextDouble();
SalaryError(salary);
employees[i] = new Employee(id,name,salary);
}
}catch(InvalidIdException | InvalidSalaryException | InvalidNameException
e){
e.printStackTrace();
}
}

--------------------------------------------------------------

PRA -1:
import java.util.*;
class course{
int id;
String name;
String admin;
int quiz;
int handson;

public course(int id, String name, String admin, int quiz, int handson){
this.id = id;
this.name = name;
this.admin =admin;
this.quiz = quiz;
this.handson = handson;
}

public int getId() {


return id;
}

public String getName() {


return name;
}

public String getAdmin() {


return admin;
}

public int getQuiz() {


return quiz;
}

public int getHandson() {


return handson;
}

public void setId(int id) {


this.id = id;
}

public void setName(String name) {


this.name = name;
}

public void setAdmin(String admin) {


this.admin = admin;
}

public void setQuiz(int quiz) {


this.quiz = quiz;
}

public void setHandson(int handson) {


this.handson = handson;
}

}
public class Main {

static void Avg(course[] c, String k){


int sum = 0;
int count = 0;
int avg;
for (int i = 0; i < c.length; i++){
if(k.equalsIgnoreCase(c[i].getAdmin())){
sum = sum + c[i].getQuiz();
count+=1;
}
}
if (sum == 0){
System.out.println("No Course Found");
}
else{
avg = sum/count;
System.out.println(avg);

}
}

static course[] hand(course[] course, int n){


course[] result = new course[0];

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


if (course[i].getHandson() < n)
{
result = Arrays.copyOf(result, result.length + 1);
result[result.length - 1] = course[i];
}
}

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


{
for (int j = i + 1; j < result.length; j++)
{
if (result[i].getHandson() > result[j].getHandson())
{
course temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}

if (result.length > 0)
{
return result;
}
else
{
return null;
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

course[] courses = new course[n];

for (int i = 0; i < n; i++){


int id = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
String admin = sc.nextLine();
int quiz = sc.nextInt();
int handson = sc.nextInt();

courses[i] = new course(id,name,admin,quiz,handson);


}
sc.nextLine();
String k = sc.nextLine();
int na = sc.nextInt();
Avg(courses,k);

course[] result = hand(courses, na);


if (result != null)
{
for (int i = 0; i < result.length; i++)
{
System.out.println(result[i].getName());
}
}
else
{
System.out.println("NO");
}

}
}

--------------------------------------------------------------

PRA-2:
import java.util.*;
class footwear{
int id ;
String name;
String type;
int price;
public footwear(int id, String name, String type, int price) {
super();
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}

public class Main {

static int getCount(footwear [] ft , String ftType){


int count=0;
for(int i=0;i<ft.length;i++){
if(ft[i].getType().equalsIgnoreCase(ftType)){
count++;
}
}
if(count==0){
return 0;
}else{
return count;
}

static footwear secondHighestPrice(footwear [] ft , String ftName){


footwear temp;
footwear[] result = new footwear[0];
for(int i=0;i<ft.length;i++){
if(ft[i].getName().equalsIgnoreCase(ftName)){
result =Arrays.copyOf(result, result.length+1);
result[result.length-1]=ft[i];
}
}
for(int i=0;i<result.length;i++){
for(int j=i+1;j<result.length;j++){
if(result[i].getPrice()<result[j].getPrice()){
temp=result[i];
result[i]=result[j];
result[j]=temp;
}
}
}

if(result.length>0){
return result[1];
}
else{
return null;
}

public static void main(String[] args){


Scanner sc = new Scanner(System.in);

int n=sc.nextInt();
footwear[] ft= new footwear[n];
for(int i=0;i<n;i++){
int id=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
String type=sc.nextLine();
int price=sc.nextInt();sc.nextLine();

ft[i]=new footwear(id,name,type,price);
}

String ftType=sc.nextLine();
String ftName=sc.nextLine();

int ans =getCount(ft,ftType);


System.out.println(ans);

footwear obj=secondHighestPrice(ft,ftName);
if(obj!=null){
System.out.println(obj.getId());
System.out.println(obj.getName());
System.out.println(obj.getPrice());
}else{
System.out.println("Brand not available.");
}

----------------------------------------------------------------

PRA-3:

import java.util.*;

class student{
int roll;
String name;
String branch;
double score;
boolean dayScholar;
public student(int roll, String name, String branch, double score, boolean
dayScholar) {
super();
this.roll = roll;
this.name = name;
this.branch = branch;
this.score = score;
this.dayScholar = dayScholar;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public boolean isDayScholar() {
return dayScholar;
}
public void setDayScholar(boolean dayScholar) {
this.dayScholar = dayScholar;
}

public class Main {

static int countOf(student[] st){


int count =0;
for(int i=0;i<st.length;i++){
if(st[i].getScore()>80 && st[i].isDayScholar()==true){
count++;
}
}
if(count==0){
return 0;
}else{
return count;
}
}

static student secondHighest(student[] st){


student temp;
student [] result = new student[0];
for(int i=0;i<st.length;i++){
if(st[i].isDayScholar()==true){
result=Arrays.copyOf(result,result.length+1);
result[result.length-1]=st[i];
}
}
for(int i=0;i<result.length;i++){
for(int j=i+1;j<result.length;j++){
if(result[i].getScore()>result[j].getScore()){
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
}
}
if(result.length!=0){
return st[1];
}
else{
return null;
}
}

public static void main(String [] args){


Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
student[] st = new student[n];

for(int i=0;i<n;i++){
int roll=sc.nextInt();sc.nextLine();
String name = sc.nextLine();
String branch=sc.nextLine();
double score = sc.nextDouble(); sc.nextLine();
boolean dayScholar=sc.nextBoolean();

st[i]=new student(roll, name, branch, score, dayScholar);

}
int ans=countOf(st);
if(ans!=0){
System.out.println(ans);
}else{
System.out.println("their no such dayScholar");
}

student obj=secondHighest(st);
if(obj!=null){
System.out.println(obj.getRoll()+"#"+obj.getName()
+"#"+obj.getScore());
}
else{
System.out.println();
}

---------------------------------------------------------------

JDBC :
package com.App;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {


public static void main(String[] args){

Connection con = null;


// PreparedStatement pst =null;
PreparedStatement crt =null;
int check=0;

try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con=DriverManager.getConnection("jdbc:derby:D:\\\\Users\\\\
2791529\\\\MyDB;create=true");
String cr="create table e23(id int,name varchar(40),salary
double,phone int)";
crt=con.prepareStatement(cr);

// String sql="insert into e44(id,name,salary) values (?,?,?)";


// pst=con.prepareStatement(sql);
// pst.setInt(1, 101);
// pst.setString(2, "Adi");
// pst.setDouble(3, 1000.00);
check=crt.executeUpdate();
System.out.println(check);
crt.close();
// pst.close();
con.close();
}
catch(ClassNotFoundException e){
// System.out.println("satyam -yatish");
e.printStackTrace();
}catch(SQLException e){
// System.out.println("satyam -yatish");
e.printStackTrace();
}
}
}

-----------------------------------------------------------------

PRA-4:

import java.util.*;

class college {
int id;
String name;
int contactNo;
String address;
int pinCode;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getContactNo() {


return contactNo;
}

public void setContactNo(int contactNo) {


this.contactNo = contactNo;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public int getPinCode() {


return pinCode;
}

public void setPinCode(int pinCode) {


this.pinCode = pinCode;
}

public college(int id, String name, int contactNo, String address, int
pinCode) {
super();
this.id = id;
this.name = name;
this.contactNo = contactNo;
this.address = address;
this.pinCode = pinCode;
}

public class Main


{

static college collegeMaxPin(college[] cl){


int max=0;

for(int i=0;i<cl.length;i++){
if(cl[i].getPinCode()>max){
max=cl[i].getPinCode();

}
}
for(int i=0;i<cl.length;i++){
if(cl[i].getPinCode()==max){
return cl[i];

}
}
// if(temp==0){
// return null;
// }
// else{
// return cl[temp];
// }
return null;
}

static college serachAddress(college[] cl , String add){


for(int i=0;i<cl.length;i++){
if(cl[i].getAddress().equalsIgnoreCase(add)){
return cl[i];
}
}
return null;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
college[] cl=new college[n];

for(int i=0;i<n;i++){
int id=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
int contactNo=sc.nextInt();sc.nextLine();
String address=sc.nextLine();
int pinCode=sc.nextInt();sc.nextLine();

cl[i] = new college(id,name,contactNo,address,pinCode);


}

college obj=collegeMaxPin(cl);
if(obj!= null){
System.out.println(obj.getId());
System.out.println(obj.getName());
System.out.println(obj.getPinCode());
System.out.println(obj.getAddress());
System.out.println(obj.getContactNo());
}else{
System.out.println("not found");
}
String add=sc.nextLine();
college obj1=serachAddress(cl,add);
if(obj1!=null){
System.out.println(obj.getId());
System.out.println(obj.getName());
System.out.println(obj.getContactNo());
System.out.println(obj.getAddress());
System.out.println(obj.getPinCode());
}else{
System.out.println("not found");
}

}
}

----------------------------------------------------------------

PRA-5 :

import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

class Motel {
int id;
String name;
String date;
int roomBooked;
String cabFacility;
double bill;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getDate() {


return date;
}

public void setDate(String date) {


this.date = date;
}

public int getRoomBooked() {


return roomBooked;
}

public void setRoomBooked(int roomBooked) {


this.roomBooked = roomBooked;
}

public String getCabFacility() {


return cabFacility;
}

public void setCabFacility(String cabFacility) {


this.cabFacility = cabFacility;
}

public double getBill() {


return bill;
}

public void setBill(double bill) {


this.bill = bill;
}

public Motel(int id, String name, String date, int roomBooked, String
cabFacility, double bill) {
super();
this.id = id;
this.name = name;
this.date = date;
this.roomBooked = roomBooked;
this.cabFacility = cabFacility;
this.bill = bill;
}

public class Main


{

static int totalNoOfRoomBooked(Motel[] ml, String add){

int count=0;
for(int i=0;i<ml.length;i++){
if(ml[i].getCabFacility().equalsIgnoreCase(add) &&
ml[i].getRoomBooked() >5){
count=ml[i].getRoomBooked();

}
}
if(count!=0){
return count;
}else{
return 0;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
int n =sc.nextInt();
Motel[] ml=new Motel[n];

for(int i=0;i<n;i++){
int id=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
String dateStr=sc.next();

try {
Date date = sdf.parse(dateStr);
// System.out.println("Input date: " + sdf.format(date));
} catch (ParseException e) {
System.out.println("Invalid date format");
}
int roomBooked=sc.nextInt();sc.nextLine();
String cabFacility=sc.nextLine();
double bill=sc.nextDouble();sc.nextLine();

ml[i] = new Motel(id,name,dateStr,roomBooked,cabFacility,bill);


}

String add=sc.nextLine();
int obj1=totalNoOfRoomBooked(ml,add);
if(obj1!=0){
System.out.println(obj1);
}else{
System.out.println("no such room booked");
}

}
}

------------------------------------------------------------------

PRA-6:

import java.util.*;
class RRT {
int no;
String raisedBy;
String assignTo;
int priority;
String project;

public RRT(int no, String raisedBy, String assignTo, int priority, String
project) {
super();
this.no = no;
this.raisedBy = raisedBy;
this.assignTo = assignTo;
this.priority = priority;
this.project = project;
}

public int getNo() {


return no;
}

public void setNo(int no) {


this.no = no;
}

public String getRaisedBy() {


return raisedBy;
}

public void setRaisedBy(String raisedBy) {


this.raisedBy = raisedBy;
}

public String getAssignTo() {


return assignTo;
}

public void setAssignTo(String assignTo) {


this.assignTo = assignTo;
}

public int getPriority() {


return priority;
}

public void setPriority(int priority) {


this.priority = priority;
}

public String getProject() {


return project;
}

public void setProject(String project) {


this.project = project;
}

}
public class Main
{

static RRT withHighestPriorityTicket(RRT[] rt, String proj){


RRT[] result = new RRT[0];
RRT temp;
for(int i=0;i<rt.length;i++){
if(rt[i].getProject().equalsIgnoreCase(proj)){
result=Arrays.copyOf(result,result.length+1);
result[result.length-1]=rt[i];
}
}

for(int i=0;i<result.length;i++){
for(int j=i+1;j<result.length;j++){
if(result[i].getPriority()>result[j].getPriority()){
temp=result[i];
result[i]=result[j];
result[j]=temp;
}
}
}

if(result.length!=0){
return result[0];
}else{
return null;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int n =sc.nextInt();
RRT[] rt=new RRT[n];

for(int i=0;i<n;i++){
int no=sc.nextInt();sc.nextLine();
String raisedBy=sc.nextLine();
String assignTo=sc.nextLine();
int priority=sc.nextInt();sc.nextLine();
String project=sc.nextLine();

rt[i] = new RRT(no,raisedBy,assignTo,priority,project);


}

String proj=sc.nextLine();
RRT obj=withHighestPriorityTicket(rt,proj);
if(obj!=null){
System.out.println(obj.getNo());
System.out.println(obj.getRaisedBy());
System.out.println(obj.getAssignTo());
}else{
System.out.println("no");
}

}
}

---------------------------------------------------------------

PRA-7:

---------------------------------------------------------------

PRA-8:

import java.util.*;
class hotel{
int id;
String name;
String date;
int no;
String wifi;
double bill;

public hotel(int id, String name, String date, int no, String wifi, double
bill){
this.id = id;
this.name = name;
this.date = date;
this.no = no;
this.wifi = wifi;
this.bill = bill;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}
public void setName(String name) {
this.name = name;
}

public String getDate() {


return date;
}

public void setDate(String date) {


this.date = date;
}

public int getNo() {


return no;
}

public void setNo(int no) {


this.no = no;
}

public String getWifi() {


return wifi;
}

public void setWifi(String wifi) {


this.wifi = wifi;
}

public double getBill() {


return bill;
}

public void setBill(double bill) {


this.bill = bill;
}

public class Main {

static int roomBooked(hotel[] ht, String month){


int count = 0;

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


if(ht[i].getDate().contains(month)){
count = count + ht[i].getNo();
}
}
if (count == 0){
return 0;

}
else{
return count;
}
}

static hotel wifiOption(hotel[] ht, String facility){


hotel temp;
hotel[] result = new hotel[0];
for(int i = 0; i < ht.length; i++){
if(ht[i].getWifi().equalsIgnoreCase(facility)){
result = Arrays.copyOf(result, result.length + 1);
result[result.length - 1] = ht[i];
}
}
for (int i = 0; i < result.length; i++){
for (int j = 1; j < result.length; j++){
if(result[i].getBill() < result[j].getBill()){
temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
if (result.length != 0){
return result[1];
}
else{
return null;
}
}

public static void main(String[] args){


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
hotel[] ht = new hotel[n];

for (int i = 0; i < n; i++){


int id = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
String date = sc.nextLine();
int no = sc.nextInt();
sc.nextLine();
String wifi = sc.nextLine();
double bill = sc.nextDouble();
sc.nextLine();

ht[i] = new hotel(id, name, date, no, wifi, bill);


}

String month = sc.nextLine();


String facility = sc.nextLine();
int ans = roomBooked(ht,month);
hotel ans2 = wifiOption(ht,facility);
if (ans == 0){
System.out.println("NO");
}
else{
System.out.println(ans);
}
if (ans2 == null){
System.out.println("NO");
}
else{
System.out.println(ans2.getNo());
}
}
}

------------------------------------------------------------

PRA 9/10 - SAME:

------------------------------------------------------------

PRA 11:

import java.util.*;
class player{
int id;
String skill;
String level;
int points;

public player(int id, String skill, String level, int points) {


super();
this.id = id;
this.skill = skill;
this.level = level;
this.points = points;

}
public int getId() {
return id;
}

public void setId(int id) {


this.id = id;
}

public String getSkill() {


return skill;
}

public void setSkill(String skill) {


this.skill = skill;
}

public String getLevel() {


return level;
}

public void setLevel(String level) {


this.level = level;
}

public int getPoints() {


return points;
}

public void setPoints(int points) {


this.points = points;
}

}
public class Main {

static int pointsForSkill(player[] pl, String s){

int sum = 0;

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


if(pl[i].getSkill().equalsIgnoreCase(s)){
sum = sum + pl[i].getPoints();
}

}
if (sum!=0){
return sum;
}
else{
return 0;
}

static player basedOnLevel(player[] pl, String l, String st){

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


if(pl[i].getLevel().equalsIgnoreCase(l) &&
pl[i].getSkill().equalsIgnoreCase(st) && pl[i].getPoints() >= 20){
return pl[i];
}
}
return null;

public static void main(String[] args){


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

player[] pl = new player[n];

for (int i = 0; i < n; i++){


int id = sc.nextInt();
sc.nextLine();
String skill = sc.nextLine();
String level = sc.nextLine();
int points = sc.nextInt();
sc.nextLine();
pl[i] = new player(id,skill,level,points);
}

String s = sc.nextLine();
String l = sc.nextLine();
String st = sc.nextLine();

int ans = pointsForSkill(pl,s);


if (ans == 0){
System.out.println("NO");
}
else{
System.out.println(ans);
}

player ans2 = basedOnLevel(pl,l,st);

if (ans2 == null){
System.out.println("NO");
}
else{
System.out.println(ans2.getId());
}
}
}

-----------------------------------------------------------

PRA double class:

// package ilp;

import java.util.*;

class empolyee {
int id;
String name;
String designation;
double salary;

public empolyee(int id, String name, String designation, double salary) {


super();
this.id = id;
this.name = name;
this.designation = designation;
this.salary = salary;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getDesignation() {


return designation;
}

public void setDesignation(String designation) {


this.designation = designation;
}

public double getSalary() {


return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}

class company{
int cName;
empolyee [] em;
int num;
public company(int cName, empolyee[] em, int num) {
super();
this.cName = cName;
this.em = em;
this.num = num;
}
public int getcName() {
return cName;
}
public void setcName(int cName) {
this.cName = cName;
}
public empolyee[] getEm() {
return em;
}
public void setEm(empolyee[] em) {
this.em = em;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}

public static double getAverageSalary(empolyee [] em){


double count=0;
double sum=0;
double avg;
for(int i=0;i<em.length;i++){
sum=sum+em[i].getSalary();
count++;
}
// avg=sum/count;
// return avg
if(sum==0){
return 0;
}else{
avg=sum/count;
return avg;
}
}

// public static void getMaxSalary(empolyee [] em){


// double max=0.0;
// for(int i=0;i<em.length;i++){
// if(em[i].getSalary()>max){
// max=em[i].getSalary();
// }
// }
// if(max==0){
// System.out.println("no");
// }else{
// System.out.println(max);
// }
// }

public static empolyee[] getEmployeeByDesignation(empolyee[] em, String d){


empolyee[] result=new empolyee[0];
for(int i=0;i<em.length;i++){
if(em[i].getDesignation().equalsIgnoreCase(d)){
result=Arrays.copyOf(result,result.length+1);
result[result.length-1]=em[i];
}
}if(result.length>0){
return result;
}else{
return null;
}

public class Main {


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// String companyName=sc.nextLine();
int n=sc.nextInt();
empolyee[] em=new empolyee[n];
for(int i=0;i<n;i++){
int id=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
String designation=sc.nextLine();
double salary=sc.nextDouble();sc.nextLine();

em[i]=new empolyee(id,name,designation,salary);
}
// company c=new company(companyName,em,n);
// System.out.println(c.getAverageSalary(em));
// System.out.println(" ");
// System.out.println(c.getMaxSalary(em));
String d=sc.nextLine();
empolyee[] obj=company.getEmployeeByDesignation(em,d);
if(obj!=null){
for(int i=0;i<obj.length;i++){
System.out.println("ID :"+obj[i].getId()
+"NAME :"+obj[i].getName()+"designation :"+obj[i].getDesignation());
}
}else{
System.out.println("no");
}

}
}
--------------------------------------------------------------
ArrayList question :(Alosh):

1:

package list;

import java.util.*;

class manfacture{
int id;
String name;
int no;
ArrayList<String> products = new ArrayList<String>();
ArrayList<Integer> prices = new ArrayList<Integer>();

public manfacture(int id, String name, int no, ArrayList<String> products,


ArrayList<Integer> prices) {
super();
this.id = id;
this.name = name;
this.no = no;
this.products = products;
this.prices = prices;
}

public int getId(){


return id;
}

public void setId(int id){


this.id=id;
}

public String getName(){


return name;
}

public void setName(String name){


this.name=name;
}

public int getNo() {


return no;
}

public void setNo(int no) {


this.no = no;
}

public ArrayList<String> getProducts() {


return products;
}

public void setProducts(ArrayList<String> products) {


this.products = products;
}

public ArrayList<Integer> getPrices() {


return prices;
}

public void setPrices(ArrayList<Integer> prices) {


this.prices = prices;
}

}
public class Manfacture {
public double avgPriceOfProductByManfacture(ArrayList<manfacture> mf, int
mid){
int sum=0;
int count=0;
double avg=0;
for(manfacture m: mf){
if(m.getId()==mid){
for(int price:m.getPrices()){
sum=sum+price;
count++;
}
}
}
if(sum>0){
avg=sum/count;
return avg;
}else{
return 0;
}
}

public ArrayList<String> findManfactureByProduct(ArrayList<manfacture> mf,


String productName){
ArrayList<String> result = new ArrayList<>();
for(manfacture m:mf){
for(String j:m.getProducts()){
if(j.equalsIgnoreCase(productName)){
result.add(m.getName());
}
}
}
if(result.size()>0){
return result;
}else{
return null;
}

public static void main(String [] arg){


Scanner sc=new Scanner(System.in);
int n=sc.nextInt();sc.nextLine();
ArrayList<manfacture> mf=new ArrayList<>();
for(int i=0;i<n;i++){
int id=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
int no=sc.nextInt();sc.nextLine();
ArrayList<String> product= new ArrayList<String>();
ArrayList<Integer> price= new ArrayList<Integer>();
for(int j=0;j<no;j++){
String p=sc.nextLine();
product.add(p);
int pr=sc.nextInt();sc.nextLine();
price.add(pr);
}
manfacture m=new manfacture(id,name,no,product,price);
mf.add(m);
}
int mid=sc.nextInt();sc.nextLine();
String productName = sc.nextLine();
Manfacture obj=new Manfacture();
double ans = obj.avgPriceOfProductByManfacture(mf, mid);
// double ans=avgPriceOfProductByManfacture(mf,mid);
if(ans==0){
System.out.println("no id");
}else{
System.out.println(ans);
}

ArrayList<String> ans2=obj.findManfactureByProduct(mf, productName);


if(ans2!=null){
for(int i=0;i<ans2.size();i++){
System.out.println(ans2.get(i));
}
}else{
System.out.println("no product");
}

----------------------------------------------------------
2:
satyam :
package list;
import java.util.*;

class Book{
int bId;
String title;
int publishedYear;

public Book(int bId,String title, int publishedYear){


this.bId=bId;
this.title=title;
this.publishedYear=publishedYear;
}

public int getbId() {


return bId;
}

public void setbId(int bId) {


this.bId = bId;
}

public String getTitle() {


return title;
}
public void setTitle(String title) {
this.title = title;
}

public int getPublishedYear() {


return publishedYear;
}

public void setPublishedYear(int publishedYear) {


this.publishedYear = publishedYear;
}

class Author{
int auId;
String auName;
String nationality;
ArrayList<Book> book=new ArrayList<Book>();

public Author(int auId, String auName, String nationality, ArrayList<Book>


book){
this.auId=auId;
this.auName=auName;
this.nationality=nationality;
this.book=book;
}

public int getAuId() {


return auId;
}

public void setAuId(int auId) {


this.auId = auId;
}

public String getAuName() {


return auName;
}

public void setAuName(String auName) {


this.auName = auName;
}

public String getNationality() {


return nationality;
}

public void setNationality(String nationality) {


this.nationality = nationality;
}

public ArrayList<Book> getBook() {


return book;
}

public void setBook(ArrayList<Book> book) {


this.book = book;
}

}
public class libary {

static ArrayList<Integer> getUniquePublishedYear(ArrayList<Author>


authors,int authorId){
HashSet<Integer> result = new HashSet<>();
for(Author au:authors){
if(au.getAuId()==authorId){
for(Book y:au.getBook()){
result.add(y.getPublishedYear());

}
}
}
ArrayList<Integer> sortedList=new ArrayList<>(result);
Collections.sort(sortedList);
if(sortedList.size()>0){
return sortedList;
}else{
return null;
}

// Collections.sort(result);

}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();sc.nextLine();
ArrayList<Author> authors=new ArrayList<Author>();
for(int i=0;i<n;i++){
int auId=sc.nextInt();sc.nextLine();
String auName=sc.nextLine();
String nationality=sc.nextLine();
int num=sc.nextInt();sc.nextLine();
ArrayList<Book> books=new ArrayList<>();
for(int j=0;j<num;j++){
int bId=sc.nextInt();sc.nextLine();
String title=sc.nextLine();
int publishedYear=sc.nextInt();sc.nextLine();
Book ans=new Book(bId,title,publishedYear);
books.add(ans);

}
Author aut=new Author(auId,auName,nationality,books);

}
libary l=new libary();
int authorId=sc.nextInt();sc.nextLine();
ArrayList<Integer> ans=l.getUniquePublishedYear(authors, authorId);
if(ans!=null){
for(int i=0;i<ans.size();i++){
System.out.println(ans.get(i));
}
}
}
}

--------------------------------------------------------------
yatish:

import java.util.*;

class Book{
int bId;
String title;
int publishedYear;

public Book(int bId,String title, int publishedYear){


this.bId=bId;
this.title=title;
this.publishedYear=publishedYear;
}

public int getbId() {


return bId;
}

public void setbId(int bId) {


this.bId = bId;
}

public String getTitle() {


return title;
}

public void setTitle(String title) {


this.title = title;
}

public int getPublishedYear() {


return publishedYear;
}

public void setPublishedYear(int publishedYear) {


this.publishedYear = publishedYear;
}

class Author{
int auId;
String auName;
String nationality;
ArrayList<Book> book=new ArrayList<Book>();

public Author(int auId, String auName, String nationality, ArrayList<Book>


book){
this.auId=auId;
this.auName=auName;
this.nationality=nationality;
this.book=book;
}

public int getAuId() {


return auId;
}

public void setAuId(int auId) {


this.auId = auId;
}

public String getAuName() {


return auName;
}

public void setAuName(String auName) {


this.auName = auName;
}

public String getNationality() {


return nationality;
}

public void setNationality(String nationality) {


this.nationality = nationality;
}

public ArrayList<Book> getBook() {


return book;
}

public void setBook(ArrayList<Book> book) {


this.book = book;
}

}
public class Main {
public static void main(String[] args){

Scanner sc = new Scanner(System.in);


int n=sc.nextInt();sc.nextLine();

ArrayList<Author> authors=new ArrayList<Author>();


for(int i=0;i<n;i++){

int auId=sc.nextInt();
sc.nextLine();
String auName=sc.nextLine();
String nationality=sc.nextLine();
int x = sc.nextInt();
ArrayList<Book> bk = new ArrayList<>();
for (int j = 0; j < x; j++){

int bid = sc.nextInt();


sc.nextLine();
String title = sc.nextLine();
int year = sc.nextInt();
Book ans = new Book(bid,title,year);
bk.add(ans);

Author obj = new Author(auId, auName, nationality, bk);


authors.add(obj);
}
int aid = sc.nextInt();sc.nextLine();
String nation=sc.nextLine();
ArrayList<Integer> ans2 = getUniquePublishedYear(authors,aid);

if(ans2 != null){
for (int j = 0; j < ans2.size(); j++){
System.out.println(ans2.get(j));
}
}
else{
System.out.println("NO");
}

ArrayList<String> ans3 = getBooksByNationality(authors,nation);


if(ans3!=null){
for (int j = 0; j < ans3.size(); j++){
System.out.println(ans3.get(j));
}
}else{
System.out.println("no");
}

static ArrayList<Integer> getUniquePublishedYear(ArrayList<Author> authors, int


aid){

HashSet<Integer> result = new HashSet<>();


for (Author au: authors){
if (au.getAuId() == aid){
for (Book y: au.getBook()){
result.add(y.getPublishedYear());
}
}
}

ArrayList<Integer> sortedlist= new ArrayList<>(result);


Collections.sort(sortedlist);

if (sortedlist.size() != 0){
return sortedlist;
}
else{
return null;
}

}
static ArrayList<String> getBooksByNationality(ArrayList<Author> authors,
String nation){
ArrayList<String> result= new ArrayList<>();
for (Author au: authors){
if(au.getNationality().equalsIgnoreCase(nation)){
for(Book y:au.getBook()){
result.add(y.getTitle());
}
}
}
if(result.size()!=0){
return result;
}else{
return null;
}

}
----------------------------------------------------------

You might also like