+ 1

How do you use Math.PI

I'm getting the error message: "illegal start of expression" and it's pointing to public void area() import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape { Square(){ public void area(){ int Sq_answer = x*x; System.out.println(Sq_answer); } } } class Circle extends Shape { Circle(){ public void area(){ Double Cir_answer = Math.PI*y*y; System.out.println(Cir_answer); } } } 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(); } }

15th Dec 2022, 10:59 AM
Natanael
8 Respostas
+ 2
Method in not allowed. Method defining in your contractor is illegal. Change to : Square() { } public void area() { // code } Do the same for Circle class...
15th Dec 2022, 5:05 PM
Jayakrishna 🇼🇳
+ 2
There actually you need take pass an argument by constructor then assign that passed value to width variable of super class.. So correct way is : Squaue( int x) { width = x ; // width is super class variable. } Now, instead if x in area() method, use width. Do the same in Circle class... If any mistakes, then post your update code link with saving ...
15th Dec 2022, 6:05 PM
Jayakrishna 🇼🇳
+ 2
// as i said, repeat changes in Circles class also, here is correct way for circle class class Circle extends Shape { Circle(int y) { width = y ; } public void area() { double Cir_answer = Math.PI*width*width; System.out.println(Cir_answer); } }
15th Dec 2022, 7:59 PM
Jayakrishna 🇼🇳
+ 2
Area of Square = width * width; Area of Circle = π * radius * radius; So here, if inputs x=2, y=3 then you are passing these through constructor. And Assigning to width=x in Square, And width=y in Circle. Now you calculate area in width*width in Square and as Math.PI*width*width in Circle. I hope you know this, It's about, implementing logic by code. Not about square, circle. you're welcome..
16th Dec 2022, 8:08 AM
Jayakrishna 🇼🇳
+ 1
Thank you, I did as you suggested, but now I'm getting message " can not find symbol " and it's pointing to variable y.
15th Dec 2022, 5:36 PM
Natanael
+ 1
Thanks, now it's saying "can not find symbol" and it's pointing to y. I don't know how to make a code link. import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape { Square(int x){ width = x; } public void area(){ int Sq_answer = width*width; System.out.println(Sq_answer); } } class Circle extends Shape { Circle(int y){ } public void area(){ Double Cir_answer = Math.PI*y*y; System.out.println(Cir_answer); } } 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(); } }
15th Dec 2022, 6:55 PM
Natanael
+ 1
Thanks, since I never studied geometry in school, I thought that the width is only needed to figure out the square area of a box, ground or rectangle, I never learned about circles so I thought the width variable did not apply for the circle, it works now but I would have never figured this out without some help, so I will try to learn how this code flows.
16th Dec 2022, 1:37 AM
Natanael
+ 1
Thanks again, compared to the Basic programming language of the '80s Java looks like rocket science. That language had no methods or functions, just GOTO statements to jump from place to place.
18th Dec 2022, 9:10 PM
Natanael