第六次实验Word文档下载推荐.docx
《第六次实验Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《第六次实验Word文档下载推荐.docx(12页珍藏版)》请在冰点文库上搜索。
System.out.println(str2[i]);
}
}
运行截图:
2、调试p14例2.8,将程序加上注释。
packageEX1_2;
publicclassEX1_2{
publicstaticvoidstringRepalce(Stringtext){
text=text.replace('
j'
'
i'
//替换函数把j替换成i
publicstaticvoidbufferRepalce(StringBuffertext){
text=text.append("
EE"
//把Object型参数的字符串表示添加到该字符串缓冲区
}
Stringts=newString("
java"
//字符型
StringBuffertb=newStringBuffer("
//StringBuffer型
stringRepalce(ts);
//调用函数
bufferRepalce(tb);
System.out.println(ts+"
"
+tb);
//打印输出
}}
stringRepalce函数中传进来的只是形参,是对原值的拷贝,原值不改变。
bufferRepalce函数是对字符串缓冲区进行修改,是对原值的修改,故发生改变。
3、调试p15例2.10,将程序加上注释。
packageEX1_3;
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassEX1_3{
SimpleDateFormatformat1=newSimpleDateFormat("
yyyy年MM月dd日HH时mm分ss秒"
//SimpleDateFormat指定格式输出
SimpleDateFormatformat2=newSimpleDateFormat("
yy/MM/dd/HH:
mm"
SimpleDateFormatformat3=newSimpleDateFormat("
yyyy-MM-ddHH:
mm:
ss"
SimpleDateFormatformat4=newSimpleDateFormat("
yyyy年MM月dd日HH时mm分ss秒E"
Datedate=newDate();
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());
//默认输出格式
4、设计一个程序计算2010-05-01日与系统当前日期相差的天数。
参照题3与书本上P17完成。
packageEX1_4;
importjava.text.ParseException;
publicclassEX1_4{
Stringinput="
2010-05-01"
;
SimpleDateFormatformatter=newSimpleDateFormat("
yyyy-MM-dd"
Dated1=null;
Dated2=newDate();
try{
d1=formatter.parse(input);
}catch(ParseExceptione){
e.printStackTrace();
}
longdiff=d2.getTime()-d1.getTime();
System.out.println("
Differenceis"
+(diff/(1000*60*60*24))+"
days."
5、设计一个类Student,类的属性有:
1)、定义学生类
classStudent{
privateStringsno,sname,sbirth,ssex,sdept;
//构造函数,set-get函数
2)、创建一个测试类
publicclassEx1_2{
publicStudent[]initStudent(){//初始化学生信息
Students[]=newStudent[5];
String[]names={"
zhou"
"
zhang"
liu"
li"
xu"
};
...//定义几个数组放置属性信息
for(inti=0;
s.length;
s[i]=newStudent(nos[i],names[i],births[i],sess[i],depts[i]);
returns;
publicvoidsortStudent(Student[]s){//排序按照姓名,选择法
s.length-1;
i++){
intmin=i;
for(intj=i+1;
j<
j++)
if((s[min].getSname().compareTo(s[j].getSname())>
0)min=j;
if(min!
=i){
Studentt=s[i];
s[i]=s[min];
s[min]=t;
publicvoiddispStudent((Student[]s){//输出学生信息
..
publicstaticvoidmain(String[]args){
Ex1_2obj=newEx1_2();
Student[]s=obj.initStudent();
obj.sortStudent(s);
obj.dispStudent(s);
packageEX1_5;
publicclassStudent{
Student(Stringsno,Stringsname,Stringsbirth,Stringssex,Stringsdept){
this.sno=sno;
this.sname=sname;
this.sbirth=sbirth;
this.ssex=ssex;
this.sdept=sdept;
publicStringgetSno(){
returnsno;
publicvoidsetSno(Stringsno){
this.sno=sno;
publicStringgetSname(){
returnsname;
publicvoidsetSname(Stringsname){
this.sname=sname;
publicStringgetSbirth(){
returnsbirth;
publicvoidsetSbirth(Stringsbirth){
this.sbirth=sbirth;
publicStringgetSsex(){
returnssex;
publicvoidsetSsex(Stringssex){
this.ssex=ssex;
publicStringgetSdept(){
returnsdept;
publicvoidsetSdept(Stringsdept){
this.sdept=sdept;
publicclassTest{
String[]snos={"
20101706"
20101707"
20101708"
20101709"
20101710"
gaojian"
cuichengdi"
huangyongsheng"
huangwei"
pengchonglin"
String[]births={"
1990-01-01"
1990-01-02"
1990-01-03"
1990-01-04"
1990-01-05"
String[]sess={"
男"
女"
String[]depts={"
计算机"
数学"
英语"
测控"
经济"
s[i]=newStudent(snos[i],names[i],births[i],sess[i],depts[i]);
0))
min=j;
s[i]=s[min];
s[min]=t;
publicvoiddispStudent(Student[]s){
System.out.println(s[i].getSno()+"
+s[i].getSname()+"
+s[i].getSbirth()+"
+s[i].getSsex()+"
+s[i].getSdept());
Testobj=newTest();
6、使用日历类等相关方法按截图做出一个日历参照书本示例,研究其中代码回顾与复习利用JavaSwing编程。
参考:
以下函数根据输入的年和月计算相应的数字
publicvoidshowCalendar(intyear,intmonth){
Calendarcal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
//计算当前月一共有多少天
intdays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//计算当前月的1号为星期几
cal.set(Calendar.DAY_OF_MONTH,1);
//设置为1号
intfirstweek=cal.get(Calendar.DAY_OF_WEEK);
。
。
packageEX1_6;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.Calendar;
importjavax.swing.*;
importjavax.swing.table.*;
publicclassEX1_6extendsJFrameimplementsActionListener{
privateJLabellbl_year=newJLabel("
年"
SwingConstants.CENTER);
privateJLabellbl_mon=newJLabel("
月"
privateJButtonok=newJButton("
确定"
String[]mon={"
-"
1"
2"
3"
4"
5"
6"
7"
8"
9"
10"
11"
12"
JComboBoxjb_year=newJComboBox();
JComboBoxjb_mon=newJComboBox(mon);
String[]cols={"
日"
一"
二"
三"
四"
五"
六"
String[][]rows=newString[6][7];
JTabletable=newJTable(rows,cols);
JScrollPanejsp=newJScrollPane(table);
EX1_6(){
jb_year.addItem("
for(inti=2020;
i>
=1990;
i--)
jb_year.addItem(i);
DefaultTableCellRendererr=newDefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class,r);
JPaneljp=(JPanel)this.getContentPane();
JPaneljp1=newJPanel();
JPaneljp2=newJPanel();
JPaneljp3=newJPanel();
jp.setLayout(null);
jp1.setLayout(newFlowLayout());
jp2.setLayout(newBorderLayout());
jp3.setLayout(newBorderLayout());
jp1.add(lbl_year);
jp1.add(jb_year);
jp1.add(lbl_mon);
jp1.add(jb_mon);
jp1.add(ok);
jp2.add(jsp);
jp3.add(jp1,BorderLayout.NORTH);
jp3.add(jp2,BorderLayout.CENTER);
jp.add(jp3);
jp3.setBounds(20,20,400,159);
ok.addActionListener(this);
this.setSize(450,230);
this.setTitle("
日历"
this.setResizable(false);
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
publicvoidshowCalendar(intyear,intmonth){
Calendarcal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
//计算当前月一共有多少天
intdays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//计算当前月的1号为星期几
cal.set(Calendar.DAY_OF_MONTH,0);
intfirstweek=cal.get(Calendar.DAY_OF_WEEK);
inti=0,j=0,k=firstweek%7;
for(i=1;
=days;
rows[j][k]=i+"
"
k++;
if(k%7==0){
k=0;
j++;
table.setModel(newDefaultTableModel(rows,cols));
publicvoidactionPerformed(ActionEvente){
Stringstr1=jb_year.getSelectedItem().toString();
Stringstr2=jb_mon.getSelectedItem().toString();
if(str1.equals("
)||str2.equals("
))
table.setModel(newDefaultTableModel(rows,cols));
else{
intyear=Integer.parseInt(str1);
intmon=Integer.parseInt(str2);
showCalendar(year,mon);
JFrame.setDefaultLookAndFeelDecorated(true);
EX1_6test=newEX1_6();
test.setVisible(true);
如有侵权请联系告知删除,感谢你们的配合!