Java编程思想第七章复用类

组合:在一个类中创建另一个类的对象。

继承:在原来的类的基础上增加一些功能,成为新类。

toString()方法,在调用一个类的对象时,需要的是String,会自动调用toString()方法。

super 超类,就是调用基类的方法。

第一题

import java.util.*;
class one {
    private String s;
    one() {
        System.out.println("one()");
        s = "oneoneone";
    }
    public String toString() {
        return s;
    }
}

public class Javaone {
    private one o1;
    public Javaone() {}
    public String toString() {
        if (o1 == null)
            o1 = new one();



        return "o1 =" + o1;
    }

    public static void main(String[] args) {

      Javaone j2 = new Javaone();
      System.out.println(j2);

    }

}


第二题

public class Sterilizer extends Detergent {
	public void scrub() { append(" Sterilizer.scrub()"); }
	public void sterilize() { append(" sterilize()"); }
	public static void main(String[] args) {
		Sterilizer x = new Sterilizer();
		x.dilute();
		x.apply();	
		x.scrub();
		x.foam();
		x.sterilize();
		System.out.println(x);
		System.out.println("Testing base class:");
		Detergent.main(args);
	}
}

第三题

略  /p129例题

第四题

略

第五题

import java.util.*;
class A {
 A() {System.out.println("A");}
}
class B {
    B() {System.out.println("B");}

}
class C extends A {
    private B b;

}


public class Javaone {


    public static void main(String[] args) {

        C c = new C();


    }

}

第六题

class Game {
    Game(int i){
        System.out.println("Game constructor");
    }
}
class BoardGame extends Game {
    BoardGame(int i) {
        super(i);
        System.out.println("BoardGame constructor");
    }
}



public class Javatwo extends BoardGame {
    Javatwo() {
        super(11);
        System.out.println("chess constructor");

    }
    public static void main(String[] args) {
        Javatwo x = new Javatwo();
    }

}

第七题

import java.util.*;
class A {
 A(int i) {System.out.println("A");}
}
class B {
    B(int i) {System.out.println("B");}

}
class C extends A {
    private B b;
    C(int i) {

        super(i);
        System.out.println("C");
        b = new B(11);
    }

}


public class Javaone {


    public static void main(String[] args) {

        C c = new C(1);


    }

}

第八题

import java.util.*;
class A {
 A(int i) {System.out.println("A");}
}

class C extends A {
    C(){
        super(11);
    }
    C(int i) {

        super(i);
        System.out.println("C");
   
    }

}


public class Javaone {


    public static void main(String[] args) {

        C c = new C(1);


    }

}

第九题

import java.util.*;
class Component1 {
    Component1() { System.out.println("Component1()"); }
}

class Component2 {
    Component2() { System.out.println("Component2()"); }
}

class Component3 {
    Component3() { System.out.println("Component3()"); }
}

class Root {
    Component1 c1root;
    Component2 c2root;
    Component3 c3root;
    Root() { System.out.println("Root()"); }
}



public class Javaone extends Root {
    Component1 c1root;
    Component2 c2root;
    Component3 c3root;
    Javaone() {
        System.out.println("Javaone()");
    }

    public static void main(String[] args) {
        Javaone j1 = new Javaone();


    }

}

第十题

 

Java编程思想第六章访问权限控制

第一题

/* In another directory:
* / access/mypackage/MyPackagedClass.java
*
* package access.mypackage;
*
* public class MyPackagedClass {
*	public MyPackagedClass() {System.out.println("MyPackagedClass()");}
* }	
*/

public class UnpackagedMyClass {
	public static void main(String[] args) {
		access.mypackage.MyPackagedClass m = new access.mypackage.MyPackagedClass();
	}
}

第二题

import net.mindview.simple.*;
import java.util.*;
 
public class Collision {
	public static void main(String[] args) {
		/ Vector v = new Vector(); / ambiquous collision
		net.mindview.simple.Vector v1 = new net.mindview.simple.Vector();
		java.util.Vector v2 = new java.util.Vector();
	}
}

第三题

/* In directory access/debugoff:
* / access/debugoff/Debug.java
* package access.debugoff;
*
* public class Debug {
*	public static void debug(String s) { }
* }
*/

package access.debug;

public class Debug {
	public static void debug(String s) {
		System.out.println(s);
	}
}


第四题

/* package access.cookie2;
*
* public class Cookie {
*	public Cookie() {
*		System.out.println("Cookie contstructor");
*	}
*	protected void bite() { System.out.println("bite"); }
* }
*/

package access.cookie2;

public class CookieMonster {
	public static void main(String[] args) {
		Cookie x = new Cookie();
		x.bite(); / package access to protected method
	}	
}

第五题

/* in same directory:
* package access; 
*
* public class FourWays {
*	int a = 0;
*	public int b = 1;
*	protected int c = 2;
*	private int d = 3;
*	FourWays() { System.out.println("FourWays() constructor"); }
*	void showa() { System.out.println(a); }
*	public void showb() { System.out.println(b); }
*	protected void showc() { System.out.println(c); }
*	private void showd() { System.out.println(d); }	
* }
*/

package access; / run command java access.AccessTest

public class AccessTest {
	public static void main(String[] args) {
		FourWays fw = new FourWays();
		fw.showa();
		fw.showb();
		fw.showc();
		fw.a = 10;
		fw.b = 20;
		fw.c = 30;
		fw.showa();
		fw.showb();
		fw.showc();
		/! fw.showd(); / private access, compiler can't touch
	}	
}


第六题

class SomeData {
	protected int a = 13;
}

class DataChanger {
	static void change(SomeData sd, int i) { sd.a = i; }
}

public class ProtectedData {
	public static void main(String[] args) {
		SomeData x = new SomeData();
		System.out.println(x.a);					
		DataChanger.change(x, 99);
		System.out.println(x.a);		
	}	
}

 

Java编程思想第五章初始化与清理

习题一

import java.util.*;

class Astring {
    String  x;
}

public class Javaone {


    public static void main(String[] args) {
      Astring a = new Astring();
      System.out.println(a.x);
    }
}

习题二

import java.util.*;

class Astring {
    String  x1;
    String  x2 = "sadds";
    String  x3;

    Astring(String m) {
        this.x3 = m;
    }
}

public class Javaone {


    public static void main(String[] args) {
      Astring a = new Astring("xiaowen");
      System.out.println(a.x1);
      System.out.println(a.x2);
      System.out.println(a.x3);
    }
}

习题三

import java.util.*;

class Tree {
   Tree() {
       System.out.println("无参构造函数");
   }
}

public class Javaone {

    public static void main(String[] args) {
       Tree t = new Tree();
    }
}

习题四

import java.util.*;

class Tree {
   Tree() {
       System.out.println("无参构造函数");
   }
   Tree(String s) {
       System.out.println(s);

   }
}

public class Javaone {

    public static void main(String[] args) {
       Tree t = new Tree();
       Tree t2 = new Tree("xiaowen");
    }
}

习题五

import java.util.*;

class Dog {
  void bark() {System.out.println("null");}
  void bark(int n) {System.out.println("int");}
  void bark(String s) {System.out.println("String");}
}

public class Javaone {

    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.bark();
        d1.bark(5);
        d1.bark("s");

    }
}

习题六

import java.util.*;

class Dog {
    void bark(int i, String s) {System.out.println("int  string");}
    void bark(String s, int i) {System.out.println("string int");}
}

public class Javaone {

    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.bark(5,"s");
        d1.bark("s",5);

    }
}

习题七

import java.util.*;

class Dog {
    void show() {System.out.println("thanks");}
}

public class Javaone {

    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.show();

    }
}

习题八

import java.util.*;

class Dog {
    void show() {System.out.println("thanks");}
    void disp() {
        show();
        this.show();
    }
}

public class Javaone {

    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.disp();

    }
}

习题九

import java.util.*;

class Dog {
   int x1;
   String s;

   Dog() {}
   Dog(int x1) { this.x1 = x1;}
   Dog(int x1, String s) {this(x1); this.s = s;}

    void show() {
       System.out.println(x1 + ' ' + s);
    }

}

public class Javaone {

    public static void main(String[] args) {
        Dog d1 = new Dog(5,"xiaowen");
        d1.show();
    }
}

习题十

import java.util.*;

class Book {
    boolean checkedOut = false;
    Book(boolean checkOut) {
        checkedOut = checkOut;
    }
    void checkIn() {
        checkedOut = false;
    }
    protected void finalize() {
        if (checkedOut)
            System.out.println("Error: checked out");
    }
}

public class Javaone {

    public static void main(String[] args) {
        Book novel = new Book(true);

        novel.checkIn();
        new Book(true);
        System.gc();

    }
}

第十一题

import java.util.*;

class WebBank {
    boolean loggedIn = false;
    WebBank(boolean logStatus) {
        loggedIn = logStatus;
    }
    void logOut() {
        loggedIn = false;
    }
    protected void finalize() {
        if(loggedIn)
            System.out.println("Error: still logged in");
        / Normally, you'll also call the base-class version:
        / super.finalize();
    }
}

public class Javaone {

    public static void main(String[] args) {
        WebBank bank1 = new WebBank(true);
        WebBank bank2 = new WebBank(true);
        new WebBank(true);
        / Proper cleanup: log out of bank1 before going home:
        bank1.logOut();
        / Forget to logout of bank2 and unnamed new bank
        / Attempts to finalize any missed banks:
        System.out.println("Try 1: ");
        System.runFinalization();
        System.out.println("Try 2: ");
        Runtime.getRuntime().runFinalization();
        System.out.println("Try 3: ");
        System.gc();
        System.out.println("Try 4: ");
        / using deprecated since 1.1 method:
        System.runFinalizersOnExit(true);
    }
}

第十二题

class Tank {
	int howFull = 0;
	Tank() { this(0); }
	Tank(int fullness) {
		howFull = fullness;		
	}
	void sayHowFull() {
		if(howFull == 0) System.out.println("Tank is empty");
		else System.out.println("Tank filling status = " + howFull);
	}
	void empty() {
		howFull = 0;
	}
	protected void finalize() {
		if(howFull != 0)
			System.out.println("Error: Tank not empty");
		/ Normally, you'll also do this:
		/ super.finalize(); / Call the base-class version
	}
}

public class TankTest {
	public static void main(String[] args) {
		Tank tank1 = new Tank();
		Tank tank2 = new Tank(3);
		Tank tank3 = new Tank(5);
		/ Proper cleanup: empty tank before going home
		tank2.empty();
		/ Drop the reference, forget to cleanup:
		new Tank(6);
		System.out.println("Check tanks:");
		System.out.println("tank1: ");
		tank1.sayHowFull();
		System.out.println("tank2: ");
		tank2.sayHowFull();
		System.out.println("tank3: ");
		tank3.sayHowFull();
		System.out.println("first forced gc():");
		System.gc();
		/ Force finalization on exit but using method
		/ deprecated since JDK 1.1:
		System.out.println("try deprecated runFinalizersOnExit(true):");
		System.runFinalizersOnExit(true);
		System.out.println("last forced gc():");
		System.gc();
	}	
}

第十三题

import java.util.*;

class Cup {
    static String s1 = "sadas";
    static String s2 ;

    static {
        s2 = "adasdca";
    }

    static void show() {
        System.out.println(s1 + s2);
    }

    Cup() {
        System.out.println("Cup");
    }
}

public class Javaone {

    public static void main(String[] args) {
        Cup.show();
    }
    static  Cup c1 = new Cup();
}

第十四题

import java.util.*;

class Cup {
    static String s2 ;

    static {
        s2 = "adasdca";
    }

    static void show() {
        System.out.println( s2);
    }

    Cup() {
        System.out.println("Cup");
    }
}

public class Javaone {

    public static void main(String[] args) {
        Cup.show();
    }
    static  Cup c1 = new Cup();
}

第十五题

import java.util.*;

class Cup {
     String s1 = "sadas";
     String s2 ;
     {
        s2 = "adasdca";
    }

    void show() {
        System.out.println(s1 + s2);
    }

    Cup() {
        System.out.println("Cup");
    }
}

public class Javaone {

    public static void main(String[] args) {
        Cup c1 = new Cup();
        c1.show();
    }

}

十六题

import java.util.*;



public class Javaone {

    public static void main(String[] args) {
       String[] s = { "xiaowen", "xiaozhang", "xiaoliu"};

       for (int i = 0; i < s.length; i++) {
           System.out.println(s[i]);
       }



    }

}


十七题

import java.util.*;

class Cup {
    Cup(String s) {
        System.out.println(s);

    }
}



public class Javaone {

    public static void main(String[] args) {
       Cup[] m = new Cup[10];

    }

}

第十八题

import java.util.*;

class Cup {
    Cup(String s) {
        System.out.println(s);

    }
}



public class Javaone {

    public static void main(String[] args) {
       Cup[] m = new Cup[10];

       for (int i = 0; i <= m.length - 1; i++) {
           m[i] = new Cup("ssss");
       }

    }

}

第十九题

import java.util.*;

class VarArgs {

}


public class Javaone {
   static void f(String ... args) {
       for (String s : args)
           System.out.println(s);
   }

    public static void main(String[] args) {
       f("one", "two", "three");

    }

}

第二十题

import java.util.*;


public class Javaone {
    
    public static void main(String ... args) {
      for (String s : args) 
          System.out.println(s);

    }

}

第二十一题

import java.util.*;


public class Javaone {
    public enum Bills {
        ONE, FIVE, TEN, TWENY, FIFTY, HUNDRED
    }

    public static void main(String ... args) {
      for (Bills b : Bills.values())
          System.out.println(b + ", ordinal" + b.ordinal());

    }

}

第二十二题

import java.util.*;


public class Javaone {
    public enum Bills {
        ONE, FIVE, TEN, TWENY, FIFTY, HUNDRED
    }

    public static void main(String ... args) {
      for (Bills b : Bills.values()){

          System.out.println(b + ", ordinal" + b.ordinal());
          switch(b) {
              case ONE: System.out.println("$1");break;
              case FIVE: System.out.println("$5");break;
              case TWENY: System.out.println("$20");break;
              case HUNDRED: System.out.println("$100");break;
              default: System.out.println("i don`t know");
          }

      }

    }

}

822-268-7361

以后小结看情况写。

第一题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
       for (int i = 0; i < 100; i++) {
           System.out.println(i + 1);
       }

    }


}

第二题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {

        Random rand1 = new Random();
        Random rand2 = new Random();
        int x = rand1.nextInt(10);
        int y;
        for(int i = 0; i < 25; i++) {
            y = rand2.nextInt(10);
            if(x < y) System.out.println(x + " < " + y);
            else if(x > y) System.out.println(x + " > " + y);
            else System.out.println(x + " = " + y);

            x = y;
        }
    }


}

第三题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {

        Random rand1 = new Random();
        Random rand2 = new Random();
        int x = rand1.nextInt(10);
        int y;
        while (true) {
       / for(int i = 0; i < 25; i++) {
            y = rand2.nextInt(10);
            if(x < y) System.out.println(x + " < " + y);
            else if(x > y) System.out.println(x + " > " + y);

            x = y;
        }
    }


}

第四题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {

        for (int i = 1; i < 10; i++) {
            int count = 0;
            for (int j = 1; j <= i; j++) {
                if (i % j == 0) count++;
            }

            if (count <= 2) {
                System.out.println(i);
            }

        }
    }


}

第五题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        int q = 1 + 4 + 16 + 64;
       
        if(q == 0) System.out.print(0);
        /判断是否为0
        else {
            int nlz = Integer.numberOfLeadingZeros(q);
            /返回二进制中最高位1之前的0的个数
            q <<= nlz;
            /把数据放到最左边
            for(int p = 0; p < 32 - nlz; p++) {
                int n = (Integer.numberOfLeadingZeros(q) == 0) ? 1 : 0;
                System.out.print(n);
                q <<= 1;
            }
        }
        System.out.println("");
    }


}

第六题

import java.util.*;

public class Javaone {
    static int test(int testval, int begin, int end) {
       if (testval >= begin && testval <= end)
           return +1;
       else return 0;
    }
    public static void main(String[] args) {
        System.out.println(test(10,5,15));

    }
}

第七题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        for (int i = 0; i <= 100; i++) {
            System.out.println(i);
            if (i == 99)/break;
                return;
        }
    }
}

第八题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        for(int i = 0; i < 5; i++) {
            switch(i) {
                case 0: System.out.println("zero"); break;
                case 1:  System.out.println("isa"); break;
                case 2:  System.out.println("dalawa"); break;

                default:  System.out.println("default");
            }
        }
    }
}

第九题

import java.util.*;

public class Javaone {
    static int fibonAcci(int n) {
        if (n < 2) return 1;
        else
             return (fibonAcci(n - 1) + fibonAcci(n - 2));
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int i = 0; i< n; i++ ) {
            System.out.println(fibonAcci(i));
        }
    }
}

第十题

import java.util.*;

public class Javaone {
    static int a(int i) {
        return i/1000;
    }
    static int b(int i) {
        return (i%1000)/100;
    }
    static int c(int i) {
        return ((i%1000)%100)/10;
    }
    static int d(int i) {
        return ((i%1000)%100)%10;
    }
    static int com(int i, int j) {
        return (i * 10) + j;
    }
    static void productTest (int i, int m, int n) {
        if(m * n == i) System.out.println(i + " = " + m + " * " + n);
    }


    public static void main(String[] args) {

        for(int i = 1001; i < 9999; i++) {
            productTest(i, com(a(i), b(i)), com(c(i), d(i)));
            productTest(i, com(a(i), b(i)), com(d(i), c(i)));
            productTest(i, com(a(i), c(i)), com(b(i), d(i)));
            productTest(i, com(a(i), c(i)), com(d(i), b(i)));
            productTest(i, com(a(i), d(i)), com(b(i), c(i)));
            productTest(i, com(a(i), d(i)), com(c(i), b(i)));
            productTest(i, com(b(i), a(i)), com(c(i), d(i)));
            productTest(i, com(b(i), a(i)), com(d(i), c(i)));
            productTest(i, com(b(i), c(i)), com(d(i), a(i)));
            productTest(i, com(b(i), d(i)), com(c(i), a(i)));
            productTest(i, com(c(i), a(i)), com(d(i), b(i)));
            productTest(i, com(c(i), b(i)), com(d(i), a(i)));
        }



    }
}

Java 编程思想第三章小结加习题

操作符

String类支持’+’ and ‘+=’;

对象赋值实际是操作引用。当设置t1 = t2时,修改t1的值的时候,t2的值也会跟着修改。

在函数中调用对象时,也是传递了一个引用。

== 和 !=能用于操作对象,但是只是比较对象的引用。对象与对象之间用equals()函数。

而且equals函数要使用覆盖@ovrried属性在类中构造。

toBinaryString()方法把参数转换成二进制数并输出为string型;

按位操作符 与 &,或|,异或^,非~。

移位操作符  左移<<低位补0,右移>>为正高位补0,为负高位补1,无符号>>>右移高位补0。对byte和short类型可能会出错。

移位操作符和按位操作符都可以和等号组合使用。System.out.println()函数中两个参数加括号就是括号内的内容相加。

类型转换分为窄化转换和扩展转换。布尔型无法进行转化。

转换会对数据进行截尾,用Math.round()函数可以进行舍入。

第一题

public class Javaone {

    public static void main(String[] args) {
        System.out.println("Hello world");

    }

}
public class Javaone {

    public static void main(String[] args) {
        print("Hello world");

    }

}

第二题

class JavaFloat {
    float  a;
}
public class Javaone {

    public static void main(String[] args) {
        JavaFloat a1 = new JavaFloat();
        JavaFloat a2 = new JavaFloat();

        a1.a = 12.2f;
        a2.a = 15.5f;

        System.out.println(" a1.a = " + a1.a + " a2.a = " + a2.a);
        a1 = a2;
        System.out.println(" a1.a = " + a1.a + " a2.a = " + a2.a);
        a1.a = 11.1f;
        System.out.println(" a1.a = " + a1.a + " a2.a = " + a2.a);
    }

}

第三题

class Letter {
    float c;
}
 class PassObiect {
    static  void f(Letter y) {
        y.c = 21.2f;
    }
}
public class Javaone {

    public static void main(String[] args) {
        Letter x = new Letter();
        x.c = 2111.2f;
        System.out.println("1: x.c: " + x.c);
        PassObiect.f(x);
        System.out.println("2: x.c: " + x.c);
    }

}

第四题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        float x, y, z;
        System.out.println("please enter the time");
        x = input.nextFloat();
        System.out.println("please enter the distance");
        y = input.nextFloat();

        z = y / x;
        System.out.println("z =" + z);

    }
}

第五题

import java.util.*;

class Dog {
    String name;
    String says;

}

public class Javaone {

    public static void main(String[] args) {
     Dog spot = new Dog();
     spot.says = "Ruff";
     spot.name = "spot";
     Dog scruffy = new Dog();
     scruffy.says = "scruffy";
     scruffy.name = "Wurf";

     System.out.println("DOG name: " + spot.name + scruffy.name);
     System.out.println("DOG says: " + spot.says + scruffy.says);
    }
}

第六题

import java.util.*;

class Dog {
    String name;
    String says;

}

public class Javaone {

    public static void main(String[] args) {
        Dog spot = new Dog();
        spot.says = "Ruff";
        spot.name = "spot";
        Dog scruffy = new Dog();
        scruffy.says = "scruffy";
        scruffy.name = "Wurf";
        
        Dog d1 = new Dog();
        d1 = spot;

        System.out.println(d1 == spot);
        System.out.println(d1.equals(spot));

    }
}

第七题

import java.util.*;



public class Javaone {

    public static void main(String[] args) {
       Random rand =new Random(47);
       int  m = rand.nextInt();
       if (m % 2 == 0) System.out.println("双");
       else
           System.out.println("单");
    }
}

第八题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        long a1 = 0x123;

        System.out.println("a1 = " + Long.toBinaryString(a1));

    }
}

 

第九题

import java.util.*;



public class Javaone {

    public static void main(String[] args) {
        
        double max = java.lang.Double.MAX_VALUE;
        System.out.println("Max double = " + max);
        double min = java.lang.Double.MIN_VALUE;
        System.out.println("Min double = " + min);
        float maxf = java.lang.Float.MAX_VALUE;
        System.out.println("Max float = " + maxf);
        float minf = java.lang.Float.MIN_VALUE;
        System.out.println("Min float = " + minf);


    }
}

第十题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        int a1 = 1 + 4 + 16 + 64;
        int a2 = 2 + 8 + 32 + 128;

        System.out.println("a1 = " + Integer.toBinaryString(a1));
        System.out.println("a2 = " + Integer.toBinaryString(a2));
        System.out.println("a1 & a2 = " + Integer.toBinaryString(a1 & a2));
        System.out.println("a1 | a2 = " + Integer.toBinaryString(a1 | a2));
        System.out.println("a1 ^ a2 = " + Integer.toBinaryString(a1 ^ a2));
        System.out.println(" ~ a1 = " + Integer.toBinaryString(~ a1));

    }
}

第十一题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        int h = 0x10000000;
        System.out.println(Integer.toBinaryString(h));
        for(int i = 0; i < 28; i++) {
            h >>>= 1;
            System.out.println(Integer.toBinaryString(h));
        }
    }
}

第十二题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        int h = -1;
        System.out.println(Integer.toBinaryString(h));
        h <<= 10;
        System.out.println(Integer.toBinaryString(h));
        for(int i = 0; i < 32; i++) {
            h >>>= 1;
            System.out.println(Integer.toBinaryString(h));
        }
    }
}

第十三题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {

        charBinary('c');
    }

    static  void charBinary(char c) {
        System.out.println(Integer.toBinaryString(c));
    }
}

第十四题

import java.util.*;

public class Javaone {

    public static void main(String[] args) {
        BijiaoChar("sss", "sss3");

    }
    static void BijiaoChar(String s1, String  s2) {
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }

}

(818) 738-8157

一切都是对象

用引用(reference)来操纵对象,创建引用的同时进行初始化。在使用任何引用时都要为他指定一个对象。

基本类型 如下 :boolean  char byte short int long float double void

基本类型的默认值 false,null,0,0,0,0l,0.0f,0.0f。

 

/boolean类型占据的存储空间没有明确指定。

对应的包装器类型如下:Boolean Character Byte Short Integer Long Float Double

高精度的包装器类型:BigInteger(支持任意精度的整数) 和 BigDecimal(支持任何精度的定点数)。

Java不能将一个较大作用域的变量进行“隐藏”。

类的关键字 class  类中有字段和方法。方法和字段都只能用类名或者类的对象来调用。

import 导入包。 static 申明静态(即使从未创建某一个类的任何对象,也可以调用其中的static方法,和static字段。同一个类创建多个对象,其中static字段为同一个字段)。

习题一:

public class Javaone {
    static int  m;
    static char n;

    public static void main(String[] args) {
        System.out.println("m =" + m);
        System.out.println("n =" + n);
    }

}

习题二

public class Javaone {

    public static void main(String[] args) {
        System.out.println("Hello,world!");
    }

}

习题三

class ATypeName {
    int i;
    char m;
    double n;
    
    void show() { 
        
        System.out.println("i = " + i);
        System.out.println("m = " + m);
        System.out.println("n = " + n);
    }
}

public class Javaone {

    public static void main(String[] args) {
        ATypeName aTypeName = new ATypeName();
        aTypeName.show();
    }

}

习题四,习题五

class DataOnly {
    int i;
    char m;
    double n;

    void show() {

        System.out.println("i = " + i);
        System.out.println("m = " + m);
        System.out.println("n = " + n);
    }
}

public class Javaone {

    public static void main(String[] args) {
        DataOnly data = new DataOnly();
        data.i = 1;
        data.m = 'a';
        data.n = 2.33;
        data.show();
    }

}

习题六

public class Javaone {
   static int storage(String s) {
        return s.length() * 2;
    }

    public static void main(String[] args) {
        System.out.println(storage("xiaowen"));
    }

}

习题7

class StaticTest {
    static int i = 47;
}
class Incrementable {
    static  void increment() {
        StaticTest.i++;
    }
}

public class Javaone {

    public static void main(String[] args) {
        Incrementable.increment();
        System.out.println(StaticTest.i);
    }

}

习题八

class StaticTest {
    static int i = 47;
    int m = 100;
    int n = 100;

    void show() {
        i++;
        System.out.println("i = " + i);
        System.out.println("m = " + m);
        System.out.println("n = " + n);
    }
}


public class Javaone {

    public static void main(String[] args) {
        StaticTest n1 = new StaticTest();
        StaticTest n2 = new StaticTest();

        n1.show();

        n2.show();

    }

}

习题九

public class Javaone {

    public static void main(String[] args) {
        boolean b = false;
        char c = 'x';
        byte t = 8;
        short s = 16;
        int i = 32;
        long l = 64;
        float f = 0.32f;
        double d = 0.64;
        Boolean B = b;
        System.out.println("boolean b = " + b);
        System.out.println("Boolean B = " + B);
        Character C = c;
        System.out.println("char c = " + c);
        System.out.println("Character C = " + C);
        Byte T = t;
        System.out.println("byte t = " + t);
        System.out.println("Byte T = " + T);
        Short S = s;
        System.out.println("short s = " + s);
        System.out.println("Short S = " + S);
        Integer I = i;
        System.out.println("int i = " + i);
        System.out.println("Integer I = " + I);
        Long L = l;
        System.out.println("long l = " + l);
        System.out.println("Long L = " + L);
        Float F = f;
        System.out.println("float f = " + f);
        System.out.println("Float F = " + F);
        Double D = d;
        System.out.println("double d = " + d);
        System.out.println("Double D = " + D);
    }



}

习题十

public class Javaone {

    public static void main(String[] args) {

        System.out.println("args[0]" + args[0]);
        System.out.println("args[1]" + args[1]);
        System.out.println("args[2]" + args[2]);
    }

}

习题十一

class AllTheColorsOfTheRainbow {
    int anIntegerRepresentingColors = 0;
    void changeTheHueOfTheColor(int newHue) {
        anIntegerRepresentingColors = newHue;
    }

    void show() {
        System.out.println(anIntegerRepresentingColors);
    }
    
}
public class Javaone {

    public static void main(String[] args) {

        AllTheColorsOfTheRainbow a1 = new AllTheColorsOfTheRainbow();
        a1.show();
        a1.changeTheHueOfTheColor(5);
        a1.show();

    }

}

习题十二

javadoc  HelloDate

习题十三,十四,十五,十六

略,涉及Javadoc,没啥代码。

 

关于ERROR 1215 (HY000): Cannot add foreign key constraint错误的解决办法

不多比比,直接上代码

Create table Manager_information_table (
manager_no char(10) primary key,
manager_name char(10) not null,
manager_sex char(10) not null,
manager_phone char(20) not null,
manager_wege int not null,
manager_address char(20) not null
)engine=InnoDB default charset=gbk;

create table Borrowing_table (
borrowing_data char(10),
return_book char(10),
renewal char(10),
manager_no char(10) not null unique,
constraint borrowing_manager_ss foreign key(manager_no) references Manager_information_table(manager_no));

代码在数据库里面运行时,直接报错。当时赶紧谷歌一下报错代码,发现这种错误多处于数据类型设计错误导致外键外发添加,于是开始赶忙的查询工作。

 

发现类型没错误,于是根据网上的内容判断可能是搜索引擎出错了,果然于是把搜索引擎改好,在运行代码就成功了

 

5709158537

  • 简单性
  • 面向对象
  • 分布式
  • 健壮性
  • 安全性
  • 体系结构中立
  • 可移植性
  • 解释性
  • 高性能
  • 多线程
  • 动态性

一些简单的PHP操作加mysql进行学生成绩查询和修改

本来准备好好写一个增删查改的学生成绩查询系统,但是最近时间比较紧,那就将就应付一下吧。
首先我们要做学生查询系统,必须先创建数据库,这里应用到一些简单的数据库知识,不会百度,毕竟作者也是新手。
首先创建学生表,和成绩表,具体实现

create table chenji(ID char(10),
xinming char(10),
kemu char(10),
fenshu int
);create table student(ID char(10),
xinming char(10),
xinbie char(10),
nianlin int,
banji char(10)
);

然后就是插入数据,各位如果觉得属性设置的太垃圾,请随意更改。

然后就是PHP进行登陆页面

<form name="form1" method="get" action="">
    用户名:<input name="user" type="text" size="12"><br>
    密&nbsp;&nbsp;码: <input name="pwd" type="password" size="12">
         <input type="submit" name="submit" value="登陆">

</form>
<?php
/**
 * Created by PhpStorm.
 * User: wenshijin
 * Date: 2018/11/29
 * Time: 19:32
 */
$user=$_GET["user"];
$pwd=$_GET["pwd"];
if(isset($_GET["submit"])&&$_GET["submit"]=="登陆") {
 if($user=="xiaowen" && $pwd == "123456")
    echo "<script>window.location.href='1.php';</script>";
 else
     echo "<script>alert('用户名,密码错误,请重新登录')</script>";
}
没有任何css加html所以很丑,页面就不放出来了
然后我的思路就是首先进入学生表,所以登陆之后就是进入学生表
<center>
    <h2>管理员,欢迎登陆</h2>
    <h3>浏览学生信息</h3>
    <table width="600" border="1">
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>班级</th>
        </tr>
        <?php
        $host = "127.0.0.1";
        $userName = "root";
        $password = "wenshijin123456";

        if ($connID = mysqli_connect($host,$userName,$password,'xiaowen')) {

        }
        else
            echo "<script type='text/javascript'>alert('No!');</script>";
            mysqli_query($connID,"set names utf-8");
            $result = mysqli_query($connID,"select * from student");
           while($myrow = mysqli_fetch_array($result)) {

       echo "<tr>";
               echo "<td align=\"center\">$myrow[ID]</td>";
               echo "<td align=\"center\">$myrow[xinming]</td>";
               echo "<td align=\"center\">$myrow[xinbie]</td>";
               echo "<td align=\"center\">$myrow[nianlin]</td>";
               echo "<td align=\"center\">$myrow[banji]</td>";


       echo "</tr>";
           }
        mysqli_free_result($result);

        ?>


    </table>
    <table width="600" border="1">
    <tr>
        <th><a href="student.php">修改学生信息</a></th>
        <th><a href="index.php">查看学生成绩</a></th>
    </tr>
        <tr>
           <th> <a href="chenji.php">修改学生成绩</a></th>
          <th><a href="index1.php">退出程序</a></th>
        </tr>
    </table>
</center>

没错就是这样丑,博主已经绝望了,打算不放页面了。
然后就是修改学生信息和学生成绩,操作一个样,直接上代码
<form  method="post" action="">


        请输入2号学生年龄: <input type="text" name="Shuju" size="12"><br>
        <input type="submit" name="submit1" value="修改">
        <input type="submit" name="submit2" value="返回">
</form>

<?php
/**
 * Created by PhpStorm.
 * User: wenshijin
 * Date: 2018/11/30
 * Time: 16:11
 */


$Shuju = $_POST["Shuju"];
$host = "127.0.0.1";
$userName = "root";
$password = "wenshijin123456";

if ($connID = mysqli_connect($host,$userName,$password,'xiaowen')) {

}
else
    echo "<script type='text/javascript'>alert('No!');</script>";
if(isset($_POST["submit1"])&&$_POST["submit1"]=="修改") {

    $result=mysqli_query($connID,"update student set nianlin=$Shuju where ID='02'");
    if($result) {
        echo "ok";

    }
    else
        echo "no";
}
if(isset($_POST["submit2"])&&$_POST["submit2"]=="返回")
    echo "<script>window.location.href='1.php';</script>";
简化后就这样了,我都感觉自己垃圾,但是实在想睡觉了,第一次写博客还是得有头有尾的,乱嗨了,随意看,反正也没人看最后在上一段代码就结束
<center>

    <h3>浏览学生信息</h3>
    <table width="600" border="1">
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>科目</th>
            <th>分数</th>
        </tr>

        <?php
        $host = "127.0.0.1";
        $userName = "root";
        $password = "wenshijin123456";

        if ($connID = mysqli_connect($host,$userName,$password,'xiaowen')) {

        }
        else
            echo "<script type='text/javascript'>alert('No!');</script>";
        mysqli_query($connID,"set names utf-8");
        $result = mysqli_query($connID,"select * from chenji");
        while($myrow = mysqli_fetch_array($result)) {

            echo "<tr>";
            echo "<td align=\"center\">$myrow[ID]</td>";
            echo "<td align=\"center\">$myrow[xinming]</td>";
            echo "<td align=\"center\">$myrow[kemu]</td>";
            echo "<td align=\"center\">$myrow[fenshu]</td>";
            echo "</tr>";
        }
        mysqli_free_result($result);
        ?>
        <form action="" method="post">
            <input type="submit" name="submit2" value="返回">

        </form>
<?php
/**
 * Created by PhpStorm.
 * User: wenshijin
 * Date: 2018/11/29
 * Time: 14:21
 */
if(isset($_POST["submit2"])&&$_POST["submit2"]=="返回")
    echo "<script>window.location.href='1.php';</script>";