0% found this document useful (0 votes)
4 views

Oppe-2 (24 July) Java (1)

The document outlines a Java programming assignment involving the creation of classes for applicants and students, including exception handling and scholarship eligibility criteria. It specifies the structure and methods required for each class, such as Applicant, AgeOutOfBoundsException, and Student, as well as the main testing class. Additionally, it provides details on expected input and output for test cases, indicating the requirements for successful completion of the assignment.

Uploaded by

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

Oppe-2 (24 July) Java (1)

The document outlines a Java programming assignment involving the creation of classes for applicants and students, including exception handling and scholarship eligibility criteria. It specifies the structure and methods required for each class, such as Applicant, AgeOutOfBoundsException, and Student, as well as the main testing class. Additionally, it provides details on expected input and output for test cases, indicating the requirements for successful completion of the assignment.

Uploaded by

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

July OPPE JAVA Von hnvp been away from exam tab 4 timers) 0

Time left fo* thie assignment 00:29:37 CalC


Course

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 Applicant has/should have the following members:


- Instance variables name and age
Constructor to initifdizc those variables
— Method checkAndGetNane should return the name of the applicant if the age is
within the given limits. Otherwise. it should throw AgeOutOfBoundsException.

• Class AgeOutOfBoundsException that defines a new checked exception.

— Constructor AgeDutOf BcundsEiception (String n) that takes the name of the


applicant as argument. The constructor initializes the error message as ’Age of
<name of the applicant> is outside the limits".

• 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.

Jaya documentation can be accessed at: https://docs.oracle.com/en/java/javase/! 1/docs/api/


Java ExceptionTest.j ava

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>( );

34’ for (int i = 8; J < 4; i++){


35
36
Applicart a
aiist.add(a);
-new Applicant(sc .next() j sc .nextlnt() );

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)

Course Time left for ms assignment 00:29:09 CalC q®

» class igeCutOFEoundsException extends Exceoticn{


25 ’ public AgeDutOF3ourdsException(Strirg nafre){

•ut of 4) Test Run Submit

Test Run Results

Summaiy Runtime Error

Public Test: 0/2 Passed

Download All A

Test Case 1
input Expected Output Actual Output

Sharka 29 Shanka [0.(j02s][warning][os.thread] Failed to start thread - pthread_cr


Nandini 12 Aye of Nandini Is uutside the limits #\n
Meenakshi 19 Meenakshi # There is insufficent memory for the Java Runtime Envlronm
Kalyani 75 Age of Kalyani is outside the limits # Cannot create worker GC thread Out of system resources \i
# Can not save log file, dump to screen An
#\n
July OPPE JAVA Vouhave been away from exam tab 4 time(s)

Time left (o' Ins oosignment 00:28:28 CoIC gg


Course

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:

— Four Instance variables name, scholarshipstatus, avgCGPA, income

- A constructor to initialize these instance variables


- Mutator and accessor methods as needed
-
Overridden method tostring to print the object.
Class streamsTest has / should have the following members:
Method main that accepts the details of four students, calls method getEligibleStream and prints the output list.

- 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.

Java documentation can be accessed at: httos://docs oracle com/en/iava/iavase/11/docs/aoi/ a


Java StreamsTcst.java

Aa + X lo 0 7

i- import java.util. ArrayList;


2 Import java.util.Scanner;
3 import java.util.List;
4 import java.util. stream.*;
5
6- class Student {
7 private String name, scnolarshipStatus;
8 private double ovgCCPA, income;
9
ra¬ public Student (String n, double a. double i){
il narre - n;
12 ovgCQPA - o;
13 incone = i;
14 scholarshipStatus = "not eligible";
15 )
16- pup.ic string toStringOt
17
18 «■ income
-
return nane " : " avgCGPA -
' : "
* " : " * scholarshipSTatus;
19
20’ public double getAvgC6PA(){
21 return avgCGPA;
22
23’
>
pub.ic double getlncone(}{
?4 return income;
25 )
26- public void setscnolarsnlpstaTus(5trLng ss)f
27 schoLarshicStatus = ss;
28
29 }
>
public class Streams^estf
31 //Define method get Fli gibl eStream here
32’ public static Strcan<St jcent> getEligibleStreain(ArrayLi5t<Student> lis){
33 stream<5tjcent> stalls. streair( ) .*llter(n -> n.getA/gCGPA()>7.5) , filter^ -> n .get income (><100000) ;
34 return st;
35 }
36 //Define nethix; updoteSctiolarshlpStatus here
37- public static void uodateScholarshipSta-:u5(List<Student> lis){
38’ s: lis){
39’ .
if(s getAvgCGPA( )>9 .0)f
40
41 3
.
s setScbolarsnipStacus ( "grace-l scholarship");
Java StreamsTest.java

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) £

Course Time left fo* Vus assignment 00:28:10 CalC ££

Summary: All Cases Passed

Public Test- 2/2 Passed


>Ut Of 4)
Download Alli

Test Case 1

Input Expected Output Actual Output

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

Input Expected Output Actual Output

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)

TlmelerHo' Vlis aseignmeit 00:28:01 CalC


Course

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

Class Athlete has/should have the following functionality

- Implements the interface cloneable


- Instance variables
String athletoChostNum to store the athlete chest number

* 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®

Class Athlete has/should have the following functionality:


-- Implements the interface Cloneable
- Instance variables
» String athleteChestNum to store the athlete chest number

* 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

- Constructor to initialize the instance variables


- Mutator methods to update athleteChestNun and the individual events

- Overridden method tostringt)


- Implement method clone 0
Class AthletecioneTest contains the main method that takes the inputs and invokes appropriate methods tc achieve the functionality.

Java documentation can be accessed at https://docs.oracle.com/en/java/javase/1t/docs/api/

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

Press Esc to exit full screen


Aa + X lo 0
i- import java.util. ArrayList;
2 Import java.util.Scanner;
3» class Athlete inplements Cloreable{
4 String athleteCnectNun;
5 Arrey _ist<5tring> events -
new ArroyList<5t-ing>( );
6
7» public Athlete(){
8
9
athleteChcstNum -
"600";
events. adoC Relay");
18 events. add(’Inc Evt 1’);
11 events .adcflnc Evt 2’);
12
13 // Add mutator methods for athleteChestIJum, individual evtl, individual evt2
14» public void setAthleteCnestNjn( String i){
15 .
this othleteChestlJum-ij
16 )
17
18’ public void setlndividualEvtlfScring s){
19 tnis. events. renove(l);
28 this. events. add (1,s) ;
21 }
22
23» public void set!ndividualE'/t2 (String s){
?4
25
. .
this event s remove( 2 );
this. events. add (2, s);
26 )
27
28 // Implenent Bethod clone( )
29» puDlic Athlete clone() throws ClaneNotSupoor-.edExc.eption{
30 Athlete a=(Athlete)super.clone( );
31 ArrayList<String> ei=new Ar''ayLict<St/'ing>( );
32 ei.add(0j"Relay");
33 ei.adc(l, tris. events. get(i));
34 ei.adC<2,rhis.events.get(2));
35 a.everts-ei;
36 return a;
37 }
38’ public String toString(){
39 return athleteChesthun-*-” ; '^events;
40
41
Java AthleteCloneT©st 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

Test Run Results

Summary Note AthleteCloneTest java uses unchecked or unsafe operations Note: Recompile with -Xlint unchecked for details

Public Test- 0/0 Passed

LownloadAllA

Test Case 1

Input Expected Output Actual Output

2854 400m 8O0m 2854: [Relay. 400m, 8 OOrr]


3251 100rn 3251: [Relay, 400m, 10Orr]

Test Case 2

Input Expected Output Actual Output

101 ShotPutBOOm 101. [Relay, SholPul. 800rn]


152 LongJump 152: [Relay, SnotPut LongJump)
July OPPE JAVA Youhave beer eway from exam tab 4 tlme(s)
Time left for this assignment. 00:27:32 CalC
Course

Last submitted on (graded): 24 Jul 2022 08:57 1ST : 2/2 Private tests passed

Last test run on (not graded): 24 Jul 2022 08:57 1ST


>Ut Of 4)

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.

Define a generic class Rectangle with the following members.


- Instance variables length and breadth
- Constructor to initialize the instance variables
-Method area ( ) that returns the area of a rectangle object
- Method compareArea ( ) that returns the larger area among that of rl and r2.

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.

Java documentation can be accessed at: httpsy/docs.oracle.conven/java/javase/11/docs/api/

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

Press Esc to exit full screen


Aa + O D

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?

Test Run Results


ut of 4)
Summary: All Cases Passed

Public Test- 2/2 Passed

Download Alli

Test Case 1

Input Expected Output Actual Output

1011 156 0 156.0\n


1212

Test Case 2

Input Expected Output Actual Output

56 78 4368.0 4368.0\n
V

You might also like