The Artima Developer Community
Sponsored Link

Java Answers Forum
abstract and interface classes

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
tiro john

Posts: 6
Nickname: motiero
Registered: Feb, 2003

abstract and interface classes Posted: Mar 4, 2003 3:56 PM
Reply to this message Reply
Advertisement
please help me to replace the abstract class with interface class in the program below. i dont know the difference between the two.


import java.util.*;
abstract class Shape7
{

// public double shape;

public string name;
public Shape(string name);{
this.name=name;
}
abstract public double area();
abstract public double perimeter();
abstract public String toString();

}


class Circle extends Shape7
{
final double PI=3.1419;
double radius;

public Circle()
{

radius=0.0;
}
public Circle ( double rad )
super(name);

{
radius = rad;
}

public void setradius(double rad)
{
radius=rad;
//return radius;
}
public double getradius(double rad)
{
return radius;
}
public double area()
{
double area=(PI)*(radius)*(radius);
return area;
}
public double perimeter()
{
double perimeter= (2*(PI)*(radius));
return perimeter;
}
public String toString()
{
return("area of a circle:"+area()+" "+"perimeter of your circle:" +perimeter());

}
}
////////////////////////////////////////////////////////////
class Triangle extends Shape7
{
double base;
double height;
double side1;
double side2;

public Triangle()
{
base=0.0;
height=0.0;
side1=0.0;
side2=0.0;
}
public Triangle(double b,double h,double s1,double s2)
super(name);
{
base=b;
height=h;
side1=s1;
side2=s2;
}
public void setbase(double b)
{
base=b;
// return base;
}
public double getbase()
{
return base;
}
public void setheight(double h)
{
height=h;
//return height;
}
public void setside1(double s1)
{
side1=s1;
//return side1;
}
public void setside2(double s2)
{
side2=s2;
// return side2;
}

public double getheight()
{
return height;
}
public double getside1()
{
return side1;
}
public double getside2()
{
return side2;
}
public double area()
{
double area=base*height/2;
return area;
}
public double perimeter()
{
double perimeter=(base+side1+side2);
return perimeter;
}
public String toString()
{
return("area of a triangle:"+area()+" "+"perimeter of my triangle:"+perimeter());
}
}
///////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////
class Rectangle extends Shape7
{
double width;
double length;

public Rectangle()
{
width=0.0;
length=0.0;
}
public Rectangle(double w,double l)
super(name);
{
width=w;
length=l;
}
public void setwidth(double w)
{
width=w;
// return width;
}
public void setlength(double l)
{
length=l;
// return length;
}
public double getwidth()
{
return width;
}
public double getlength()
{
return length;
}
public double area()
{
double area= length * width;
return area;
}
public double perimeter()
{
double perimeter=2*(length+width);
return perimeter;
}
public String toString()
{
return("area of a rectangle:"+area()+" "+"perimeter of your rectangle:"+perimeter());
}
}
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////
class Square extends Rectangle
{
//double length=0.0;
public Square(double length)
super(name);
{
super(length,length);
}
public double area()
{
double area= (length*length );
return area;
}
public double perimeter()
{
double perimeter= (4 * length);
return perimeter;
}
public String toString()
{
return("area of a square:"+area()+" "+"perimeter of my square:"+perimeter());
}}
//////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
class Regularpentagon extends Shape2
{
double base;
double height;
double side;

public Regularpentagon()
{
base=0.0;
height=0.0;
side=0.0;
}
public Regularpentagon(double b,double h,double s)
super(name);
{
base=b;
height=h;
side=s;
}
public void setbase(double b)
{
base=b;
//return base;
}
public double getbase()
{
return base;
}
public void setheight(double h)
{
height=h;
// return height;
}
public double getheight()
{
return height;
}
public void setside(double s)
{
side=s;
//return side;
}
public double getside()
{
return side;
}
public double area()
{
double area=5*(base*height/2);
return area;
}
public double perimeter()
{
double perimeter=5*side;
return perimeter;
}
public String toString()
{
return("area of a regularpentagon:"+area()+" "+"perimeter of my regularpentagon:"+perimeter());
}
}
class TestShape7
{
public static void main(String[]args)
{
try{
//shape[]ss=new shape[10];
Circle ss = new Circle(4.0);
Triangle ss1 = new Triangle(3.0,6.0,9.0,10.0);
Rectangle ss2 = new Rectangle(2.0,4.0);
Square ss3 = new Square(2.0);
Regularpentagon ss4 = new Regularpentagon(5.0,7.0,5.0);

// for(int i=0;i<ss.length;i++)
{
System.out.println(ss.toString());
System.out.println(ss1.toString());
System.out.println(ss2.toString());
System.out.println(ss3.toString());
System.out.println(ss4.toString());
}
}
catch(Exception e)
{
System.out.println(" ");
}
}
}

/////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// ////////////
the two.

Topic: reg:java/j2ee Previous Topic   Next Topic Topic: Java help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use