KnowledgeBoat Logo
|

Computer Applications

Define a class Student described as below

Data members/instance variables
name, age, m1, m2, m3 (marks in 3 subjects), maximum, average

Member methods
(i) Student (…) : A parameterised constructor to initialise the data members.

(ii) compute() : To compute the average and the maximum out of three marks.

(iii) display() : To display the name, age, marks in three subjects, maximum and average.

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

Java

Java Classes

32 Likes

Answer

import java.util.Scanner;

public class Student
{
    private String name;
    private int age;
    private int m1;
    private int m2;
    private int m3;
    private int maximum;
    private double average;

    public Student(String n, int a, int s1, 
    int s2, int s3) {
        name = n;
        age = a;
        m1 = s1;
        m2 = s2;
        m3 = s3;
    }

    public void compute() {
        average = (m1 + m2 + m3) / 3.0;
        
        maximum = Math.max(m1, m2);
        maximum = Math.max(maximum, m3);   
    }

    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Subject 1 Marks: " + m1);
        System.out.println("Subject 2 Marks: " + m2);
        System.out.println("Subject 3 Marks: " + m3);
        System.out.println("Maximum Marks: " + maximum);
        System.out.println("Average Marks: " + average);
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter name: ");
        String n = in.nextLine();
        System.out.print("Enter age: ");
        int a = in.nextInt();
        System.out.print("Enter Subject 1 Marks: ");
        int sm1 = in.nextInt();
        System.out.print("Enter Subject 2 Marks: ");
        int sm2 = in.nextInt();
        System.out.print("Enter Subject 3 Marks: ");
        int sm3 = in.nextInt();
        Student obj = new Student(n, a, sm1, 
                                    sm2, sm3);
        obj.compute();
        obj.display();
    }
}

Output

BlueJ output of Define a class Student described as below Data members/instance variables name, age, m1, m2, m3 (marks in 3 subjects), maximum, average Member methods (i) Student (…) : A parameterised constructor to initialise the data members. (ii) compute() : To compute the average and the maximum out of three marks. (iii) display() : To display the name, age, marks in three subjects, maximum and average. Write a main method to create an object of a class and call the above member methods.

Answered By

14 Likes


Related Questions