Java - Method Overriding - Area
Coding Problem Keys
Java - Method Overriding - Area
Problem Statement
The main method in Hello.java is given below.
import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int side = scan.nextInt(); Square square = new Square(side); Square cube = new Cube(side); System.out.println(square.getArea()); System.out.println(cube.getArea()); } }
Please fill in the missing lines of code for the class Square.java and Cube.java so that the program prints the output as mentioned below.
Area of Square = a², Area of Cube = 6a²
Example Input/Output
Input
5
Output
25
150
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Java Language
Square.java
class Square {
int num, a;
Square(int n) {
num = n;
a = num*num;
}
public int getArea() {
return num*num;
}
}
// Published By PKJCODERS
Cube.java
class Cube extends Square {
int b;
Cube (int n) {
super(n);
b = a*a;
}
public int getArea() {
return a*6;
}
}
// Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments