Question: Develop an abstract class “GeometricObject” which will have two variables colourand weight. It would have constructor function for setting the colour as “white” and the weight as 1.0 as default values. The class should have methods getColour() and getWeight() to return the colour and weight values to the caller. The class should have two abstract methods findArea() and findCircumference(). Write a subclass for “GeometricObject” called “Triangle” which will be able to calculate area and circumference for a triangle.
Question: Develop an abstract class “GeometricObject” which will have two variables colourand weight. It would have constructor function for setting the colour as “white” and the weight as 1.0 as default values. The class should have methods getColour() and getWeight() to return the colour and weight values to the caller. The class should have two abstract methods findArea() and findCircumference(). Write a subclass for “GeometricObject” called “Triangle” which will be able to calculate area and circumference for a triangle.
import java.util.*;
import java.math.*;
abstract class GeometricObject
{
String colour;
double weight;
public
GeometricObject()
{
colour="White";
weight=1.0;
}
public String
getColour()
{
return
colour;
}
public Double
getWeight()
{
return
weight;
}
abstract void
findArea(double x,doubley,double z);
abstract void
findCircum(double x,doubley,double z);
}
class Triangle extends GeometricObject
{
double
height,base,s1,s2,s3;
public void
findArea(double x,doubley,double z)
{
double
area,p;
s1=x;
s2=y;
s3=z;
p=(s1+s2+s3)/2;
area=Math.sqrt((p*(p-s1)*(p-s2)*(p-s3)));
System.out.println("The
area of triangle: "+area);
}
public void
findCircum(double x,doubley,double z)
{
s1=x;
s2=y;
s3=z;
double
circum=s1+s2+s3;
System.out.println("The
circumference of triangle: "+circum);
}
}
class Geometry
{
public static
void main(String arg[])
{
Scanner
sc=new Scanner(System.in);
double
c,d,e;
System.out.print("Enter
side1 of triangle:");
c=sc.nextDouble();
System.out.print("Enter
side2 of triangle:");
d=sc.nextDouble();
System.out.print("Enter
side3 of triangle:");
e=sc.nextDouble();
Triangle
t=new Triangle();
t.findArea(c,d,e);
t.findCircum(c,d,e);
GeometricObject
go=t;
System.out.println("The
colour of triangle is:"+t.getColour());
System.out.println("The
weight of triangle is:"+t.getWeight());
}
}
OUTPUT:Keywords:
How to recover the classnotfoundexception in Java?
What are the top most requirements or expectations of client for develop Java web application?
Java program question paper in 2020
Comments
Post a Comment