JavaEE编程实验 实验1 Java常用工具类编程(未完成)
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”单词提取输出,单词以空格或','分隔
public static void main(String[] args) { String str="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"; String[] string=str.split(" |,"); for (String string2 : string) { System.out.print(string2+","); } }
2.设计一个类Student,类的属性有姓名,学号,出生日期,性别,所在系等。并生成学生类对象数组存储学生数据,然后按照学生的姓名将学生排序输出,使用String类的compareTo方法。函数原型输入两个学生对象,返回一个整型数字表示大小,以学生姓名为排序依据。
package com.littlepage.test2; import java.util.Date; public class Student implements Comparable<Student>{ private String name; private int stuNum; private Date birthDate; private String sex; private String department; public Student() { name=""; stuNum=0; birthDate=new Date(); sex="男"; department=""; } public Student(String name, int stuNum, Date birthDate, String sex, String department) { super(); this.name = name; this.stuNum = stuNum; this.birthDate = birthDate; this.sex = sex; this.department = department; } @Override public String toString() { return "Student [name=" + name + ", stuNum=" + stuNum + ", birthDate=" + birthDate + ", sex=" + sex + ", department=" + department + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStuNum() { return stuNum; } public void setStuNum(int stuNum) { this.stuNum = stuNum; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public int compareTo(Student o) { return this.getName().compareTo(o.getName()); } }
3.设计一个程序,计算任意的一个日期与系统当前日期相差天数
public static void main(String[] args) { Calendar c=Calendar.getInstance(); int y=2018;//年 int M=11;//月 int d=10;//日 int H=0;//时 int m=0;//分 int s=0;//秒 c.set(Calendar.YEAR, y); c.set(Calendar.MONTH, M-1); c.set(Calendar.DATE, d); c.set(Calendar.HOUR_OF_DAY, H); c.set(Calendar.MINUTE, m); c.set(Calendar.SECOND, s); Calendar now=Calendar.getInstance(); long aTime=now.getTimeInMillis(); long bTime=c.getTimeInMillis(); long cTime=aTime-bTime; long sTime=cTime/1000;//时间差,单位:秒 long mTime=sTime/60; long hTime=mTime/60; long dTime=hTime/24; SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间:"+f.format(now.getTime())); System.out.println("设定时间:"+f.format(c.getTime())); System.out.println("时间差:"+dTime+"天"+hTime%24+"时"+mTime%60+"分"+sTime%60+"秒"); }
3.设计一个日历类等相关方法,按照图15.1样式编制一个日历程序,复习Swing等相关知识