System.out.print(i[x] + "、") ; }
}
}
对照范例写出如下题目:
(1). 创建GrandFather类,其中包括
a) 属性:姓名(name),年龄(age)
b) 方法getGrandFather():显示爷爷的信息
c) 构造方法:给爷爷的姓名,年龄赋值
(2). 创建Father类,继承Grandfather类
a) 属性:除了继承爷爷的属性以外,还要增加自己的属性:“职业”(occupation)
b) 构造方法:显式调用父类的构造方法,为Father类的姓名和年龄赋初始值。再为职业输入初始值。
c) 方法getFather(): 显示父亲的相关信息
(3). 创建ClassMain()类,定义main()方法,构造GrandFather类的对象和Father类的对象,并分别显示详细信息。
3、面向对象多态性
范例:计算柱体的体积。柱体体积计算公式是:底部面积乘以高度
柱体底部分为 圆形和矩形
要求:通过抽象类和多态实现
package cn.jit.demo;
abstract class Bottom { //父类抽象类 底部
public abstract double calculatorArea();
}
class CircleBottom extends Bottom{ //圆形底
/**
* 半径
*/
private double radius;
@Override
public double calculatorArea() {
return Math.PI * radius * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public CircleBottom(double radius) {
super();
this.radius = radius;
}
}
class SquareBottom extends Bottom{ //矩形底
private double sideA;
private double sideB;
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSideB() {
return sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
@Override
public double calculatorArea() {
return sideA * sideB;
}
public SquareBottom(double sideA, double sideB) {
super();
this.sideA = sideA;
this.sideB = sideB;
}
}
class ZhuTi { //柱体类,完成形状的拼装
/**
* 底
*/
private Bottom bottom;
/**
* 高
*/
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("圆柱体的体积是:" + result);
bottom = new SquareBottom(1.0,1.0);
zhuTi.changeBottom(bottom);
result = zhuTi.calculatorVolumn();
System.out.println("立方体的体积是:" + result);
}
}
范例:接口和多态的应用,例如:电脑上实现了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("U盘开始工作。") ;
}
public void stop(){
System.out.println("U盘停止工作。") ;
}
};
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.name + ";书的价格:" + this.price + ";书的数量:"
+ this.count;
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SuperMarket {
private List allGoods; public SuperMarket() {
this.allGoods = new ArrayList(); }
public void add(Goods goods) {
this.allGoods.add(goods);
}
public void remove(Goods goods) {
this.allGoods.remove(goods);
}
public List search(String keyWord) { List temp = new ArrayList(); Iterator iter = this.allGoods.iterator(); while (iter.hasNext()) {
Goods g = iter.next();
if (g.getName().indexOf(keyWord) != -1) {
temp.add(g);
}
}
return temp;
}
public List getAllGoods() { return this.allGoods;
}
}
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
SuperMarket mak = new SuperMarket();
mak.add(new Book("Java", 2, 30.9f));
mak.add(new Book("C++", 3, 10.9f));
mak.add(new Book("JSP", 5, 80.9f));
print(mak.search("J")) ;
mak.remove(new Book("Java", 2, 30.9f)) ;
print(mak.search("J")) ;
}
public static void print(List all) {
Iterator iter = all.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
对照范例写出如下题目:
(1)宠物商店,要求可以添加、删除和查找宠物
(2)实现以下两个关系
A、一个学校可以有多个学生,所有学生属于一个学校
B、一门课程可以有多个学生选,一个学生可以选多门课程
四、实验结果与分析(程序运行结果及其分析)
五、实验体会
实验项目名称: Java IO操作 实验学时: 6
同组学生姓名: 实验地点:
实验日期: 实验成绩:
批改教师: 批改时间:
实验3 Java IO操作
一、实验目的和要求
(1)理解输入输出流概念
(2)掌握文件输入输出流
(3)掌握键盘的输入、显示器的输出
(4)理解其他输入输出流
二、实验仪器和设备
奔腾以上个人计算机, windows操作系统。
配置好JDK环境,安装集成开发环境(Eclipse)
三、实验内容与过程
1、编写类模拟命令Copy
范例:实现文件的复制代码。 参考代码如下:
File file1 = new File(“d:”+File.seperator +”demo.txt”);
// 找到第一个文件的File对象
File file2 = new File(“d:”+File.seperator +”cemo.txt”); // 找到目标文件路径
InputStream input = new FileInputStream(file1); // 输入流
OutputStream output = new FileOutputStream(file2);// 输出流
int temp = 0; // 定义一个整数表示接收的内容
while ((temp = input.read()) != -1) { // 表示还有内容可以继续读
output.write(temp);// 写入数据
}
input.close(); // 关闭
output.close();// 关闭
2、通过键盘的输入,实现简单的选项操作。
*********XXXX管理系统***********
[1]添加
[2]删除
[3]修改
[4]查询
[5]退出
3、编写一个简单管理系统,实现真实的操作。
四、实验结果与分析(程序运行结果及其分析)
五、实验体会
实验项目名称: JDBC 实验学时: 6
同组学生姓名: 实验地点:
实验日期: 实验成绩:
批改教师: 批改时间:
实验4 JDBC
一、实验目的和要求
(1)理解JDBC分类
(2)掌握JDBC数据库连接步骤
(3)掌握JDBC连接MySQL数据库代码
(4)理解JDBC连接其他数据库方式
二、实验仪器和设备
奔腾以上个人计算机, windows操作系统。
配置好JDK环境,安装集成开发环境(Eclipse)
三、实验内容与过程
1、安装MySQL数据库,配置好数据库
创建一个数据库表,按要求给出详细的字段设计
pid name age birthday salary
主要操作:
2、创建Eclipse项目,配置驱动包
每个数据库厂商都会提供对Java开发技术的支持,即都会提供对应的Java驱动,也就是一个jar包
主要操作:
3、项目中建立一个详细例子,按照要求连接、操作、关闭数据库
按照标准的步骤完成对MySQL数据库的操作
主要代码:(添加、修改、删除和查询)
4、试着连接其他类型数据库。
四、实验结果与分析(程序运行结果及其分析)
五、实验体会