Oppe-2 (24 July) Java (1)
Oppe-2 (24 July) Java (1)
Complete the Java program that, given a list of four applicants for a job. raises an exception
if any applicant’s age is not within the prescribed age limit. For each applicant a. if a’s age
mt of 4) is > 18 and 30. then the program should print the name of a, otherwise it should print a
custom message.
• Class ExceptionTest has the main method. It takes the name and age of four ap¬
plicants as input, and invokes the method checkAndCetMama in class Applicant tn
produce the output as specified.
Aa + O 0 7
i- import java,util.Scanner;
2 import Java.util. ArrayList;
3
4’ class Applicant-;
5 String name;
6 int age;
7
8’ Applicant (String n, int a)(
9 name = n;
10 age = a:
11 }
12’ public String cneckAnoGctNomeO throws AgeQjt0fDound3Exccption-(
13 //Complete definition of nethod checkAnduetlJaue
14’ if(this.age>=18 £& this . age<=30){
15 return this.none;
16 )
17’ else{
18 .
AgeCutCf BourdsException e-new AgeOutOfBcundsExcepti:>n(thi£ nane);
19 throw e;
20 }
21
22
>
23 //Define class AgetAitOfBoundsException
24’ class AgeCutOf BourdsException extends Exception{
25’ public AgeOut3fBounCsExccption( String nam“)(
26 super ("Age of "-mame*" is outside the Units");
27
28 }
>
29’ public class ExceptionTestf
30’ public static void main(String[] args){
31 Scanner sc = new Scanner (System. in);
32
33
ArrayList<Applicont> aList -
new ArrayList<Applicant>( );
37 }
38’ for (Applicant a: aList){
39’ try(
40 String nane = a.checkAndGetMameQ;
41 Svstem.nut .nrintlnf name) :
Java Exception!est.j ava
Aa + O D
y name = n;
te age = a;
11 )
12’ puoiic string cnecKAncGetName() throws AgeOutDfBounasException-(
13
14- if(this.age>-18 && tnis . age<-30) £
15 return this, rane;
16 }
17» else£
is AgeCutOfBoundsException e=new AgeOutOfBcundsExcept ion( this. name) j
19 throw e;
20
21
>)
22 )
23 //Define class AgeCutOfEoundsExceptian
24- class AgeCutOf Bounds Exception extends Excepticn£
25’ public AgeOutDfBoundsException(String name)£
26 supe"("Age of "-mame*" is outside the limits");
27
28 }
>
29’ puoiic class Exceptionrest£
30’ public static void main(String[] arg$){
31
32
Scanner sc -
new Scanner (System. in);
ArrayList<Applicant> aList = new ArrayLLst<Appllcant>O;
33
34- for (int i = 0; i < 4; i**)£
35 Applicant a -
new Applicant (sc .next (), sc .nextlnt() );
36 atist.aGd(a);
37 }
38-
39’
for (Applicant a: aList)
try£
<
40 String nane = a.checkAndGet'lameO;
41 System. out.print In(name);
42 1
43’ catch (AgeOutOfBourdsException ce){
44 System. out .print ln(oe. getRecsage' ) ) ;
45 1
46 1
47 sc. closed;
48
49
July OPPE JAVA You have been away from exam tab 4 tlmefs)
Download All A
Test Case 1
input Expected Output Actual Output
Complete the Java program that, given a list of students, prints the list of students who are eligible for a scholarship. These include the students with an
average CGPA > 7.5 and whose annual family income is less than Rs.1,00,000. The program should also update the scholarship status of eligible students as
•ut of 4)
"grade-1 scholarship" if their average CGPA is > 0 0: otherwise, the scholarship status should be updated as "grade-2 scholarship"
Class student has the following members:
- Method gotEligibloStroam that accepts a list of students, filters the students eligible for scholarship, and returns a stream of eligible students.
- Method updatescholarshipstatus that accepts the list of eligible students and update their scholarship status.
Aa + X lo 0 7
Aa O 0 7
24 return income;
25
26’
>
public void setScholarsnipStatus (String 55)(
27 scnoiarsnlpstatus = ss;
?8
29 }
>
30’ public class StreamsTestf
31 //Define method getEligibleStreair here
32’ public static Str«M<Student> getEligibleStream( Array List' Student > lis){
33 . .
StreunK5tudent> st-lis streom() f ilter(n -> n.getA/gCGPA()>7.5) .filter(n -> n .getlncome()<100000);
34 return st;
35 }
36 //Define methofl updateScholarchipStatus here
37’ public static void Lpaatescnoia"snip5tatus(Llst<student> iis)(
38’ for (Student s: lis){
39’ . .
if ( s getAvgCGPA( ) >9 0) {
40 .
s setScholarshipStatus ( "grade-1 scholarship");
41 }
42 elce{
43
’ s. 5etSchoLarship5tatu5("graae-2 scholarship");
44
45 }
46 }
47’ public static void naln(5trlng[] args)(
48 Scanner sc = new Scanner(Systen.in);
49 - new ArrayLirt<Student>();
ArrayList 'Student > sList
50 Student s;
51’ for (int 1 = 0; i < 4; i+-){
52 s = new Stucient(sc. next(), sc.nextDoubleO, sc.nextDoubleO);
53 sList.add(s);
54
55 List<Student> eList =
56 getEligibleStrean(sList) .collect (Collectors. touist());
57 updateScholar shipStatus (eList);
58
59 for 'Student es : eList)
60 5y3ten.out.println(es);
61
62 sc.closeO;
63 }
64 $
July OPPE JAVA Vou hsve been away from exam tab 4 tlme(s) £
Test Case 1
geet 9.5 80000 geet : 9.5 : 80000.0 : grade-1 scholarship geet :9.S 80000.0 :grade 1 scholarships
preet 8 90000 preet 8 0 • 90000 0 •grade-2 scholarship preet 8 0 90000 0 • orsde-2 scholarship^
ravi 7 80000
kuinar 8.5 200000
Tost Case 2
anuska 7 9 70000 anuska 79 70000 0 crade-2 scholarship anuska 7 9- 70000 0 : orade-2 scholarship^
ram 9.8 250000 qeetha : 9.1 90O0U.O : qrade-1 scholarship qeetha 9.1 : 90000.0 qrade-1 scholarships
geetha 91 90000 riya . 8.5 . 90000 0 . grade-2 scholarship riya . 8.5 . 90000.0 . grade-2 scholarshlp\n
riya 8.5 90000
July OPPE JAVA Vou have been away from eram tab 4 timers)
In an athletic meet, the athletes from each school should register for 1 relay event and 2 individual events Complete the Java program that does the
lilt Of 4) following: create a dummy athlete object, create object al of type Athlete by cloning the dummy athlete object, create another object a2 of type Athlete
by cloning al, and then update the chest number, and individual events of al and a2
* Arrayi,ist<string> events whose first element is the relay event which is common for all the athletes, the
second element is the first individual event and the third element is the second individual event
- Constructor to initialize the instance vahables
- Mutator methods to update athletechestNun and the individual events
- Overridden method tostrlngO
- Implement method clone ( )
f'laevc » 4-1-.1
— 4- oonfaino 4 Iha —-.4 nnnlkmrl In al fabar* fl-ia ram i4e» anrj imrnLao anrrAnrlata mattanrl e> 4m a /ala in* m fka fl > r»z»4ianaI«4»r
July OPPE JAVA You have bean away from exam tab 4 timers)
Course
Time left for th s assignment 00:27:55 CalC q®
* ArrayList<string> events whose first element is the relay event which is common for all the athletes, the
second element is the first individual event and the third element is the second individual event
This assignment has public test cases. Please dick on Test Run' button to see the status of public test cases. Assignment will be evaluated only after submrtti
using 'Submit' button below. If you only test run the program, your assignment will not be graded and you will not see your score after the deadline.
Java AthleteCloneTest java
Aa O 0 7
21 )
22
23’ public void setIndividualEvt2(String s){
24 tnis.events.remcve(2);
25 this. events. add(2,s);
26 }
27
28 // Implenent net hod clone( )
29’ public Athlete clone() throws CLoneNotSupocr-:edException{
30 Athlete a- (Athlete) super, done ( );
31 ArrayList<String> ei=new ArrayList<St-ing>( );
32 ei .add(0, 'Relay’ );
33 ei .add(l, this .events .get(l));
34 el.aaoGjtnis.events .get(z));
35 a.events«ei;
36 returr a;
37
38’ public String toStringOf
39 return athleteChesthun*" : '*events;
40 }
41 }
42’ public class AthleteCloreTest{
43’ public static void main(String[] args){
44 Scanner sc = new Scanner (System. in);
45 Athlete dunnyAthlete = new AtbleteO;
46’ try{
47 Athlete al -
(Atnlete)dumnyAthlete.clone()i
48 . .
al set AtnieteChestNum( sc next; ) );
49 .
al .setIrc1ividualE\tl(sc next () );
50 al.setIrdividualEvt2(sc.next());
51
52 Athlete a2 = (Atnlete) al. clone () ;
53 .
a2 £etAthleteChestNum( sc . next; ) ) j
54 . .
a2 setIndivicualEvt2(sc next () );
55 .
5 stem. cut println( al+”\n"*a2) ;
56
57’
>cotch(CloretlotSupportedException e){
58
59 sc.doseO;
60
611 $
>
July OPPE JAVA Youhave been eway from exam tab 4 tlme(s)
Time left for thia assignment 00:27:41 CoIC
Summary Note AthleteCloneTest java uses unchecked or unsafe operations Note: Recompile with -Xlint unchecked for details
LownloadAllA
Test Case 1
Test Case 2
Last submitted on (graded): 24 Jul 2022 08:57 1ST : 2/2 Private tests passed
You are given two integers as input to form an object (rl) of type Rectangle and two double values as input to form an object r2 of type Rectangle.
Complete the Java code to print the larger area among the areas of xl and x2.
Class Test has method main ( ) , and takes two integers and two double values as input to create two objects of Rectangle type. It then invokes the
necessary methods and prints iarge_area.
This assignment has public test cases. Pleese click on "Test Run" button to see the status of public test cases. Assignment will be evaluated only after submitti
Java Test.java
import java.util.*;
2’ class Rectangle<T extends NunDer>{
3 private 7 length;
6 private I breadth;
5’ public Rectcngle(T len, T bre){
6 lengtn = len;
7 breadth = bre;
8 )
9 //Define metnoc puoiic double areaQ nere
10’ public <T extends Number) double area(){
11 double areal;
12 oreol - tnis .length. doubleValue () * .
this breadth .doubleVolue( );
13 return areal;
14 }
15 //Define method compareArea( ) here
16’ public <T extends Number) Double compareA''ea(Rectangie<T> x){
17’ if(thjs.area()>x.area()){
18 return this.area();
19 1
20’ else{
21 return x.areaf);
22
23 }
24 }
25’ public class Test (
26’ public static void main(string[] args) (
27 Scanner sc = new Scanner(System.in);
28 Rectangle<Integer> rl = new RectangleO [sc .nextlnt() sc. ne<tlnt());
29 Rectangle<Double> r2 - .
new Rectongle<>(sc.nextDouble(), sc nextDouble( ) ) ;
30 double large area = rl.ccmpareA"ea(r2) ;
31 Sy stem.out .prir.tln( large_area);
32
a 1
July OPPE JAVA Youhave been away from exam tab 4 tlme(s) 0
Course
Time left for this assignment 00:77:21 CalC 9?
Download Alli
Test Case 1
Test Case 2
56 78 4368.0 4368.0\n
V