JavaEE-實驗一 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單詞提取輸出。單詞以空格或,分割。

package java常用工具類編程;
public class Splittest {
    public static void main(String[] args) {
        String s1=new String("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[] s=s1.split(" ");
        for(String str:s) {
            System.out.print(str+",");
        }    
    }
}

示例截圖

2、  調試p14 2.8,將程序加上注釋。

package java常用工具類編程;
public class StringAndStringBuffer {
    /*
     * 對string對象使用替換函數 生成新string其地址被改變 
     */
    public static void stringReplace(String text) {
        text=text.replace('j', 'i');
    }
    /*
     * 對StringBuffer對象使用append函數 指向地址不變 內容改變
     */
    public static void bufferReplace(StringBuffer text) {
        text=text.append(" EE");
    }
    public static void main(String[] args) {
        //聲明String和StringBuffer
        String ts=new String("java");
        StringBuffer tb=new StringBuffer("java");
        //調用函數
        stringReplace(ts);
        bufferReplace(tb);
        System.out.println(ts+","+tb);
    }
}

示例截圖

 

 

 

 

3調試p15 例2.10,將程序加上注釋。

package java常用工具類編程;

import java.text.SimpleDateFormat;
import java.util.Date;

public class 日期格式化示例 {
    public static void main(String[] args) {
        //設置時間格式化
        SimpleDateFormat format1=new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
        SimpleDateFormat format2=new SimpleDateFormat("yy/MM/dd HH:mm");
        SimpleDateFormat format3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format4=new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒E");
        //獲取當前時間
        Date date=new Date();
        //輸出當前時間 對應的4個格式化
        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日與系統當前日期相差的天數。

package java常用工具類編程;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class 計算日期差 {
    public static void main(String[] args) throws ParseException {
        Date date1=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String s=new String("2010-5-1");
        Date date2 = format.parse(s);
        int days=(int)((date1.getTime()-date2.getTime())/(1000*60*60*24));
        System.out.println("今天日期:"+format.format(date1)+"距離"+s+" "+days+"天");
    }
}

 

示例截圖

 

 

 

 

5、 完成一個日期工具類MyCalendar,實現功能上與Calendar相似(即實現其get以及set函數),利用Date以及DateFormat類去完成.

package java常用工具類編程;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyCalendar {
    Date date;
    MyCalendar(){
        date=new Date();
    }
    public String get() {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    public String set(String s1,int num) {
        String currenttime = get();
        String changetime="";
        String[] arr = currenttime.split("-");
        if(s1.equalsIgnoreCase("year")) {
            arr[0]=Integer.toString(num);
        }else if(s1.equalsIgnoreCase("month")) {
            arr[1]=Integer.toString(num);
        }else if(s1.equalsIgnoreCase("day")) {
            arr[2]=Integer.toString(num);
        }else
            return "數據有誤";
        for(int i=0;i<2;i++)
        {
            changetime+=arr[i];
            changetime+="-";
        }
        changetime+=arr[2];
        return changetime;
    }
    public static void main(String[] args) {
        MyCalendar c = new MyCalendar();
        System.out.println("得到當前日期"+c.get());
        System.out.println("年份修改后日期"+c.set("year", 2011));
    }
}

 

示例截圖

 

 

 

 

6、設計一個類Student,類的屬性有:姓名,學號,出生日期,性別,所在系等。並生成學生類對象數組。按照學生的姓名將學生排序輸出。使用String類的compareTo方法。

1)、定義學生類

package java常用工具類編程;

public class Student {
    private String sno;
    private String sname;
    private String sbirth;
    private String ssex;
    private String sdept;
    
    public Student(String sno, String sname, String sbirth, String ssex, String sdept) {
        super();
        this.sno = sno;
        this.sname = sname;
        this.sbirth = sbirth;
        this.ssex = ssex;
        this.sdept = sdept;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSbirth() {
        return sbirth;
    }
    public void setSbirth(String sbirth) {
        this.sbirth = sbirth;
    }
    public String getSsex() {
        return ssex;
    }
    public void setSsex(String ssex) {
        this.ssex = ssex;
    }
    public String getSdept() {
        return sdept;
    }
    public void setSdept(String sdept) {
        this.sdept = sdept;
    }
    @Override
    public String toString() {
        return "sno=" + sno + ", sname=" + sname + ", sbirth=" + sbirth + ", ssex=" + ssex + ", sdept=" + sdept;
    }
}

 

2)、定義測試類

 package java常用工具類編程;

public class TestStudent {
    public Student[] initStudent(){    //初始化學生信息
        Student s[]=new Student[5];
        String[] names={"zhou","zhang","liu","li","xu"};
        String[] nos= {"1","2","3","4","5"};
        String[] births= {"1999/4/5","1998/12/7","1996/11/6","1999/1/25","1999/3/2"};
        String[] sess= {"M","F","F","M","F"};
        String[] depts= {"計算機","經管","自動化","電氣","國交"};
        
        for(int i=0;i<s.length;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<s.length-1;i++){
           int min=i;
           for(int j=i+1;j<s.length;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){//輸出學生信息
         for(Student ss:s) {
                 System.out.println(ss); 
             }
     }
     public static void main(String[] args){
        TestStudent obj=new TestStudent();
        Student[] s=obj.initStudent(); 
        obj.sortStudent(s);
        obj.dispStudent(s);
    }
}

 

示例截圖

 

 

 

 

7、使用日歷類等相關方法 按截圖做出一個日歷 參照書本示例,研究其中代碼回顧與復習利用Java Swing編程。

package java常用工具類編程;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class CalendarSwing  extends JFrame{
    private static final long serialVersionUID = 1L;
    JTable table;
    JPanel jp1;
    CalendarSwing(){
        JLabel year=new JLabel("年");
        JLabel month=new JLabel("月");
        JButton confirm=new JButton("確認");
        
        JComboBox<String> yearchoose=new JComboBox<>();
        for(int i=1980;i<2050;i++)
            yearchoose.addItem(i+"");
        JComboBox<String> monthchoose=new JComboBox<>();
        for(int i=1;i<13;i++)
            monthchoose.addItem(i+"");
        jp1=new JPanel();
        jp1.add(yearchoose);
        jp1.add(year);
        jp1.add(monthchoose);
        jp1.add(month);
        jp1.add(confirm);
        add(jp1,BorderLayout.NORTH);
        setTitle("cc的日歷");
        setBounds(400,300,400,400);
        validate();
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showCalendar(Integer.parseInt(yearchoose.getSelectedItem().toString()),Integer.parseInt(monthchoose.getSelectedItem().toString()));
                
            }
        });
        
    }
    public  void showCalendar(int year,int month){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);
        //計算當前月一共有多少天
        int days=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //計算當前月的1號為星期幾
        cal.set(Calendar.DAY_OF_MONTH, 1);//設置為1號
        int firstweek=cal.get(Calendar.DAY_OF_WEEK)-1;
        Object[] title = {"日", "一", "二", "三", "四", "五", "六"};
        Object[][] a=new Object[6][7];
        int day=1;
        boolean flag=false;
        boolean ispass=false;
        for(int i=0;i<6;i++) {
            for(int j=0;j<7;j++) {
                if((firstweek%7)==j&&!ispass) {
                    flag=true;
                    ispass=true;
                }
                if(!flag)
                    a[i][j]="";
                else {
                    a[i][j]=Integer.toString(day);
                    if(day<days)
                        day++;
                       else
                           flag=false;
                }
                
            }
        }
        getContentPane().removeAll();
        table=new JTable(a,title);
        add(jp1,BorderLayout.NORTH);
        add(new JScrollPane(table),BorderLayout.CENTER);
        
        validate();
}
    public static void main(String[] args) {
        new CalendarSwing();
    }
}

 

 

 示例截圖

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM