java language
Code:
abstract class Shape
{
abstract public double area(); //Method to be
overridden
abstract public double
perimeter(); //Method to be overridden
}
class Circle extends Shape
{
private double radius;
public Circle(double radius)
{
this.radius=radius;
}
public Circle()
{
this.radius=0;
}
public double area()
{
return
Math.PI*this.radius*this.radius;
}
public double perimeter()
{
return 2*Math.PI*this.radius;
}
public String toString()
{
return "Circle";
}
}
class Rectangle extends Shape
{
private double width,length;
public Rectangle(double length,double width)
{
this.length=length;
this.width=width;
}
public Rectangle()
{
this.width=0;
this.length=0;
}
public double area()
{
return
this.length*this.width;
}
public double perimeter()
{
return
2*(this.length+this.width);
}
public String toString()
{
return
"Rectangle";
}
}
class Triangle extends Shape
{
private double a,b,c;
public Triangle(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
}
public Triangle()
{
this.a=0;
this.b=0;
this.c=0;
}
public double area()
{
double
s=0.5*(this.a+this.b+this.c);
return
Math.sqrt(s*(s-this.a)*(s-this.b)*(s-this.c));
}
public double perimeter()
{
return this.a+this.b+this.c;
}
public String toString()
{
return "Triangle";
}
}
class Main
{
public static void main(String[] args) { //Testing
code
Shape shapes[]=new Shape[3];
shapes[0]=new Circle(3);
shapes[1]=new Rectangle(4,2);
shapes[2]=new
Triangle(1,2,3);
for(Shape polygon:shapes)
{
double
area=polygon.area();
double
perimeter=polygon.perimeter();
System.out.printf("%s: area: %.01f perimeter:
%.01f\n",polygon,area,perimeter);
}
}
}
Sample i/o:
Explanation:
Class Shape, Circle, Rectangle, Triangle is defined. The methods are and perimeter in the base class are overridden in the derived classes. Default and a parameterised constructor is given for all three derived classes. A test code is written using Main class and a sample i/o is attached for reference
java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...
Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEmployee, HourlyEmployee which extend the Employee class and implements that abstract method. A Salary Employee has a salary, a Commision Employee has a commission rate and total sales, and Hourly Employee as an hourly rate and hours worked Software Architecture: The Employee class is the abstract super class and must be instantiated by one of...
Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...
JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...
Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...
Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...
NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...
Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...
Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1. A private variable of type double named radius to represent the radius. Set to 1. 2. Constructor Methods: • Python : An overloaded constructor method to create a default circle. • C# & Java: Default Constructor with no arguments. • C# & Java: Constructor method that creates a circle with user-specified radius. 3. Method getRadius() that returns the radius. 4. ...
Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...
SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...