学 生 实 验 报 告 册
(理工类)
课程名称:JAVA程序设计实验专业班级:M11计算机科学与技术II
学生学号: 学生姓名:
所属院部: XX学院 指导教师:
20XX ——20XX 学年 第 二 学期
金陵科技学院教务处制
实验报告书写要求
实验报告原则上要求学生手写,要求书写工整。若因课程特点需打印的,标题采用四号黑体,正文采用小四号宋体,单倍行距。纸张一律采用A4的纸张。
实验报告书写说明
实验报告中实验目的和要求、实验仪器和设备、实验内容与过程、实验结果与分析这四项内容为必需项。教师可根据学科特点和实验具体要求增加项目。
填写注意事项
(1)细致观察,及时、准确、如实记录。
(2)准确说明,层次清晰。
(3)尽量采用专用术语来说明事物。
(4)外文、符号、公式要准确,应使用统一规定的名词和符号。
(5)应独立完成实验报告的书写,严禁抄袭、复印,一经发现,以零分论处。
实验报告批改说明
实验报告的批改要及时、认真、仔细,一律用红色笔批改。实验报告的批改成绩采用五级记分制或百分制,按《金陵科技学院课堂教学实施细则》中作业批阅成绩评定要求执行。
实验报告装订要求
实验批改完毕后,任课老师将每门课程的每个实验项目的实验报告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课程的实验大纲。
实验项目名称: 面向对象编程实验实验学时: 8
同组学生姓名: 实验地点: 1414
实验日期: 3.28 4.4 实验成绩:
批改教师: 批改时间:
实验1 面向对象编程实验
一、实验目的和要求
(1)理解Java概念、掌握JDK环境配置
(2)熟悉Java开发过程
(3)掌握Java面向对象编程基础:封装、继承、多态
(4)掌握Java接口编程,理解开发模式
二、实验仪器和设备
奔腾以上个人计算机, windows操作系统。
配置好JDK环境,安装集成开发环境(Eclipse)
三、实验内容与过程
1、JDK环境配置
2、面向对象的封装性
范例:设计一个表示学生的类,里面有学生的三项成绩:计算机成绩、数学成绩、英语成绩。要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息。 代码如下:
class Student{
private String name ;
private int age ;
private float english ;
private float computer ;
private float math ;
public Student(){}
public Student(String n,int a,float e,float c,float m){
this.setName(n) ;
this.setAge(a) ;
this.setEnglish(e) ;
this.setComputer(c) ;
this.setMath(m) ;
}
public float sum(){
return english + computer + math ;
}
public float avg(){
return this.sum() / 3 ;
}
public float max(){
float max = computer>math?computer:math ;
max = max>english?max:english ;
return max ;
}
public float min(){
float min = computer
min = min
return min ;
}
public String getInfo(){
return "学生信息: \n" +
"\t|- 姓名:" + this.getName() + "\n" +
"\t|- 年龄:" + this.getAge() + "\n" +
"\t|- 数学成绩:" + this.getMath() + "\n" +
"\t|- 英语成绩:" + this.getEnglish() + "\n" +
"\t|- 计算机成绩:" + this.getComputer() ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
age = a ;
}
public void setEnglish(float e){
english = e ;
}
public void setComputer(float c){
computer = c ;
}
public void setMath(float m){
math = m ;
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
public float getEnglish(){
return english ;
}
public float getComputer(){
return computer ;
}
public float getMath(){
return math ;
}
}
public class ExecDemo{
public static void main(String args[]){
Student stu = new Student("张三",30,89.0f,91.0f,87.0f) ;
System.out.println("总分:" + stu.sum()) ;
System.out.println("平均分:" + stu.avg()) ;
System.out.println("最高分:" + stu.max()) ;
System.out.println("最低分:" + stu.min()) ;
System.out.println(stu.getInfo()) ;
}
}
对照范例写出如下题目:
(1)编写并测试一个代表地址的Address类,包括以下的属性:国家、省份、城市、街道、邮编。
可以从此地址类中得到一个完整的地址信息。
(2)定义并测试一个代表员工的Employee类。它的属性包括“员工姓名”、“员工号码”、“员工基本薪水”、“员工薪水增长额”;它的方法包括“构造方法”、“获取员工姓名”、“获取员工号码”、“获取员工基本薪水”、“计算薪水增长额”及“计算增长后的工资总额”。
(3)、定义一个圆形类,可以返回圆的面积与周长。
(4)、思考设计:一个人有一本书,一本书属于一个人。
3、面向对象的继承性
范例:要求定义一个数组类Array,里面定义了一个整型数组,但是此整型数组属于动态分配大小,即:所有的大小由程序指定,并在此基础上实现以下的两个子类:
反转类:可以将数组的内容反转排列
排序类:可以对数组进行排序的操作
class Array{
private int temp[] = null ; // 只是声明数组,但是大小未知
private int foot = 0 ; // 用于保存下一个的记录点
public Array(int len){
if(len>0){
this.temp = new int[len] ; // 此时大小由外部决定
}else{
this.temp = new int[1] ; // 至少开辟一个空间
}
}
public boolean add(int i){ // 加入数据操作
if(this.foot
this.temp[this.foot] = i ; // 加入内容
this.foot++ ; // 改变长度
return true ; // 加入成功返回true
}else{
return false ; // 加入失败
}
}
public int[] getArray(){ // 返回全部的数组
return this.temp ;
}
}
class SortArray extends Array{
public SortArray(int len){
super(len) ;
}
public int[] getArray(){
java.util.Arrays.sort(super.getArray()) ; // 排序操作
return super.getArray() ; // 返回的是排序后的内容
}
}
class ReverseArray extends Array{
public ReverseArray(int len){
super(len) ;
}
public int[] getArray(){
int rt[] = new int[super.getArray().length] ; // 根据大小开辟新数组
int count = rt.length-1 ;
for(int x=0;x
rt[count] = super.getArray()[x] ;
count-- ;
}
return rt ;
}
}
public class ArrayDemo{
public static void main(String args[]){
ReverseArray arr = new ReverseArray(6) ;
System.out.println(arr.add(3)) ;
System.out.println(arr.add(23)) ;
System.out.println(arr.add(1)) ;
System.out.println(arr.add(5)) ;
System.out.println(arr.add(6)) ;
System.out.println(arr.add(8)) ;
System.out.println(arr.add(11)) ;
System.out.println(arr.add(16)) ;
print(arr.getArray()) ;
}
public static void print(int i[]){
for(int x=0;x
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类的对象,并分别显示详细信息。
4、面向对象多态性
范例:计算柱体的体积。柱体体积计算公式是:底部面积乘以高度
柱体底部分为 圆形和矩形
要求:通过抽象类和多态实现
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) 计算机模拟
四、实验结果与分析(程序运行结果及其分析)
2.(3)
class Circle{
private int radius;
public Circle(int r){
this.setRadius(r);
}
public void setRadius(int r){
radius=r;
}
public int getRadius(){
return radius;
}
public double printArea(){
return 3.14*radius*radius;
}
public double printChang(){
return 2*3.14*radius;
}
}
public class TestCircle{
public static void main(String args[])
{
Circle c1=new Circle(6);
System.out.println("面积为"+c1.printArea());
System.out.println("周长为"+c1.printChang());
}
}
3.
class GrandFather{
private String name;
private int age;
public GrandFather(String name,int age){
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String getGrandFather(){
return "爷爷的姓名:"+getName()+" "+"年龄:"+getAge();
}
}
class Father extends GrandFather{
private String occupation;
public Father(String name,int age,String occupation){
super(name,age);
this.setOccupation(occupation);
}
public void setOccupation(String occupation){
this.occupation=occupation;
}
public String getOccupation(){
return occupation;
}
public String getFather(){
return "爸爸的姓名:"+getName()+" "+"年龄:"+getAge()+" "+"职业:"+getOccupation();
}
}
public class ClassMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GrandFather gf=new GrandFather("wuzongyao",78);
System.out.println(gf.getGrandFather());
Father f=new Father("wushengguang",48,"工人");
System.out.println(f.getFather());
}
}
4.(1)
abstract class Instrument{
abstract void play();
}
class Piano extends Instrument{
public void play(){
System.out.println("钢琴是弹的!不是拉的");
}
}
class Violin extends Instrument{
public void play(){
System.out.println("小提琴是拉的!不是弹得");
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Instrument p=new Piano();
p.play();
Instrument v=new Violin();
v.play();
}
}
(2)
interface VideoCard{
void videoRunning();
void videoClosing();
}
interface Cpu{
void cpuRunning();
void cpuClosing();
}
class IntelCpu implements Cpu{
public IntelCpu(){
System.out.println("Make an Intel's CPU");
}
public void cpuRunning(){
System.out.println("Intel's CPU is Working......");
}
public void cpuClosing(){
System.out.println("Intel's CPU is Closing......");
}
}
class DmengVideoCard implements VideoCard{
public DmengVideoCard(){
System.out.println("Make a DmengVideoCard's VideoCard");
}
public void videoRunning(){
System.out.println("Dmeng's VideoCard is Working......");
}
public void videoClosing(){
System.out.println("Dmeng's VideoCard is Closing......");
}
}
class Mainboard{
private IntelCpu cpu;
private DmengVideoCard videocard;
public Mainboard(IntelCpu cpu,DmengVideoCard videocard){
this.cpu=cpu;
this.videocard=videocard;
}
public void run(){
cpu.cpuRunning();
videocard.videoRunning();
System.out.println("Mainboard is running.....");
}
public void close(){
cpu.cpuClosing();
videocard.videoClosing();
System.out.println("Mainboard is closing.....");
}
}
public class Computer {
public static void openPC(){
System.out.println("开机了。。。。。");
}
public static void closePC(){
System.out.println("关机了。。。。。");
}
public static void main(String[] args) {
Mainboard m=new Mainboard(new IntelCpu(),new DmengVideoCard());
openPC();
m.run();
closePC();
m.close();
}
}
五、实验体会
本次实验是关于JAVA的基础知识的检验,基础知识是整个java学习中最重要的,对以后的学习来说,这是个台阶。通过此次实验,我对Java有了更进一步的了解,特别是理论联系实际。对学习Java帮助很大。也使我对Java产生了深厚的情趣, 我会更加努力学习Java。
实验项目名称: 类集 实验学时: 4
同组学生姓名: 实验地点: 1414
实验日期: 4.11 4.18 实验成绩:
批改教师: 批改时间:
实验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、一门课程可以有多个学生选,一个学生可以选多门课程
四、实验结果与分析(程序运行结果及其分析)
(1)
interface Pet{
public String getName(); // 得到商品名称
public String getColor(); // 得到商品数量
public float getPrice();
}
public class Dog implements Pet {
private String name;
private String color;
private float price;
public Dog() {
}
public Dog(String name, String color, float price) {
this.name = name;
this.color = color;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
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 Dog)) {
return false;
}
Dog d = (Dog) obj;
if (d.name.equals(this.name) && d.color == this.color
&& d.price == this.price) {
return true;
} else {
return false;
}
}
public int hashCode() {
return this.name.hashCode() + new Integer(this.color).hashCode()
+ new Float(this.price).hashCode();
}
public String toString() {
return "狗的品种:" + this.name + ";狗的价格:" + this.price + ";狗的颜色:"
+ this.color;
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PetSupermarket {
private List allpets;
public PetSupermarket() {
this.allpets = new ArrayList();
}
public void add(Pet pet) {
this.allpets.add(pet);
}
public void remove(Pet pet) {
this.allpets.remove(pet);
}
public List search(String keyWord) {
List temp = new ArrayList();
Iterator iter = this.allpets.iterator();
while (iter.hasNext()) {
Pet p = iter.next();
if (p.getName().indexOf(keyWord) != -1) {
temp.add(p);
}
}
return temp;
}
public List getAllpets() {
return this.allpets;
}
}
import java.util.Iterator;
import java.util.List;
public class Testp {
public static void main(String[] args) {
PetSupermarket mak = new PetSupermarket();
mak.add(new Dog("牧羊犬", "黑色", 5000));
mak.add(new Dog("中华田园犬", "黑色", 200));
mak.add(new Dog("藏獒", "褐色", 50000));
print(mak.search("牧")) ;
mak.remove(new Dog("藏獒", "褐色", 50000)) ;
print(mak.search("中")) ;
}
public static void print(List all) {
Iterator iter = all.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
(2)
A、
import java.util.ArrayList;
import java.util.List;
public class School {
private String name;
private List stu;
public School() {
this.stu = new ArrayList();
}
public School(String name) {
this();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addstu(Student stu){
this.stu.add(stu);
}
public List getAllStudents() {
return stu;
}
public String toString(){
return "学校信息:" + this.name ;
}
}
public class Student {
private String name;
private int year;
private String num;
private School school;
public Student(){
}
public Student(String name, int year, String num) {
super();
this.name = name;
this.year = year;
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
result = prime * result + year;
return result;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student s = (Student) obj;
if (s.name.equals(this.name) && s.year == this.year
&& s.num == this.num) {
return true;
} else {
return false;
}
}
@Override
public String toString() {
return "Student [name=" + name + ", year=" + year + ", num=" + num
+ ", getName()=" + getName() + ", getYear()=" + getYear()
+ ", getNum()=" + getNum() + ", hashCode()=" + hashCode()
+ ", getClass()=" + getClass() + ", toString()="
+ super.toString() + "]";
}
}
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
School school = new School("JIT");
Student stu1 = new Student("张三", 23,"110");
Student stu2 = new Student("李四", 25,"111");
Student stu3 = new Student("王五", 22,"112");
school.addstu(stu1); // 一个学校有多个学生
stu1.setSchool(school); // 一个学生属于一个学校
school.addstu(stu2); // 一个学校有多个学生
stu2.setSchool(school); // 一个学生属于一个学校
school.addstu(stu3); // 一个学校有多个学生
stu3.setSchool(school); // 一个学生属于一个学校
System.out.println(school);
Iterator iter = school.getAllStudents().iterator();
while (iter.hasNext()) {
System.out.println("\t|- " + iter.next());
}
}
}
五、实验体会
通过这次实验,收获很大,我了解了类集,掌握了类集的一些简单的操作,如添加,删除,并且遍历容器里的数据。在学习中还发现好多关于类集的知识还不是理解,但通过同学的帮助,解决了难题,我觉得还需要更进一步的学习。
实验项目名称: Java IO操作 实验学时: 6
同组学生姓名: 实验地点: 1414
实验日期: 4.25 5.2 实验成绩:
批改教师: 批改时间:
实验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();// 关闭
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class copy {
public static void main(String[] args) throws Exception{
if(args.length!=2){
System.out.println("语句不合法!");
System.exit(1);
}
if(args[0].equals(args[1])){
System.out.println("不能复制自身文件!");
System.exit(1);
}
else{
File file1=new File(args[0]);
if(file1.exists()){
File file2=new File(args[1]);
InputStream input=new FileInputStream(file1);
OutputStream output=new FileOutputStream(file2);
int temp=0;
while((temp=input.read())!=-1){
output.write(temp);
}
System.out.println("复制完成!");
input.close();
output.close();
}
else{
System.out.println("找不到源文件");
}
}
}
}
2、通过键盘的输入,实现简单的选项操作。
*********XXXX管理系统***********
[1]添加
[2]删除
[3]修改
[4]查询
[5]退出
3、编写一个简单管理系统,实现真实的操作。
四、实验结果与分析(程序运行结果及其分析)
1.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class copy {
public static void main(String[] args) throws Exception{
if(args.length!=2){
System.out.println("语句不合法!");
System.exit(1);
}
if(args[0].equals(args[1])){
System.out.println("不能复制自身文件!");
System.exit(1);
}
else{
File file1=new File(args[0]);
if(file1.exists()){
File file2=new File(args[1]);
InputStream input=new FileInputStream(file1);
OutputStream output=new FileOutputStream(file2);
int temp=0;
while((temp=input.read())!=-1){
output.write(temp);
}
System.out.println("复制完成!");
input.close();
output.close();
}
else{
System.out.println("找不到源文件");
}
}
}
}
五、实验体会
通过本节课的实验,我理解了输入输出流概念,掌握了文件输入输出流,键盘的输入、显示器的输出,深刻领悟老师将Java中的文件流比喻“管道”。达到本次实验的目的,练习使用Java语言的输入与输出流类,实现文件的处理。虽然,在实验过程中,也遇到了不少问题,但我会再接再厉,更加认真的学习!
实验项目名称: JDBC 实验学时: 6
同组学生姓名: 实验地点:
实验日期: 5.9 5.16 实验成绩:
批改教师: 批改时间:
实验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包
主要操作:
右击项目名称,选择 Build Path,点击Add External Archives ,选中要加载的jar包即可。
3、项目中建立一个详细例子,按照要求连接、操作、关闭数据库
按照标准的步骤完成对MySQL数据库的操作
主要代码:(添加、修改、删除和查询)
添加:
Connection con=getConnection();
String sql="insert into tb_worker (name,age,birthday,salary)values('王七','22','1992-12-12','4500')";
Statement sta = con.createStatement();
int num=sta.executeUpdate(sql);
修改:
Connection con=getConnection();
Statement sta=con.createStatement();
String sql=" update tb_worker set salary=4600 where name='王五'";
sta.executeUpdate(sql);
删除:
Connection con=getConnection();
Statement sta=con.createStatement();
String sql="delete from tb_worker where name='李四'";
sta.executeUpdate(sql);
查询:
Connection con=getConnection();
Statement sta=con.createStatement();
String sql="select name,age,birthday,salary from tb_worker where pid=1";
ResultSet rs=sta.executeQuery(sql);
while(rs.next()){
String username=rs.getString(1);
String password=rs.getString(2);
String email=rs.getString(3);
System.out.println(username+" "+password+" "+email);
}
4、试着连接其他类型数据库。
四、实验结果与分析(程序运行结果及其分析)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Testjdbc2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("请输入选择的操作:");
int n=s.nextInt();
switch(n){
case 1: addData();
break;
case 2: queryData();
break;
case 3: updateData();
break;
case 4: deleteData();
break;
}
}
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "123456");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static int addData(){
Connection con=getConnection();
String sql="insert into tb_worker (name,age,birthday,salary)values('王七','22','1992-12-12','4500')";
try {
Statement sta = con.createStatement();
int num=sta.executeUpdate(sql);
return num;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
public static void queryData(){
Connection con=getConnection();
try {
Statement sta=con.createStatement();
String sql="select name,age,birthday,salary from tb_worker where pid=1";
ResultSet rs=sta.executeQuery(sql);
while(rs.next()){
String username=rs.getString(1);
String password=rs.getString(2);
String email=rs.getString(3);
System.out.println(username+" "+password+" "+email);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void deleteData(){
Connection con=getConnection();
try {
Statement sta=con.createStatement();
String sql="delete from tb_worker where name='李四'";
sta.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void updateData(){
Connection con=getConnection();
try {
Statement sta=con.createStatement();
String sql=" update tb_worker set salary=4600 where name='王五'";
sta.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
五、实验体会
通过本次实验,我理解了JDBC分类,掌握了JDBC数据库连接步骤,JDBC连接MySQL数据库代码,理解了JDBC连接其他数据库方式等。虽然不是很熟练,但在同学的帮助下完成了本次试验。这几次的实验,让我对书本知识有了更进一步的理解和掌握。对java的认识和掌握也更深刻了,相信在以后的学习和生活中,他也会给我带来帮助!
学生实验报告册(理工类)课程名称:JAVA程序设计实验专业班级:M11计算机科学与技术II学生学号:学生姓名:所属院部:XX学院指…
JAVA程序设计实验报告院系专业班级11计算机01班姓名任课教师XXX实验题目JAVA简单记事本程序设计目录目录1一引言211课程…
Java语言基础实验指导报告书20xx20xx年秋季学期使用班级姓名计算机科学与技术学院实验一Java编程开发环境一实验学时1学时…
Java程序设计实验指导报告书目录实验1熟悉Java开发环境2实验2简单的输入输出处理3实验3类的练习6实验4Java图形用户界面…
Java项目开发实训总结设计人:浦江峰班级和班内序号:1132304112项目选题意义:在民营企业、中小型公司高速蓬勃发展的今天,…
Java项目开发实训总结设计人:杨莉莉班级和班内序号:软件11128号项目选题意义:今年来,随着网络的普及,越来越多的企业借用网络…
Java程序设计实训报告实训名称仓库管理系统实训专业计算机应用班级计算机10A1学号姓名指导教师侯丽敏20xx年12月8日实训成绩…
1Java程序设计实训报告班级学号姓名指导教师成绩2实训内容题目1用java语言设计一个关于ATM机的程序并实现相应的功能解题思路…
Java程序设计实验指导报告书目录实验1熟悉Java开发环境2实验2简单的输入输出处理3实验3类的练习6实验4Java图形用户界面…
河南机电高等专科学校学生课程设计报告实验课程名称Java程序设计开课实验室实验楼308机房系、部计科系年级11专业班软件技术学生姓…