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

For this program you will add and test 2 new member functions to the class in...
For this program you will add and test 2 new member functions to the class in //************************ intSLList.h ************************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; }; class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; }...
Add the following method below to the CardDeck class, and create a test driver to show...
Add the following method below to the CardDeck class, and create a test driver to show that they work correctly. int cardsRemaining() //returns a count of the number of undealt cards remaining in the deck. Complete in Java programming language. // Models a deck of cards. Includes shuffling and dealing. //---------------------------------------------------------------------- package Homework4; import java.util.Random; import java.util.Iterator; import javax.swing.ImageIcon; public class CardDeck { public static final int NUMCARDS = 52; protected ABList<Card> deck; protected Iterator<Card> deal; public CardDeck() { deck...
I have a BigInt class I created that works correctly. I would like to add a...
I have a BigInt class I created that works correctly. I would like to add a constructor BigInt(string s, int base) that creates a BigInt operating in the given base. The newly extended class should work with any base between 2 and 36, using the digits 0, 1, …, 9, A, B, C, …, Z. How do I do this from what I have? (you may add new data members and such of course) #include <string> #include <vector> class BigInt...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT