+ 5

Can anyone please tell, What mistak I had done here?

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape { public Square(int w) { width = w; } public void area(){ //width = w width *= width; System.out.println(width); } } class Circle extends Shape { public Circle(int w) { width = w; } public void area(){ //width = w double PI = Math.PI; double areaCircle = (double) width * width * PI; System.out.println(areaCircle); } } 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); } }

15th Feb 2021, 12:11 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
16 Réponses
+ 4
abstract method is declared without parameters. but in a.area(x) you passed x. so change it in Shape class and define it in the extended classes. abstract void area(int w) ; public void area(int w){ width = w; } a.area(x); b.area(y);
15th Feb 2021, 12:28 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 3
Then what should I do
15th Feb 2021, 12:30 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 3
Can you please share code here
15th Feb 2021, 12:39 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 3
Since you want to polymorphic ally program your classes, I will first advice you to 1. You are to store the object of the subclasses to the variables of it's super class in this case the abstract class... Let me demonstrate these concepts for you.. abstract class Shapes{ int width; abstract void area(); }//abstract class declaration class Square extends Shapes { public Square (int w) { width = w; //your constructor here //method area public void area(){ width = width * width; }//Subclass declaration //Main methods public class program { public static void main (String[] args){ //To polymorphically process their methods..u do this Shapes[] shape = new Shapes();//these creates arrays of superclass to keep the object of subclasses shape[ 0] = new Square(2);//2 is your width shape[ 1] = new Circle(3);//3 is your width //So to access the area methods //in any of those classes.. //Use this. for(Shapes currentShape : shape) { System.out.println (currentShape); }
15th Feb 2021, 6:55 AM
Ibidunni Ridwan Ayodele
Ibidunni Ridwan Ayodele - avatar
+ 2
Ok I'll try
15th Feb 2021, 12:35 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 2
It will be more easy to understand.
15th Feb 2021, 12:40 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 1
or you can keep it the way it is. just don't pass x and y to area() since they are passed to the constructor should use a.area(); b.area(); you're missing a dot between objects and method.
15th Feb 2021, 12:34 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Means
15th Feb 2021, 12:44 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 1
I really don't understand what you said
15th Feb 2021, 12:45 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 1
Thanks Bahha
15th Feb 2021, 12:13 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 1
Thanks Ibidunni Ridwan🙏
15th Feb 2021, 12:14 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 1
Thanks
15th Feb 2021, 2:45 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
0
your code modified normally the constructor is enough, so the area does not have to be modified.
15th Feb 2021, 12:43 AM
Bahhaⵣ
Bahhaⵣ - avatar
0
this code is hella wrong the ( PrintIn ) is used for “ private static void “.
15th Feb 2021, 2:22 PM
someone
someone - avatar
0
Bonsoir
16th Feb 2021, 10:17 PM
Zoubairou Ali
Zoubairou Ali - avatar