学 生 实 验 报 告
(理工类)
课程名称: Java程序设计 专业班级:
学生学号: 学生姓名:
所属院部: 信息技术学院 指导教师: 曾 岳
20XX ——20XX 学年 第 二 学期
金陵科技学院教务处制
实验报告书写要求
实验报告原则上要求学生手写,要求书写工整。若因课程特点需打印的,要遵照以下字体、字号、间距等的具体要求。纸张一律采用A4的纸张。
实验报告书写说明
实验报告中一至四项内容为必填项,包括实验目的和要求;实验仪器和设备;实验内容与过程;实验结果与分析。各院部可根据学科特点和实验具体要求增加项目。
填写注意事项
(1)细致观察,及时、准确、如实记录。
(2)准确说明,层次清晰。
(3)尽量采用专用术语来说明事物。
(4)外文、符号、公式要准确,应使用统一规定的名词和符号。
(5)应独立完成实验报告的书写,严禁抄袭、复印,一经发现,以零分论处。
实验报告批改说明
实验报告的批改要及时、认真、仔细,一律用红色笔批改。实验报告的批改成绩采用百分制,具体评分标准由各院部自行制定。
实验报告装订要求
实验批改完毕后,任课老师将每门课程的每个实验项目的实验报告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课程的实验大纲。
实验项目名称: JAVA编程基础 实验学时: 4
同组学生姓名: 实验地点: A203
实验日期: 实验成绩:
批改教师: 批改时间:
一、实验目的和要求
1、实验目的
(1) 熟练掌握JDK编写调试Java应用程序及Java小程序的方法。
(2) 熟练掌握Java应用程序的结构。
(3) 了解Java语言的特点,基本语句、运算符及表达式的使用方法。
(4) 熟练掌握常见数据类型的使用。
(5) 熟练掌握if-else、switch、while、do-while、for、continue、break、return语句的使用方法。
(6) 熟练掌握数组和字符串的使用。
2、实验要求
(1)调试程序要记录调试过程中出现的问题及解决办法;
(2)编写程序要规范、正确,上机调试过程和结果要有记录,不断积累编程及调试经验;
(3)做完实验后给出本实验的实验报告。
二、实验仪器和设备
奔腾以上计算机,Windows XP 、JDK、MyEclipse8.5或Eclipse、IE浏览器
三、实验过程
(1)分别在JDK和MyEclipse中运行例1.1和例1.2中程序,并记录操作过程。
(2)请编写一个java程序判断两个数是否能够整除。
(3)调试并运行以下两个程序段,并写出执行结果和体会。
① int a=56;
int b=a++;
System.out.println(a);
System.out.println(b);
② int a=56;
int b=++a;
System.out.println(a);
System.out.println(b);
③ int a=56;
int b=90;
a-=34;
b%=a;
System.out.println(a);
System.out.println(b);
④int a=90; int b=90;
if (a==b){System.out.println ("ok!");}
b--;
if (a>b) {System.out.println("ok2");}
if (a>=b){System.out.println("ok3");}
⑤int a=90; int b=90;
if (a==b||a>8) {System.out.print("ok1");}
b--;
if (a>b&&a>45){System.out.print("ok2");}
if (!(a<=b)){System.out.print("ok3");}
⑥int a=121;
if (a>18){
System.out.println("a>18");
}else if (a>7){
System.out.println("a>10&a<18");
}else if (a>6){
System.out.println("other!");
}else {
System.out.println("YY");
}
(4)请编写一个程序,该程序可以接收两个数(可以是整数,也可是小数),并判断两个数的是大于?小于还是等于?
(5)编写一个程序,可以输入人的年龄,如果该同志的年龄大于18岁,则输出”你的年龄大于18岁,要对自己的行为负责,送入监狱”,如果该同志的年龄大于10岁并且小于等于18,则输出”你的年龄也不小了,把你送少管所.”.其它情况,则输出”小孩子,下次注意”.
(6)请编写程序,该程序可以接收一个字符,比如:a,b,c,d,e,f,g.a表示星期一,b表示星期二…,根据用户的输入显示相依的信息,要求使用switch语句完成。
(7)请编写一个程序,可以接收一个整数,表示层数,打印出金字塔一半。如图
(8)请编写一个程序,可以接收一个整数,表示层数,打印出如图所示的金字塔
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
(9) 请编写一个程序,可以接收一个整数,表示层数,打印出如图所示的金字塔(中间是空心的金字塔)
(10)一个养鸡场有6只鸡,它们的体重分别是3kg,5kg,1kg,3.4kg,2kg,50kg.请问这六只鸡的总体重是多少?平均体重是多少?请利用数组存储和非数组存储两种方法编程序.
(11)请编写一个程序,可以计算他们的平均体重,可以找出体重最大的和最小狗的名字,可以通过输入狗的名字,查找它的体重。
四、实验结果与分析
例1.1
public class Hello {
public static void main(String[] args) {
if(args.length==0){
System.out.println("Hello!");
}
else{
for(int i=0;i
System.out.println(args[i]);
}
}
}
例1.2:
package demo;
import mypackage.*;
public class Line {
public Point start,end;
public Line(Point start,Point end){
this.start=start;
this.end=end;
}
public String toString(){
return this.getClass().getName()+"("+this.start.toString()+","+this.end.toString()+")";
}
public static void main(String[] args) {
Point start=new Point(100,100);
Point end=new Point(100,200);
System.out.println(new Line(start,end).toString());
}
}
package mypackage;
public class Point {
public int x,y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public Point()
{ this(0,0); }
public String toString(){
return "Point("+this.x+","+this.y+")";
}
}
(2)import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String args[]) throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("请输入两个整数11");
String a1=br.readLine();
String a2=br.readLine();
int num1=(int) Float.parseFloat(a1);
int num2=(int) Float.parseFloat(a2);
int t;
if(num1
{
t=num1;
num1=num2;
num2=t;
}
if(num1%num2==0)
System.out.println("能整除!");
else
System.out.println("不能整除!");
}
}
(3)1、57 56
2、57 57
3、22 2
4、ok1 ok2 ok3
5、ok1 ok2 ok3
6、a>18
(4)package t4;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
int x=sc.nextInt();
//System.out.println(x);
//System.out.print(s);
if(s
else if(s>x)System.out.println(x+"小于"+s);
else System.out.println(s+"等于"+x);
}
}
(5)package t5;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("请输入一个同志的年龄:");
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
if(s>=18) System.out.println("你的年龄大于18岁,要对自己的行为负责,送入监狱");
else if(s>10) System.out.println("你的年龄也不小了,把你送少管所.");
else System.out.println("小孩子,下次注意");
}
}
(6)package t6;
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.print("请输入一个 字符:");
char c;c=(char)System.in.read();
switch(c)
{
case 'a':System.out.println("星期一");break;
case 'b':System.out.println("星期二");break;
case 'c':System.out.println("星期三");break;
case 'd':System.out.println("星期四");break;
case 'e':System.out.println("星期五");break;
case 'f':System.out.println("星期六");break;
case 'g':System.out.println("星期日");break;
}
}
}
(7)package t7;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.print("请输入金字塔的层数:");
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
for(int i=1;i<=s;i++)
{
for(int j=1;j<=i;j++)
System.out.print("* ");
System.out.println();
}
}
}
(8)package t8;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.print("请输入金字塔的层数:");
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
for(int i=1;i<=s;i++)
{
for(int j=s;j>i;j--)
System.out.print(" ");
for(int j=1;j<=i*2-1;j++)
System.out.print("*");
System.out.println();
}
}
}
(9)package t9;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.print("请输入金字塔的层数:");
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
for(int i=1;i<=s;i++)
{
for(int j=s;j>i;j--)
System.out.print(" ");
if(i==1||i==s)
{ for(int j=1;j<=i*2-1;j++)
System.out.print("*");
System.out.println();
}
else {System.out.print("*");
for(int j=1;j<=(i-1)*2-1;j++)
System.out.print(" ");
System.out.println("*");}
}
}
}
(10)
1、非数组存储:
package t10;
public class Test {
public static void main(String[] args) {
double a1=3;double a2= 5;double a3= 1;
double a4=3.4;double a5= 2;double a6= 50;
double avg1;
double sum1;
sum1=a1+a2+a3+a4+a5+a6;
avg1=sum1/6;
System.out.println(avg1);
System.out.println(sum1);
}
}
2、数组存储:
package t10;
public class Test {
public static void main(String[] args) {
double avg2;
double sum2=0;
double [] b={3,5,1,3.4,2,50};
for(int i=0;i<6;i++)
{ sum2+=b[i]; }
avg2=sum2/6;
System.out.println(avg2);
System.out.println(sum2);
}
}
(11)import java.util.Scanner;
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException{
System.out.println("一共有几只狗:");
Scanner s=new Scanner(System.in);
int n; n=s.nextInt();
dog []dogs=new dog[n];
String name;String maxname="as";String minname="sd";
double weight,max=0,min=100;
double sum=0;
for(int i=0;i
{
System.out.println("输入第"+(i+1)+"只狗的名字和体重:");
name=s.next();weight=s.nextInt();
dogs[i]=new dog(name,weight);
sum+=weight;
if(weight>=max) {max=weight;maxname=name;}
if(weight<=min) {min=weight;minname=name;}
}
System.out.println(n+"只狗的平均体重:"+sum/n);
System.out.println("体重最大狗的名字:"+maxname);
System.out.println("体重最小狗的名字:"+minname);
System.out.print("输入狗的名字,查询体重:");
name=s.next();
for(int i=0;i
{ if(dogs[i].name.equals(name ))
System.out.print("查询的体重为:"+dogs[i].weight);
}
}
}
class dog{
public dog() { }
public dog(String name,double weight){
this.name=name; this.weight=weight;}
public String name;
public double weight;
}
五、实验注意事项
1、实验过程中注意爱护计算机,实验完毕要按正常操作关闭计算机
2、输入程序时应严格注意大小写
实验项目名称: 面向对象编程 实验学时: 8
同组学生姓名: 实验地点: A203
实验日期: 实验成绩:
批改教师: 批改时间:
一、实验目的和要求
1、实验目的
(1) 熟练掌握Java语言类定义的基本语法;
(2) 熟练掌握类成员的访问控制,对象建立的方法;
(3) 熟练掌握类构造方法、成员方法的定义和重载;
(4) 熟练掌握类继承、多态和抽象性;
(5) 熟练掌握接口的定义和实现方法;
(6) 掌握基本的异常处理方法。
2、实验要求
(1)调试程序要记录调试过程中出现的问题及解决办法;
(2)编写程序要规范、正确,上机调试过程和结果要有记录,不断积累编程及调试经验;
(3)做完实验后给出本实验的实验报告。
二、实验仪器和设备
奔腾以上计算机,Windows XP 、JDK、MyEclipse8.5或Eclipse、IE浏览器
三、实验过程
(1) 张老太养了两只猫猫:一只名字叫小白,今年3岁,白色.还有一只叫小花,今年100岁,花色.请编程,当用户输入小猫的名字时,就显示该猫的名字,颜色.如果用户输入的小猫名错误,则显示张老太没有这只猫.(利用非类的方法实现);提取以上猫的特征,写出猫类程序;实例化猫类,创建张老太的二只猫的程序。
(2) 编写函数,使给定的一个二维数组(3*3)转置.
(3) 编程调试下面程序的结果
public class Test1 {
public static void main(String args[]){
Person a = new Person();
a.age=10;
a.name="小明";
Person b;
b=a;
System.out.println(b.age);
Person c;
c=b;
c.age=9;
System.out.println(a.age);
System.out.println(b.age);
System.out.println(c.age);
}
}
class Person {
int age;
String name;}
(4) 编写程序定义一个人类(person)(包括名字,年龄).用一步到位法创建一个对象
(5) 编写程序,完善以(5)题中的人类,添加以下方法:添加speak成员方法,输出我是一个好人;添加jisuan成员方法,可以计算从1+…+100结果;修改jisuan成员方法,该方法可以接收一个数n,计算从1+…+n的结果;添加add成员方法,可以计算两个数的和.
(6)
编写一个成员函数,从键盘输入一个整数(1-9),打印出对应的乘法表。
(7) 设计计算机类,要求如下:
属性:品牌,颜色,cpu型号,内存容量,硬盘大洲,价格,工作状态
方法:打开,关闭,休眠
创建一个计算机对象,调用打开,关闭方法
(8) 有一群小孩在玩堆雪人,不时有新的小孩加入,请问如何知道现在共有多少人在玩?请使用面向对象的思想,编写程序解决。
(9) 现在有两个程序共同开发区一个java项目,程序员xiaoming希望定义一个类取名Dog,程序员xiaoqiang也想定义一个类也叫Dog.两个程序员为此还吵了起来,怎么办?试编写程序完成.
(10) 请编写一个类(Abc),编写方法可以接收两个整数,返回两数中较大的数.请编写实现(1):如果要求可以接收两个float 型的数,返回较大数?(2):如果要求可以接收三个int型的数,返回最大数?(应用方法重载)
四、实验结果与分析
(1) package t1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
cat []cats=new cat[2];
cats[0]=new cat("小白",3,"白色");
cats[1]=new cat("小花",100,"花色");
System.out.print("输入猫的名字:");
String nameString=s.next();
int f=0;
for(int i=0;i<2;i++)
{
if(cats[i].name.equals(nameString))
{ System.out.println("猫的名字为:"+cats[i].name+" 猫的颜色是:"+cats[i].clor);
f=1;
}
}
if(f==0)System.out.println("张老太没有这只猫!");
}
}
class cat{
public cat(){}
public cat(String name,int age,String clor)
{
this.name=name;
this.age=age;
this.clor=clor;
}
String name;
int age;
String clor;
}
(2) package t2;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][]a={{1,2,3},{4,5,6},{7,8,9}};
int [][]b=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
int n;
n=a[i][j];
b[j][i]=n;
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
(3)
10
9
9
9
(4) public class Test {
public static void main(String args[]) {
person a=new person();
a.name="张三";
a.age=18;
System.out.println("姓名为:"+a.name+",年龄为:"+a.age);
}
}
class person
{
String name;
int age;
}
(5) package t5;
import java.util.Scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p=new Person();
System.out.print("输入一个数:");
Scanner s=new Scanner(System.in);
int n=s.nextInt(),m;
p.jisuan();
p.jisuan(n);
System.out.print("输入两个数求和:");
n=s.nextInt();
m=s.nextInt();
p.add(n, m);
}
}
class Person {
int age;
String name;
public void speak()
{
System.out.println("我是一个好人");
}
public void jisuan()
{
int sum=0;
for(int i=1;i<=100;i++)
sum+=i;
System.out.println("1到100的和为:"+sum);
}
public void jisuan(int n)
{
int sum=0;
for(int i=1;i<=n;i++)
sum+=i;
System.out.println("1到"+n+"的和为:"+sum);
}
public void add(int n,int m)
{
int sum;
sum=n+m;
System.out.println(n+"+"+m+"的和为:"+sum);
}
}
(6) package t6;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("输入一个数:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
System.out.print(i+"*"+j+"="+i*j+" ");
System.out.println();
}
}
}
(7) package t7;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
computer computer=new computer();
computer.open();
computer.show();
computer.close();
computer.show();
computer.xiu();
computer.show();
}
}
class computer{
String log="lenovo";
String colorString="black";
String cpu="core7";
int nc=500;
char c='c';
double price=5000;
String work;
public void open()
{
this.work="open";
}
public void close()
{
this.work="close";
}
public void xiu()
{
this.work="xiumian";
}
public void show ()
{
System.out.print(this.log+" ");
System.out.print(this.colorString+" ");
System.out.print(this.cpu+" ");
System.out.print(this.nc+" ");
System.out.print(this.c+" ");
System.out.print(this.price+" ");
System.out.print(this.work+" ");
System.out.println();
}
}
(8) package t8;
public class Test {
public static void main(String[] args) {
Child ch1 = new Child(3," ");
ch1.joinGame();
Child ch2 = new Child(4,"");
ch2.joinGame();
Child ch3 = new Child(5,"");
ch3.joinGame();
System.out.println(ch2.total);
}
}
//定义小孩类
class Child
{
int age;
String name;
static int total=0;
public Child(int age,String name){
this.age=age;
this.name=name;
}
public void joinGame()
{
total++;
System.out.println("有一个小孩子加入");
}
}
(9) package t9;
public class Test {
public static void main(String[] args) {
Dog dog=new Dog();
dog.ab1();
dog.ab2();
dog.ab3();
dog.ab4();
}
}
class Dog
{
public int a;
protected String name;
String color;
private float price;
// 同类访问/
public void ab1(){
System.out.println(this.a);
}
public void ab2(){
System.out.println(this.name);
}
public void ab3(){
System.out.println(this.color);
}
public void ab4(){
System.out.println(this.price);
}
}
class Dog1
{
public int a;
protected String name;
String color;
private float price;
// 同类访问/
public void ab1(){
System.out.println(this.a);
}
public void ab2(){
System.out.println(this.name);
}
public void ab3(){
System.out.println(this.color);
}
public void ab4(){
System.out.println(this.price);
}
}
(10) package t9;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ABC abc=new ABC();Scanner s=new Scanner(System.in);
int a,b,c;
float n;
a=s.nextInt();b=s.nextInt();c=s.nextInt();
n=Float.parseFloat(s.next());//接收float型数据
System.out.println(n);
System.out.println(abc.returns(a, b));
System.out.println(abc.returns(4.2f,5.3f));
System.out.println(abc.returns(a, b,c));
}}
class ABC{
public int returns(int a,int b){
if(a>b)return a;
else return b;
}
public float returns(float a,float b){
if(a>b)return a;
else return b;
}
public int returns(int a,int b,int c ){
int max=-99999;
if(a>max)max=a;
if(b>max)max=b;
if(c>max)max=c;
return max;
}
}
五、实验注意事项
1、实验过程中注意爱护计算机,实验完毕要按正常操作关闭计算机
2、注意类成员访问权限的设置
实验项目名称: 图形用户界面 实验学时: 6
同组学生姓名: 实验地点: A203
实验日期: 实验成绩:
批改教师: 批改时间:
一、实验目的和要求
1、实验目的
(1)掌握Swing组件的使用方法;
(2)熟练掌握Swing中常用布局管理器的使用方法;
(3)掌握用户界面动作与事件的处理程序的编写方法;
(4)熟练掌握构造用户界面的方法和常见界面元素的使用;
(5)熟练掌握Java绘图的主要方法。
2、实验要求
(1)调试程序要记录调试过程中出现的问题及解决办法;
(2)编写程序要规范、正确,上机调试过程和结果要有记录,不断积累编程及调试经验;
(3)做完实验后给出本实验的实验报告。
二、实验仪器和设备
奔腾以上计算机,Windows XP 、JDK、MyEclipse8.5或Eclipse、IE浏览器
三、实验过程
编写并调试如下程序:
编程实现如下图所示的网格布局案例
- 编程实现如下图所示的会员管理系统界面
- 编程实现如下图所示。
- 编程实现如下图所示的记事本
5.编程实现四叶玫瑰线。
6.编程实现如下图所示,当点击黑色按键,中间panal会变成黑色,当点击红色按键,中间会变成红色。
四、实验结果与分析
(1) package t1;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
//定义组件
public static void main(String[] args) {
Test Test1 = new Test();
}
public Test ()
{String str[]={"0","1","2","3","4","5","6","7","8",};
this.setLayout(new GridLayout(3,3));
for(int i=0;i
this.add(new Button(str[i]));
this.setTitle("计算器");
this.setSize(600, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
(2) package t2;
import java.awt.*;
import javax.swing.*;
public class Dome extends JFrame{
JPanel jp1,jp2,jp3;
JLabel jl1,jl2;
JButton jb1,jb2;
JCheckBox jc1,jc2,jc3;
JRadioButton jrb1,jrb2;
ButtonGroup b;
public static void main(String[] args) {
Dome dome=new Dome();
}
public Dome()
{
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jl1=new JLabel("你喜欢的运动");
jl2=new JLabel("你的性别");
jb1=new JButton("注册用户");
jb2=new JButton("取消注册");
jc1=new JCheckBox("足球");
jc2=new JCheckBox("篮球");
jc3=new JCheckBox("网球");
jrb1=new JRadioButton("男");
jrb2=new JRadioButton("女");
ButtonGroup b=new ButtonGroup();
b.add(jrb1);
b.add(jrb2);
this.setLayout(new GridLayout(3,1));
jp1.add(jl1);
jp1.add(jc1);
jp1.add(jc2);
jp1.add(jc3);
jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setTitle("会员管理系统");
this.setSize(300, 150);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
(3) package t4;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.*;
public class Test extends JFrame{
JPanel jp1,jp2;
JLabel jl1,jl2;
JComboBox jcb1;
JList jlist;
JScrollPane jsp;
public static void main(String[] args) {
Test test_5 = new Test();
}
//构造函数
public Test(){
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("你的籍贯");
jl2 = new JLabel("旅游地点");
String[] jg = {"北京","上海","天津","火星"};
jcb1 = new JComboBox(jg);
String[] dd = {"九寨沟","故宫","长城","天安门"};
jlist = new JList(dd);
jlist.setVisibleRowCount(2);
jsp = new JScrollPane(jlist);
//设置布局
this.setLayout(new GridLayout(3,1));
//添加组件
jp1.add(jl1);
jp1.add(jcb1);
jp2.add(jl2);
jp2.add(jlist);
jp2.add(jsp);
this.add(jp1);
this.add(jp2);
this.setTitle("");
this.setLocation(400, 300);
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
(4) package t4;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame{
JMenuBar jmb;//菜单条组件
JMenu menu1,menu2,menu3,menu4,menu5;
JMenuItem item2,item3,item4,item5,item6,item7;
JMenu xinjian;
JMenuItem file,project;
JTextArea jta;
JToolBar jtb;
JButton jb1,jb2,jb3,jb4,jb5,jb6;
public Test ()
{
jtb = new JToolBar();
jb1 = new JButton(new ImageIcon("image/1.gif"));
jb1.setToolTipText("新建");
jb2 = new JButton(new ImageIcon("image/1.gif"));
jb2.setToolTipText("打开");
jb3 = new JButton(new ImageIcon("image/1.gif"));
jb4 = new JButton(new ImageIcon("image/1.gif"));
jb5 = new JButton(new ImageIcon("image/1.gif"));
jb6 = new JButton(new ImageIcon("image/1.gif"));
jmb = new JMenuBar();
menu1 = new JMenu("文件(F)");
menu1.setMnemonic('F');
menu2 = new JMenu("编辑(E)");
menu2.setMnemonic('E');
menu3 = new JMenu("格 式(O)");
menu3.setMnemonic('O');
menu4 = new JMenu("查看(V)");
menu4.setMnemonic('v');
menu5 = new JMenu("帮 助(H)");
menu5.setMnemonic('H');
xinjian = new JMenu("新建");
file = new JMenuItem("文件");
project = new JMenuItem("项目");
item2 = new JMenuItem("打开",new ImageIcon("image/1.gif"));
item3 = new JMenuItem("保存(S)");
item3.setMnemonic('s');
//给菜单项添加快捷方式
//item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S))
item4 = new JMenuItem("另存为");
item5 = new JMenuItem("页面设置");
item6 = new JMenuItem("打印");
item7 = new JMenuItem("退出");
jta = new JTextArea();
jtb.add(jb1);
jtb.add(jb2);
jtb.add(jb3);
jtb.add(jb4);
jtb.add(jb5);
jtb.add(jb6);
xinjian.add(file);
xinjian.add(project);
menu1.add(xinjian);
menu1.add(item2);
menu1.add(item3);
menu1.add(item4);
menu1.addSeparator();//添加分割线
menu1.add(item5);
menu1.add(item6);
menu1.addSeparator();
menu1.add(item7);
jmb.add(menu1);
jmb.add(menu2);
jmb.add(menu3);
jmb.add(menu4);
jmb.add(menu5);
//将菜单添加到窗体
this.setJMenuBar(jmb);
this.add(jtb,BorderLayout.NORTH);
JScrollPane jsp = new JScrollPane(jta);
// jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS)
this.add(jsp);
this.setTitle("记事本");
ImageIcon icon = new ImageIcon("image/1.GIF");
this.setIconImage(icon.getImage());
this.setSize(500,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String args[])
{
new Test();
}
}
(5) package t5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RoseJFrame extends JFrame implements ActionListener {
private RoseCanvas canvas;
public RoseJFrame()
{
super("四叶玫瑰线");
Dimension dim=getToolkit().getScreenSize();
this.setBounds(dim.width/4, dim.height/4, dim.width/2, dim.height/2);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jpanel=new JPanel();
this.getContentPane().add(jpanel,"North");
JButton button_color=new JButton("选择颜色");
jpanel.add(button_color);
button_color.addActionListener(this);
this.canvas=new RoseCanvas(Color.red);
this.getContentPane().add(this.canvas,"Center");
this.setVisible(true);
}
public void actionPerformed(ActionEvent ev)
{
Color color=JColorChooser.showDialog(this, "选择颜色", Color.blue);
this.canvas.setColor(color);
this.canvas.repaint();
}
public static void main(String[] args) {
new RoseJFrame();
}
}
class RoseCanvas extends Canvas{
private Color color;
public RoseCanvas(Color color){this.setColor(color);}
public void setColor(Color color){this.color=color;}
public void paint(Graphics g)
{
int x0=this.getWidth()/2;
int y0=this.getHeight()/2;
g.setColor(this.color);
g.drawLine(x0, 0, x0, y0*2);
g.drawLine(0, y0, x0*2, y0);
for(int j=40;j<200;j+=20)
for(int i=0;i<1023;i++)
{double angle=i*Math.PI/512;
double radius=j*Math.sin(2*angle);
int x=(int)Math.round(radius*Math.cos(angle)*2);
int y=(int)Math.round(radius*Math.sin(angle));
g.fillOval(x0+x, y0+y, 2, 2);
}
}
}
(6)package t6;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener {
JPanel mp=null;
JButton jb1=null;
JButton jb2=null;
public static void main(String[] args) {
Test test_1 = new Test();
}
public Test (){
mp = new JPanel();
jb1 = new JButton("红色");
jb2 = new JButton("黑色");
this.add(jb1,BorderLayout.NORTH);
mp.setBackground(Color.red);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
//注册监听
jb1.addActionListener(this);
//指action命令
jb1.setActionCommand("aa");
jb2.addActionListener(this);
jb2.setActionCommand("bb");
this.setTitle("事件驱动演示案例");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//对事件处理的方法
@Override
public void actionPerformed(ActionEvent e) {
//判断是哪个按钮被点击
if (e.getActionCommand().equals("aa"))
{
System.out.println("你点击了红色按钮");
mp.setBackground(Color.red);
}else { if (e.getActionCommand().equals("bb")){
System.out.println("你点击了黑色按钮");
mp.setBackground(Color.black);
}else{
System.out.println("不知道!");
}
}
}
}
五、实验注意事项
1、实验过程中注意爱护计算机,实验完毕要按正常操作关闭计算机
2、Java图形界面中应如何选择合适的布局管理器
3、对不同的对象应该注册何种类型的事件监听器
实验项目名称: Java高级编程 实验学时: 6
同组学生姓名: 实验地点: A203
实验日期: 实验成绩:
批改教师: 批改时间:
一、实验目的和要求
1、实验目的
(1) 了解文件的概念和文件对象的创建方法;
(2) 掌握使用文件输入输出流读写文件的方法;
(3) 了解线程的基本概念和多线程程序设计的基本方法;
(4) 掌握数据库连接的方法;
(5) 创建SQL查询;
(6) 查询并更新数据库中的信息。
2、实验要求
(1) 调试程序要记录调试过程中出现的问题及解决办法;
(2) 编写程序要规范、正确,上机调试过程和结果要有记录,不断积累编程及调试经验;
(3) 做完实验后给出本实验的实验报告。
二、实验仪器和设备
奔腾以上计算机,Windows XP 、JDK、MyEclipse8.5或Eclipse、MySQL、IE浏览器
三、实验过程
(1)使用文件字节输入/输出流,合并两个指定文件;当文件中的数据已排序时,合并后的数据也要求是已排序的。
(2)将Java的关键字保存在一个文本文件中,判断一个字符串是否为Java的关键字。
(3)编写在构造方法中产生一个1-5之间的随机数的继承Thread类的线程类DelayPrintThread,使得线程体每休眠此随机数时间就打印输出线程号和休眠时间;另外编写应用DelayPrintThread类的Java应用程序TwoThread.java,在main()方法中创建两个线程,并应用sleep()控制主应用程序延迟一段时间。
(4)编写继承Runnable接口的Applet多线程小程序类MultiThreadApplet,编写继承该类的Applet小程序类Clock,在Clock中重新构造父类的run()方法,实现数字时钟的功能,要求不断刷新显示时、分、秒。
(5)为课程表和学生成绩表(两表字段参见教材P325页表结构)设计数据库应用程序,包括数据的输入、删除和查询功能。
四、实验结果与分析
(1)
package t1;
import java.awt.Frame;
import java.io.*;
public class Test {
public static void main(String[] args)throws IOException {
try {
FileInputStream fr1=new FileInputStream("F:\\aa.txt");
FileInputStream fr2=new FileInputStream("F:\\bb.txt");
FileOutputStream fw=new FileOutputStream("F:\\cc.txt");
int n=0,m=0; //记录实际读取的字符数
char a[]=new char[1024];
char b[]=new char[1024];
byte c[]=new byte[2];
int count=0;
while ((count =fr1.read(c))!=-1)
{
fw.write(c,0,count);
}
while ((count =fr2.read(c))!=-1)
{
fw.write(c,0,count);
}
FileReader fr=new FileReader("F:\\cc.txt");
while(fr.read(a)!=-1)
System.out.println(a);
fr.close();
FileWriter fw2=new FileWriter("F:\\cc.txt");
while(a[m]!='\0')
{
m++;
}
for(int i=1;i
{
char d;
for(int j=0;j
{
if((a[j]>a[j+1]))
{
d=a[j];
a[j]=a[j+1];
a[j+1]=d;
}
}
}
System.out.println(a);
fw2.write(a);//写入文件cc.txt中
fr1.close();
fr2.close();
} catch (Exception e) {}
}
}
(2)package t2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unused")
public class Test{
public static void main(String[] args) throws IOException {
boolean yon = doCheck("main");
if(yon)
{ System.out.println("yes"); }
else
{ System.out.println("no"); }
}
private static boolean doCheck(String input) throws IOException {
boolean isHotKey = false;
File file = new File("D:/test.txt");
String content = "";
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String temp; while (null != (temp = br.readLine()))
{ content += temp; }
String[] keys = content.split(",");
List keyList = new ArrayList();
for (String key : keys)
{ keyList.add(key.trim()); }
isHotKey = keyList.contains(input);
return isHotKey;
}
}
(3)package t3;
public class Test{
public static void main(String[] args) {
DelayPrintThread d1=new DelayPrintThread();
d1.start();
try {
d1.sleep(d1.N()*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
DelayPrintThread d2=new DelayPrintThread();
d2.start();
try {
d1.sleep(d2.N()*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class DelayPrintThread extends Thread
{
private static int NUM=0;
private int n;
public void DelayPrintThread()
{
this.n=(int) (Math.random()%5+1);
}
public int N()
{
return n;
}
public void run()
{
try {
Thread.sleep(this.n*1000);
this.NUM++;
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("睡眠时间:"+this.n*1000+" ,线程号:"+this.NUM);
}
}
(4)package t4;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.util.Date;
public class Test extends JFrame implements Runnable {
private JLabel timeLabel=null;
private Date d=null;
public static void main(String args[])
{ new Demo(); }
public Demo() {
this.setTitle("多线程");
this.setSize(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timeLabel=new JLabel();
this.getContentPane().add(timeLabel,BorderLayout.NORTH);
Thread t=new Thread(this);
t.start();
}
public void run() {
while (true)
{
d=new Date();
timeLabel.setText(d.toString());
}
}
}
(5)import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
public class Test {
public static Statement stmt;
public static void main(String[] args) {
Test test=new Test();
test.ConnectionODBC();
}
public void ConnectionODBC()
{ try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=table.mdb";
Connection con = DriverManager.getConnection(url);
System.out.println("数据源连接成功...");
stmt=con.createStatement();
ResultSet Sfs=stmt.executeQuery("SELECT * FROM student");
for(int i=0;Sfs.getArray(i)!=null;i++)
System.out.println(Sfs.getArray(i));
Sfs.next();
Sfs.close();
ResultSet ISinto=stmt.executeQuery("insert into student(name,score)values('张三','90')");
ISinto.next();
ISinto.close();
ResultSet Delete=stmt.executeQuery("delete from student where name='张三'");
Delete.next();
Delete.close();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
五、实验注意事项
1、 实验过程中注意爱护计算机,实验完毕要按正常操作关闭计算机
2、 对字节流和字符流的输入、输出方法有所不同
3、 当连接不同类型数据库时,添加的驱动程序有所不同