l练习题代码
求4.3后面的练习题代码??
求4.3后面的练习题代码??
2016-11-02
package Shape; public abstract class Shape { public abstract void Girth(); public abstract void Area(); public static void main(String args[]){ Shape r = new Rectangle(5,4); r.Girth(); r.Area(); Shape c = new Circle(5); c.Girth(); c.Area(); } } package Shape; public abstract class Shape { public abstract void Girth(); public abstract void Area(); public static void main(String args[]){ Shape r = new Rectangle(5,4); r.Girth(); r.Area(); Shape c = new Circle(5); c.Girth(); c.Area(); } } package Shape; public class Circle extends Shape { int radius; public Circle(int radius){ this.radius = radius; } public void Girth() { double Girth = radius * 2 * 3.14; System.out.println("圆形的周长为:" + Girth); } public void Area() { double Area = radius * radius * 3.14; System.out.println("圆形的面积为:" + Area); } }
class Text //测试类
{
public static void main(String[] args)
{
Shape s1 = new Rectangle1();
Shape s2 = new Circle();
s1.girth(3,4);
s1.size(5,2);
s2.girth(6.0);
s2.size(10.0);
}
}
abstract class Shape //父类
{
public abstract void girth(int a,int b);
public abstract void size(int a,int b);
public abstract void girth(double r);
public abstract void size(double r);
}
class Rectangle1 extends Shape//子类1
{
public void girth(int a,int b)
{
int gir;
gir = (a + b)*2;
System.out.println("矩形的周长为:" + gir );
}
public void size(int a,int b)
{
int si;
si = a * b;
System.out.println("矩形的面积为:" + si);
}
public void girth(double r){}
public void size(double r){}
}
class Circle extends Shape//子类2
{
double pai = 3.1415926;
double r;
public void girth(double r)
{
double gir = pai *r;
System.out.println("圆的周长为:" + gir);
}
public void size(double r)
{
double si;
si = pai * (r *r)/4;
System.out.println("圆的面积为:" + si);
}
public void girth(int a,int b){}
public void size(int a,int b){}
}
我用抽象写不出来啊,一直报错 类不是抽象的,并且未覆盖抽象方法,最后只能这样写了
public class Text //测试类
{
public static void main(String[] args)
{
Shape s1 = new Rectangle1();
Shape s2 = new Circle();
s1.zhouChang(3,4);
s1.squareMeasure(5,2);
s2.zhouChang(6.0);
s2.squareMeasure(10.0);
}
}
class Shape //父类
{
public void zhouChang(int a,int b){}
public void squareMeasure(int a,int b){}
public void zhouChang(double r){}
public void squareMeasure(double r){}
}
class Rectangle1 extends Shape //矩形
{
public void zhouChang(int a,int b)
{
int zc;
zc = (a + b)*2;
System.out.println("矩形的周长为:" + zc );
}
public void squareMeasure(int a,int b)
{
int sm;
sm = a * b;
System.out.println("矩形的面积为:" + sm);
}
}
class Circle extends Shape //圆形
{
double pai = 3.1415926;
public void zhouChang(double r)
{
double zc = pai *r;
System.out.println("圆的周长为:" + zc);
}
public void squareMeasure(double r)
{
double sm;
sm = pai * (r *r)/4;
System.out.println("圆的面积为:" + sm);
}
}
package com.Shape; public abstract class Shape { public abstract void girth(); public abstract void area(); } package com.Shape; public class Rentanle extends Shape { public void girth() { // TODO Auto-generated method stub System.out.println("长方形的周长=2*(长+宽)"); } public void area() { // TODO Auto-generated method stub System.out.println("长方形的面积=长*宽"); } } package com.Shape; public class Circle extends Shape { public void girth() { // TODO Auto-generated method stub System.out.println("圆形的周长=π*直径"); } public void area() { // TODO Auto-generated method stub System.out.println("圆形的面积=π*r*r"); } } package com.Shape; public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Shape s1 =new Rentanle(); s1.area(); s1.girth(); Shape s2=new Circle(); s2.area(); s2.girth(); } }
亲测可用,好像有个单词写错了,但是懒得改了。给你参考一下吧,交流一下。
举报