Can somebody help me | Sololearn: Learn to code for FREE!

0

# 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

12/15/2020 8:24:36 AM

Alessandro

-1

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); } }

+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

+12

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(); } }

+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(); } }

+2

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(); } }

+1

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

0

This project test cases had given me a migraine

0

Panos thank you so much!

0

/*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(); } }

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(); } }

-1

Adama Diaby Thank you, but there is always the third hidden case that is Wrong, i dont know why

-1

What do you mean by third hidden case, can you explain it a little bit

-1

Same problem

-1

In an third case, of the problem is always wrong I don't know why

-1

has anyone find the solution this problem

-2

Your did not create constructor for the subclasses and the area method doesn't need a parameter it uses the instance variable width directly. These are the changes I made: import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape { public Square(int w) { width = w; } public void area(){ // width = x; width *= width; System.out.println(width); } } class Circle extends Shape { public Circle(int w) { width = w; } public void area(){ // width = x 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(); b.area();

-3

Hallo, class Square extends Shape{ Square(int x){ width=x; } public void area(){ System.out.println(width*width); } } class Circle extends Shape{ Circle(int y){ width=y; } public void area(){ double PI=Math.PI; System.out.println(PI*width*width); } it´s worked!

-5

Hi

-6