JavaEE- experiment a common Java programming tools

The blog is only designed to provide for my little partner and additional reference, no time to tag specific analysis, hope you understanding

1,  division split the String class using the class string  "Solutions to the Selected exercises at The Electronic CAN BE found in the Document at The Thinking in the Java Annotated Solution Guide , the Available for A Small Fee from bruceeckel " word extraction output. Word or a space , split.

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+",");
        }    
    }
}

Example screenshots

2,  debugging p14 example 2.8 , the program with comments.

Package Java programming tools used;
 public  class StringAndStringBuffer {
     / * 
     * function is used to generate a new replacement string is changed to the address of the object string 
     * / 
    public  static  void StringReplace (String text) { 
        text = text.replace ( 'J', 'I ' ); 
    } 
    / * 
     * function using the append point to the same address StringBuffer object content change 
     * / 
    public  static  void bufferReplace (StringBuffer text) { 
        text = text.Append the ( "EE" ); 
    } 
    public  static  void main (String [] args) {
         // declare a String and StringBuffer
        String ts=new String("java");
        StringBuffer tb=new StringBuffer("java");
        //调用函数
        stringReplace(ts);
        bufferReplace(tb);
        System.out.println(ts+","+tb);
    }
}

Example screenshots

 

 

 

 

3 , debugging p15 cases of 2.10, the program with comments.

Package Java programming tools used; 

Import the java.text.SimpleDateFormat;
 Import java.util.Date; 

public  class date format example {
     public  static  void main (String [] args) {
         // set time formatting 
        the SimpleDateFormat format1 = new new the SimpleDateFormat ( "when dd day HH yyyy, mM month mm ss a" ); 
        the SimpleDateFormat FORMAT2 = new new the SimpleDateFormat ( "YY / mM / dd HH: mm" ); 
        the SimpleDateFormat format 3 = new new the SimpleDateFormat ( "YYYY-mM-dd HH: mm : SS " ); 
        the SimpleDateFormat format4 = new newSimpleDateFormat ( "dd day when HH yyyy, MM month mm ss a E" );
         // get the current time 
        a Date DATE = new new a Date ();
         // outputs the current corresponding to four time format 
        System.out.println (format1 .format (DATE)); 
        System.out.println (format2.format (DATE)); 
        System.out.println (format3.format (DATE)); 
        System.out.println (format4.format (DATE)); 
        / / output current time with the initial formatting 
        System.out.println (Date.toString ()); 
    } 
}

 

Example screenshots

 

 

 

 

4, design a program to calculate the number of days 2010-05-01 date with the current system date of difference.

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 ( "today's date:" + format.format (date1) + " from" + s + "" + days + " days" ); 
    } 
}

 

Example screenshots

 

 

 

 

5, to complete a date tools MyCalendar, implemented on a function Calendar similar ( i.e., achieving its get and set function ) , using Date and DateFormat class to complete .

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 new MyCalendar (); 
        the System.out .println ( "to get the current date" + c.get ()); 
        System.out.println ( "year after the date of modification" + c.set ( "year", 2011 )); 
    } 
}

 

Example screenshots

 

 

 

 

6, the design of a class Student, class attributes are: name, student number, date of birth, gender, location-based and so on. And generates an object array of student class. In accordance with the student's name will sort the output of the student. Use compareTo method of the String class.

1), the definition of class students

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), defines the test class

 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"};I = 0; I <s.length; I ++int(for};
        
        = { "computer," "administered", "automatic", "electric", "cross-country"
        String [] depts)
            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);
    }
}

 

Example screenshots

 

 

 

 

7, using the correlation method and the like by a calendar-based theme to make a calendar reference books example , studies in which a code review and review using Java Swing programming.

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);
         // calculate the current month to 1 week 
        cal.set (Calendar.DAY_OF_MONTH, 1); // set to 1 
        int firstweek = cal.get (the Calendar.DAY_OF_WEEK) -1 ; 
        Object [] title = { "day", "one", "two", "three", "four", "five", "six" }; 
        Object [] [] a = new new Object [. 6] [. 7 ];
         int Day =. 1 ;
         Boolean In Flag = to false ;
         Boolean ispass = to 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();
    }
}

 

 

 Example screenshots

 

 

Guess you like

Origin www.cnblogs.com/cc123nice/p/11494501.html