-
Notifications
You must be signed in to change notification settings - Fork 87
/
FindMaxMarks_and_specific_Student.java
112 lines (97 loc) · 2.66 KB
/
FindMaxMarks_and_specific_Student.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.Scanner;
class Student {
private int id;
private String name;
private double marks;
private int age;
public Student(int id, String name, double marks, int age){
this.id=id;
this.name=name;
this.marks=marks;
this.age=age;
}
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 double getMarks(){
return marks;
}
public void setMarks(double marks){
this.marks=marks;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
};
class Solution{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();sc.nextLine();
Student[] student= new Student[n];
for(int i=0;i<4;i++)
{
int id=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
double marks = sc.nextDouble();sc.nextLine();
int age=sc.nextInt();sc.nextLine();
student[i]= new Student(id, name, marks, age);
}
double marks=sc.nextDouble();sc.nextLine();
Student std = searchStudentbyMarks(student, marks);
if(std != null){
System.out.println("id - "+std.getId());
System.out.println("name - "+std.getName());
System.out.println("marks - "+std.getMarks());
System.out.println("age - "+std.getAge());
}else{
System.out.println("NO Student found with mentioned marks");
}
Student maxage_student = findStudentWithMaximumAge(student);
System.out.println("id - "+maxage_student.getId());
System.out.println("name - "+maxage_student.getName());
System.out.println("marks - "+maxage_student.getMarks());
System.out.println("age - "+maxage_student.getAge());
}
public static Student searchStudentbyMarks(Student[] student, double marks)
{
if(student!=null)
{
for(int i=0;i<4;i++)
{
if(marks == student[i].getMarks()){
return student[i];
}
}
}
return null;
}
public static Student findStudentWithMaximumAge(Student[] student)
{
int max_age = 0;
int leastindex = -1;
if(student!=null)
{
for(int i=0;i<4;i++)
{
if(student[i].getAge() > max_age){
max_age = student[i].getAge();
leastindex = i;
}
}
return student[leastindex];
}
return null;
}
}