Write a program in Java, define a STUDENT class with Roll No, Name and Marks of 3 subjects. Define one function to find the best two subject marks for student.
Hint:-
-get user input from user of student name, roll no and mark of 3 subjects
-give best two subject mark along with student information
import java.util.*;
class STUDENT {
public String RollNo;
private String Name;
private int Marks[];
private int counter;
public STUDENT(String RollNo, String Name) {
this.RollNo = RollNo;
this.Name = Name;
this.Marks = new int[3];
this.counter = 0;
}
public void addMark(int mark) {
Marks[counter] = mark;
counter++;
}
public void display() {
Arrays.sort(Marks);
System.out.println("RollNo: " + RollNo);
System.out.println("Name: " + Name);
System.out.println("The best two subject marks for student: " + Marks[1] + " " + Marks[2]);
}
}
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a RollNo: ");
String RollNo = in.nextLine();
System.out.print("Enter a Name: ");
String Name = in.nextLine();
STUDENT s = new STUDENT(RollNo, Name);
for (int i = 0; i < 3; i++) {
System.out.print("Enter a mark: ");
int mark = in.nextInt();
s.addMark(mark);
}
s.display();
in.close();
}
}
Comments
Leave a comment