import java.lang.Math.*;
import java.io.*;abstract class Shape {
void draw(){ };
abstract double area();
abstract double length();
} class Circle extends Shape {
private int radius;
Circle(int radius){
this.radius=radius;
}
void draw() {
System.out.println("Circle.draw()");
}
double area(){
double ara=3.14*radius*radius;
System.out.println(ara);
return ara;
}
double length() {
double leng;
leng=6.28*radius;
System.out.println(leng);
return leng;
}
}
class Triangle extends Shape {
private int weight,height;
Triangle(int weight,int height){
this.weight=weight;
this.height=height;
}
void draw() {
System.out.println("Triangle.draw()");
}
double area() {
double ara;
ara=weight*height;
System.out.println(ara);
return ara;
} double length() {
double leng;
leng=2*weight+2*height;
System.out.println(leng);
return leng;
}
} class Tangle extends Shape {
private int edge1,edge2,edge3;
Tangle(int edge1,int edge2,int edge3){
this.edge1=edge1;
this.edge2=edge2;
this.edge3=edge3;
}
void draw() {
System.out.println("Tangle.draw()");
}
double area() {
double ara;
int s=(edge1+edge2+edge3)/2;
ara=Math.sqrt((double)(s*(s-edge1)*(s-edge2)*(s-edge3)));
System.out.println(ara);
return ara;
} double length() {
double leng;
leng=(double)(edge1+edge2+edge3);
System.out.println(leng);
return leng;
}
}
public class Shapes {
public static void main(String[] args) {
double length=0;
double area=0;
Shape pt[] =new Shape[4];
for(int i=0;i<4;i++)
{
char ch=(char)(Math.random()*3+'a');
switch(ch)
{
case 'a':
int w=(int)(Math.random()*8+10);
int h=(int)(Math.random()*8+10);
System.out.println("weight is:"+w);
System.out.println("height is:"+h);
pt[i]=new Triangle(w,h);
pt[i].draw();
pt[i].area();
pt[i].length();
length+=pt[i].length();
area+=pt[i].area();
break;
case 'b':
int r=(int)(Math.random()*8+10);
System.out.println("radius is:"+r);
pt[i]=new Circle(r);
pt[i].draw();
pt[i].area();
pt[i].length();
length+=pt[i].length();
area+=pt[i].area();
break;
case 'c':
int e1=(int)(Math.random()*8+10);
int e2=(int)(Math.random()*8+10);
int e3=(int)(Math.random()*8+10);
System.out.println("first edge is:"+e1);
System.out.println("second edge is:"+e2);
System.out.println("third edge is:"+e3);
pt[i]=new Tangle(e1,e2,e3);
pt[i].draw();
pt[i].area();
pt[i].length();
length+=pt[i].length();
area+=pt[i].area();
break;
}
System.out.println("the total length is:"+length);
System.out.println("the total area is:"+area);
}
}
}
我是真人吗?
为啥我的画图不出现就交呢!
各位你们好吧
小的不是人吗
要帮住
出来了
交了
受在up
有分
import java.io.*;abstract class Shape {
void draw(){ };
abstract double area();
abstract double length();
} class Circle extends Shape {
private int radius;
Circle(int radius){
this.radius=radius;
}
void draw() {
System.out.println("Circle.draw()");
}
double area(){
double ara=3.14*radius*radius;
System.out.println(ara);
return ara;
}
double length() {
double leng;
leng=6.28*radius;
System.out.println(leng);
return leng;
}
}
class Triangle extends Shape {
private int weight,height;
Triangle(int weight,int height){
this.weight=weight;
this.height=height;
}
void draw() {
System.out.println("Triangle.draw()");
}
double area() {
double ara;
ara=weight*height;
System.out.println(ara);
return ara;
} double length() {
double leng;
leng=2*weight+2*height;
System.out.println(leng);
return leng;
}
} class Tangle extends Shape {
private int edge1,edge2,edge3;
Tangle(int edge1,int edge2,int edge3){
this.edge1=edge1;
this.edge2=edge2;
this.edge3=edge3;
}
void draw() {
System.out.println("Tangle.draw()");
}
double area() {
double ara;
int s=(edge1+edge2+edge3)/2;
ara=Math.sqrt((double)(s*(s-edge1)*(s-edge2)*(s-edge3)));
System.out.println(ara);
return ara;
} double length() {
double leng;
leng=(double)(edge1+edge2+edge3);
System.out.println(leng);
return leng;
}
}
public class Shapes {
public static void main(String[] args) {
double length=0;
double area=0;
Shape pt[] =new Shape[4];
for(int i=0;i<4;i++)
{
char ch=(char)(Math.random()*3+'a');
switch(ch)
{
case 'a':
int w=(int)(Math.random()*8+10);
int h=(int)(Math.random()*8+10);
System.out.println("weight is:"+w);
System.out.println("height is:"+h);
pt[i]=new Triangle(w,h);
pt[i].draw();
pt[i].area();
pt[i].length();
length+=pt[i].length();
area+=pt[i].area();
break;
case 'b':
int r=(int)(Math.random()*8+10);
System.out.println("radius is:"+r);
pt[i]=new Circle(r);
pt[i].draw();
pt[i].area();
pt[i].length();
length+=pt[i].length();
area+=pt[i].area();
break;
case 'c':
int e1=(int)(Math.random()*8+10);
int e2=(int)(Math.random()*8+10);
int e3=(int)(Math.random()*8+10);
System.out.println("first edge is:"+e1);
System.out.println("second edge is:"+e2);
System.out.println("third edge is:"+e3);
pt[i]=new Tangle(e1,e2,e3);
pt[i].draw();
pt[i].area();
pt[i].length();
length+=pt[i].length();
area+=pt[i].area();
break;
}
System.out.println("the total length is:"+length);
System.out.println("the total area is:"+area);
}
}
}
我是真人吗?
为啥我的画图不出现就交呢!
各位你们好吧
小的不是人吗
要帮住
出来了
交了
受在up
有分
解决方案 »
- 英文的文档,有些地方看不懂,请大家帮忙!
- JTable如何设某行只读
- java程序需要用jdk1.1编辑,但是程序中有jdk2的类,该怎么办呢??
- 如何统计一个目录下的文件数量?
- 求助大大们,jframe关闭问题
- 有没有进程之间通信的资料?
- 用java编译成.class出德问题,请指点下:
- 为什么用javamail连接邮箱出现NoSuchProviderException?怎么没有provider呢?求救
- 安完JDK 1.4怎么一运行总出错啊??
- Java小编程
- 大家帮我看一下这个程序,为什么执行后会提示有错误!!急,在线等!
- 多多都给...急急急。。。请问哪里有保存邮件附件的JSP源程序??
import java.io.InputStreamReader;
import java.io.IOException;abstract class CShape{
public abstract float getLength();
public abstract float getArea();
public abstract boolean isValid();
public abstract int getType();
public abstract String getStrType();
}class CRectangle extends CShape{
public CRectangle(float fWidth,float fHeight)
{
m_fWidth=fWidth;
m_fHeight=fHeight;
if(m_fWidth<=0.0f)
m_fWidth=1.0f;
if(m_fHeight<=0.0f)
m_fHeight=1.0f;
} public float getLength()
{
if(!isValid())
return 0.0f; return m_fWidth*2+m_fHeight*2;
} public float getArea()
{
if(!isValid())
return 0.0f;
return m_fWidth*m_fHeight;
} public boolean isValid()
{
if(m_fWidth>0.0f && m_fHeight>0.0f)
return true; return false;
} public int getType()
{
return 1;
} public String getStrType()
{
return "Rectangle";
}public float m_fWidth;
public float m_fHeight;
}class CCircle extends CShape{
public CCircle(float fRadius)
{
m_fRadius=fRadius;
if(!isValid())
m_fRadius=1.0f;
} public float getLength()
{
if(!isValid())
return 0.0f; return m_fRadius*2*3.14f;
} public float getArea()
{
if(!isValid())
return 0.0f;
return m_fRadius*m_fRadius*3.14f;
} public boolean isValid()
{
if(m_fRadius>0.0f)
return true; return false;
} public int getType()
{
return 2;
} public String getStrType()
{
return "Circle";
}public float m_fRadius;
}class CTriangle extends CShape{
public CTriangle(float fEdge1,float fEdge2,float fEdge3)
{
m_fEdge1=fEdge1;
m_fEdge2=fEdge2;
m_fEdge3=fEdge3; if(!isValid()){
m_fEdge1=3.0f;
m_fEdge2=4.0f;
m_fEdge3=5.0f;
}
} public float getLength()
{
if(!isValid())
return 0.0f;
return m_fEdge1+m_fEdge2+m_fEdge3;
} public float getArea()
{
if(!isValid())
return 0.0f; double e1s=m_fEdge1*m_fEdge1,e2s=m_fEdge2*m_fEdge2,e3s=m_fEdge3*m_fEdge3;
double dVals=(e1s+e3s-e2s)*(e1s+e3s-e2s);
double fTmp=java.lang.Math.sqrt(4*e1s*e3s-dVals); return (float)(0.25f*fTmp);
} public boolean isValid()
{
if(m_fEdge1<=0.0f || m_fEdge2<=0.0f || m_fEdge3<=0.0f)
return false;
if(m_fEdge1+m_fEdge2<=m_fEdge3 || m_fEdge1+m_fEdge3<=m_fEdge2 ||
m_fEdge3+m_fEdge2<=m_fEdge1)
return false; return true;
} public int getType()
{
return 3;
} public String getStrType()
{
return "Triangle";
}public float m_fEdge1;
public float m_fEdge2;
public float m_fEdge3;
}public class shapes{
static int numOfTypes=3;
public static void main(String args[]){
int numOfInstance=20;
System.out.println("\n");
if(args.length==1){
int count;
try{
count = Integer.parseInt(args[0]);
}catch(NumberFormatException e){
count=numOfInstance;
System.out.println("Invalid parameter number '"+args[0]+"',use default value ("+numOfInstance+").");
}
if(count>0 && count<=50)
numOfInstance=count;
if(count > 50){
System.out.println("WARNNING:Too large shape number: "+count+";\n default value("+numOfInstance+") used.\n");
}
}
CShape pShapes[]=new CShape[numOfInstance];
double rv;
for(int i=0;i<numOfInstance;i++){
rv=java.lang.Math.random();
if(rv>=0 && rv<1.0f/numOfTypes)
pShapes[i]=new CRectangle((float)java.lang.Math.random()*10,(float)java.lang.Math.random()*10); if(rv>=1.0f/numOfTypes && rv<2*1.0f/numOfTypes)
pShapes[i]=new CCircle((float)java.lang.Math.random()*10); if(rv>=2*1.0f/numOfTypes && rv<=3*1.0f/numOfTypes)
pShapes[i]=new CTriangle((float)java.lang.Math.random()*10,(float)java.lang.Math.random()*10,
(float)java.lang.Math.random()*10);
}
float fTotalArea=0.0f,fTotalLen=0.0f,fArea,fLen;
int numOfCircle=0,numOfRect=0,numOfTriangle=0;
for(int i=0;i<numOfInstance;i++){
if(pShapes[i].getType()==1)
numOfRect++;
if(pShapes[i].getType()==2)
numOfCircle++;
if(pShapes[i].getType()==3)
numOfTriangle++; fArea=pShapes[i].getArea();
fLen=pShapes[i].getLength();
fTotalArea+=fArea;
fTotalLen+=fLen;
System.out.println((i+1)+"th: Length("+fLen+") Area("+fArea+") Type("+pShapes[i].getStrType()+").");
}
System.out.println("________________________________________________________");
System.out.println("\n\n **statistic**\n"); System.out.println(" Total area: "+fTotalArea);
System.out.println(" Total length: "+fTotalLen+"\n");
System.out.println(" Number of shapes: "+numOfInstance);
System.out.println("Number of rectangles: "+numOfRect);
System.out.println(" Number of circles: "+numOfCircle);
System.out.println(" Number of Triangles: "+numOfTriangle);
}
}我同学做的大家看一下
我上面的人都加分