Challenge 3: Calculate the Student's Total Marks

In this exercise, you have to calculate the student's total marks using the concept of Classes

Problem Statement

Write a Java class called Student with

  • private fields:

    • name(String type)

    • mark1 and mark2 (double type)

And methods:

  • getMarks(int markNumber), a method which should return mark1 if markNumber equals 1 and mark2 otherwise.

  • calcTotal() method should take the two marks entered and return their sum.

Also define two constructors:

  • A default constructor that takes no parameters and initializes the values to zeros and null.
  • A constructor that takes the three variables and sets them as the values of the appropriate fields.


Name of the student and the marks obtained in the first and second tests


Sum of both marks

Sample Input

    Student student = new Student("Jack", 60, 70);

Sample Output

    getMarks(1)  => 60
    getMarks(2)  => 70
    calcTotal() => 130.0

Get hands-on with 1000+ tech skills courses.