I can't find error in my Java Shapes Project .Help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can't find error in my Java Shapes Project .Help

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } public class Square extends Shape{ Square(int width){ @Override void area() { System.out.println(width*width); } } } public class Circle extends Shape{ Circle(int width){ @Override void area() { System.out.println(Math.PI*width*width); } } } 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(); } }

1st Nov 2022, 7:53 PM
Smith
Smith - avatar
5 Answers
+ 4
You are adding method inside a constructor method which is invalid. Complete constructor by assinging passed parameter value into width variable and next, after it add area() method..
1st Nov 2022, 8:03 PM
Jayakrishna 🇮🇳
+ 3
Rez Smith In your code , example for Square: Square ( int w) { this.width = w; } Here, passing to w, then assign it width of super class Shape variable which is value you are using to find area.. Similar do in Circle.
1st Nov 2022, 8:37 PM
Jayakrishna 🇮🇳
+ 1
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } public class Square extends Shape{ Square(int width){} @Override void area() { System.out.println(width*width); } } public class Circle extends Shape{ Circle(int width){} @Override void area() { System.out.println(Math.PI*width*width); } } 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(); } }
1st Nov 2022, 7:57 PM
I am offline
I am offline - avatar
+ 1
I don't know how to use passed parameter .can anyone show answer code
1st Nov 2022, 8:30 PM
Smith
Smith - avatar
1st Nov 2022, 8:38 PM
Smith
Smith - avatar