4.1(抽象类)1)定义shape抽象类,包含求⾯积和求周长的⽅法;2)定义
Circle类。。。
摩尔的Java学习笔记4.1
第四周作业:
1、(抽象类)1)定义shape抽象类,包含求⾯积和求周长的⽅法;2)定义Circle类和Rectangle类继承shape类;3)定义
Square类继承Rectangle类;4)运⾏时,让⽤户选择输⼊图形的种类,然后输⼊相应数据,求出该图形的⾯积和周长;
2、(数组)课程管理每学期都要开设多门课程,每门课程的信息包括课程名称、是否是必修课、授课教师、学时、学分;编程实现对
课程信息进⾏创建、添加、删除、修改和查询操作;可以按课程名、是否是必修课、授课教师来进⾏查询;修改或删除之前要先查询;
第五周作业:
3、(接⼝)1)定义shape接⼝,包含求⾯积和求周长的⽅法;2)定义Circle类、Rectangle类、Square类;3)要求Circle类和
Rectangle类实现shape接⼝,Square类继承Rectangle类;4)运⾏时,让⽤户选择输⼊什么图形,然后输⼊相应数据,求出该图形的⾯积和周长;
4、(向量)课程管理 每学期都要开设多门课程,每门课程的信息包括课程名称、是否是必修课、授课教师、学时、学分;编程实现
对课程信息进⾏创建、添加、删除、修改和查询操作;可以按课程名、是否是必修课、授课教师来进⾏查询;修改或删除之前要先查询。
1、(抽象类)1)定义shape抽象类,包含求⾯积和求周长的⽅法;2)定义Circle类和Rectangle类继承shape类;3)定义Square类继承Rectangle类;4)运⾏时,让⽤户选择输⼊图形的种类,然后输⼊相应数据,求出该图形的⾯积和周长;
/**
* @author薛莲婷
*1)定义shape抽象类,包含求⾯积和求周长的⽅法;
*2)定义Circle类和Rectangle类继承shape类;
*3)定义Square类继承Rectangle类;
*4)运⾏时,让⽤户选择输⼊图形的种类,然后输⼊相应数据,求出该图形的⾯积和周长
*/
import java.util.*;
abstract class Shape{//抽象类Shape
abstract float getArea();                  //求⾯积
abstract float getCircumference();          //求周长
}
class Circle extends Shape{//Circle类
private final float PI=3.14f;
protected float radius;
Circle(){}                                  //Circle类构造⽅法
Circle(float r){
radius=r;
}
public float getArea(){                    //Circle类求⾯积
return PI*radius*radius;
}
public float getCircumference(){            //Circle类求周长
return2*PI*radius;
}
}
class Rectangle extends Shape{//Rectangle类
protected float width;
protected float height;
Rectangle(){}                              //Rectangle类构造⽅法
Rectangle(float width,float height){
this.width=width;
this.height=height;
}
public float getArea(){                    //Rectangle类求⾯积
return width*height;
}
public float getCircumference(){            //Rectangle类求周长
return2*(width+height);
}
}
class Square extends Rectangle{//Square类,继承Rectangle类
Square(){}                                  //Square类构造⽅法
Square(float length){
super(length,length);
}
}
class ShapeCalculate {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Shape shape=null;
//得写“null”,不然输出句会显⽰说shape:“The local variable shape may not have been initialized”,它是考虑到else中没有初始化shape对象,虽然从我们        int y;
do{
y=0;nextint()方法
System.out.println("请选择输⼊的图形:1、圆形,2、矩形,3、正⽅形:");
int Int();
//圆形
if(x==1){
System.out.println("请输⼊圆形的半径:");
float Float();
shape=new Circle(radius);
}
//矩形
else if(x==2){
System.out.println("请输⼊矩形的长和宽,以空格相隔:");
float Float();
float Float();
shape=new Rectangle(width,height);
}
/
/正⽅形
else if(x==3){
System.out.println("请输⼊正⽅形的边长:");
float Float();
shape=new Square(length);
}
//输⼊错误
else{
y=1;
System.out.println("输⼊错误,请重新输⼊:");
}
}
}
while(y==1);
System.out.println("⾯积:"+Area()+"\t\t周长:"+Circumference());
sc.close();
}
}