金陵科技学院实验报告
private double height; /**
* 计算体积 * @return */
public double calculatorVolumn(){
return bottom.calculatorArea() * height; }
public ZhuTi(Bottom bottom, double height) { super();
this.bottom = bottom; this.height = height; }
public Bottom getBottom() { return bottom; }
public void setBottom(Bottom bottom) { this.bottom = bottom; }
public double getHeight() { return height; }
public void setHeight(double height) { this.height = height; }
public void changeBottom(Bottom bottom){ this.bottom = bottom; } }
public class VolumnTest { //测试类 public static void main(String[] args) { Bottom bottom = new CircleBottom(1.0); double height = 1.0;
ZhuTi zhuTi = new ZhuTi(bottom,height); double result = zhuTi.calculatorVolumn();
System.out.println(\圆柱体的体积是:\ bottom = new SquareBottom(1.0,1.0); zhuTi.changeBottom(bottom);
result = zhuTi.calculatorVolumn();
System.out.println(\立方体的体积是:\ } }
范例:接口和多态的应用,例如:电脑上实现了USB接口,U盘,打印机等等也都实现了此标准。
金陵科技学院实验报告
interface USB{
public void start() ; // 开始工作 public void stop() ; // 结束工作 }
class Computer{
public static void plugin(USB usb){ usb.start() ; usb.stop() ; } };
class Flash implements USB{ public void start(){
System.out.println(\盘开始工作。\ }
public void stop(){
System.out.println(\盘停止工作。\ } };
class Print implements USB{ public void start(){
System.out.println(\打印机开始工作。\ }
public void stop(){
System.out.println(\打印机停止工作。\ } };
public class InterPolDemo02{
public static void main(String args[]){ Computer.plugin(new Flash()) ; Computer.plugin(new Print()) ; } };
对照范例,写出以下程序: (1) 乐器(Instrument)的标准为弹奏(play),而乐器类型分为:钢琴(Piano)
和小提琴(Violin),各种乐器的弹奏方法各不同。编写代码实现不同乐器的弹奏。 (2) 计算机模拟
四、实验结果与分析(程序运行结果及其分析) 五、实验体会
金陵科技学院实验报告
实验项目名称: 类集 实验学时: 4 同组学生姓名: 实验地点: 实验日期: 实验成绩: 批改教师: 批改时间:
金陵科技学院实验报告
实验2 类集
一、实验目的和要求 (1)理解类集概念
(2)熟悉Collection接口、List接口、Set接口和Map接口 (3)掌握ArrayList类、HashSet类和TreeSet类 (4)理解TreeMap、HashMap 二、实验仪器和设备
奔腾以上个人计算机, windows操作系统。
配置好JDK环境,安装集成开发环境(Eclipse) 三、实验内容与过程 1、类集应用
范例:实现一个超市管理系统,要求可以添加货物,删除货物和查询货物:
。 代码如下:
public interface Goods {
public String getName(); // 得到商品名称
public int getCount(); // 得到商品数量
public float getPrice(); // 得到商品价格 }
public class Book implements Goods {
private String name; private int count; private float price;
public Book() { }
public Book(String name, int count, float price) { this.name = name; this.count = count; this.price = price; }
public String getName() { return name; }
public void setName(String name) { this.name = name;
金陵科技学院实验报告
}
public int getCount() { return count; }
public void setCount(int count) { this.count = count; }
public float getPrice() { return price; }
public void setPrice(float price) { this.price = price; }
public boolean equals(Object obj) { if (this == obj) { return true; }
if (!(obj instanceof Book)) { return false; }
Book b = (Book) obj;
if (b.name.equals(this.name) && b.count == this.count && b.price == this.price) { return true; } else {
return false; } }
public int hashCode() { return this.name.hashCode() + new Integer(this.count).hashCode() + new Float(this.price).hashCode(); }
public String toString() {
return \书名:\;书的价格:\;书的数量:\
+ this.count; }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java程序设计实验2(3)在线全文阅读。
相关推荐: