标签:要求 成员变量 ext bubuko test sys 一个 port 子类
设计思路:声明一个父类Shape并声明一个getArea()计算面积的空方法让子类Circle,Echelon,Triangle,Rectangle 继承父类的方法并根据类的不同重写getArea()方法。最后声明一个Test类实现这四个类的方法,实现时用子类赋给父类的方法实现(学以致用);
代码:
package area_design;
//父类
public class Shape {
public double getArea() {
return 0;
}
}
//矩形
public class Rectangle extends Shape {
private double x,y;
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double getArea() {
return x*y;
}
}
//梯形
public class Echelon extends Shape{
private double x1,x2,h;
public void setX1(double x1) {
this.x1 = x1;
}
public void setX2(double x2) {
this.x2 = x2;
}
public void setH(double h) {
this.h = h;
}
public double getArea() {
return (x2+x1)*h/2;
}
}
//三角形
public class Triangle extends Shape{
private double x,h;
public void setX(double x) {
this.x = x;
}
public void setH(double h) {
this.h = h;
}
public double getArea() {
return x*h/2;
}
}
//圆
public class Circle extends Shape{
private double r;
public void setR(double r) {
this.r = r;
}
public double getArea() {
return Math.PI*r*r;
}
}
//Test测试类
import java.util.Scanner;
public class TestShape {
static Scanner scan = new Scanner(System.in);
static double area=0;
static public void countArea(Shape s) {
int x,y,h;
Circle a = new Circle();
Triangle b = new Triangle();
Rectangle c = new Rectangle();
Echelon d = new Echelon();
s = a;
System.out.print("请设置圆形的半径:");
x = scan.nextInt();
a.setR(x);
area+=s.getArea();
s = b;
System.out.print("请设置三角形的底边:");
x = scan.nextInt();
System.out.print("请设置三角形的高度:");
y = scan.nextInt();
b.setH(y);
b.setX(x);
area+=s.getArea();
s = c;
System.out.print("请设置矩形的高:");
x = scan.nextInt();
System.out.print("请设置矩形的宽:");
y = scan.nextInt();
c.setX(x);
c.setY(y);
area+=s.getArea();
s = d;
System.out.print("请设置梯形的高:");
h = scan.nextInt();
System.out.print("请设置梯形的上底边长:");
x = scan.nextInt();
System.out.print("请设置梯形的下底边长:");
y = scan.nextInt();
d.setH(h);
d.setX1(x);
d.setX2(y);
area+=s.getArea();
}
public static void main(String[] args) {
Shape s = new Shape();
TestShape.countArea(s);
System.out.println("他们的面积之和为:"+TestShape.area);
}
}
运行结果
2,抽象类和接口的设计(设计型)。
设计思路:该实验任务有两个,第一个是要求应用abstract抽象类,先声明一个抽象类Shape并声明一个抽象方法showArea();然后声明Rectangle,Circle,Square类继承抽象类Shape并重写showArea()方法;最后用子类化身父类的方法显示他们的面积;
第二个要求是应用interface接口类;
首先声明一个DiagArea接口类,并声明两个方法分别是:getDiagonal(),getArea();然后声明两个类分别是Rectangle_interfaces和Square_interfaces类,在Rectangle_interfaces类进行两个接口方法的重写,Square_interfaces类继承Rectangle_interfaces类并重写继承下来的两个接口方法再声明一个showDiagonalArea()方法输出它的对角线长度,边长以及它的面积;最后声明一个它们的对象实现它们的功能;
注:接口类继承用:implements,抽象类等类继承用:extends;接口可以继承多个用“,”隔开,但抽象类等类只能继承一个;
代码:
package abstract_and_interfaces;
//抽象类
public abstract class Shape {
abstract public void showArea();//用来计算并显示面积
}
//矩形类继承抽象类
public class Rectangle extends Shape{
private double x,y;
public void setRectangle(double a,double b) {
x = a;
y = b;
}
public void showArea() {
System.out.println("矩形的面积为:"+x*y);
}
}
//圆形类继承抽象类
public class Circle extends Shape{
private double r;
public void setCircle(double a) {
r = a;
}
public void showArea() {
System.out.println("圆的面积为:"+r*Math.PI);
}
}
//正方形类继承抽象类
public class Square extends Shape{
private double a;
public void setSquare(double m) {
a = m;
}
public void showArea() {
System.out.println("正方形的面积为:"+a*a);
}
}
//接口类
public interface DiagArea {
public double getDiagonal();
public double getArea();
}
//连接接口
public class Rectangle_interfaces implements DiagArea{
private double w,h;
public void setRectangle(double a,double b) {
w = a;
h = b;
}
@Override
public double getDiagonal() {
return Math.sqrt(w*w+h*h);
}
@Override
public double getArea() {
return w*h;
}
}
//正方形继承长方形
public class Square_interfaces extends Rectangle_interfaces{
private double x,diagonal,area;
public void setSquare(double a) {
x = a;
}
public double getDiagonal() {
diagonal = Math.sqrt(x*x*2);
return diagonal;
}
public double getArea() {
area = x*x;
return area;
}
public void showDiagonalArea() {
System.out.println("正方形的边长为:"+x);
System.out.println("正方形的对角线为:"+getDiagonal());
System.out.println("正方形的面积为:"+ getArea());
}
}
//测试类
public class Test {
public static void main(String[] args) {
/*---------抽象类--------*/
Shape A;
Rectangle a = new Rectangle();
Square b = new Square();
Circle c = new Circle();
a.setRectangle(3, 4);
b.setSquare(2);
c.setCircle(1);
A = a;
A.showArea();
A = b;
A.showArea();
A = c;
A.showArea();
/*------------接口------------*/
DiagArea s;
Rectangle_interfaces n = new Rectangle_interfaces();
Square_interfaces m = new Square_interfaces();
n.setRectangle(3, 4);
s = n;
System.out.println("矩形的对角线长为:"+s.getDiagonal());
System.out.println("矩形的面积为:"+s.getArea());
m.setSquare(3);
m.showDiagonalArea();
}
}
实验截图:
3,班级信息管理类设计(设计综合型实验)
设计思路:很简单就是声明一个人的基本类Mankind,再声明一个大学生类Student,让Student继承Mankind类在两个类中分别声明它们属性即成员变量的获取和设置的方法;由于增删改查系统做过太多次就不做详细介绍;直接拿原来的增删改查系统改变一下类就可以了;
代码:
package Class_message_management;
//人的基本类
public class Mankind {
private String sex,bornday;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBornday() {
return bornday;
}
public void setBornday(String bornday) {
this.bornday = bornday;
}
//大学生类
public class Student extends Mankind{
private String name,number,comfrom,score;
Student() {
number = null;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getComfrom() {
return comfrom;
}
public void setComfrom(String comfrom) {
this.comfrom = comfrom;
}
}
}
import java.util.Scanner;
public class Manager {
public Scanner scan;
final int cc=5;
private int select;
public Student student[];
Manager() //构造函数
{
student=new Student[cc]; //对Student类student初始化
for(int i=0;i<cc;i++)
{
student[i]=new Student();
}
scan=new Scanner(System.in);
}
//-----<<--判断条件-->>-----
public boolean addcan () //判断是否可以添加
{
for(int i = 0;i < cc;i++)
{
if(student[i].getNumber()==null )
return true;
}
return false;
}
public int number() //找到可以添加学生信息的下标
{
for(int i = 0;i < cc;i++)
{
if(student[i].getNumber()==null )
return i;
}
return -1;
}
public int lookp(String num) //查找学生编号
{
String mm;
for(int i = 0;i < student.length;i++)
{
mm = student[i].getNumber();
if(mm.equals(num))
return i;
}
return -1;
}
public boolean test(String num) //判断输入的学号是否重名
{
String mm;
for(int i = 0;i <student.length;i++)
{
mm = student[i].getNumber();
if(mm==null)
return true;
if(mm.equals(num))
return false;
}
return true;
}
//----<<--实现功能-->>-----
public void add(String nam,String num,String sex,String bornday,String score,String comfrom) //添加学生信息
{
int he = number();
student[he].setName(nam);
student[he].setNumber(num);
student[he].setSex(sex);
student[he].setBornday(bornday);
student[he].setScore(score);
student[he].setComfrom(comfrom);
}
public void delet(String num) //删除学生信息
{
int i=lookp(num);
student[i].setNumber(null);;
}
public void show() //展示学生信息
{
for(int i = 0;i < cc;i++)
{
if(student[i].getNumber()!=null)
{
System.out.println("学生姓名:"+student[i].getName());
System.out.println("学生学号:"+student[i].getNumber());
System.out.println("学生性别:"+student[i].getSex());
System.out.println("学生出生日期:"+student[i].getBornday());
System.out.println("学生入学成绩:"+student[i].getScore());
System.out.println("学生籍贯:"+student[i].getComfrom());
System.out.println("-----------------------------------");
System.out.println();
}
}
}
//----<<--界面,界面信息与功能实现连接-->>----
public void start()
{
System.out.println("---------------------------------");
System.out.println("\t"+ "学生教务系统v2.0");
System.out.println("---------------------------------");
System.out.println("\t"+"1.添加学生信息。");
System.out.println("\t"+"2.修改学生信息。");
System.out.println("\t"+"3.查询学生信息。");
System.out.println("\t"+"4.删除学生信息。");
System.out.println("\t"+"5.展示学生信息。");
System.out.println("\t"+"6.退出学生系统。");
System.out.println("---------------------------------");
System.out.println("请输入您想要实现功能的序号");
if(scan.hasNext())
{
select=scan.nextInt();
}
switch (select) {
case 1: //实现1所代表的功能
{
if(addcan())
{
System.out.println("请输入学生的学号");
String nam,ids,comfrom,score,sex,bornday;
ids = scan.next();
if(!test(ids))
{
System.out.println("该学号已存在");
//System.out.println("请重新输入!");
start();
}
else
{
System.out.println("请输入学生姓名");
nam = scan.next();
System.out.println("请输入学生性别");
sex = scan.next();
System.out.println("请输入学生出生日期");
bornday = scan.next();
System.out.println("请输入学生入学成绩");
score = scan.next();
System.out.println("请输入学生籍贯");
comfrom = scan.next();
add(nam,ids,sex,bornday,score,comfrom);
System.out.println("添加成功");
show();
start();
}
}
else
{
System.out.println("教务系统人数已满");
start();
}
}break;
case 2: //实现2所代表的功能
{
System.out.print("请输入您所想要修改的学生学号:");
String ids,comfrom,score,sex,bornday;
ids = scan.next();
if(!test(ids))
{
String id;
System.out.print("请输入设置后的学生学号:");
id = scan.next();
student[lookp(ids)].setNumber(id);
System.out.print("请输入设置后的学生姓名:");
String nam;
nam = scan.next();
student[lookp(id)].setName(nam);
System.out.print("请输入设置后的学生性别:");
sex = scan.next();
student[lookp(id)].setSex(sex);
System.out.print("请输入设置后的学生出生日期:");
bornday = scan.next();
student[lookp(id)].setBornday(bornday);
System.out.print("请输入设置后的学生入学成绩:");
score = scan.next();
student[lookp(id)].setScore(score);
System.out.print("请输入设置后的学生籍贯:");
comfrom = scan.next();
student[lookp(id)].setComfrom(comfrom);
System.out.println("成功修改该学生信息");
show();
start();
}
else
{
System.out.println("您所输入的学号不存在!");
//System.out.println("请重新输入!");
start();
}
}break;
case 3: //实现3所代表的功能
{
System.out.print("请输入您想要查询的学生学号:");
String num;
num = scan.next();
if(!test(num))
{
System.out.println("学生姓名:"+student[lookp(num)].getName());
System.out.println("学生学号:"+student[lookp(num)].getNumber());
System.out.println("学生性别:"+student[lookp(num)].getSex());
System.out.println("学生出生日期:"+student[lookp(num)].getBornday());
System.out.println("学生入学成绩:"+student[lookp(num)].getScore());
System.out.println("学生籍贯:"+student[lookp(num)].getComfrom());
System.out.println("-----------------------------------");
System.out.println();
start();
}
else
{
System.out.println("该学号不存在!");
//System.out.println("请重新输入:");
start();
}
}break;
case 4:
{
System.out.print("请输入您想要删除的学生学号:");
String ids;
ids = scan.next();
if(!test(ids))
{
delet(ids);
System.out.println("删除成功!");
start();
}
else
{
System.out.println("您输入的学生学号不存在!");
//System.out.println("请重新输入:");
start();
}
}break;
case 5:
{
show();
start();
}break;
case 6:
{
return ;
}
default: {
System.out.println("您输入的序号错误!");
start();
}
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Manager aa = new Manager();
aa.start();
}
}
实验截图:
4,按照题目要求设计并编写一个 JAVA 程序(综合设计型)
设计思路:声明两个接口Student和Teacher;Student接口中声明setFee()和getFee()方法,Teacher接口声明setPay()和getPay()方法;声明一个Graduate类连接这两个接口并重写接口里的方法,再声明一个方法计算是否贷款;
代码:
package teachers_students;
//老师接口
public interface TeacherManageInterface {
public void setPay(int a);
public int getPay();
}
//学生接口
public interface StudentInterface {
public void setFee(int a);
public int getFee();
}
//研究生类连接两个接口
public class Graduate implements TeacherManageInterface,StudentInterface{
private int fee,pay,age;
private String name,sex;
Graduate(String nam,String Sex,int Age){
name = nam;
sex = Sex;
age = Age;
}
@Override
public void setPay(int a) {
pay = a;
}
@Override
public int getPay() {
return pay;
}
@Override
public void setFee(int a) {
// TODO 自动生成的方法存根
fee = a;
}
@Override
public int getFee() {
// TODO 自动生成的方法存根
return fee;
}
public void whetherloan() {
if(getPay()*12-getFee()*2<2000)
System.out.println("provide a loan");
}
public static void main(String[] args) {
Graduate a = new Graduate("zhangsan", "woman", 35);
a.whetherloan();
}
}
实验截图:
标签:要求 成员变量 ext bubuko test sys 一个 port 子类
原文地址:https://www.cnblogs.com/goubb/p/9899999.html