Can somebody help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can somebody help me

You are working on a graphical app, which includes multiple different shapes. The given code declares a base Shape class with an abstract area() method and a width attribute. You need to create two Shape subclasses, Square and Circle, which initialize the width attribute using their constructor, and define their area() methods. The area() for the Square class should output the area of the square (the square of the width), while for the Circle, it should output the area of the given circle (PI*width*width). The code in main creates two objects with the given user input and calls the area() methods. Sample Input: 5 2 Sample Output: 25 12.566370614359172 The area of the square is 5*5=25, while the area of the circle is PI*2*2=12.566370614359172 https://code.sololearn.com/c379tkGDnY4P/?ref=app

15th Dec 2020, 8:24 AM
Alessandro
Alessandro - avatar
35 Answers
- 10
Hello. I solved it. Here it is: class Square extends Shape { Square(int x) { this.width = x; } void area() { //return x * x; System.out.println(width * width); } } class Circle extends Shape { Circle(int y){ this.width = y; } public void area(){ System.out.println(Math.PI*width*width); } }
19th Feb 2021, 7:48 AM
Panos
+ 56
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 = width*width; System.out.println(width); } } class Circle extends Shape { public Circle(int w){ width = w; } public void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } }
16th Feb 2021, 11:08 AM
András Timár
András Timár - avatar
+ 15
class Square extends Shape { Square(int x) { width = x; } void area() { System.out.println(width * width); } } class Circle extends Shape { Circle(int y){ width = y; } public void area(){ System.out.println(Math.PI*width*width); } } it's really worked
27th Feb 2021, 12:16 PM
Shohruh Berdikulov
Shohruh Berdikulov - avatar
+ 7
//vote me, my code is work import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape{ Square(int x){ this.width = x; } @Override public void area(){ System.out.println(width * width); } } class Circle extends Shape{ Circle(int y){ this.width = y; } @Override public void area(){ System.out.println(Math.PI * width * width); } } public class Main { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); sc.close(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }
1st Feb 2022, 6:38 PM
Avrian Shandy
Avrian Shandy - avatar
+ 4
This is working! 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 = width*width; System.out.println(width); } } class Circle extends Shape { public Circle(int w){ width = w; } public void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } }
23rd Mar 2021, 5:00 PM
Tejashree Suryawanshi
Tejashree Suryawanshi - avatar
+ 3
import java.util.Scanner; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U abstract class Shape { int width; abstract void area(); } class Square extends Shape{ public Square(int x) { this.width = x; } @Override void area() { width = width*width; System.out.println(width); } } class Circle extends Shape{ public Circle(int y) { this.width = y; } @Override void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } }
4th Sep 2021, 3:14 PM
Fazal Haroon
Fazal Haroon - avatar
+ 2
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 = width*width; System.out.println(width); } } class Circle extends Shape { public Circle(int w){ width = w; } public void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } } Good Luck
25th Dec 2021, 11:31 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
/*w and wi is very important to initialize*/ import java.util.Scanner; abstract class Shape { int wi,w; abstract void area(); } //your code goes here class Circle extends Shape{ Circle(int a){ w = a; } void area(){ System.out.println(Math.PI*w*w); } } class Square extends Shape{ Square(int b){ wi = b; } void area(){ System.out.println(wi*wi); } } 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(); } }
12th Jun 2021, 9:02 AM
Parmar Darshil
Parmar Darshil - avatar
+ 1
#include <iostream> using namespace std; class Number { private: int num; public: Number(int n) { num = n; } //complete the method int square() const{ int n = num; n*=n; return n; }; }; int main() { int x; cin >> x; const Number myNum(x); cout << myNum.square(); } Good Luck
25th Jan 2022, 5:59 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
It's working import java.util.Scanner; public abstract class Shape { abstract void area(int width); } public class Square extends Shape { void area(int width) { System.out.println(width * width); } } public class Circle extends Shape { void area(int width) { System.out.println(Math.PI * width * width); } } public class Program { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Shape a = new Square(); Shape b = new Circle(); a.area(x); b.area(y); } }
7th Feb 2022, 11:24 AM
ADITHYA K P
ADITHYA K P - avatar
0
Adama Diaby im trying to learn java and im at the section "more in classes" and at the end you have to complete a Project, in this case im doing "Shape". When you think you have complete it you can run it and it shows you if you have done well showing some of the possibile solutions. But not all the solutions are shown because you can print them and complete it without learning the lessons. So i dont know were im doing wrong
15th Dec 2020, 7:03 PM
Alessandro
Alessandro - avatar
0
This project test cases had given me a migraine
22nd Feb 2021, 5:00 PM
Kiprotich Kincaid
Kiprotich Kincaid - avatar
0
Panos thank you so much!
27th Feb 2021, 1:00 PM
Alessandro
Alessandro - avatar
0
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape{ Square(int x){ this.width = x; } @Override public void area(){ System.out.println(width * width); } } class Circle extends Shape{ Circle(int y){ this.width = y; } @Override public void area(){ System.out.println(Math.PI * width * width); } } public class Main { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); sc.close(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }
16th Jul 2021, 7:59 PM
Mohammed Hussein Ahmed Farag
Mohammed Hussein Ahmed Farag - avatar
0
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 = width*width; System.out.println(width); } } class Circle extends Shape { public Circle(int w){ width = w; } public void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } }
2nd Oct 2021, 9:42 AM
Hariom Kumar
Hariom Kumar - avatar
0
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 = width*width; System.out.println(width); } } class Circle extends Shape { public Circle(int w){ width = w; } public void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } } Worked :)
23rd Oct 2021, 6:08 PM
Alireza Nasrollahzadeh
Alireza Nasrollahzadeh - avatar
0
class Square extends Shape { Square(int x) { width = x; } void area() { System.out.println(width * width); } } class Circle extends Shape { Circle(int y){ width = y; } public void area(){ System.out.println(Math.PI*width*width); } }
15th Nov 2021, 6:11 AM
Nabila Muftia Ma'ruf Kartono
Nabila Muftia Ma'ruf Kartono - avatar
0
#trythis 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 = width*width; System.out.println(width); } } class Circle extends Shape { public Circle(int w){ width = w; } public void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } }
17th Dec 2021, 9:38 AM
Mirmahmud Ilyosov
Mirmahmud Ilyosov - avatar
0
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 = width*width; System.out.println(width); } } class Circle extends Shape { public Circle(int w){ width = w; } public void area() { double areaCircle = (double)Math.PI*(int)width*(int)width; 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(); b.area(); } }
1st Jan 2022, 8:38 AM
M.I.M.Aqueel
M.I.M.Aqueel - avatar
0
class Shape: def __init__(self, width, height): self.width = width self.height = height @staticmethod def area(self): return self.width * self.height w = int(input()) h = int(input()) s = Shape(w, h) print(Shape.area(s))
8th Feb 2022, 11:59 AM
Engr.Qaiser Javed
Engr.Qaiser Javed - avatar