help with java shapes challenge (the fifth official java challenge,more on classes module) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

help with java shapes challenge (the fifth official java challenge,more on classes module)

hey there. i tried to solve the fifth official challenge, for all the test cases it is correct but there is a locked case that i cannot see and incorrect. please help me, thank you. my code: import java.util.Scanner; abstract class Shape { int width; //const pi= Math.PI; public static void area(int width){ System.out.println(""); } } //your code goes here class Square extends Shape { Square (int x){ this.width =x; } public static void area (int width ){ int area = width*width; System.out.println(area); } } class Circle extends Shape { Circle (int y){ this.width =y; } public static void area (int r){ double radius=r; double area= radius*radius*Math.PI; System.out.println(area); } } 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(x); b.area(y); } }

10th Nov 2022, 3:27 PM
Abdessalam Othman
Abdessalam Othman - avatar
4 Answers
+ 4
I suspect this has to do with the int type that you use, it would make more sense to use doubles instead double x = sc.nextDouble(); you should also change it in the area method I hope this help :)
10th Nov 2022, 3:40 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
import java.util.Scanner; abstract class Shape { double width ; public static void area(double width){ System.out.println(""); } } //your code goes here class Square extends Shape { Square (int x){ this.width =x; } public static void area (int width ){ int area = width*width; System.out.println(area); } } class Circle extends Shape { Circle (double y){ this.width =y; } public static void area (double r) { double area= r*r*Math.PI; System.out.println(area); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); double y = sc.nextDouble(); Square a = new Square(x); Circle b = new Circle(y); a.area(x); b.area(y); } } i made the code as above, i guess I followed the changes you suggested, it worked but exactly the same 3rd case wrong
29th Nov 2022, 10:43 PM
Abdessalam Othman
Abdessalam Othman - avatar
0
you also have to do it for the square double x = sc.nextDouble(); double y = sc.nextDouble(); public static void area(double width) { }
1st Dec 2022, 1:48 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
that made all the cases wrong, the area has become outputting not only whole number for the square area but with a 0 after the point
1st Dec 2022, 5:15 PM
Abdessalam Othman
Abdessalam Othman - avatar