Define a class Student with the following specifications:

Java Programming Language (Article) (Program)

35

Define a class Student with the following specifications:

  • Data Members/Instance Variables:

    • name, age, m1, m2, m3, maximum, average
  • Member Methods:

    1. A parameterized constructor to initialize the data members.
    2. A method to accept the details of a student.
    3. A method to compute the average and the maximum out of the three marks.
    4. A method to display the name, age, marks in three subjects, maximum, and average.

Write a main method to create an object of the Student class and call the above member methods.

Given Input:


Expected Output:

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

Program:

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

Output:


                                        

Explanation:

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.

  • Parameterized Constructor: Initializes the attributes and computes the average and maximum values.
  • 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.

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.