Computer Applications

Define a class Student with the following specifications:

Class name : Student

Data MembersPurpose
String nameTo store the name of the student
int engTo store marks in English
int hnTo store marks in Hindi
int mtsTo store marks in Maths
double totalTo store total marks
double avgTo store average marks
Member MethodsPurpose
void accept()To input marks in English, Hindi and Maths
void compute()To calculate total marks and average of 3 subjects
void display()To show all the details viz. name, marks, total and average

Write a program to create an object and invoke the above methods.

Java

Java Classes

47 Likes

Answer

import java.util.Scanner;

public class Student
{
    private String name;
    private int eng;
    private int hn;
    private int mts;
    private double total;
    private double avg;
    
    public void accept() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter student name: ");
        name = in.nextLine();
        System.out.print("Enter marks in English: ");
        eng = in.nextInt();
        System.out.print("Enter marks in Hindi: ");
        hn = in.nextInt();
        System.out.print("Enter marks in Maths: ");
        mts = in.nextInt();
    }
    
    public void compute() {
        total = eng + hn + mts;
        avg = total / 3.0;
    }
    
    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Marks in English: " + eng);
        System.out.println("Marks in Hindi: " + hn);
        System.out.println("Marks in Maths: " + mts);
        System.out.println("Total Marks: " + total);
        System.out.println("Average Marks: " + avg);
    }
    
    public static void main(String args[]) {
        Student obj = new Student();
        obj.accept();
        obj.compute();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

17 Likes


Related Questions