java2实用教程实验代码及答案
更新时间:2024-06-04 17:52:01 阅读量: 综合文库 文档下载
- java推荐度:
- 相关推荐
上机实践1 初识JAVA ................................................................................................... 3 实验1 一个简单的应用程序 ........................................................................................ 3 实验2 一个简单的JAVA APPLET程序............................................................................ 3 实验3 联合编译.......................................................................................................... 4 上机实践2 基本数据类型与控制语句............................................................................. 5 实验1 输出希腊字母表 ............................................................................................... 5 实验2 回文数 ............................................................................................................. 6 实验3 猜数字游戏 ...................................................................................................... 8 上机实践3 类与对象 ..................................................................................................... 9 实验1 三角形、梯形和圆形的类封装 .......................................................................... 9 实验2 实例成员与类成员.......................................................................................... 12 实验3 使用PACKAGE语句与IMPORT语句 .................................................................. 14 上机实践4 继承与接口 ................................................................................................ 16 实验1 继承............................................................................................................... 16 实验2 上转型对象 .................................................................................................... 19 实验3 接口回调 ....................................................................................................... 21 上机实践5 字符串、时间与数字 .................................................................................. 23 实验1 STRING类的常用方法 ..................................................................................... 23 实验2 比较日期的大小 ............................................................................................. 24 实验3 处理大整数 .................................................................................................... 25 上机实践6 组件及事件处理 ......................................................................................... 26 实验1 算术测试........................................................................................................ 26 实验2 信号灯 ........................................................................................................... 29 实验3 布局与日历 .................................................................................................... 31 上机实践7 组件及事件处理2 ...................................................................................... 35 实验1 方程求根........................................................................................................ 35 实验2 字体对话框 .................................................................................................... 38 实验3 英语单词拼写训练.......................................................................................... 41 上机实践8 多线程 ....................................................................................................... 45 实验1 汉字打字练习................................................................................................. 45 实验2 旋转的行星 .................................................................................................... 47 实验3 双线程接力 .................................................................................................... 51 上机实践9 输入输出流 ................................................................................................ 54 实验1 学读汉字........................................................................................................ 54 实验2 统计英文单词字 ............................................................................................. 57
1
实验2 读取ZIP文件 ................................................................................................. 61 上机实践10 JAVA 中的网络编程................................................................................. 62 实验1 读取服务器端文件.......................................................................................... 62 实验2 使用套接字读取服务器端对象 ........................................................................ 64 实验3 基于UDP的图像传输 .................................................................................... 67 上机实践11 数据结构 .................................................................................................. 71 实验1 扫雷小游戏 .................................................................................................... 71 实验2 排序与查找 .................................................................................................... 76 实验3 使用TREESET排序 ......................................................................................... 77 上机实践12 JAVA SWING .......................................................................................... 79 实验1 JLAYEREDPANE分层窗格 ................................................................................. 79 实验2 使用表格显示日历.......................................................................................... 80 实验3 多文档界面(MDI) ...................................................................................... 83
2
上机实践1 初识Java
实验1 一个简单的应用程序
1.答案:
【代码1】: System.out.println(\你好,很高兴学习Java\【代码2】: System.out.println(\
2.模板代码 Hello.java
public class Hello {
public static void main (String args[ ]) {
【代码1】 //命令行窗口输出\你好,很高兴学习Java\ A a=new A(); a.fA(); } } class A {
void fA() {
【代码2】 //命令行窗口输出\ } }
实验2 一个简单的Java Applet程序
1.答案:
【代码1】: g.drawString(\这是一个Java Applet 程序\【代码2】: g.drawString(\我改变了字体\
2.模板代码 FirstApplet.java
import java.applet.*;
3
import java.awt.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.setColor(Color.blue);
【代码1】//在Java Applet中绘制一行文字:“这是一个Java Applet 程序” g.setColor(Color.red);
g.setFont(new Font(\宋体\
【代码2】//在Java Applet中绘制一行文字:“我改变了字体” } }
实验3 联合编译
1.答案:
【代码1】: System.out.println(\你好,只需编译我\【代码2】: System.out.println(\【代码3】: System.out.println(\【代码4】: System.out.println(\ C\
2.模板代码 模板1:Hello.java
public class MainClass {
public static void main (String args[ ]) {
【代码1】 //命令行窗口输出\你好,只需编译我\ A a=new A(); a.fA(); B b=new B(); b.fB(); } }
模板2 :A.java
public class A {
void fA() {
【代码2】 //命令行窗口输出\ } }
4
模板3 :B.java
public class B {
void fB() {
【代码3】 //命令行窗口输出\ } }
模板4 :C.java
public class C {
void fC() {
【代码4】 //命令行窗口输出\ }
}
上机实践2 基本数据类型与控制语句实验1 输出希腊字母表
1.答案:
【代码1】: startPosition=(int)cStart;
【代码2】: endPosition=(int)cEnd ; 【代码3】: c=(char)i;
2.模板代码 GreekAlphabet.java
public class GreekAlphabet {
public static void main (String args[ ]) {
int startPosition=0,endPosition=0; char cStart='α',cEnd='ω';
【代码1】 //cStart做int型转换据运算,并将结果赋值给startPosition 【代码2】 //cEnd做int型转换运算,并将结果赋值给endPosition System.out.println(\希腊字母\\'α\\'在unicode表中的顺序位置:\ System.out.println(\希腊字母表:\ for(int i=startPosition;i<=endPosition;i++) {
5
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()
11
{
【代码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(\圆的周长:\
【代码19】 // circle调用方法返回面积并赋值给area System.out.println(\圆的面积:\
【代码20】 // trangle调用方法返回周长并赋值给length System.out.println(\三角形的周长:\
【代码21】 // trangle调用方法返回面积并赋值给area System.out.println(\三角形的面积:\
【代码22】 // lader调用方法返回面积并赋值给area System.out.println(\梯形的面积:\
【代码23】 // trangle调用方法设置三个边,要求将三个边修改为12,34,1。 【代码24】 // trangle调用方法返回面积并赋值给area System.out.println(\三角形的面积:\
【代码25】 // trangle调用方法返回周长并赋值给length System.out.println(\三角形的周长:\ } }
实验2 实例成员与类成员
1.答案:
12
【代码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();
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);
13
} }
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语句
1.答案:无 2.模板代码
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 {
14
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\ } 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 {
15
public static void main(String args[ ]) {
SquareEquation equation=new SquareEquation(4,5,1); equation.getRoots();
equation.setCoefficient(-3,4,5); equation.getRoots(); } }
上机实践4 继承与接口
实验1 继承
1.答案:
【代码1】: public void speakHello()
{
System.out.println(\你好,吃饭了吗?\ }
【代码2】:
public void averageHeight()
{
height=173;
System.out.println(\中国人的平均身高:\厘米\ }
【代码3】:
public void averageWeight()
{
weight=67.34;
System.out.println(\中国人的平均体重:\公斤\ } 【代码4】: System.out.println(\坐如钟,站如松,睡如弓\
【代码5】:
public void speakHello()
{
System.out.println(\ou do\ }
【代码6】:
public void averageHeight()
{
16
height=188;
System.out.println(\verage height:\ }
【代码7】:
public void averageWeight() {
weight=80.23;
System.out.println(\verage weight:\
}
【代码8】: System.out.println(\直拳、钩拳\
【代码9】:
public void speakHello()
{
System.out.println(\您好\ }
【代码10】:
public void averageHeight()
{
height=16;
System.out.println(\北京人的平均身高:\厘米\ }
【代码11】:
public void averageWeight()
{
weight=6;
System.out.println(\北京人的平均体重:\公斤\ } 【代码12】: System.out.println(\京剧术语\2.模板代码
Example.java
class People {
protected double weight,height; public void speakHello() {
System.out.println(\ }
public void averageHeight() {
height=173;
System.out.println(\ }
public void averageWeight()
17
{
weight=70;
System.out.println(\ } }
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();
18
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 上转型对象
1.答案:
【代码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 { salaries=salaries+employee[i].earnings(); } 2.模板代码 19 HardWork.java 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 if(i%3==0) employee[i]=new WeekWorker(); else if(i%3==1) employee[i]=new MonthWorker(); else if(i%3==2) 20 employee[i]=new YearWorker(); } Company company=new Company(employee); System.out.println(\公司年工资总额:\ } } 实验3 接口回调 1.答案: 【代码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 totalWeights=totalWeights+goods[k].computeWeight(); } 2.模板代码 Road.java interface ComputerWeight { public double computeWeight(); } class Television implements ComputerWeight { 【代码1】 //实现computeWeight()方法。 } class Computer implements ComputerWeight { 【代码2】 //实现computeWeight()方法。 } class WashMachine implements ComputerWeight 21 { 【代码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[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[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()); } } 22 上机实践5 字符串、时间与数字 实验1 String类的常用方法 模板代码 StringExample.java class StringExample { public static void main(String args[]) { String s1=new String(\ s2=new String(\ if(【代码1】) // 使用equals方法判断s1与s2是否相同 { System.out.println(\与s2相同\ } else { System.out.println(\与s2不相同\ } String s3=new String(\ if(【代码2】) //判断s3的前缀是否是“220302”。 { System.out.println(\吉林省的身份证\ } String s4=new String(\你\ s5=new String(\我\ if(【代码3】)//按着字典序s4大于s5的表达式。 { System.out.println(\按字典序s4大于s5\ } else { System.out.println(\按字典序s4小于s5\ } int position=0; String path=\ position=【代码5】 //获取path中最后出现目录分隔符号的位置 System.out.println(\中最后出现\\\\的位置:\ String fileName=【代码6】//获取path中“A.java”子字符串。 System.out.println(\中含有的文件名:\ String s6=new String(\ s7=new String(\ 23 int n1=【代码7】 //将s6转化成int型数据。 double n2=【代码8】 //将s7转化成double型数据。 double m=n1+n2; System.out.println(m); String s8=【代码9】 //String调用valuOf(int n)方法将m转化为字符串对象 position=s8.indexOf(\ String temp=s8.substring(position+1); System.out.println(\数字\有\位小数\ String s9=new String(\ char a[]=【代码10】 //将s8存放到数组a中。 for(int i=a.length-1;i>=0;i--) { System.out.print(\ } } } 实验2 比较日期的大小 模板代码 DateExample import java.util.*; import javax.swing.JOptionPane; public class DateExample { public static void main(String args[ ]) { String str=JOptionPane.showInputDialog(\输入第一个日期的年份:\ int yearOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该年的月份:\ int monthOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该月份的日期:\ int dayOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入第二个日期的年份:\ int yearTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该年的月份:\ int monthTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该月份的日期:\ int dayTwo=Integer.parseInt(str); Calendar calendar=【代码1】 //初始化日历对象 【代码2】 //将calendar的时间设置为yearOne年monthOne月dayOne日 long timeOne=【代码3】 //calendar表示的时间转换成毫秒 【代码4】 //将calendar的时间设置为yearTwo年monthTwo月dayTwo日 24 long timeTwo=【代码5】 //calendar表示的时间转换成毫秒。 Date date1=【代码6】 // 用timeOne做参数构造date1 Date date2=【代码7】 // 用timeTwo做参数构造date2 if(date2.equals(date1)) { System.out.println(\两个日期的年、月、日完全相同\ } else if(date2.after(date1)) { System.out.println(\您输入的第二个日期大于第一个日期\ } else if(date2.before(date1)) { System.out.println(\您输入的第二个日期小于第一个日期\ } long days=【代码8】//计算两个日期相隔天数 System.out.println(yearOne+\年\月\日和\ +yearTwo+\年\月\相隔\天\ } } 实验3 处理大整数 模板代码 BigintegerExample import java.math.*; class BigIntegerExample { public static void main(String args[]) { BigInteger n1=new BigInteger(\ n2=new BigInteger(\ result=null; result=【代码1】//n1和n2做加法运算 System.out.println(\和:\ result=【代码2】//n1和n2做减法运算 System.out.println(\差:\ result=【代码3】//n1和n2做乘法运算 System.out.println(\积:\ result=【代码4】//n1和n2做除法运算 System.out.println(\商:\ BigInteger m=new BigInteger(\ COUNT=new BigInteger(\ ONE=new BigInteger(\ 25 signal.setYellow(0); signal.setGreen(0); signal.setPosition(w/3,0); signal.repaint(); } } else if(item.equals(itemYellow)) { if(signal!=null) { signal.setRed(0); signal.setYellow(1); signal.setGreen(0); signal.setPosition(w/3,h/3); signal.repaint(); } } else if(item.equals(itemGreen)) { if(signal!=null) { signal.setRed(0); signal.setYellow(0); signal.setGreen(1); signal.setPosition(w/3,2*h/3); signal.repaint(); } } } } SignalMainClass.java public class SignalMainClass { public static void main(String args[]) { SignalFrame frame; frame=new SignalFrame() ; frame.setTitle(\信号灯\ } } 实验3 布局与日历 1.答案: 【代码1】: pCenter.setLayout(new GridLayout(7,7)); 【代码2】: pCenter.add(titleName[i]); 【代码3】: pCenter.add(labelDay[i]); 【代码4】: add(scrollPane,BorderLayout.CENTER); 31 【代码5】: add(pNorth,BorderLayout.NORTH); 【代码6】: add(pSouth,BorderLayout.SOUTH); 2.模板代码 CalendarBean.java import java.util.Calendar; public class CalendarBean { String day[]; int year=2005,month=0; public void setYear(int year) { this.year=year; } public int getYear() { return year; } public void setMonth(int month) { this.month=month; } public int getMonth() { return month; } public String[] getCalendar() { String a[]=new String[42]; Calendar 日历=Calendar.getInstance(); 日历.set(year,month-1,1); int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1; int day=0; if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { day=31; } if(month==4||month==6||month==9||month==11) { day=30; } if(month==2) { if(((year%4==0)&&(year0!=0))||(year@0==0)) { day=29; } else { day=28; } } for(int i=星期几,n=1;i<星期几+day;i++) { 32 a[i]=String.valueOf(n) ; n++; } return a; } } CalendarFrame.java import java.util.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class CalendarFrame extends Frame implements ActionListener { Label labelDay[]=new Label[42]; Button titleName[]=new Button[7]; String name[]={\日\一\二\三\四\五\六\ Button nextMonth,previousMonth; int year=2006,month=10; CalendarBean calendar; Label showMessage=new Label(\ public CalendarFrame() { Panel pCenter=new Panel(); 【代码1】 //将pCenter的布局设置为7行7列的GridLayout 布局。 for(int i=0;i<7;i++) { titleName[i]=new Button(name[i]); 【代码2】//pCenter添加组件titleName[i]。 } for(int i=0;i<42;i++) { labelDay[i]=new Label(\ 【代码3】//pCenter添加组件labelDay[i]。 } calendar=new CalendarBean(); calendar.setYear(year); calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } nextMonth=new Button(\下月\ previousMonth=new Button(\上月\ nextMonth.addActionListener(this); previousMonth.addActionListener(this); Panel pNorth=new Panel(), pSouth=new Panel(); 33 pNorth.add(previousMonth); pNorth.add(nextMonth); pSouth.add(showMessage); showMessage.setText(\日历:\ear()+\年\月\ ScrollPane scrollPane=new ScrollPane(); scrollPane.add(pCenter); 【代码4】// 窗口添加scrollPane在中心区域 【代码5】// 窗口添加pNorth 在北面区域 【代码6】// 窗口添加pSouth 在南区域。 } public void actionPerformed(ActionEvent e) { if(e.getSource()==nextMonth) { month=month+1; if(month>12) month=1; calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } } else if(e.getSource()==previousMonth) { month=month-1; if(month<1) month=12; calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } } showMessage.setText(\日历:\ear()+\年\月\ } } CalendarMainClass.java public class CalendarMainClass { public static void main(String args[]) { CalendarFrame frame=new CalendarFrame(); frame.setBounds(100,100,360,300); frame.setVisible(true); frame.validate(); frame.addWindowListener(new java.awt.event.WindowAdapter() 34 { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } } ); } } 上机实践7 组件及事件处理2 实验1 方程求根 1.答案: 【代码1】: controlButton.addActionListener(this);; 【代码2】: textA.getText() 【代码3】: textB.getText() 【代码4】: textC.getText() 2.模板代码 SquareEquation.java public class SquareEquation { double a,b,c; double root1,root2; public void setA(double a) { this.a=a; } public void setB(double b) { this.b=b; } public void setC(double c) { this.c=c; } public double getRootOne() throws NoRealRootException,NoSquareEquationException { if(a!=0) { double disk=b*b-4*a*c; if(disk>=0) { root1=(-b+Math.sqrt(disk))/(2*a); } else { throw new NoRealRootException(\没有实根\ 35 } } else { throw new NoRealRootException(\不是二次方程\ } return root1; } public double getRootTwo() throws NoRealRootException,NoSquareEquationException { if(a!=0) { double disk=b*b-4*a*c; if(disk>=0) { root2=(-b-Math.sqrt(disk))/(2*a); } else { throw new NoRealRootException(\没有实根\ } } else { throw new NoRealRootException(\不是二次方程\ } return root2; } } class NoRealRootException extends Exception { String message; NoRealRootException(String s) { message=s; } public String getMessage() { return message; } } class NoSquareEquationException extends Exception { String message; NoSquareEquationException(String s) { message=s; } public String getMessage() { return message; } } EquationFrame.java 36 import java.awt.*; import java.awt.event.*; public class EquationFrame extends Frame implements ActionListener { SquareEquation equation; TextField textA,textB,textC; TextArea showRoots; Button controlButton; public EquationFrame() { equation=new SquareEquation(); textA=new TextField(8); textB=new TextField(8); textC=new TextField(8); showRoots=new TextArea(); controlButton=new Button(\确定\ Panel pNorth=new Panel(); pNorth.add(new Label(\二次项系数:\ pNorth.add(textA); pNorth.add(new Label(\一次项系数:\ pNorth.add(textB); pNorth.add(new Label(\常数项系数:\ pNorth.add(textC); pNorth.add(controlButton); 【代码1】 //当前窗口作为controlButton的ActionEvent事件的监视器 add(pNorth,BorderLayout.NORTH); add(showRoots,BorderLayout.CENTER); setBounds(100,100,630,160); setVisible(true); validate(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public void actionPerformed(ActionEvent e) { try{ double a=Double.parseDouble(【代码2】); //textA调用方法获取其中的文本 double b=Double.parseDouble(【代码3】); //textB调用方法获取其中的文本 double c=Double.parseDouble(【代码4】); // textC调用方法获取其中的文本 equation.setA(a); equation.setB(b); equation.setC(c); textA.setText(\ 37 textB.setText(\ textC.setText(\ showRoots.append(\根:\ showRoots.append(\根:\ } catch(Exception ex) { showRoots.append(\ } } } EquationMainClass.java public class EquationMainClass { public static void main(String args[]) { EquationFrame frame=new EquationFrame(); } } 实验2 字体对话框 1.答案: 【代码1】: setModal(true); 【代码2】: setVisible(false); 【代码3】: setVisible(false); 【代码4】: new FontDialog(this); 【代码5】: setVisible(true); 【代码6】: dialog.setTitle(\字体对话框\ 2.模板代码 FontFamilyNames.java import java.awt.GraphicsEnvironment; public class FontFamilyNames { String fontName[]; public String [] getFontName() { GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); fontName=ge.getAvailableFontFamilyNames(); return fontName; } } FontDialog.java import java.awt.event.*; import java.awt.*; import javax.swing.JLabel; 38 public class FontDialog extends Dialog implements ItemListener,ActionListener { FontFamilyNames fontFamilyNames; int fontSize=38; String fontName; Choice fontNameList; JLabel label; Font font; Button yes,cancel; static int YES=1,NO=0; int state=-1; FontDialog(Frame f) { super(f); fontFamilyNames=new FontFamilyNames(); 【代码1】 //对话框设置为有模式 yes=new Button(\ cancel=new Button(\ yes.addActionListener(this); cancel.addActionListener(this); label=new JLabel(\奥运\ fontNameList=new Choice(); String name[]=fontFamilyNames.getFontName(); for(int k=0;k fontNameList.addItemListener(this); add(fontNameList,BorderLayout.NORTH); add(label,BorderLayout.CENTER); Panel pSouth=new Panel(); pSouth.add(yes); pSouth.add(cancel); add(pSouth,BorderLayout.SOUTH); setBounds(100,100,280,170); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { state=NO; setVisible(false); } } ); validate(); } public void itemStateChanged(ItemEvent e) { fontName=(String)fontNameList.getSelectedItem(); font=new Font(fontName,Font.BOLD,fontSize); 39 label.setFont(font); label.repaint(); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==yes) { state=YES; 【代码2】 //对话框设置为不可见 } else if(e.getSource()==cancel) { state=NO; 【代码3】 //对话框设置为不可见 } } public int getState() { return state; } public Font getFont() { return font; } } FrameHaveDialog.java import java.awt.event.*; import java.awt.*; import javax.swing.JTextArea; public class FrameHaveDialog extends Frame implements ActionListener { JTextArea text; Button buttonFont; FrameHaveDialog() { buttonFont=new Button(\设置字体\ text=new JTextArea(\实用教程(第三版)\ buttonFont.addActionListener(this); add(buttonFont,BorderLayout.NORTH); add(text); setBounds(60,60,300,300); setVisible(true); validate(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } 40
正在阅读:
java2实用教程实验代码及答案06-04
机组直流蓄电池整组更换的安全、组织和技术措施(修改版)10-18
基于分数Black_Scholes模型的外汇期权定价及其评判检验05-10
C++上机题目复习及答案210-16
民诉课件12-13
广中医针灸学试卷 十套10-15
2019-2020年七年级历史上学期期中试题(新版)新人教版(I)01-08
最新人教版新课标数学小学一年级下册十几减8、7、6公开课教学设06-30
矿井轨道提升运输安全技术规范01-19
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 答案
- 实验
- 代码
- 实用
- 教程
- java2