《Java程序设计》实验报告二
班级: 学号: 姓名:
1 题目 编写图形界面程序,根据用户输入的5个浮点数和一个文件目录名,将这5个数保存才文件中,再将5个数读取后排序追加到文件中
2 目的和要求 学习并掌握java文件读写的相关操作
3 功能说明 输入5个数字和一个文件名,点击转换后将5个数字保存到文件中并将读取到得5个数字排序后追加到文件中
4 设计流程图
5 源程序
import javax.swing.*;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
public class homework2 {
public static void main(String[] args) {
SimpleFrame sf=new SimpleFrame();
sf.setVisible(true);
}
}
class SimpleFrame extends JFrame{
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JTextField tf4;
private JTextField tf5;
private JTextField tf6;
SimpleFrame(){
setSize(300,400);
setTitle("test");//框架的标题和大小
setLocation(500,250);//设置框架在屏幕上的位置
tf1=new JTextField(6);
tf2=new JTextField(6);
tf3=new JTextField(6);
tf4=new JTextField(6);
tf5=new JTextField(6);
tf6=new JTextField(20);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭方式
JButton btn =new JButton("转换");//按钮名
btn.addActionListener(new ActionListener(){//添加监听器
public void actionPerformed(ActionEvent e){
String s;
s=tf6.getText();
double num[]=new double [5];//定义一个数组
num[0] = Double.parseDouble(tf1.getText());
num[1] = Double.parseDouble(tf2.getText());
num[2] = Double.parseDouble(tf3.getText());
num[3] = Double.parseDouble(tf4.getText());
num[4] = Double.parseDouble(tf5.getText());//分别输入5个数
try{//捕获可能出现的异常
RandomAccessFile randf=new RandomAccessFile(s,"rw");//以读写方式打开s文件
File out=new File(s);
FileOutputStream fis=new FileOutputStream(out);
DataOutputStream dos=new DataOutputStream(fis);
PrintStream ps=new PrintStream(fis);
for(int i=0;i<num.length;i++)
randf.writeDouble(num[i]);
for(int i=0;i<5;i++)
{
randf.seek(i*8);
String x=String.valueOf(randf.readDouble());
num[i]=Double.valueOf(x);
}
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(num[i]<num[j])
{
double temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}//从小到大排序
for(int i=0;i<num.length;i++)
{
randf.writeDouble(num[i]);
System.out.println(num[i]);
}
randf.seek(5*8);
String x=String.valueOf(randf.readDouble());
tf1.setText(x);
randf.seek(6*8);
x=String.valueOf(randf.readDouble());
tf2.setText(x);
randf.seek(7*8);
x=String.valueOf(randf.readDouble());
tf3.setText(x);
randf.seek(8*8);
x=String.valueOf(randf.readDouble());
tf4.setText(x);
randf.seek(9*8);
x=String.valueOf(randf.readDouble());
tf5.setText(x);
randf.close();
}catch(IOException ee){System.out.println("File access error:"+ee);}//处理异常
}
});
JPanel panel=new JPanel();
setContentPane(panel);
add(tf1);
add(tf2);
add(tf3);
add(tf4);
add(tf5);
add(tf6);//添加文本框
panel.add(btn);//添加文本框
}
}
6 数据结果
7 实验心得与体会 通过这一次实验加深了我对java文件读取操作的相关了解
第二篇:Java 实验项目二
Java 实验项目二
实验3 类与对象
[实验目的]
1、 熟悉Java面向对象程序设计的基本思想。
2、 掌握类与对象的定义及使用方法。
3、 掌握package语句与import语句的用法。
[实验要求]
1、 复习理论教学中所学的内容。
2、 认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。
3、 认真总结实验并书写实验报告。
[实验课时] 2学时
[实验教学方式] 学生上机实验,教师随堂指导。
[实验内容]
1、三角形、梯形和圆形的类封装:分别定义三角形、梯形和圆形类,要求每个类中包含构造方法、求周长及面积的方法。最后在主方法中为三角形、梯形和圆形类创建对象并打印输出各种图形的周长及面积。该程序的模板代码如下:请将其补充完整并调试运行。 AreaAndLength.java
class Trangle
{
double sideA,sideB,sideC,area,length;
boolean boo;
public Trangle(double a,double b,double c)
{
【代码1】 //参数a,b,c分别赋值给sideA,sideB,sideC
if(【代码2】) //a,b,c构成三角形的条件表达式
{
【代码3】 //给boo赋值。
}
else
{
【代码4】 //给boo赋值。
}
}
double getLength()
{
【代码5】 //方法体,要求计算出length的值并返回
}
public double getArea()
{
if(boo)
{
double p=(sideA+sideB+sideC)/2.0;
area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ;
return area;
}
else
{
System.out.println("不是一个三角形,不能计算面积"); return 0;
}
}
public void setABC(double a,double b,double c)
{
【代码6】 //参数a,b,c分别赋值给sideA,sideB,sideC
if(【代码7】) //a,b,c构成三角形的条件表达式
{
【代码8】 //给boo赋值。
}
else
{
【代码9】 //给boo赋值。
}
}
}
class Lader
{
double above,bottom,height,area;
Lader(double a,double b,double h)
{
【代码10】 //方法体,将参数a,b,c分别赋值给above,bottom,height }
double getArea()
{
【代码11】 //方法体,,要求计算出area返回
}
}
class Circle
{
double radius,area;
Circle(double r)
{
【代码12】 //方法体
}
double getArea()
{
【代码13】 //方法体,要求计算出area返回
}
double getLength()
{
【代码14】 //getArea方法体的代码,要求计算出length返回
}
void setRadius(double newRadius)
{
radius=newRadius;
}
double getRadius()
{
return radius;
}
}
public class AreaAndLength
{
public static void main(String args[])
{
double length,area;
Circle circle=null;
Trangle trangle;
Lader lader;
【代码15】 //创建对象circle
【代码16】 //创建对象trangle。
【代码17】 //创建对象lader
【代码18】 // circle调用方法返回周长并赋值给length
System.out.println("圆的周长:"+length);
【代码19】 // circle调用方法返回面积并赋值给area
System.out.println("圆的面积:"+area);
【代码20】 // trangle调用方法返回周长并赋值给length
System.out.println("三角形的周长:"+length);
【代码21】 // trangle调用方法返回面积并赋值给area
System.out.println("三角形的面积:"+area);
【代码22】 // lader调用方法返回面积并赋值给area
System.out.println("梯形的面积:"+area);
【代码23】 // trangle调用方法设置三个边,要求将三个边修改为12,34,1。
【代码24】 // trangle调用方法返回面积并赋值给area
System.out.println("三角形的面积:"+area);
【代码25】 // trangle调用方法返回周长并赋值给length
System.out.println("三角形的周长:"+length);
}
}
2、学习实例成员与类成员的区别:按照下面模板代码中的注释信息将该程序补充完整并调试运行。
Example.java
class A
{
【代码1】 //声明一个float型实例变量a
【代码2】 //声明一个float型类变量b,即static变量b
void setA(float a)
{
【代码3】 //将参数a的值赋值给成员变量a
}
void setB(float b)
{
【代码4】 //将参数b的值赋值给成员变量b
}
float getA()
{
return a;
}
float getB()
{
return b;
}
void inputA()
{
System.out.println(a);
}
static void inputB()
{
System.out.println(b);
}
}
public class Example
{
public static void main(String args[])
{
【代码5】 //通过类名操作类变量b,并赋值100
【代码6】 //通过类名调用方法inputB()
A cat=new A();
A dog=new A();
【代码7】 //cat象调用方法setA(int a)将cat的成员a的值设置为200
【代码8】 //cat调用方法setB(int b)将cat的成员b的值设置为400
【代码9】 //dog象调用方法setA(int a)将dog的成员a的值设置为150
【代码10】 //dog调用方法setB(int b)将dog的成员b的值设置为300
【代码11】 //cat调用inputA()。
【代码12】 //cat调用inputB()。
【代码13】 //dog调用inputA()。
【代码14】 //dog调用inputB()。
}
}
3、使用package语句与import语句:上机调试下面程序,学会package与import语句的用法。
SquareEquation.java
package tom.jiafei;
public class SquareEquation
{
double a,b,c;
double root1,root2;
boolean boo;
public SquareEquation(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
if(a!=0)
{
boo=true;
}
else
{
boo=false;
}
}
public void getRoots()
{
if(boo)
{
System.out.println("是一元2次方程");
double disk=b*b-4*a*c;
if(disk>=0)
{
root1=(-b+Math.sqrt(disk))/(2*a);
root2=(-b-Math.sqrt(disk))/(2*a);
System.out.printf("方程的根:%f,%f\n",root1,root2);
}
else
{
System.out.printf("方程没有实根\n"); }
}
else
{
System.out.println("不是一元2次方程"); }
}
public void setCoefficient(double a,double b,double c) {
this.a=a;
this.b=b;
this.c=c;
if(a!=0)
{
boo=true;
}
else
{
boo=false;
}
}
}
SunRise.java
import tom.jiafei.*;
class SunRise
{
public static void main(String args[ ])
{
SquareEquation equation=new SquareEquation(4,5,1); equation.getRoots();
equation.setCoefficient(-3,4,5);
equation.getRoots();
}
}
[实验参考代码]
1、【代码1】: sideA=a;
sideB=b;
sideC=c;
【代码2】: a+b>c&&a+c>b&&c+b>a
【代码3】: boo=true;
【代码4】: boo=false;
【代码5】:
if(boo)
{
length=sideA+sideB+sideC;
return length;
}
else
{
System.out.println("不是一个三角形,不能计算周长"); return 0;
}
【代码6】: sideA=a;sideB=b;sideC=c;
【代码7】: a+b>c&&a+c>b&&c+b>a
【代码8】: boo=true;
【代码9】: boo=false;
【代码10】:
above=a;
bottom=b;
height=h;
【代码11】:
area=(above+bottom)/2*height;
return area;
【代码12】: radius=r;
【代码13】: return 3.14*radius*radius;
【代码14】: return 3.14*2*radius;
【代码15】: circle=new Circle(10);
【代码16】: trangle=new Trangle(3,4,5);
【代码17】: lader=new Lader(3,4,10);
【代码18】: length=circle.getLength();
【代码19】: area=circle.getArea();
【代码20】: length=trangle.getLength();
【代码21】: area=trangle.getArea();
【代码22】: area=lader.getArea();
【代码23】: trangle.setABC(12,34,1);
【代码24】: area=trangle.getArea();
【代码25】: length=trangle.getLength();
2、【代码1】: float a;
【代码2】: static float b;
【代码3】: this.a=a;
【代码4】: this.b=b;
【代码5】: A.b=100;
【代码6】: A.inputB();
【代码7】: cat.setA(200);
【代码8】: cat.setB(400);
【代码9】: dog.setA(150);
【代码10】:dog.setB(300);
【代码11】:cat.inputA();
【代码12】:cat.inputB();
【代码13】:dog.inputA();
【代码14】:dog.inputB();
实验4 继承与接口
[实验目的]
1、掌握java 继承中父类及其子类的定义方法。
2、掌握子类重写父类同名方法的方法。
3、掌握用法。
[实验要求]
1、 复习理论教学中所学的内容。
2、 认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。
3、 认真总结实验并书写实验报告。
[实验课时] 2学时
[实验教学方式] 学生上机实验,教师随堂指导。
[实验内容]
1、定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。该程序的模板代码如下:请将其补充完整并调试运行。
class People
{
protected double weight,height;
public void speakHello()
{
System.out.println("yayawawa");
}
public void averageHeight()
{
height=173;
System.out.println("average height:"+height);
}
public void averageWeight()
{
weight=70;
System.out.println("average weight:"+weight);
}
}
class ChinaPeople extends People
{
【代码1】 //重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的 //汉语信息
【代码2】 //重写public void averageHeight()方法,要求输出类似
//“中国人的平均身高:168.78厘米”这样的汉语信息
【代码3】 //重写public void averageWeight()方法,
//要求输出类似“中国人的平均体重:65公斤”这样的汉语信息
public void chinaGongfu()
{
【代码4】//输出中国武术的信息,例如:"坐如钟,站如松,睡如弓"等
}
}
class AmericanPeople extends People
{
【代码5】 //重写public void speakHello()方法,要求输出类似
//“How do you do”这样的英语信息。
【代码6】 //重写public void averageHeight()方法
【代码7】 //重写public void averageWeight()方法
public void americanBoxing()
{
【代码8】//输出拳击的信息,例如,“直拳”、“钩拳”等
}
}
class BeijingPeople extends ChinaPeople
{
【代码9】 //重写public void speakHello()方法,要求输出类似“您好”这样的汉语信息
【代码10】 //重写public void averageHeight()方法
【代码11】 //重写public void averageWeight()方法
public void beijingOpera()
{
【代码12】//输出京剧的信息
}
}
public class Example
{
public static void main(String args[])
{
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera() ;
beijingPeople.chinaGongfu();
}
}
2、读懂下面模板代码,按要求补充程序并调试运行。掌握抽象类的定义及其实现方法,学习上转型对象的运用方法。
abstract class Employee
{
public abstract double earnings();
}
class YearWorker extends Employee
{
【代码1】 //重写earnings()方法
}
class MonthWorker extends Employee
{
【代码2】 //重写earnings()方法。
}
class WeekWorker extends Employee
{
【代码3】 //重写earnings()方法。
}
class Company
{
Employee[] employee;
double salaries=0;
Company(Employee[] employee)
{
this.employee=employee;
}
public double salariesPay()
{
salaries=0;
【代码4】 //计算salaries。
return salaries;
}
}
public class HardWork
{
public static void main(String args[])
{
Employee[] employee=new Employee[20];
for(int i=0;i<employee.length;i++)
{
if(i%3==0)
employee[i]=new WeekWorker();
else if(i%3==1)
employee[i]=new MonthWorker();
else if(i%3==2)
employee[i]=new YearWorker();
}
Company company=new Company(employee);
System.out.println("公司年工资总额:"+company.salariesPay());
}
}
3、读懂下面模板代码,按要求补充程序并调试运行。掌握接口的定义及其实现方法,学习接口回调的运用方法。
interface ComputerWeight
{
public double computeWeight();
}
class Television implements ComputerWeight
{ 【代码1】 //实现computeWeight()方法。
}
class Computer implements ComputerWeight
{ 【代码2】 //实现computeWeight()方法。
}
class WashMachine implements ComputerWeight
{ 【代码3】 //实现computeWeight()方法。
}
class Car
{ ComputerWeight[] goods;
double totalWeights=0;
Car(ComputerWeight[] goods)
{
this.goods=goods;
}
public double getTotalWeights()
{
totalWeights=0;
【代码4】 //计算totalWeights
return totalWeights;
}
}
public class Road
{
public static void main(String args[])
{ ComputerWeight[] goodsOne=new ComputerWeight[50],
goodsTwo=new ComputerWeight[22] ;
for(int i=0;i<goodsOne.length;i++)
{ if(i%3==0)
goodsOne[i]=new Television();
else if(i%3==1)
goodsOne[i]=new Computer();
else if(i%3==2)
goodsOne[i]=new WashMachine();
}
for(int i=0;i<goodsTwo.length;i++)
{ if(i%3==0)
goodsTwo[i]=new Television();
else if(i%3==1)
goodsTwo[i]=new Computer();
else if(i%3==2)
goodsTwo[i]=new WashMachine();
}
Car 大货车=new Car(goodsOne);
System.out.println("大货车装载的货物重量:"+大货车.getTotalWeights()); Car 小货车=new Car(goodsTwo);
System.out.println("小货车装载的货物重量:"+小货车.getTotalWeights()); }
}
[参考答案]
1.答案:
【代码1】: public void speakHello()
{
System.out.println("你好,吃饭了吗?");
}
【代码2】:
public void averageHeight()
{
height=173;
System.out.println("中国人的平均身高:"+height+"厘米"); }
【代码3】:
public void averageWeight()
{
weight=67.34;
System.out.println("中国人的平均体重:"+weight+"公斤"); }
【代码4】: System.out.println("坐如钟,站如松,睡如弓");
【代码5】:
public void speakHello()
{
System.out.println("How do You do");
}
【代码6】:
public void averageHeight()
{
height=188;
System.out.println("Amerian Average height:"+height+" cm"); }
【代码7】:
public void averageWeight()
{
weight=80.23;
System.out.println("Amerian Average weight:"+weight+" kg");
}
【代码8】: System.out.println("直拳、钩拳");
【代码9】:
public void speakHello()
{
System.out.println("您好");
}
【代码10】:
public void averageHeight()
{
height=16;
System.out.println("北京人的平均身高:"+height+"厘米"); }
【代码11】:
public void averageWeight()
{
weight=6;
System.out.println("北京人的平均体重:"+weight+"公斤"); }
【代码12】: System.out.println("京剧术语");
2.答案:
【代码1】:
public double earnings()
{
return 50000.456;
}
【代码2】:
public double earnings()
{
return 12*2300;
}
【代码3】:
public double earnings()
{
return 52*500;
}
【代码4】:
for(int i=0;i<employee.length;i++)
{
salaries=salaries+employee[i].earnings();
}
3.答案:
【代码1】:
public double computeWeight()
{ return 45.5;
}
【代码2】:
public double computeWeight()
{ return 65.5;
}
【代码3】:
public double computeWeight()
{
return 145;
}
【代码4】:
for(int k=0;k<goods.length;k++)
{
totalWeights=totalWeights+goods[k].computeWeight(); }
[作业]
课外继续完成实验项目,认真完成实验报告。