Computer Applications
Define a class Student as given below:
Data members/instance variables:
name, age, m1, m2, m3 (marks in 3 subjects), maximum, average
Member methods:
- A parameterized constructor to initialize the data members.
- To accept the details of a student.
- To compute the average and the maximum out of three marks.
- 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 Constructors
ICSE 2010
134 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 Student() {
name = "";
age = 0;
m1 = 0;
m2 = 0;
m3 = 0;
maximum = 0;
average = 0;
}
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter age: ");
age = in.nextInt();
System.out.print("Enter Subject 1 Marks: ");
m1 = in.nextInt();
System.out.print("Enter Subject 2 Marks: ");
m2 = in.nextInt();
System.out.print("Enter Subject 3 Marks: ");
m3 = in.nextInt();
}
public void compute() {
if (m1 > m2 && m1 > m3)
maximum = m1;
else if (m2 > m1 && m2 > m3)
maximum = m2;
else
maximum = m3;
average = (m1 + m2 + m3) / 3.0;
}
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[]) {
Student obj = new Student();
obj.accept();
obj.compute();
obj.display();
}
}
Output

Answered By
50 Likes
Related Questions
Consider the following Java class and answer the questions given below:
class Student { String name; int age; Student(String n, int a) { name = n; age = a; } void display() { System.out.println("Name: " + name + ", Age: " + age); } public static void main(String[] args) { Student s1 = new Student("John", 15); Student s2 = new Student(); s1.display(); s2.display(); } }
(a) Will this program compile successfully? If not, explain the error.
(b) How can this program be modified to work correctly?
Fill in the blanks to design a class:
class __________{
int l, b;
Area(int __________, int __________) {
l = __________;
b = __________;
}
}Consider the given program and answer the questions given below:
class temp { int a; temp() { a=10; } temp(int z) { a=z; } void print() { System.out.println(a); } void main() { temp t = new temp(); temp x = new temp(30); t.print(); x.print(); } }
(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?
Assertion (A): The default constructor initializes object fields to zero or null depending on their data type.
Reason (R): Java constructors are inherently tied to the initialization of an object and therefore do not specify a return type.