77范文网 - 专业文章范例文档资料分享平台

Java程序设计实验2(3)

来源:网络收集 时间:2020-04-16 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

金陵科技学院实验报告

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)在线全文阅读。

Java程序设计实验2(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/978160.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: