7.
Program to create a student class with following attributes :
Enrollment no,name,marks of subject 1, subject2 , subject 3, total marks. Total of the three marks
be calculated only when the student passes in all athe three subject. The pass marks for each
subject is 50. If candidate fails in any one subject his total marks must be declared as zero(0), using
this condition write a constructor for this class . write separate functions for accepting & displaying
students details. In the main method create an array of three students objects and display the
details.
import java.util.Scanner;
class Student {
String studentName;
String registrationNumber;
int sub1, sub2, sub3, total;
Student(String name, String regno, int m1, int m2, int m3) {
studentName = name;
registrationNumber = regno;
sub1 = m1;
sub2 = m2;
sub3 = m3;
void display() {
if (sub1 < 50 || sub2 < 50 || sub3 < 50) {
total = 0;
} else {
total = sub1 + sub2 + sub3;
System.out.println("Name: " + studentName);
System.out.println("Registration Number: " + registrationNumber);
System.out.println("Subject One Marks: " + sub1);
System.out.println("Subject Two Marks: " + sub2);
System.out.println("Subject Three Marks: " + sub3);
System.out.println("Total Marks: " + total);
}
}
class StudentDemo {
public static void main(String[] args) {
String name, regno;
int i, m1, m2, m3;
Scanner in = new Scanner(System.in);
Student[] students = new Student[3];
System.out.println("Enter the details of 3 students");
for (i = 0; i < 3; i++) {
System.out.println("Enter name, regno, m1, m2, m3 of student " + (i + 1));
// Read name using in.nextLine()
name = in.nextLine();
// Consume the leftover newline character (if any)
regno = in.nextLine(); // Read registration number
m1 = in.nextInt(); // Read marks 1
in.nextLine();
m2 = in.nextInt(); // Read marks 2
in.nextLine();
m3 = in.nextInt(); // Read marks 3
in.nextLine();
students[i] = new Student(name, regno, m1, m2, m3);
System.out.println("The Student Details are:");
for (i = 0; i < 3; i++) {
System.out.println("Information of Student " + (i + 1));
students[i].display();
in.close(); // Close the Scanner to avoid resource leaks
}
OUTPUT
Enter the details of 3 students
Enter name , regno, m1,m2,m3 of 1student
likhith b
0079
29
49
50
Enter name , regno, m1,m2,m3 of 2student
mayur jadav
0101
99
100
98
Enter name , regno, m1,m2,m3 of 3student
lokesh
0089
100
100
49
The Student Details are:
Information of Students1
name=likhith b
registor number=0079
subject one marks=29
subject two marks=49
subject three marks=50
total marks is=0
Information of Students2
name=mayur jadav
registor number=0101
subject one marks=99
subject two marks=100
subject three marks=98
total marks is=297
Information of Students3
name=lokesh
registor number=0089
subject one marks=100
subject two marks=100
subject three marks=49
total marks is=0
8. In a college First year classes are havig the following name of the class (BCA ,B Com,BSc),name
of the staff ,no of the students in the class. Array of student in class
import java.util.Scanner;
class College{
String cname,staffname;
int num;
College(String name, String sname, int n)
cname=name;
staffname=sname;
num=n;
void display(){
System.out.println("Class name="+cname);
System.out.println("Staff Name="+staffname);
System.out.println("number of Students="+num);
}
}
class Cdemo{
public static void main(String args[]){
String name, sname;
int n, i=0;
Scanner in = new Scanner(System.in);
College c[]= new College[3];
System.out.println("Enter the details");
for( i=0;i<3;i++){
System.out.println("Enter the Class name, Staff name , number of Students");
name = in.nextLine();
sname=in.nextLine();
n=in.nextInt();
in.nextLine();
c[i]= new College(name,sname,n);
System.out.println("The College Details :");
for( i=0;i<3;i++){
c[i].display();
OUTPUT:
Enter the details
Enter the Class name, Staff name , number of Students
DATA STRUCTURES
REKHA MAAM
57
Enter the Class name, Staff name , number of Students
JAVA
RASHMI MAAM
57
Enter the Class name, Staff name , number of Students
MATHEMATICS
BHAVANA MAAM
57
The College Details :
Class name=DATA STRUCTURES
Staff Name=REKHA MAAM
number of Students=57
Class name=JAVA
Staff Name=RASHMI MAAM
number of Students=57
Class name=MATHEMATICS
Staff Name=BHAVANA MAAM
number of Students=57
9. Define a class called first year with above attributes ad define a suitable constructor. Also write a
method called best Studet() which process a first-year object and return the student with the
highest total marks. In the main method define a first-year object and find the best student of the
class.
import java.util.Scanner;
class College{
String cname,staffname;
int num;
College(String name, String sname, int n)
cname=name;
staffname=sname;
num=n;
}
void display(){
System.out.println("Class name="+cname);
System.out.println("Staff Name="+staffname);
System.out.println("number of Students="+num);
class Cdemo{
public static void main(String args[]){
String name, sname;
int n, i=0;
Scanner in = new Scanner(System.in);
College c[]= new College[3];
System.out.println("Eter the details");
for( i=0;i<3;i++){
System.out.println("Enter the Class name, Staff name , number of Students");
name = in.nextLine();
sname=in.nextLine();
n=in.nextInt();
in.nextLine();
c[i]= new College(name,sname,n);
System.out.println("The College Details :");
for( i=0;i<3;i++){
c[i].display();
OUTPUT:
Enter the Details of the Three Students :
Enter name,regno,m1,m2,m3
likhith
100
99
87
67
Enter name,regno,m1,m2,m3
mayur
88
89
99
100
Enter name,regno,m1,m2,m3
lokesh
100
100
100
99
Total Marks os the Student is:1is253
Total Marks os the Student is:2is288
Total Marks os the Student is:3is299
BEST STUDENT IS:
NAME=lokeshHIGHSCORE=299
10. Program to define a class called employee with the name and state of appointment. Create ten
employee objects or an array and sort them as per their date of appontment i.e., print them as per
the time seniorty.
import java.util.*;
import java.io.*;
class Emp{
String name,date;
Emp(String name,String date){
this.name=name;
this.date = date;
class Sort implements Comparator<Emp>{
public int compare(Emp a,Emp b){
return a.date.compareTo(b.date);
public class EmpSort{
public static void main(String args[]){
ArrayList<Emp> list=new ArrayList<Emp>();
list.add(new Emp("RAJU","2020-03-19"));
list.add(new Emp("SANJANA","2019-02-27"));
list.add(new Emp("RAM","2019-01-27"));
list.add(new Emp("ARUN","1998-02-26"));
list.add(new Emp("VELAN","2023-09-15"));
list.add(new Emp("ASHOK","2023-08-16"));
list.add(new Emp("PRAJAN","2023-05-17"));
list.add(new Emp("CHETHAN","2022-05-18"));
list.add(new Emp("HARSHITHA","2023-06-17"));
list.add(new Emp("PALLAVI","2024-01-15"));
System.out.println("BEFORE SORTING:");
for(Emp d:list)
System.out.println(d.name+"\t"+d.date);
Collections.sort(list,new Sort());
System.out.println("SORTED IN ASCENDING ORDER");
for(Emp d:list)
System.out.println(d.name+"\t"+d.date);
}
OUTPUT
BEFORE SORTING:
RAJU 2020-03-19
SANJANA 2019-02-27
RAM 2019-01-27
ARUN 1998-02-26
VELAN 2023-09-15
ASHOK 2023-08-16
PRAJAN 2023-05-17
CHETHAN 2022-05-18
HARSHITHA 2023-06-17
PALLAVI 2024-01-15
SORTED IN ASCENDING ORDER
ARUN 1998-02-26
RAM 2019-01-27
SANJANA 2019-02-27
RAJU 2020-03-19
CHETHAN 2022-05-18
PRAJAN 2023-05-17
HARSHITHA 2023-06-17
ASHOK 2023-08-16
VELAN 2023-09-15
PALLAVI 2024-01-15