icse-promo

Question

 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) A parameterized constructor to initialize the data members.

(ii) To accept the details of a student.

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

(iv) 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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class Student
{
    String name;
    int m1, m2, m3,age,maximum;
    double average;
    
    public Student(String n, int a, int marks1, int marks2, int marks3, int max, double avg)
    {
        name = n;
        age = a;
        m1 = marks1;
        m2 = marks2;
        m3 = marks3;
        maximum = max;
        average = avg;
    }

    /* accept() method to accept name,age and marks*/
    public void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter name: ");
        name = sc.nextLine();
        System.out.print("Enter age: ");
        age =sc.nextInt();
        System.out.print("Enter marks of 1st subject: ");
        m1 = sc.nextInt();
        System.out.print("Enter marks of 2nd subject: ");
        m2 = sc.nextInt();
        System.out.print("Enter marks of 3rd subject: ");
        m3 = sc.nextInt();
    }

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

    public void display()
    {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Marks1 " + m1);
        System.out.println("Marks2 " + m2);
        System.out.println("Marks3 " + m3);
        System.out.println("Maximum: " + maximum);
        System.out.println("Average: " + average);
    }

    public static void main(String[] args)
    {
        Student ob1 = new Student("",0,0,0,0,0,0);
        ob1.accept();
        ob1.calculate();
        ob1.display();
    }

}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *