JavaEE- second experiment experimental Java Collections Framework

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

1, the use of class division split the String class 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" word extraction output. Words with spaces or split.

package experiment;
public class Split {
    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[] s = str.split(" "); for(String ss:s) { System.out.print(ss+","); } } }

Example screenshots

 

 

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

 package experiment;

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

public class days {
    public static void main(String[] args) throws ParseException  {
        Date date1=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String s=new String("2011-05-01");
        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

 

 

3 , the courses offered this semester name added to the HashSet and use iterates over output.

package experiment;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Courses {
    public static void main(String[] args) {
        Set<String> set=new HashSet<String>();
        set.add("Python");
        set.add("JavaEE");
        set.add("软件工程");
        set.add("操作系统");
        set.add("计算机组成");
        Iterator<String> it=set.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

Example screenshots

 

How to understand the elements are sorted. Complete the following experiment:

 1 ) the definition of a student categories: attributes number, name, specialty, the high number of achievements, foreign language achievement, Java course grade.

package experiment;

public class Student {
    private String sno;
    private String sname;
    private String department;
    private int maths;
    private int english;
    private int java;
    private int score;
    
    
    public int getScore() {
        return score;
    }
    public Student(String sno, String sname, String department, int maths, int english, int java) {
        this.sno = sno;
        this.sname = sname;
        this.department = department;
        this.maths = maths;
        this.english = english;
        this.java = java;
        this.score=english+java+maths;
    }
    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 getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public int getMaths() {
        return maths;
    }
    public void setMaths(int maths) {
        this.maths = maths;
    }
    public int getEnglish() {
        return english;
    }
    public void setEnglish(int english) {
        this.english = english;
    }
    public int getJava() {
        return java;
    }
    public void setJava(int java) {
        this.java = java;
    }
    @Override
    public String toString() {
        return "Student [sno=" + sno + ", sname=" + sname + ", department=" + department + ", maths=" + maths
                + ", english=" + english + ", java=" + java + ", 总分=" + score + "]";
    }
    
}

Comparison of class

package experiment;

import java.util.Comparator;

public class MyCmp implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o2.getScore()-o1.getScore();
    }
 }

   2) generating a plurality of objects in the test class student class into a TreeSet, sorted in descending required a total score of three classes, student number equal sorted by total score. Sort results output.

 

package experiment;

import java.util.Iterator;
import java.util.TreeSet;

public class test {
    public static void main(String[] args) {
        Student s1=new Student("1", "c1", "计算机", 98, 80, 92);
        Student s2=new Student("2", "c2", "软工", 80, 84, 99);
        Student s3=new Student("3", "c3", "国交", 88, 79, 82);
        Student s4=new Student("4", "c4", "计算机", 78, 90, 82);
        TreeSet<Student> ts=new TreeSet<Student>(new MyCmp());
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        Iterator<Student> it=ts.iterator();
        while(it.hasNext()){
            Student s=(Student)it.next();
            System.out.println(s.toString());
            }

    }
}

Example screenshots

 

Guess you like

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