Jav
Jav
Jav
class rating
//Create all the required classes here abstract class Player{ private String
firstName; private String lastName; public Player(String firstName,String lastName)
{ this.firstName=firstName; this.lastName=lastName; } public String getName()
{ return this.firstName+" "+this.lastName; } public abstract int getRating(); }
class CricketPlayer extends Player{ private double averageRuns; public
CricketPlayer(String firstName,String lastName,double averageRuns)
{ super(firstName,lastName); this.averageRuns=averageRuns; } public int getRating()
{ int i=1,j=0; if(this.averageRuns>55 && i==1) { i=0; j=7; } if(this.averageRuns>50
&& i==1) { i=0; j=6; } if(this.averageRuns>40 && i==1) { i=0; j=5; }
if(this.averageRuns>30 && i==1) { i=0; j=3; } if(this.averageRuns>20 && i==1)
{ i=0; j=2; } if(this.averageRuns<=20 && i==1) { i=0; j=1; } return j; } } class
FootballPlayer extends Player{ private int goal; public FootballPlayer(String
firstName,String lastName,int goal) { super(firstName,lastName); this.goal=goal; }
public int getRating() { int i=1,j=0; if(this.goal>20 && i==1) { i=0; j=5; }
if(this.goal>15 && i==1) { i=0; j=4; } if(this.goal>10 && i==1) { i=0; j=3; }
if(this.goal>5 && i==1) { i=0; j=2; } if(this.goal<=5 && i==1) { i=0; j=1; } return
j; } } public class Source{ public static void main(String[] args){ //CODE HERE
CricketPlayer cp=new CricketPlayer("abc","abc",0); cp.getName(); cp.getRating();
FootballPlayer fp = new FootballPlayer("abc","abc",0); fp.getName();
fp.getRating(); } }
coustemer information:
import java.util.Scanner; class SimpleDate { private int day; private int month;
private int year; public SimpleDate(){} public SimpleDate(int day, int month, int
year) { this.day=day; this.month=month; this.year= year; } public int getDay()
{ return day; } public int getMonth() { return month; } public int getYear()
{ return year; } public void setDate(int day,int month,int year) { this.day=day;
this.month=month; this.year=year; } @Override public String toString() { return
day+"/"+month+"/"+year; } publice sd) { if(sd.year<2000) { return false; }
if(sd.month<1 || sd.month>12) { return false; } if(sd.month==2 && (sd.day>28 ||
sd.day <1)) { return false; } if((sd.month==4 ||sd.month==6 ||sd.month== 9 ||
sd.month== 11) && (sd.day>30 || sd.day<1)) { return false; } if((sd.month==1 ||
sd.month==3 || sd.month==5 || sd.month==7 || sd.month==8 || sd.month==10 ||
sd.month==12) && (day>31 && day<1)) { return false; } return true; } }
Account Details:
import java.util.Scanner; class Account{ static int counter=0; int accountNo;
double balance; String accountType; Account(double balance, String accountType)
{ accountNo = ++counter; this.balance = balance; this.accountType = accountType; }
public void depositAmount(double amount){ this.balance = this.balance + amount; }
public void printAccountDetails(){ System.out.println("[Acct No : "+accountNo+",
Type : "+accountType+", Balance: "+balance+"]"); } public double getBalance()
{ return balance; } } class Source{ public static void main(String args[]){ Scanner
s = new Scanner(System.in); double balance,amount,balance2,amount2; String
acctType,acctType2; balance = s.nextDouble(); acctType = s.next(); amount =
s.nextDouble(); //System.out.println(balance+acctType+amount); s.nextLine();
balance2 = s.nextDouble(); acctType2 = s.next(); amount2 = s.nextDouble(); Account
a1 = new Account(balance,acctType); a1.printAccountDetails();
a1.depositAmount(amount); System.out.println("New Balance : "+a1.getBalance());
Account a2 = new Account(balance2,acctType2); a2.printAccountDetails();
a2.depositAmount(amount2); System.out.println("New Balance :
"+a2.getBalance()); } }
Mergeandsort exercise:
import java.util.*; public class Source { public static void main(String[] args)
{ Scanner sc = new Scanner(Ssytem.in); List<Integer> al1 = new
ArrayList<Integer>(5); List<Integer> al2 = new ArrayList<Integer>(5); for(int
i=0;i<5;i++) { al1.add(sc.nextInt()); } for(int i=0;i<5;i++)
{ al2.add(sc.nextInt()); } System.out.println(mergeAndSort(al1,al2)); } public
static List<Integer> mergeAndSort(List<Integer>al1,List<Integer>al2)
{ if((al1.size()!=5) || (al2.size()!=5)) { throw new Illegal Argument Exception ();
} else { List<Integer> ans = new ArrayList<Integer>(10); ans.add(al1);
ans.add(al2); Collections.Sort(ans); List<Integer> bns = new ArrayList<Integer>();
bns.add(ans.get(2)); bns.add(ans.get(6)); bns.add(ans.get(8)); return bns; } } }
marks :
import java.util.Scanner; public class Source { public static void main (String[]
args){ Scanner sc = new Scanner(System.in); double d,averageMarks; Double a =
sc.nextDouble(); Double b = sc.nextDouble(); Double c = sc.nextDouble(); d = a+b+c;
averageMarks = d/3; if(a>100){ System.out.println("INVALIDMARKS"); } else if
(b>100){ System.out.println("INVALIDMARKS"); } else if (c>100)
{ System.out.println("INVALIDMARKS"); } else if (a<0)
{ System.out.println("INVALIDMARKS"); } else if (b<0)
{ System.out.println("INVALIDMARKS"); } else if (c<0)
{ System.out.println("INVALIDMARKS"); } else{ if(averageMarks>=70)
{ System.out.println("DISTINCTION"); } else if (averageMarks>=60)
{ System.out.println("FIRST"); } else if (averageMarks>=50)
{ System.out.println("SECOND"); } else if (averageMarks>=40)
{ System.out.println("THIRD"); } else{ System.out.println("FAIL"); } } } }
sort list:
public static void main(String[] args) { //CODE HERE Scanner s = new
Scanner(System.in); String strin = s.nextLine(); strin =
strin.replaceAll("\\s",""); strin = strin.trim(); strin2lis(strin); List<Integer>
firstList = strin2lis(strin); strin = s.nextLine(); strin =
strin.replaceAll("\\s",""); strin = strin.trim(); strin2lis(strin); List<Integer>
secondList = strin2lis(strin); List<Integer> sumList =
getSumOfListElements(firstList, secondList); System.out.println(sumList); }
ARRA1:*************
COUSTEMER INFORMATION:
import java.lang.*;
import java.util.*;
class SimpleDate {
int day;
int month;
int year;
SimpleDate(int day,int month,int year) {
this.day=day; this.month=month;
this.year=year;
}
int getDay() {
return this.day;
}
int getMonth() {
return this.month;
}
int getYear() {
return this.year;
}
void setDate(int day,int month,int year) {
this.day=day;
this.month=month;
this.year=year;
}
public String toString() {
return String.format(day+"/"+month+"/"+year);
}
boolean validateDate() {
if(year<2000)
return false;
else if(month <1 || month>12)
return false;
else if(month==2) {
if(day>28 || day<1) return false; else return
false;
}
else if(month==1 || month==3 || month==5 ||
month==7 || month==9 || month==11) {
if(day<1 || day>31)
return false;
else return true;
}
else if(month==4 || month==6 || month==8 ||
month==10 || month==12 ) {
if(day<1 || day>30)
return false;
else return true;
}
else return true;
}
}
class Address {
String area;
String city;
Address(String area,String city) {
this.area=area; this.city=city;
}
String getArea() {
return this.area;
}
String getCity() {
return this.city;
}
void setArea(String area) {
this.area=area;
}
void setCity(String city) {
this.city=city;
}
public String toString() {
return String.format(area+", "+city);
}
class Customer {
int custId;
String name;
Address address;
SimpleDate registrationDate;
Customer(int custId, String name, Address address,SimpleDate
registrationDate) {
this.custId=custId;
this.name=name;
this.address=address;
this.registrationDate=registrationDate;
if(registrationDate.validateDate()==false)
registrationDate=null;
}
int getCustId() {
return this.custId;
}
String getName() {
return this.name;
}
Address getAddress() {
return this.address;
}
SimpleDate getRegistrationDate() {
return this.registrationDate;
}
void setCustId(int custId) {
this.custId=custId;
}
void setName(String name) {
this.name=name;
}
void setAddress(int name) {
this.address=address;
}
void setRegistrationDate(int name) {
this.registrationDate=registrationDate;
if(registrationDate.validateDate()==false)
registrationDate=null;
}
public String toString() {
if(address ==null) {
return String.format("Id : "+custId+", Name :
"+name+"\n Address : Unkown \n Registered on : "+registrationDate);
}
else if(registrationDate==null) {
return String.format("Id : "+custId+", Name :
"+name+"\n Address : "+address+"\n Registered on : Unkown");
}
else return String.format("Id : "+custId+", Name :
"+name+"\n Address : "+address+"\n Registered on : "+registrationDate);
}
class Source{
public static void main(String args[]) {
int custId,day,month,year;
String name,area,city;
Scanner sc=new Scanner(System.in);
custId=sc.nextInt();
name=sc.next();
area=sc.next();
city=sc.next();
day=sc.nextInt();
month=sc.nextInt();
year=sc.nextInt();
SimpleDate a=new SimpleDate(day,month,year);
Address ad=new Address(area,city);
Customer c=new Customer(custId,name,ad,a);
System.out.println(c.toString());
}
}
---------------------
day of date:
import statements here import java.util.*; import java.text.ParseException; import
java.text.SimpleDateFormat; import java.util.Date; public class Source { public
static void main(String[] args) throws ParseException{ //CODE HERE Scanner sc=new
Scanner(System.in); String input=sc.nextLine();
System.out.println(getDayOfDate(input)); } public static String getDayOfDate(String
date) throws ParseException{ //CODE HERE Date dt=new Date(); SimpleDateFormat
sdf=new SimpleDateFormat("dd/m/yyyy"); dt=sdf.parse(date); String day=new
SimpleDateFormat("EEEEE").format(dt); return day; } }
----------------
age calculation
import java.util.*; public class Source{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int a[] =
AgeCalculator.calculateAge(s); if(a==null){ System.out.println("Invalid date of
birth"); } else{ if(a[0]!=0){ System.out.print("Years : "+a[0]); } if(a[0]!
=0&&a[1]!=0)System.out.print(","); if(a[1]!=0){ System.out.print(" Months :
"+a[1]); } } } } class AgeCalculator{ public static int[] calculateAge(String d)
{ String []str = d.split("/"); if(str.length!=3)return null; int day =
Integer.parseInt(str[0]); int mon = Integer.parseInt(str[1]); int yr =
Integer.parseInt(str[2]); if(mon <1 || mon >12)return null; int arr[] =
{31,28,31,30,31,30,31,31,30,31,30,31}; if(day <1 || day> arr[mon-1])return null;
if(yr>2019)return null; if(yr==2019&&mon>=4&&day>=1)return null; yr = 2019 - yr;
if(mon<=4)mon = 4-mon; else{ mon = 12 - mon; mon+=4; yr--; } int a[] = new int[2];
a[0] = yr; a[1] = mon; return a; } }
---------------
Sum of list elements:
---------------
search doctor:
import java.util.*;
class Doctor implements Comparable
{ private String name; private String speciality;
private int experience;
Doctor(String name,String speciality,int experience)
{ this.name=name; this.speciality=speciality;
this.experience=experience;}
public String getName(){ return name; }
public String getSpec(){return speciality; }
public int getExp(){return experience;}
@Override public String toString(){ StringBuilder s = new StringBuilder();
s.append(name+" ");
s.append(speciality+" ");
s.append(experience+" ");
return s.toString(); }
@Override public int compareTo(Object o)
{ throw new UnsupportedOperationException("Not supported yet.");
}}
class ChainComp implements Comparator<Doctor>{ private List<Comparator<Doctor>>
listComparators;
@SafeVarargs public ChainComp(Comparator<Doctor>... comparators)
{ this.listComparators = Arrays.asList(comparators);}
@Override public int compare(Doctor doc1, Doctor doc2)
{ for (Comparator<Doctor> comparator : listComparators)
{ int result = comparator.compare(doc1, doc2);
if (result != 0) {return result; } }return 0;}}
class DoctorName implements Comparator<Doctor>{ @Override public int
compare(Doctor doc1,Doctor doc2)
{ return doc1.getName().compareTo(doc2.getName());}}
class DoctorSpec implements Comparator<Doctor>{ @Override public int
compare(Doctor doc1,Doctor doc2)
{ return doc1.getSpec().compareTo(doc2.getSpec());}}
class DoctorExp implements Comparator<Doctor>{ @Override public int compare(Doctor
doc1,Doctor doc2)
{ return doc2.getExp()-doc1.getExp();}} class DoctorService { private
List<Doctor> doctorsRepository;
public DoctorService(List<Doctor> doctorsRepository)
{ this.doctorsRepository = doctorsRepository;}
public List<Doctor> getExperiencedDoctors(int yoe)
{ Collections.sort(doctorsRepository,new ChainComp(new DoctorSpec(),new
DoctorExp()));
int check=yoe;
List<Doctor> expd = new ArrayList<>();
for(Doctor d : doctorsRepository){ if(d.getExp() >= check){ expd.add(d);}}
return expd;
}
public Set<Doctor> getSpecialityDoctor(String spec)
{ Collections.sort(doctorsRepository,new ChainComp(new DoctorName()));
String check = spec; Set<Doctor> specd = new HashSet<>();
for(Doctor d: doctorsRepository)
{ if(d.getSpec().equalsIgnoreCase(check))
{ specd.add(d);}} return specd;}} public class Source {
private static String doctorsData;
static { StringBuilder doctors = new StringBuilder();
doctors.append("Amy-Pediatrics-16;");
doctors.append("John-Dermatology-10;");
doctors.append("David-Dermatology-15;");
doctors.append("Bob-Pediatrics-6;");
doctors.append("Cathy-Dermatology-5;");
doctors.append("Mavis-Pediatrics-11;");
doctors.append("Robin-Pediatrics-7;");
doctors.append("Minty-Dermatology-9;");
doctors.append("Jim-Cardiology-25;");
doctorsData = doctors.toString(); }
public static void main(String[] args)
{ List<Doctor> l1 = new ArrayList<>();
String name = null,speciality = null;
int experience = 0;
String data = doctorsData;
for (String word : data.split(";"))
{ int count = 0; for (String part : word.split("-"))
{ switch (count)
{ case 0: name = part;count++;break;
case 1: speciality = part;count++; break;
case 2: experience = Integer.parseInt(part); String exp =
String.valueOf(experience); count++;break;
default: break;}} l1.add(new Doctor(name,speciality,experience));}
DoctorService d = new DoctorService(l1);
Scanner s =new Scanner(System.in);
List<Doctor> ret = new ArrayList<>();
Set<Doctor> ret2 = new HashSet<>();
int one, yoe;String spec;
one = s.nextInt();
if(one==1){yoe = s.nextInt();ret=d.getExperiencedDoctors(yoe);
if(ret.isEmpty()){ System.out.println("No Doctors Found");}
for(Doctor d1:ret){System.out.println(d1); }}
else if(one==2){spec = s.next();
ret2=d.getSpecialityDoctor(spec.toLowerCase());
if(ret2.isEmpty()) {System.out.println("No Doctors Found");}
for(Doctor d1:ret2) { System.out.println(d1);} }
else{System.out.print("Inavalid Choice"); } } }
-------------
sort list:
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Source {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in); //creating and initializing two
List<Integer> al1=new ArrayList<>(5);
List<Integer> al2=new ArrayList<>(5); //Taking numbers from STDIN in
ArrayList
for(int i=0;i<5;i++)
{
al1.add(sc.nextInt());
}
for(int j=0;j<5;j++)
{
al2.add(sc.nextInt());
}
//output to STDOUT
System.out.println( mergeAndSort(al1,al2));
}
//Static Method for Merge and sort the list
public static List<Integer>
mergeAndSort (List<Integer> al1, List<Integer> al2)
{
if (al1.size()!=5 ||al2.size()!=5)
{
throw new IllegalArgumentException();
}
else
{
//creating new List for resulting merged list
List<Integer> sorted_list=new ArrayList<>(10);
sorted_list.addAll(al1);
sorted_list.addAll(al2); //Sorting numbers in list
Collections.sort(sorted_list); //creating new List for
storing 2nd,6th and 8th index elements
List<Integer> res=new ArrayList<>();
res.add(sorted_list.get(2));
res.add(sorted_list.get(6));
res.add(sorted_list.get(8)); //returning list as
return res;
}
}