java实验课程教学大纲-孙晶京(7)

2019-03-03 16:50

实验要求: 用户输入两个日期,程序将判断两个日期的大小关系,以及两日期间隔的天数。

实验内容: CompareDate.java

import java.util.*;

public class CompareDate {

public static void main(String args[ ]) { Scanner scanner = new Scanner(System.in); System.out.println(\输入第一个年,月,日数据\ System.out.print(\输入年份\ short yearOne = scanner.nextShort(); System.out.print(\输入月份\ byte monthOne = scanner.nextByte(); System.out.print(\输入日期\ byte dayOne = scanner.nextByte();

System.out.println(\输入第二个年,月,日数据\ System.out.print(\输入年份\ short yearTwo = scanner.nextShort(); System.out.print(\输入月份\ byte monthTwo= scanner.nextByte(); System.out.print(\输入日期\ byte dayTwo = scanner.nextByte();

Calendar calendar = 【代码1】 //初始化日历对象

【代码2】 //将calendar的时间设置为yearOne年monthOne月dayOne日 long timeOne =【代码3】 //calendar表示的时间转换成毫秒 calendar.set(yearTwo,monthTwo-1,dayTwo); long timeTwo=calendar.getTimeInMillis();

Date date1 = 【代码4】 // 用timeOne做参数构造date1 Date date2 =new Date(timeTwo); 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=【代码5】//使用timeTwo,timeOne计算两个日期相隔天数 System.out.println(yearOne+\年\月\日和\

+yearTwo+\年\月\相隔\天\ } }

实验内容要求:将【代码】部分替换为java程序代码。

实验报告要求:要有实验名称,实验目的,实验要求,实验内容(源代码),运行结果和总结。

实验4 处理大整数

实验教学目标: 掌握Biginteger类的常用方法。 实验场地或主要设备与器材:计算机

实验要求: 计算2个大整数的和差积商,并计算出一个大整数的因子个数(不包括1

和本身)。

实验内容: HandleBigInteger.java

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(\ TWO=new BigInteger(\

System.out.println(m.toString()+\的因子有:\

for(BigInteger i=TWO;i.compareTo(m)<0;i=i.add(ONE)) { if((n1.remainder(i).compareTo(BigInteger.ZERO))==0) { COUNT=COUNT.add(ONE); System.out.print(\ \ } }

System.out.println(\

System.out.println(m.toString()+\一共有\个因子\ } }

实验内容要求:将【代码】部分替换为java程序代码。

实验报告要求:要有实验名称,实验目的,实验要求,实验内容(源代码),运行结果和总结。

实验5 替换IP

实验教学目标: 掌握怎样使用patten类和Match类检索字符串。 实验场地或主要设备与器材:计算机

实验要求: 字符串“登录网站:222.128.89.253”中的正确IP是202.192.78.56。编写程序,输出把错写的IP:222.128.89.253替换为正确的IP:202.192.78.56

实验内容: ReplaceErrorWord.java

import java.util.regex.*; public class ReplaceIP{

public static void main(String args[ ]) {

String str = \登录网站: 222.128.89.253\ Pattern pattern; Matcher matcher;

String regex = \ pattern = 【代码1】 //使用regex初试化模式对象pattern matcher = 【代码2】 //得到检索str的匹配对象matcher String IP=\

while(matcher.find()) { IP= matcher.group();

System.out.print(matcher.start()+\位置出现:\ System.out.println(IP); }

System.out.printf(\将%s替换为202.192.78.56\\n\ String result = matcher.replaceAll(\ System.out.println(result); } }

实验内容要求:将【代码】部分替换为java程序代码。

实验报告要求:要有实验名称,实验目的,实验要求,实验内容(源代码),运行结果和总结。

实验项目九:java swing图形用户界面 实验1算术测试

实验教学目标: 学习处理ActionEvent事件。 实验场地或主要设备与器材:计算机

实验要求: 编写一个算术测试小软件,用来训练小学生的算术能力。程序有3个类,Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确。ComputerFrame对象负责为算术题目提供视图,比如用户可以通过ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案;MainClass是主类。

实验内容: MainClass.java

public class MainClass {

public static void main(String args[]) { ComputerFrame frame; frame=new ComputerFrame(); frame.setTitle(\算术测试\ frame.setBounds(100,100,650,180); } }

ComputerFrame.java

import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class ComputerFrame extends JFrame { JMenuBar menubar;

JMenu choiceGrade; //选择级别的菜单 JMenuItem grade1,grade2;

JTextField textOne,textTwo,textResult; JButton getProblem,giveAnwser; JLabel operatorLabel,message; Teacher teacherZhang; ComputerFrame() {

teacherZhang=new Teacher(); teacherZhang.setMaxInteger(20); setLayout(new FlowLayout()); menubar = new JMenuBar();

choiceGrade = new JMenu(\选择级别\ grade1 = new JMenuItem(\幼儿级别\ grade2 = new JMenuItem(\儿童级别\ grade1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { teacherZhang.setMaxInteger(10); } });

grade2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { teacherZhang.setMaxInteger(50); } }); choiceGrade.add(grade1); choiceGrade.add(grade2); menubar.add(choiceGrade); setJMenuBar(menubar);

textOne=new JTextField(5); //创建textOne,其可见字符长是5 textTwo=new JTextField(5); textResult=new JTextField(5); operatorLabel=new JLabel(\

operatorLabel.setFont(new Font(\ message=new JLabel(\你还没有回答呢\ getProblem=new JButton(\获取题目\ giveAnwser=new JButton(\确认答案\ add(getProblem); add(textOne); add(operatorLabel); add(textTwo); add(new JLabel(\ add(textResult); add(giveAnwser); add(message);

textResult.requestFocus(); textOne.setEditable(false); textTwo.setEditable(false);

getProblem.setActionCommand(\ textResult.setActionCommand(\

giveAnwser.setActionCommand(\

teacherZhang.setJTextField(textOne,textTwo,textResult); teacherZhang.setJLabel(operatorLabel,message);

getProblem.addActionListener(teacherZhang);//将teacherZhang注册为getProblem的ActionEvent事件监视器

giveAnwser.addActionListener(teacherZhang);//将teacherZhang注册为giveAnwser的ActionEvent事件监视器

textResult.addActionListener(teacherZhang);//将teacherZhang注册为textResult的ActionEvent事件监视器

setVisible(true); validate();

setDefaultCloseOperation(DISPOSE_ON_CLOSE); } }

Teacher.java

import java.util.Random; import java.awt.event.*; import javax.swing.*;

public class Teacher implements ActionListener { int numberOne,numberTwo; String operator=\ boolean isRight;

Random random; //用于给出随机数 int maxInteger; //题目中最大的整数 JTextField textOne,textTwo,textResult; JLabel operatorLabel,message; Teacher() {

random = new Random(); }

public void setMaxInteger(int n) { maxInteger=n; }

public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if(str.equals(\

numberOne = random.nextInt(maxInteger)+1;//1至maxInteger之间的随机数; numberTwo=random.nextInt(maxInteger)+1; double d=Math.random(); // 获取(0,1)之间的随机数 if(d>=0.5) operator=\ else

operator=\

textOne.setText(\ textTwo.setText(\ operatorLabel.setText(operator); message.setText(\请回答\ textResult.setText(null); }


java实验课程教学大纲-孙晶京(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:青岛版科学六年级上册基础训练答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: