java shape class | Sololearn: Learn to code for FREE!


java shape class

My code fails at the third locked test case and I can't view it. What is the problem? It contains three classes (Shape, Square, and Circle) and we need to calculate the area of each shape. This is the link to code: This is the whole code: import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape{ public Square (int width ){ super.width =width; } public void area(){ System.out.println((int)Math.pow(super.width ,2)); } } class Circle extends Shape { public Circle (int width){ super.width =width; } public void area(){ System.out.println( Math.PI*Math.pow(super.width ,2)); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(; int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } } Which one is correct? super.width or this.width

8/19/2021 2:09:14 PM


7 Answers

New Answer


Nobody can see your code. For this you should save it on SL Playground and link here.


You should to replace „super“ with „this“ and „Math.pow“ with „this.width * this.width“.


JaScript What is wrong with super and Math.pow? why super is not correct? width is defined in parent class so when I want to initialize it I think super is needed.


The answer to the second question


Does it work what I have shown? PS. For more information please see advanced tutorials, books or maybe try the web search. Happy coding.


I edited the URL


Code Below Passed All Test. 💯% class Square extends Shape{ public Square (int width ){ this.width =width; } public void area(){ System.out.println(width*width); } } class Circle extends Shape { public Circle (int width){ this.width =width; } public void area(){ System.out.println(Math.PI*width*width); } }