第六次实验

更新时间:2024-07-06 20:39:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实实验验66 常常用用基基础础类类库库与与工工具具类类((11))

6.1、实验目的

? 掌握java常用的基础类库,如String的使用

6.2、实验准备

(1) JDK安装 (2) Eclipse的安装 (3) 书本第8章节的复习

6.3、实验内容与要求

1、使用类String类的分割split 将字符串 “Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from BruceEckel” 单词提取输出。单词以空格或,分割。

2、设计一个类Student,类的属性有:姓名,学号,出生日期,性别,所在系等。并生成学生类对象数组。按照学生的姓名将学生排序输出。使用String类的compareTo方法。

3、设计一个程序计算2010-05-01日与系统当前日期相差的天数。

§§11..22实实验验题题目目

1、 使用类String类的分割split 将字符串 “Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from BruceEckel” 单词提取输出。单词以空格或,分割。 参照课本P10 例2.5 运行代码: package EX1_1;

public class EX1_1 {

public static void main(String[] args) {

String str1=new String(\

electronic document The Thinking in Java Annotated Solution Guide,available for a small fee from BruceEckel\ }

String[] str2=str1.split(\for(int i=0;i

System.out.println(str2[i]);

}

运行截图:

2、 调试p14 例2.8,将程序加上注释。 运行代码: package EX1_2;

public class EX1_2 {

public static void stringRepalce(String text){ }

public static void bufferRepalce(StringBuffer text){

text=text.append(\把 Object 型参数的字符串表示添加到该字text=text.replace('j','i');//替换函数 把j替换成i

符串缓冲区

}

public static void main(String[] args) { }}

String ts=new String(\字符型

StringBuffer tb=new StringBuffer(\型 stringRepalce(ts);//调用函数 bufferRepalce(tb);

System.out.println(ts+\打印输出

运行截图:

stringRepalce函数中传进来的只是形参,是对原值的拷贝,原值不改变。bufferRepalce函数是对字符串缓冲区进行修改,是对原值的修改,故发生改变。 3、 调试p15 例2.10,将程序加上注释。 运行代码: package EX1_3;

import java.text.SimpleDateFormat; import java.util.Date; public class EX1_3 {

public static void main(String[] args) {

SimpleDateFormat format1=new SimpleDateFormat(\年MM月dd

日HH时mm分ss秒\指定格式输出

SimpleDateFormat

format2=new

SimpleDateFormat(\

SimpleDateFormat format3=new SimpleDateFormat(\

HH:mm:ss\

SimpleDateFormat format4=new

SimpleDateFormat(\年MM月dd日

HH时mm分ss秒 E\ }

运行截图:

4、 设计一个程序计算2010-05-01日与系统当前日期相差的天数。 参照题3与书本上P17完成。 运行代码: package EX1_4; import java.util.Date;

import java.text.ParseException;

}

Date date=new Date();

System.out.println(format1.format(date));//格式化输出 System.out.println(format2.format(date)); System.out.println(format3.format(date)); System.out.println(format4.format(date));

System.out.println(date.toString());//默认输出格式

import java.text.SimpleDateFormat;

public class EX1_4 {

public static void main(String[] args) { } }

运行截图:

5、 设计一个类Student,类的属性有:姓名,学号,出生日期,性别,所在系等。并生成学生类对象数组。按照学生的姓名将学生排序输出。使用String类的compareTo方法。 1)、定义学生类 class Student{

String input = \

SimpleDateFormat formatter = new SimpleDateFormat(\ Date d1 = null; Date d2 = new Date(); try {

d1= formatter.parse(input);

} catch (ParseException e) { }

long diff = d2.getTime() - d1.getTime();

System.out.println(\

e.printStackTrace();

private String sno,sname,sbirth,ssex,sdept;//构造函数,set-get函数 }

2)、创建一个测试类 public class Ex1_2{

public Student[] initStudent(){ //初始化学生信息 Student s[]=new Student[5];

String[] names={\ ...//定义几个数组放置属性信息 for(int i=0;i

s[i]=new Student(nos[i],names[i],births[i],sess[i],depts[i]); return s; }

public void sortStudent(Student[] s){//排序按照姓名,选择法 for(int i=0;i

for(int j=i+1;j

if((s[min].getSname().compareTo(s[j].getSname())>0) min=j; if(min!=i){

Student t=s[i];s[i]=s[min];s[min]=t; } } }

public void dispStudent((Student[] s){//输出学生信息

.. }

public static void main(String[] args){ Ex1_2 obj=new Ex1_2(); Student[] s=obj.initStudent(); obj.sortStudent(s); obj.dispStudent(s); } } 运行代码: package EX1_5;

public class Student {

private String sno,sname,sbirth,ssex,sdept;

Student(String sno,String sname,String sbirth,String ssex,String sdept){ }

public String getSno() {

this.sno=sno; this.sname=sname; this.sbirth=sbirth; this.ssex=ssex; this.sdept=sdept;

}

return sno;

public void setSno(String sno) { }

public String getSname() { }

public void setSname(String sname) { }

public String getSbirth() { }

public void setSbirth(String sbirth) { }

public String getSsex() { }

public void setSsex(String ssex) { }

public String getSdept() {

this.ssex = ssex; return ssex; this.sbirth = sbirth; return sbirth; this.sname = sname; return sname; this.sno = sno;

}

}

return sdept;

public void setSdept(String sdept) { }

this.sdept = sdept;

package EX1_5;

public class Test {

public Student[] initStudent(){ //初始化学生信息

Student s[]=new Student[5]; String[]

snos={\ String[]

names={\};

String[]

births={\ String[] sess={\男\男\男\女\男\

String[] depts={\计算机\数学\英语\测控\经济\

for(int i=0;i

s[i]=new Student(snos[i],names[i],births[i],sess[i],depts[i]); return s;

public void actionPerformed(ActionEvent e) { }

public static void main(String[] args) { } }

运行截图:

String str1=jb_year.getSelectedItem().toString(); String str2=jb_mon.getSelectedItem().toString(); if(str1.equals(\

table.setModel(new DefaultTableModel(rows,cols));

else{ }

int year=Integer.parseInt(str1); int mon=Integer.parseInt(str2); showCalendar(year,mon);

JFrame.setDefaultLookAndFeelDecorated(true); EX1_6 test=new EX1_6(); test.setVisible(true);

本文来源:https://www.bwwdw.com/article/7mm.html

Top