0

HELP ME Java

Hi I'm doing the "Shapes" exercise in the Java 6 exercise course. It doesn't seem like a difficult exercise to me, but out of 6 tests 1 gives me an error. I tried to format the results, I output the result as String, to use nextLine, I put some ifs to avoid null results. I haven't tried with exceptions because it doesn't seem on topic. In short, maybe it's not that simple ... I'm afraid it's a rounding problem, but I haven't found any solutions. Does anyone have ideas. Thanks

2nd Dec 2020, 2:44 PM
Michele
Michele - avatar
14 Answers
+ 5
Test 3 is failed how to debug it ...plz help me
17th Jan 2021, 1:44 AM
Sachin C R
Sachin C R - avatar
+ 1
It does not work. Better turns 5 times out of 6. While if you skip classes and go straight to never turn 6 out of 6. I don't know. Thanks
2nd Dec 2020, 7:14 PM
Michele
Michele - avatar
0
Where is the description of the task, Michele?
2nd Dec 2020, 2:58 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
I solved in a functional way, buy not in OOP. I dont' likes It, buy It work (Sic).
2nd Dec 2020, 3:00 PM
Michele
Michele - avatar
0
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
2nd Dec 2020, 3:02 PM
Michele
Michele - avatar
0
WITHOUT USING ABSTRACT CLASS import java.util.*; public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); System.out.println(x*x); System.out.println(Math.PI*y*y); } }
25th Jan 2021, 10:05 AM
K.Aiswaryaa Maanshree
K.Aiswaryaa Maanshree - avatar
0
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:47 AM
Panos
0
Pretty simple, you have to use "this" in order to complete it. "this" refers to the method or variable that is currently being called, so basically use "this" keyword if you want to refer to the current object, and in this case we want to refer to the current object to define width. The code: public class Square extends Shape { Square(int x) { this.width = x; } public void area() { System.out.println(width * width); } } public class Circle extends Shape { Circle(int x) { this.width = x; } public void area() { System.out.println(Math.PI * width * width); } }
26th Feb 2021, 7:13 AM
Deniz
Deniz - 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; } 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); } } 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 Mar 2021, 3:54 AM
Sasanka Vikum Vitharana
Sasanka Vikum Vitharana - avatar
0
Write once, run everywhere. This slogan means that:
2nd Jan 2023, 1:34 AM
Shofiyyah Ismi Labibah
- 1
Michele , can you post the whole description of the task and your code, so somebody can help you.
2nd Dec 2020, 2:50 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
- 1
Michele Using opps concepts. Both inputs are integer so no need to take it as String. 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); } }
2nd Dec 2020, 3:47 PM
A͢J
A͢J - avatar
- 1
I am sending you the example of the square class extension of the shape class. The rest should turn ... abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape { public Square(int x){ this.width = x; } void area(){ System.out.println(width * width); } }
17th Jan 2021, 7:54 PM
Michele
Michele - avatar