Define a class student described as below:
Data Members: Name, class, m1, m2.m3, m4, total, perc
Member methods:
(i) To accept the details of marks in four subjects.
(ii) To display the details of the student and percentage and total.
(iii) To calculate the total and percentage of a student.
Write main method to create object of a class and call the above member method.
Answers
Answer:
import java.util.*;
public class Student {
int m1, m2, m3, m4, total;
double perc;
String name;
public void acceptDetails() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name: ");
name = sc.nextLine();
System.out.println("Enter marks in 4 subjects:");
m1 = sc.nextInt();
m2 = sc.nextInt();
m3 = sc.nextInt();
m4 = sc.nextInt();
}
public void calculate() {
total = m1 + m2 + m3 + m4;
perc = total / 400.0 * 100;
}
public void display() {
System.out.println("Student name: " + name);
System.out.println("Mark in 1st subject: " + m1);
System.out.println("Mark in 2nd subject: " + m2);
System.out.println("Mark in 3rd subject: " + m3);
System.out.println("Mark in 4th subject: " + m4);
System.out.println("Total mark: " + total);
System.out.println("Percentage: " + perc);
}
public static void main(String args[]) {
Student s = new Student();
s.acceptDetails();
s.calculate();
s.display();
}
}