【毎日少しずつ】JAVASE 01(1)

34364 ワード

昨日はプログラムが遅くて更新できなかったので、今日補充します.本当に毎日新しい始まりです.主に、やはりWEBの方向を探さなければならないようで、だから普通はすべてJAVAを募集して开発して、1つの0基础の私について、最初から学ぶのはやはり少し时间がかかりますが、昨日SEを见てC++と差が多くなくて、计画は先にSEを学んでそれからEEを回転することを始めます.要するに、多くの言語の基礎を学んだため、更に1つの新しい言語を学んで、基礎の話はすぐに上手になることができるでしょう、たぶん!  
基本的なデータ構造
////-----------------    -----------
    public static void main(String[] args) {
        int[] a=new int[]{1,4,2,1,6,435,132,-23};
        int[] b={1,3,4,2,32,56,8};
        Arrays.sort(a);    //      
        for(int i:a)
        System.out.println(i);
    }


////-----------     ------------------
    public static void main(String[] args) {
        int[] arr2=new int[]{1,2,3,4,5,6};    //   

        int[] arr1={1,2,3,4,5};    //   

////----------------    -------------------
        int[] arr=arr1;  //arr   arr1   ,               
        arr[1]=10;   //  arr1[1]=10

//---------------------     -----------
        int[] copyArr=Arrays.copyOf(arr1, arr1.length);
        copyArr[1]=11;     //  copyArr[1]     

        for(int i:arr)
            System.out.print(i+" ");
        System.out.println("");
        for(int i:arr1)
            System.out.print(i+" ");
        System.out.println("");
        for(int i:arr2)
            System.out.print(i+" ");
        System.out.println("");
        for(int i:copyArr)
            System.out.print(i+" ");




////------------------   -------------
    public static void main(String[] args) {
        BigInteger a=BigInteger.valueOf(100);  // 100      
        BigInteger b=a.add(a);     //b=a+20
        BigInteger c=a.subtract(b); //c=a-2
        BigInteger d=a.multiply(c);  //d=a*c
        BigInteger e=a.divide(d);  //e=a/d
        int f=a.compareTo(e);   //= 0;> + ;< -
        System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f);

//  }

////-----------file process------------
    public static void main(String[] args) throws IOException{ 
        Scanner in=new Scanner(Paths.get("E:\\java\\workspace\\helloworld\\bin\\a.txt"));
        String name=in.nextLine();
        System.out.println(name);
        PrintWriter out=new PrintWriter("E:\\java\\workspace\\helloworld\\bin\\a.txt");


    }

////---------------      -Console---------- ???????????
    public static void main(String[] args) {


    Console cons=System.console();
    String name=cons.readLine("user name: ");
    Scanner in=new Scanner(System.in);
    name=in.nextLine();
    char[] passwd=cons.readPassword("password: ");

    }
//  

//---------------cin,cout-----------------
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);  //    Scanner  ,  System.in  
        System.out.println("what is your name?");
        String name=in.nextLine();      //    
        System.out.println("what is your sex");
        String sex=in.next();    //             
        System.out.println("How old are you");
        int age=in.nextInt();   //      
        System.out.println("name: "+name+"; sex: "+sex+" age: "+age);
        in.close();

    }

//  

//----------java.lang.StringBuilder--------
    public static void main(String[] args) {


    StringBuilder z=new StringBuilder();
    String str="i m "; 
    z.append(str);
    z.append("reimu ");
    z.insert(2,"a");  
    String s=z.toString();
    System.out.println(s);   //output i am reimu
    //if z refers to a string builder object whose
    //current contents are "start", then the method call z.append("le")
    //would cause the string builder to contain "startle", 
    //whereas z.insert(4, "le") would alter the string builder
    //to contain "starlet".
    z.delete(5,11);  //delete index: 5-10
    System.out.println(z);  //output i am .
    }

//-------other java.lang.string-----------------
    public static void main(String[] args) {
        String s=" hello,world! ";
        s=s.toUpperCase();  //upper
        System.out.println(s);
        s=s.toLowerCase();  //lower
        System.out.println(s);
        s=s.trim();
        System.out.println(s); //delete head and end spaces 

    }


//-------------code units and code point-----   
    public static void main(String[] args) {
        String s="hello";
        System.out.println(s.length());  //s.length
        int count=s.codePointCount(0,s.length());//     
        System.out.println(count);    //output 5
        int cPA=s.codePointAt(0);  //  ascii 
        System.out.println(cPA); 
        char first=s.charAt(0);  
        char last=s.charAt(s.length()-1);
        System.out.println(first+" "+last);

    }

////------------empty string:length=0;---------
    public static void main(String[] args) {
        String str="";   //no spaces
        //str=null;  if str=null,cannot use function
        if(str.length()==0||str.equals("")){
            System.out.println("empty string");
        }
        else
            System.out.println("it isn't a empty string");
    }



////-------------equals------yes->true;no->false------
////-----------equalsIgnoreCase-----------
    public static void main(String[] args) {
        String s1="hello";
        String s2="HELLO";
        if(s1.equalsIgnoreCase(s2)){
            if(s1.equals(s2))
                System.out.println("s1=s2");
            else
                System.out.println("ignorecase,s1=s2");
        }
        else
            System.out.println("s1!=s2");

    }
    //output:ignorecase,s1=s2

////---------------substring---from a father string to getting a son string--
    public static void main(String[] args) {
        String s="i am lyy!";
        String s1=s.substring(2);   //cut 0-1,at 2 start
        System.out.println(s1);     //output :am lyy!
        String s2=s.substring(2, 4);
        System.out.println(s2);     //output :am. string length:4-2=2
        s2="i am";
        System.out.println(s2);

    }

////------------type cast---------------
    public static void main(String[] args) {
        double x=9.9;
        int y=(int)x;
        System.out.println(y);  //output 9,get close x's min int

    }


////------Math function--------------------
    public static void main(String[] args) {
        double[] x=new double[6];
        double y=4;
        x[0]=Math.sqrt(y);
        x[1]=Math.sin(y);
        x[2]=Math.PI;
        x[3]=Math.E;
        x[4]=Math.pow(y,2);
        x[5]=Math.exp(y);
        for(double i:x)
        System.out.println(i);
    }



////------------variable---final----------------
    public static void main(String[] args) {        
        int val=10;
        final int con=12;  //it's not similar to c++,using 'final' to define a constant
        val=11;
        System.out.println(val+con);

    }


    public static void main(String[] args){
        System.out.print("helloworld");
        for(int i=0;i<10;i++){
            System.out.print(i);
        }
    }

////-----------hello world--------------------------------
    public stat```````
c void main(String[] args) {
        String[] hello=new String[3];
        hello[0]="hello ";
        hello[1]="i am ";
        hello[2]="lyy.";
        for(String i:hello){
            System.out.println(i);  //    
            System.out.print(i);  //     

        }
クラス、継承:
//----------------------   Person----------------------------
abstract public class Person {
    private String Name;
    public Person(String name){
        Name=name;
    }
    public abstract String getDescription();   //    ,      ,       

    public String getName() {
        return Name;
    }   
}


//-------------------------   Student  Person----------------------
public class Student extends Person{
    private String Major;
    public Student(String name,String major){
        super(name);    // person     
        Major=major;        
    }
    public String getDescription(){
        return "i am a student,my name is: "+super.getName()+",major is: "+Major;
    }
    public String getMajor() {
        return Major;
    }
    public void setMajor(String major) {
        Major = major;
    }
}

//-----------------------   Employee  Person------------------------
import java.util.Date;
import java.util.GregorianCalendar;

public class Employee extends Person{

    private int Age;
    private double Salary;
    private Date Hireday;
    private static int NextId=1;  //   ,     。        NextId=1,       NextID   
    private int Id;

    public Employee(String name,int age ,double salary,int year,int month,int day){
        super(name);
        this.Age=age;
        this.Salary=salary;
        GregorianCalendar cal=new GregorianCalendar(year,month-1,day); //0  1 
        this.Hireday=cal.getTime();     
    }
    public Employee(String name){
        super(name);
    }

    public Employee(){
        super("");
        this.Age=0;
        this.Salary=0;
        this.Hireday=new Date();
    }

    //    ,            
    {
        this.Id=NextId;
        NextId++;
    }

    public String getDescription(){
        return "i am a employee,my name is: "+super.getName();
    }

    public void raiseSalary(double byPercent){
        double raise=Salary*byPercent/100;
        this.Salary+=raise;
    }
    public int getId(){
        return Id;
    }

    public void setId(){
        this.Id=NextId;
        NextId++;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int age) {
        Age = age;
    }

    public double getSalary() {
        return Salary;
    }

    public void setSalary(double salary) {
        Salary = salary;
    }


    public String getName() {
        return super.getName();
    }



    public Date getHireday() {
        return  (Date) Hireday.clone();
    }

    public void setHireday(Date hireday) {
        Hireday = hireday;
    }




}

//----------------------   Manage  Employee----------------------
import java.util.GregorianCalendar;
final public class Manage extends Employee{   //        Manage

    private static int BossId=1;
    private double Bounds;
    private int Bid;
    public Manage(String name,int age ,double salary,int year,int month,int day,double bounds){
        super(name);
        super.setAge(age);
        this.setSalary(salary);
        GregorianCalendar cal=new GregorianCalendar(year,month-1,day);
        this.setHireday(cal.getTime());
        this.Bounds=bounds;

    }
    {
        Bid=BossId;
        BossId++;
    }

    public double getBounds() {
        return Bounds;
    }

    public void setBounds(double bounds) {
        Bounds = bounds;
    }

    public String getName()
{
        return super.getName();
    }


    public int getBid() {
        return Bid;
    }

    public void setBid(int bid) {
        Bid = bid;
    }




}

//-------------------main-----------------
import java.util.Date;


public class Helloworld {
    public static void main(String[] args) {
        Employee[] staff=new Employee[4];
        staff[0]=new Employee("alex", 18, 2000, 2016, 2, 3);
        staff[1]=new Employee("Bob", 19, 2500, 2016, 3, 2);
        staff[2]=new Employee("Carl", 20, 3000, 2016, 5, 3);
        staff[3]=new Employee();
        for(Employee i:staff){
            //i.setId();
            i.raiseSalary(5);
            }

        for(Employee e:staff)
            System.out.println("id="+e.getId()+",name= "+e.getName()+",age="+e.getAge()+",salary="+e.getSalary()+
                    ",hireday="+e.getHireday());
        Date d=staff[0].getHireday();                     //              ,        ,     .clone()
        d.setTime(d.getTime()-(long)365*24*60*60*1000);   
        System.out.println(staff[0].getHireday());

        Manage boss=new Manage("Boss", 18, 6000, 2014, 5, 3, 500);
        System.out.println("bid="+boss.getBid()+",name="+boss.getName()+",age="+boss.getAge()+
                ",salary="+boss.getSalary()+",bounds="+boss.getBounds()+",hireday="+boss.getHireday());


        Person p1,p2;
        p1=new Student("xxm", "math");

        System.out.println(p1.getDescription());
        p2=new Employee("gws", 19, 2000, 2016, 5, 5);
        p2.getDescription();

        Manage m=new Manage("ssss", 54, 15, 2015, 2, 2, 500);
        System.out.println(m.getName()+" "+m.getBounds());

        Employee e=new Manage("ssdq", 445, 12, 2012, 12, 12, 500);
        System.out.println(e.getName()+" "+e.getDescription());



    }