changes and additions are needed in the Parent and Child classes: (1) Output statements should not be
Question:
changes and additions are needed in the Parent and Child classes:
(1) Output statements should not be coded in the super-class and child classes. Methods should return a String data value.
(2) Parent class and Child classes need to have constructors, attributes definition (private variables), Set and Get methods, toString() methods.
(3) Child classes need to invoke the Parent (super-class) class constructors.
(4) Parent and Child classes must have meaningful names that represent a concrete object or concept like: a Chair, a Boat, a BankAccount, a School.
(5) Program requires a programmer defined Exception class.
(6) Program requires a throw statement where an Exception is thrown when an error is detected.
(7) The Exception can be thrown in Child classes.
(8) Program requires a try{} catch{} block in the Main client class.
import java.io.*;
import java.util.*;
class one_cls {
public int s=0;
//parent class method
public void print() {
System.out.println("Parent class method: G for geek "+s);
}
}
class two_cls extends one_cls {
public int o=1;
//overloading method
public void print(int a) {
//prints the message
System.out.println("This method "+o+
" overloads the parent method with a parameter storing a value "+a);
//calling the parent method
super.print();
}
}
class three_cls extends one_cls {
public int t=2;
//print method override method
@Override
public void print() {
//calling the super class method
super.print();
System.out.println("This function call overrides the parent method: "+t);
}
}
public class Main {
public static void main(String[] args) {
three_cls g = new three_cls();
two_cls a = new two_cls();
//calling the print method of three_cls
g.print();
System.out.println();
//calling the print method of two_cls
a.print(2);
//OBJECT OF SCANNER CLASS CREATION FOR INPUT ACCEPTANCE
Scanner sc = new Scanner(System.in);
//The input must be greater than or equal to 0 or else a user-defined exception will be thrown.
System.out.println("Please Enter an Integer value");
int value = sc.nextInt();
if(value<0){
try{
throw new LessThanZeroException("please Enter positive Value");
}
catch(LessThanZeroException l){
System.out.println(l);
}
}
else {
System.out.println(value);
}
}
}
//creation of a class of user-defined exceptions
class LessThanZeroException extends Exception
{
String msg;
LessThanZeroException(String msg){
this.msg = msg;
}
@Override
public String toString() {
return "LessThanZeroException{" + "msg=" + msg + '}';
}
}
fix the code and show output
Management Accounting Information for Decision-Making and Strategy Execution
ISBN: 978-0137024971
6th Edition
Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young