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

Answered By
14 Likes
Related Questions
Write the return types of the following library functions:
(a)
isLetterOrDigit(char)(b)
replace(char, char)Define a class to accept a number and check whether the number is Neon or not. A number is said to be Neon if sum of the digits of the square of the number is equal to the number itself.
E.g.
Input: 9Output:
9 * 9 = 81, 8 + 1 = 9
9 is Neon number.Define a class to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.
Define a class to overload the method display as follows:
void display(): To print the following format using nested loop
5 5 5 5 5 4 5 5 5 5 3 4 5 5 5 2 3 4 5 5 1 2 3 4 5void display(int n): To check and display if the given number is a Perfect number or not. A number is said to be perfect, if sum of the factors of the number excluding itself is equal to the original number.
E.g.
6 = 1 + 2 + 3, where 1, 2 and 3 are factors of 6 excluding itself.