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

+4

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: https://code.sololearn.com/cvE90UeATprg 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(System.in); 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

Niloufar

7 Answers

New Answer

+1

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

+1

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

+1

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.

+1

The answer to the second question

+1

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

0

I edited the URL

0

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