Question

In: Computer Science

I have listed below two functions. Please add these two functions to StudentMath class. Test it...

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");
   }
}

Solutions

Expert Solution

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
}
}


Related Solutions

python please take it out from class and make it all functions, add subtraction and multiplication...
python please take it out from class and make it all functions, add subtraction and multiplication functions with it. than you so much for helping me out. import random image = 'w' class cal(): def __init__(self, a, b): self.a = a self.b = b def add(self): return self.a + self.b a = random.randint(0, 9) b = random.randint(0, 9) obj = cal(a, b) print("0. Exit") print("1. Add") choice = int(input("Enter choice: ")) cnt = 0 # To hold number of tries...
For each of utility functions listed below in (a) through (h), answer the following questions: i)...
For each of utility functions listed below in (a) through (h), answer the following questions: i) Does the utility function exhibit diminishing marginal utility for good #1 (??1)? ii) Does the utility function exhibit diminishing marginal utility for good #2 (??2)? iii) Does the utility function exhibit a strictly diminishing marginal rate of substitution? a. ??(??1, ??2) = ??1 ⋅ ??2 b. ??(??1, ??2) = ??1 2 + ??2 2 c. ??(??1, ??2) = ??1 2/3 ⋅ ??2 1/3 d....
I have purchased the stocks listed below. My portfolio beta is 1.031. Please answer the following...
I have purchased the stocks listed below. My portfolio beta is 1.031. Please answer the following questions: STOCKS MARKET VALUE BETA WEIGHT BETA * WEIGHT TSLA $       25,099.20 1.64 0.076092 0.124791056 TWTR $       24,280.83 1.12 0.073611 0.082444423 GOOG $       23,865.60 1.08 0.072352 0.078140439 SBUX $       25,363.38 0.79 0.076893 0.060745478 DELL $       24,890.25 1.36 0.075459 0.102623755 MCD $       24,772.41 0.68 0.075101 0.051068947 WMT $       27,137.70 0.29 0.082272 0.023858919 AAPL $       25,159.68 1.28 0.076275 0.097632591 AMZN $       26,356.96 1.32 0.079905 0.105474866 NKE...
Please note, questions 1 and 2 have already been answered and are listed down below. I...
Please note, questions 1 and 2 have already been answered and are listed down below. I need help with question 3. Case Study: Topic and Detailed Instructions A central theme of contemporary operations management is focus on the customer. This is commonly understood to mean that if a company does focus on its customers and it if is able to consistently deliver what the customer wants in a cost-effective manner, then the company should be successful. The hard part is...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Part I: The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
  Part I:   The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class. * Update the function display() to be a virtual function     * Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects. Part II: The Derived Classes Add an weeklyEarning() function to each...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
This is a question for my biochemistry class. I have copied and pasted it below. This...
This is a question for my biochemistry class. I have copied and pasted it below. This is all that was given to me. Diagram the flow of genetic material in a cell.
I have a quiz for the cell structures and their functions. Please answer these questions. 1....
I have a quiz for the cell structures and their functions. Please answer these questions. 1. CHEEK CELLS Cheek cells are not the major producers of mucus in the mouth, they do produce a small amount of mucopolysaccharide. What inclusions should be especially numerous in the cytoplasm of these cells in order to make and excrete such a substance? What function does their flat shape provide? 2. BLOOD SMEAR (FROG & HUMAN) How they differ? How is each adapted to...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT