Topic: 大家来看看这个简单的问题

  Print this page

1.大家来看看这个简单的问题 Copy to clipboard
Posted by: decun
Posted on: 2005-03-12 13:07

定义抽象类SHAPE表示一般二维图形。Shape具有抽象方法area和perimeter分别计算形状的面积和周长。试定义一些二维形状类(如矩形、三角、圆形、椭圆),这些类均为Shape类的子类。完成相关类的定义后,定义含有main方法的类JLab0506来使用这些类的相应对象,输出相关信息:如对象所代表图形形状、基本数据(半径、长宽高)以及面积、周长等计算公式

谁有最好的方法??

2.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: decun
Posted on: 2005-03-12 14:18


abstract class Shape{
  double width;
  double length;
  double radius;
  double area(double width,double length,double radius);{
    return width*length;
  }
    
  
  abstract double perimeter(double width,double length,double radius){

  }
    
  
  abstract void draw();
}
class Rectangle extends Shape{
  Rectangle(double width,double length,double radius){
    
}
  double perimeter(double width,double length,double radius){

  }
  void draw(){
    
  }
  
}
class Circle extends Shape{
  void draw(){
    
  }
  double perimeter(double width,double length,double radius){

  }
  Circle(double width,double length,double radius){
    
  }  
}
class He4 {

  public static void main(String[] args) {
    Rectangle myRect =new Rectangle(1,1,1);
    Circle myCir =new Circle(2,2,2);
    double theArea;double thePerimeter;
    theArea =myRect.area();
    theArea =myCir.area();
    thePerimeter =myRect.perimeter();
    thePerimeter=myCir.perimeter();
    
  }
}

3.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: decun
Posted on: 2005-03-12 14:46

没人会吗?

4.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: decun
Posted on: 2005-03-12 16:35


class A {
public static void main(String args[]) {
Figue f;
Rectangle r=new Rectangle(12, 14);
f=r;
System.out.println("This is a R" + f.area());
System.out.println("This is a R" + f.perimeter());
}
}
abstract class Figue {
double d1; double d2;
Figue(double a, double b) {
d1=a; d2=b;
}
abstract double area();
abstract double perimeter();

}
class Rectangle extends Figue {
Rectangle(double a, double b) {
super(a, b);
}
double area() {
return d1*d2;
}
double perimeter() {
return d1*2+d2*2;
}
}

5.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: decun
Posted on: 2005-03-12 16:40


class A {
public static void main(String args[]) {
Figue f;
Rectangle r=new Rectangle(12, 14);
f=r;
System.out.println("This is a R" + f.area());
System.out.println("This is a R" + f.perimeter());
}
}
abstract class Figue {
double d1; double d2;
Figue(double a, double b) {
d1=a; d2=b;
}
abstract double area();
abstract double perimeter();

}
class Rectangle extends Figue {
Rectangle(double a, double b) {
super(a, b);
}
double area() {
return d1*d2;
}
double perimeter() {
return d1*2+d2*2;
}
}

6.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: dengyinjun
Posted on: 2005-05-25 03:54

interface class Shape{
double area();{
}
double perimeter(){
}
}
class Rectangle implements Shape{
int width,height;
Rectangle(double width,double length){
this.width = width;
this.height = height;
}
public double area();{
return width*height;
}
public double perimeter(){
return 2*(width+height);
}
}
class Test {
public static void main(String[] args){
Shape shape = new Rectangle(4,5);
System.out.println(""+shape.area());
System.out.println(""+shape.perimeter());
}
}

7.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: dengyinjun
Posted on: 2005-05-25 03:55

interface class Shape{
double area();{
}
double perimeter(){
}
}
class Rectangle implements Shape{
int width,height;
Rectangle(double width,double length){
this.width = width;
this.height = height;
}
public double area();{
return width*height;
}
public double perimeter(){
return 2*(width+height);
}
}
class Test {
public static void main(String[] args){
Shape shape = new Rectangle(4,5);
System.out.println(""+shape.area());
System.out.println(""+shape.perimeter());
}
}
由于在网吧里,就没有测试,...

8.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: 2352439
Posted on: 2005-05-25 19:27

interface Shape
{
   void area();
   void perimeter();
}
    
class Rectangle implements Shape
{
   int width=5;
   int height=6;
   public void area()
   {
     System.out.println("area="+width*height);
   }
   public void perimeter()
   {
     System.out.println("perimeter="+2*(width+height));
   }
}

    
class RT
{
   public void getInfo(Shape s)
   {
     s.area();
     s.perimeter();
   }
}
    
class JLab0506
{
   public static void main(String[] args)
   {
     RT rt=new RT();
     Rectangle r=new Rectangle();
     rt.getInfo(r);
   }
}

其它的自己去扩展

9.Re:大家来看看这个简单的问题 [Re: decun] Copy to clipboard
Posted by: decun
Posted on: 2005-05-26 00:31

thanks everyone
我已经解决了谢谢大家


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923