0

Can someone help me, I don't know where I'm going wrong exactly but 1 of my test cases gives an error

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape { Square (int width){ this.width = width; } public void area(){ int a = width * width; System.out.println(a); } } class Circle extends Shape { Circle (int width){ this.width = width; } public void area(){ double b = Math.PI * (width * width); System.out.println(b); } } 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(); } } My code is just the 2 subclasses. This is from the soloLearn java course and 4 of 5 of my test cases are correct. The 3rd, of which I'm unable to see the error, is wrong.

11th Jul 2021, 7:49 PM
Ndeji Chiputa
Ndeji Chiputa - avatar
2 Antworten
+ 5
Your code appears to be technically correct, however, Sololearn is picky about this one when it comes to the area calculation for the circle. If you remove the parentheses from the area functions width * width it will probably pass.
11th Jul 2021, 8:19 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg thank you so much, that worked
11th Jul 2021, 9:10 PM
Ndeji Chiputa
Ndeji Chiputa - avatar