Student with the following specifications:
Define a class Student with the following specifications:
Data Members/Instance Variables:
name, age, m1, m2, m3, maximum, averageMember Methods:
Write a main method to create an object of the Student class and call the above member methods.
Name: John Doe Age: 20 Marks in Subject 1: 85 Marks in Subject 2: 90 Marks in Subject 3: 88 Maximum Marks: 90 Average Marks: 87.66666666666667
import java.io.*;
class Student {
// Data members/instance variables
String name;
int age, m1, m2, m3, maximum;
double average;
// Parameterized constructor to initialize the data members
public Student(String name, int age, int m1, int m2, int m3) {
this.name = name;
this.age = age;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
compute(); // Calculate average and maximum during initialization
}
// Method to accept the details of a student
void accept() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
name = br.readLine();
System.out.println("Enter age:");
age = Integer.parseInt(br.readLine());
System.out.println("Enter marks in three subjects:");
m1 = Integer.parseInt(br.readLine());
m2 = Integer.parseInt(br.readLine());
m3 = Integer.parseInt(br.readLine());
compute(); // Calculate average and maximum after updating details
}
// Method to compute the average and the maximum out of three marks
void compute() {
average = (m1 + m2 + m3) / 3.0;
if (m1 >= m2 && m1 >= m3) {
maximum = m1;
} else if (m2 >= m1 && m2 >= m3) {
maximum = m2;
} else {
maximum = m3;
}
}
// Method to display the name, age, marks in three subjects, maximum, and average
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks in Subject 1: " + m1);
System.out.println("Marks in Subject 2: " + m2);
System.out.println("Marks in Subject 3: " + m3);
System.out.println("Maximum Marks: " + maximum);
System.out.println("Average Marks: " + average);
}
// Main method to create an object of the class and call the member methods
public static void main(String[] args) throws IOException {
// Create an object with initial values
Student student = new Student("John Doe", 20, 85, 90, 88);
// Display initial details
student.display();
// Update details with user input
student.accept();
// Display updated details
student.display();
}
}
Here is the variable table for the Student class with type and description:
| Variable | Type | Description |
|---|---|---|
name |
String |
Stores the name of the student. |
age |
int |
Stores the age of the student. |
m1 |
int |
Stores the marks in subject 1. |
m2 |
int |
Stores the marks in subject 2. |
m3 |
int |
Stores the marks in subject 3. |
maximum |
int |
Stores the highest mark among m1, m2, m3. |
average |
double |
Stores the average of the marks m1, m2, m3. |
This table summarizes the data members of the Student class, including their types and purposes.
accept() Method: Reads user input to update student details and recalculates average and maximum.compute() Method: Computes the average and determines the highest mark among the three subjects.display() Method: Outputs all relevant student information.main() Method: Creates a Student object, displays initial details, updates details via user input, and then displays the updated information.First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.