In: Computer Science
I have listed below two functions. Please add these two functions to StudentMath class.
Test it from the Test class
public int divideByZero(int gradeA, int gradeB, int gradeC)
{
int numberOfTests = 0;
int gradeAverage = 0;
try
{
gradeAverage = (gradeA + gradeB + gradeC)/numberOfTests;
}
catch(ArithmeticException ex)
{
System.out.println("Divide by zero exception has occured" +
ex.getMessage());
}
return gradeAverage;
}
public int ArrayOutOfBoundEception() {
int i = 0;
try {
// array of size 4.
int[] arr = new int[4];
// this statement causes an exception
i = arr[4];
// the following statement will never execute
System.out.println("Hi, I want to execute");
}catch(ArrayIndexOutOfBoundsException ar) {
System.out.println("Array index out of bound exception: " +
ar.getMessage());
}catch(Exception ex) {
System.out.println("An exception occured" + ex.getMessage());
}finally {
System.out.println("Finally block is always executed. It is used to
close the resources such as database connection or open file,
etc.");
}
return i;
}
Part 2
Write the function StudentFeePaid() in the Student class. . (Assume the fee is paid. you can hard code it)
It will return a string which will have the value "PAID" which means the string which you will return will be like String fee = "PAID".
You will write the try/catch and finally block in this function. In the finally block add the following
System.out.println("Finally block was executed");
Overide this function in the StudentMath class [Hint Use Override attribute and super.StudentFeePaid()]
The returned string will have "PAID" in it. You will modify it to "PAID Thank You" in the overridden function
Test it in the StudentTest Class.
---------------------------------------------------------------------------------------------------------
public class Student
{
int gradeA;
int gradeB;
int gradeC;
int average;
Student(int gradeA, int gradeB, int gradeC)
{
this.gradeA = gradeA;
this.gradeB = gradeB;
this.gradeC = gradeC;
}
public int Average()
{
this.average = (this.gradeA + this.gradeC + this.gradeC)/3;
return average;
}
public String AssignLetterGrade()
{
String grade = "";
if(average >= 90)
{
grade = "A";
}
else if((average < 90) && (average >= 80))
{
grade = "B";
}
return grade;
}
public static void main(String[] args)
{
int grade1 = 80;
int grade2 = 90;
int grade3 = 60;
System.out.println("Instantiate Student class");
Student stu = new Student(grade1, grade2,
grade3);
int Average = stu.Average();
System.out.println("The average grade is: " +
Average);
String strStudentGrade =
stu.AssignLetterGrade();
System.out.println("The letter grade is: " +
strStudentGrade);
}
}
------------------------------------------------------------------------------------------------------
public class StudentMath extends Student
{
public StudentMath(int gradeA, int gradeB, int
gradeC)
{
// invoking base-class
constructor
super(gradeA, gradeB,
gradeC);
}
@Override
public int Average()
{
int newAverage = super.Average() +
5;
return newAverage;
}
@Override
public String AssignLetterGrade()
{
String grade =
super.AssignLetterGrade();
if (grade.equals("A"))
{
grade = grade +
" : Excellent";
}
return grade;
}
}
-------------------------------------------------------------------------------
public class Test
{
public static void main(String[] args)
{
// TODO Auto-generated method
stub
int gradeA = 90;
int gradeB = 91;
int gradeC = 92;
StudentMath stMath = new
StudentMath(gradeA, gradeB, gradeC);
int average =
stMath.Average();
System.out.println("The average is
" + average);
String grade =
stMath.AssignLetterGrade();
System.out.println(grade + "
Excellent");
}
}
Part A:
class Student
{
int gradeA;
int gradeB;
int gradeC;
int average;
Student(int gradeA, int gradeB, int gradeC)
{
this.gradeA = gradeA;
this.gradeB = gradeB;
this.gradeC = gradeC;
}
public int Average()
{
this.average = (this.gradeA + this.gradeC + this.gradeC)/3;
return average;
}
public String AssignLetterGrade()
{
String grade = "";
if(average >= 90)
{
grade = "A";
}
else if((average < 90) && (average >= 80))
{
grade = "B";
}
return grade;
}
}
class StudentMath extends Student
{
public StudentMath(int gradeA, int gradeB, int gradeC)
{
// invoking base-class constructor
super(gradeA, gradeB, gradeC);
}
@Override
public int Average()
{
int newAverage = super.Average() + 5;
return newAverage;
}
@Override
public String AssignLetterGrade()
{
String grade = super.AssignLetterGrade();
if (grade.equals("A"))
{
grade = grade + " : Excellent";
}
return grade;
}
//Two new methods provided are added to this class here
public int divideByZero(int gradeA, int gradeB, int gradeC) {
int numberOfTests = 0;
int gradeAverage = 0;
try
{
gradeAverage = (gradeA + gradeB + gradeC)/numberOfTests;
}
catch(ArithmeticException ex)
{
System.out.println("Divide by zero exception has occured" +
ex.getMessage());
}
return gradeAverage;
}
public int ArrayOutOfBoundEception() {
int i = 0;
try {
// array of size 4.
int[] arr = new int[4];
// this statement causes an exception
i = arr[4];
// the following statement will never execute
System.out.println("Hi, I want to execute");
}catch(ArrayIndexOutOfBoundsException ar) {
System.out.println("Array index out of bound exception: " +
ar.getMessage());
}catch(Exception ex) {
System.out.println("An exception occured" + ex.getMessage());
}finally {
System.out.println("Finally block is always executed. It is used to
close the resources such as database connection or open file,
etc.");
}
return i;
}
}
public class Test
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
int gradeA = 90;
int gradeB = 91;
int gradeC = 92;
StudentMath stMath = new StudentMath(gradeA, gradeB,
gradeC);//object is created
System.out.println("Divide by Zero method is
called."+stMath.divideByZero(gradeA, gradeB, gradeC));//methods are
called using the object
System.out.println("ArrayIndexOutOfBounds method is
called."+stMath.ArrayOutOfBoundEception());
}
}
Part B:
class Student
{
int gradeA;
int gradeB;
int gradeC;
int average;
Student(int gradeA, int gradeB, int gradeC)
{
this.gradeA = gradeA;
this.gradeB = gradeB;
this.gradeC = gradeC;
}
public String StudentFeePaid(){//method definition
String fee=" ";
try{
fee="PAID";//set value to PAID
}
catch(Exception e){
System.out.println("Exception raised");
}
finally{
System.out.println("Finally block was executed");
}
return fee;//return value
}
public int Average()
{
this.average = (this.gradeA + this.gradeC + this.gradeC)/3;
return average;
}
public String AssignLetterGrade()
{
String grade = "";
if(average >= 90)
{
grade = "A";
}
else if((average < 90) && (average >= 80))
{
grade = "B";
}
return grade;
}
}
class StudentMath extends Student
{
public StudentMath(int gradeA, int gradeB, int gradeC)
{
// invoking base-class constructor
super(gradeA, gradeB, gradeC);
}
@Override
public int Average()
{
int newAverage = super.Average() + 5;
return newAverage;
}
@Override
public String AssignLetterGrade()
{
String grade = super.AssignLetterGrade();
if (grade.equals("A"))
{
grade = grade + " : Excellent";
}
return grade;
}
@Override
public String StudentFeePaid(){//override the method from base
class Student
String fee=" ";
try{
fee="PAID Thank You";//set fee to PAID Thank You
}
catch(Exception e){
System.out.println("Exception raised");
}
finally{
System.out.println("Finally block was executed");
}
return fee;//return value
}
}
public class Test
{
public static void main(String[] args)//code provided
{
// TODO Auto-generated method stub
int gradeA = 90;
int gradeB = 91;
int gradeC = 92;
StudentMath stMath = new StudentMath(gradeA, gradeB, gradeC);
Student stu=new Student(gradeA, gradeB, gradeC);
int average = stMath.Average();
System.out.println("The average is " + average);
String grade = stMath.AssignLetterGrade();
System.out.println(grade + " Excellent");
System.out.println(stMath.StudentFeePaid());//call the method
}
}