+ 2
Any one done?
Can any one please help me in shapes project in java
9 Answers
+ 2
Can you please share your code? Copy and paste it into the code playground, then attach it here.
Also, post what the task is.
+ 2
Sure
+ 2
import java.util.Scanner;
import java.lang.Math.*;
abstract class Shape {
int width;
abstract void area();
}
//your code goes here
class Square extends Shape {
public int area2;
public Square(int x)
{
width=x;
}
public void area() {
area2=width*width;
System.out.println(area2);
}
}
class Circle extends Shape {
public double area1;
public Circle(int y) {
width=y;
}
public void area() {
area1=Math.PI*width*width;
System.out.println(area1);
}
}
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();
}
}
..... Above one that is my code but what's wrong with that can you please tell me once...
+ 2
GANJIKUNTA SHAMITHA Declaration is important Square and Circle you haven't declared
+ 1
import java.util.Scanner;
abstract class Shape {
int width;
abstract void area();
}
//your code goes here
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
I have done this code but it's showing cannot find symbol
+ 1
Try adding "this" to your constructor variables.
+ 1
GANJIKUNTA SHAMITHA
Try again testing the code. I think it works fine..
*There is no need to Math class import
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