作业day07
来源:互联网 发布:国外注册域名要备案吗 编辑:程序博客网 时间:2024/05/29 14:31
总结与体会:
今天继续Java的类与对象。自己设计类、接口、抽象类,当自己设置好了还是蛮有成就感。
Debug的使用还是挺方便理解程序的运行过程。
习题
1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。创建类Student实现Person接口,并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。
package com.jiale.day07;
public interface Person {
void setDate(Stringname, String sex, Stringbirthday);
String getDate();
}
package com.jiale.day07;
public class Student implements Person {
private Stringname;
private Stringsex;
private Stringbirthday;
private StringsID;
private Stringspeciality;
public Student(Stringname, String sex, Stringbirthday, String sID, Stringspeciality) {
// TODO自动生成的构造函数存根
this.name =name;
this.sex =sex;
this.birthday =birthday;
this.sID =sID;
this.speciality =speciality;
}
public Student(){}
public String getsID() {
returnsID;
}
public void setsID(String sID) {
this.sID =sID;
}
public String getSpeciality() {
returnspeciality;
}
public void setSpeciality(String speciality) {
this.speciality =speciality;
}
@Override
public void setDate(String name, Stringsex, String birthday) {
// TODO自动生成的方法存根
this.name =name;
this.sex =sex;
this.birthday =birthday;
}
@Override
public String getDate() {
// TODO自动生成的方法存根
String date = "姓名:"+name+"\n性别:"+sex+"\n生日:"+birthday+"\n学生ID:"+sID+"\n特长:"+speciality;
returndate;
}
}
package com.jiale.day07;
public class demo01 {
public static void main(String[] args) {
// TODO自动生成的方法存根
Student stu1 = new Student();
stu1.setDate("张三","男","1月1日");
stu1.setsID("001");
stu1.setSpeciality("编程");
System.out.println(stu1.getDate()+"\n");
Student stu2 = new Student("李四","男","2月2日","002", "数学");
System.out.println(stu2.getDate());
}
}
输出结果:
姓名:张三
性别:男
生日:1月1日
学生ID:001
特长:编程
姓名:李四
性别:男
生日:2月2日
学生ID:002
特长:数学
2、编写程序,求柱体的体积:
(1)、为柱体的底面设计一个接口Geometry,包含计算面积的方法getArea();
(2)、为柱体设计类pillar,要求:
a)有两个成员变量,底面和高度。底面是任何可以计算面积的几何形状。
b)实现构造方法,对成员变量赋值。
c)包含成员方法,计算柱体pillar的体积。
(3)、编写测试类圆形类、矩形类实现Geometry接口,编写测试类Test,分别用圆形、矩形作为柱体的底面,并计算其体积。
package com.jiale.day07;
public interface Geometry {
double getArea();
}
package com.jiale.day07;
public class Circle implements Geometry {
private double radius;
public Circle() {}
public Circle(doubleradius) {
// TODO自动生成的构造函数存根
this.radius =radius;
}
public double getRadius() {
returnradius;
}
public void setRadius(doubleradius) {
this.radius =radius;
}
@Override
public double getArea() {
// TODO自动生成的方法存根
return 3.14*radius*radius;
}
}
package com.jiale.day07;
public class Rect implements Geometry {
private double length;
private double wide;
public Rect() {}
public Rect(doublelength, doublewide) {
// TODO自动生成的构造函数存根
this.length =length;
this.wide =wide;
}
public double getLength() {
returnlength;
}
public void setLength(intlength) {
this.length =length;
}
public double getWide() {
returnwide;
}
public void setWide(intwide) {
this.wide =wide;
}
@Override
public double getArea() {
// TODO自动生成的方法存根
returnlength*wide;
}
}
package com.jiale.day07;
public class Pillar {
private double bottomArea;
private double high;
public Pillar() {}
public Pillar(doublebottomArea, doublehigh) {
// TODO自动生成的构造函数存根
this.bottomArea =bottomArea;
this.high =high;
}
public double getBottomArea() {
returnbottomArea;
}
public void setBottomArea(doublebottomArea) {
this.bottomArea =bottomArea;
}
public double getHigh() {
returnhigh;
}
public void setHigh(doublehigh) {
this.high =high;
}
public void getPillarVolume(){
System.out.println("此柱体的体积为:"+bottomArea*high);
}
}
package com.jiale.day07;
public class dome02 {
public static void main(String[] args) {
// TODO自动生成的方法存根
Circle cir = new Circle(3.0);
Rect rec = new Rect(1.5, 2.1);
Pillar pil1 = new Pillar(cir.getArea(), 5.0);
Pillar pil2 = new Pillar(rec.getArea(), 5.0);
pil1.getPillarVolume();
pil2.getPillarVolume();
}
}
输出结果:
此柱体的体积为:141.29999999999998
此柱体的体积为:15.750000000000002
3设计一个系统:
xxx纯净水生产线
目前流程是:从某个地方把水取出来,然后经过缓冲,过滤,加热和放糖的步骤
abstract 水{
public void 水();
}
interface 过滤{}
interface 缓冲{}
interface 加热{}
interface 放糖{}
class 纯净水1 extends水 imps 过滤,缓冲{}
class 纯净水2 extends水 imps 缓冲{}
class 纯净水2 extends水 imps 过滤{}
package com.jiale.day03;
public abstract class Water {
public abstract void Water();
}
package com.jiale.day03;
public interface Buffer {
public abstract void Buffer();
}
package com.jiale.day03;
public interface Filter {
public abstract void Filter();
}
package com.jiale.day03;
public interface Heat {
public abstract void Heat();
}
package com.jiale.day03;
public interface Sugar {
public abstract void Sugar();
}
package com.jiale.day03;
public class Water1 extends Waterimplements Filter, Buffer {
@Override
public void Buffer() {
// TODO自动生成的方法存根
System.out.println("水已经缓冲\n");
}
@Override
public void Filter() {
// TODO自动生成的方法存根
System.out.println("水已经过滤\n");
}
@Override
public void Water() {
// TODO自动生成的方法存根
System.out.println("水1的制造过程:\n");
}
}
package com.jiale.day03;
public class Water2 extends Waterimplements Buffer {
@Override
public void Buffer() {
// TODO自动生成的方法存根
System.out.println("水已经缓冲\n");
}
@Override
public void Water() {
// TODO自动生成的方法存根
System.out.println("水2的制造过程:\n");
}
}
package com.jiale.day03;
public class Water3 extends Waterimplements Filter {
@Override
public void Filter() {
// TODO自动生成的方法存根
System.out.println("水已经过滤\n");
}
@Override
public void Water() {
// TODO自动生成的方法存根
System.out.println("水3的制造过程:\n");
}
}
运算结果:
水1的制造过程:
水已经缓冲
水已经过滤
水2的制造过程:
水已经缓冲
水3的制造过程:
水已经过滤
- 作业day07
- day07
- day07
- day07
- day07
- day07
- day07
- day07
- day07
- day07
- day07
- day07
- Day07
- day07
- day07
- day07
- Java 大作业回忆--day07-- 我们的作品完成啦
- day07-tomcat
- vnc 全屏设置 window和ubuntu客户端的使用
- leetcode之first bad version
- WPF 按钮背景图片替换设置
- JSON数据格式
- Random intersection
- 作业day07
- MOOC Machine Learning 作业交流帖7
- 构造函数 和 析构函数
- (GOF23设计模式)_单例模式_应用场景_饿汉式_懒汉式
- No Launcher activity found!The launch will only sync the application package on the device!
- Unity3D学习笔记(十四)GUI
- solr4.9.0部署到tomcat7,并整合IKAnalyzer分词器
- java基本语法
- XCode7 导入第三方库Apple Pay报警告的处理