📄 java2.java
字号:
import java.lang.StrictMath;
abstract class Shape extends Object{
double xPos;
double yPos;
public Shape(){
xPos=0;
yPos=0;
}
public Shape(double x,double y){
xPos=x;
yPos=y;
}
abstract public double area();
abstract public void stretchBy(double factor);
abstract public void printScr();
public final double getXPos(){
return xPos;
}
public final double getYPos(){
return yPos;
}
}
class Rect extends Shape{
double width;
double height;
Rect(double x,double y,double w,double h){
super(x,y);
width=w;
height=h;
}
public void stretchBy(double factor){
width=width*factor;
height=height*factor;
};
public double area(){
double area=width*height;
return area;
}
public void printScr(){
String str;
str="RECTANAGLE\n";
str+="(X,Y)Position:("+super.getXPos()+","+super.getYPos()+")\n";;
str+="Width & Height:"+width+" & "+height+"\n";
str+="Area:"+area()+"\n";
System.out.println(str);
}
}
class Circle extends Shape{
double radius;
Circle(double x,double y,double r){
super(x,y);
radius=r;
}
public void stretchBy(double factor){
radius=radius*factor;
}
public double area(){
double area=StrictMath.PI*radius*radius;
return area;
}
public double Girth(){
double girth=2*StrictMath.PI*radius;
return girth;
}
public void printScr(){
String str;
str="CIRCLE\n";
str+="(X,Y)Position:("+super.getXPos()+","+super.getYPos()+")\n";;
str+="Radius:"+radius+"\n";
str+="Area:"+area()+"\n";
System.out.println(str);
}
}
class Cylinder extends Circle{
double zPos;
double height;
Cylinder(double x,double y,double z,double r,double h){
super(x,y,r);
zPos=z;
height=h;
}
public void stretchBy(double factor){
radius=radius*factor;
height=height*factor;
}
public double area(){
double area=2*super.area()+super.Girth()*height;
return area;
}
public double Volume(){
double volume=super.area()*height;
return volume;
}
public void printScr(){
String str;
str="CYLINDER\n";
str+="(X,Y,Z)Position:("+super.getXPos()+","+super.getYPos()+","+zPos+")\n";;
str+="Radius & Height:"+radius+" & "+height+"\n";
str+="Area:"+area()+"\n";
str+="Volume:"+Volume();
System.out.println(str);
}
}
class TestShape{
public static void main(String args[]){
Shape[] sha=new Shape[3];
sha[0]=new Circle(1.0,2.0,1.0);
sha[1]=new Rect(1.0,2.0,2.0,1.0);
sha[2]=new Cylinder(1.0,2.0,3.0,1.0,2.0);
System.out.println("Before stretching....\n");
sha[0].printScr();
sha[1].printScr();
sha[2].printScr();
sha[0].stretchBy(2.0);
sha[1].stretchBy(2.0);
sha[2].stretchBy(2.0);
System.out.println("\n");
System.out.println("After stretched....\n");
sha[0].printScr();
sha[1].printScr();
sha[2].printScr();
}
}
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////
abstract class Shape extends Object{
private double xPos;
private double yPos;
public Shape(){
xPos = 0;
yPos = 0;
}
abstract public double area();
abstract public void strethBy(double factor);
public final double getXpos(){
return xPos;
}
public final double getYpos(){
return yPos;
}
public void moveTo (double xLoc,double yLoc){
xPos = xLoc;
yPos = yLoc;
}
public String toString(){
String str = "(X,Y)Position:("+ xPos +","+ yPos +")\n";
return str;
}
}
public class Circle extends Shape(){
private R;
final double Pi=3.141592653589793;
public Circle(double r){
super();
R = r;
}
public Circle(double x,double y, double r){
super (x,y);
R = r;
}
public double area(){
return R*R*Pi;
}
public void strechBy(double factor){
R = R*factor;
}
public String toString(){
String str1=super.toString()+"Radius:"+R+"\n"+"Area:"+area()+"\n";
return str1;
}
public class Rectangle extends Shape(){
private W,H;
public Rectangle (double w,double h){
super();
W = w;
H = h;
}
public Rectangle(double x,double y,double w ,double h){
super(x,y);
W = w;
H = h;
}
public double area(){
return W*H;
}
public void strethBy(double factor){
return W = factor*W,H = factor*H;
}
public String toString(){
String str1=super.toString()+"width:"+W+"\n"+"high:"+H+"area:"+area()+"\n";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -