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
14 Answers
+ 5
Test 3 is failed how to debug it ...plz help me
+ 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
0
Where is the description of the task, Michele?
0
I solved in a functional way, buy not in OOP. I dont' likes It, buy It work (Sic).
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
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);
}
}
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);
}
}
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);
}
}
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();
}
}
0
Write once, run everywhere. This slogan means that:
- 1
Michele , can you post the whole description of the task and your code, so somebody can help you.
- 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);
}
}
- 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);
}
}