java shape class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

19th Aug 2021, 2:09 PM
Niloufar
7 Answers
+ 2
Nobody can see your code. For this you should save it on SL Playground and link here.
19th Aug 2021, 3:06 PM
JaScript
JaScript - avatar
+ 1
You should to replace „super“ with „this“ and „Math.pow“ with „this.width * this.width“.
19th Aug 2021, 3:47 PM
JaScript
JaScript - avatar
+ 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.
19th Aug 2021, 5:03 PM
Niloufar
+ 1
The answer to the second question
20th Aug 2021, 10:14 AM
Loulou El kourya
+ 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.
20th Aug 2021, 11:47 AM
JaScript
JaScript - avatar
0
I edited the URL
19th Aug 2021, 3:18 PM
Niloufar
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); } }
28th Sep 2021, 4:05 PM
Jameel Ur Rehman
Jameel Ur Rehman - avatar