Created
March 27, 2018 17:31
-
-
Save EthanZ85/c09250408be7360a338b1cfa06cda7fb to your computer and use it in GitHub Desktop.
JavaEssential Practices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project version="4"> | |
| <component name="CheckStyle-IDEA"> | |
| <option name="configuration"> | |
| <map> | |
| <entry key="checkstyle-version" value="8.8" /> | |
| <entry key="copy-libs" value="false" /> | |
| <entry key="location-0" value="BUNDLED:(bundled):Sun Checks" /> | |
| <entry key="location-1" value="BUNDLED:(bundled):Google Checks" /> | |
| <entry key="scan-before-checkin" value="false" /> | |
| <entry key="scanscope" value="JavaOnly" /> | |
| <entry key="suppress-errors" value="false" /> | |
| </map> | |
| </option> | |
| </component> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <module type="JAVA_MODULE" version="4"> | |
| <component name="NewModuleRootManager" inherit-compiler-output="true"> | |
| <exclude-output /> | |
| <content url="file://$MODULE_DIR$" /> | |
| <orderEntry type="inheritedJdk" /> | |
| <orderEntry type="sourceFolder" forTests="false" /> | |
| </component> | |
| </module> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project version="4"> | |
| <component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK" /> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project version="4"> | |
| <component name="ProjectModuleManager"> | |
| <modules> | |
| <module fileurl="file://$PROJECT_DIR$/.idea/JavaEssential.iml" filepath="$PROJECT_DIR$/.idea/JavaEssential.iml" /> | |
| </modules> | |
| </component> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.Ethan.JavaTest; | |
| public class Cat { | |
| private static int cid = 0; | |
| private String name; | |
| private int id; | |
| Cat(String name) { | |
| this.name = name; | |
| this.id = cid++; | |
| } | |
| public void info() { | |
| System.out.println("Cat name: " + name + " | No." + id); | |
| } | |
| public static void main(String[] args) { | |
| Cat.cid = 1000; | |
| Cat c1 = new Cat("Dudu"); | |
| Cat c2 = new Cat("Mimi"); | |
| Cat.cid = 2000; | |
| Cat c3 = new Cat("Titi"); | |
| c1.info(); | |
| c2.info(); | |
| c3.info(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Dog { | |
| static int furColor; | |
| float weight; | |
| float height; | |
| static class Mouse { | |
| // | |
| } | |
| void catchMouse(Mouse m) { | |
| //m.scream(); | |
| System.out.println("Catching mouse."); | |
| } | |
| public static void main(String[] args) { | |
| int i=9; | |
| System.out.println(i); | |
| Dog d = new Dog(); | |
| Mouse m = new Mouse(); | |
| d.catchMouse(m); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Fibonacci { | |
| public long f(int n) { | |
| if(n==1||n==2) return 1; | |
| else | |
| return f(n-1) + f(n-2); | |
| } | |
| } | |
| public class FibonacciSequence { | |
| public static void main(String[] args) { | |
| int target = 47; | |
| Fibonacci fs = new Fibonacci(); | |
| System.out.println( "Fibonacci target:" + fs.f(target) + "\n" ); | |
| for( int i = 1; i <= target; i++) { | |
| System.out.println("F("+i+"):" + fs.f(i)); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HelloWorld { | |
| public static void main(String[] args) { | |
| print(); | |
| } | |
| private static void print() { | |
| System.out.println("HelloEthan!"); | |
| } | |
| public String getName() { | |
| return hiEthan(); | |
| } | |
| private String hiEthan() { | |
| return "Hello Ethan"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Leaf { | |
| int i = 0; | |
| Leaf(int i) { | |
| this.i = i; | |
| } | |
| Leaf increament() { | |
| i++; | |
| return this; | |
| } | |
| void print() { | |
| System.out.println("i = " + i); | |
| } | |
| public static void main(String[] args) { | |
| Leaf leaf = new Leaf(100); | |
| leaf.increament().increament().print(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class OddSum { | |
| public static void main(String[] args) { | |
| long result = 0; | |
| for(int i=1; i<=99; i+=2) { | |
| result += i; | |
| } | |
| System.out.println("Result = " + result); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class StringCount { | |
| public int countNum(String str) { | |
| int num = 0; | |
| for(int i=0;i<str.length();i++) { | |
| str.charAt(i); | |
| return num; | |
| } | |
| } | |
| public static void main(String[] args) { | |
| String s = "AaaaaaBBCNMCMHJ827349829%^&*hahkcjkKCJKH"; | |
| Stri | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Test { | |
| public static void main(String[] args) { | |
| System.out.println("Hello World!!!"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestBreak { | |
| public static void main(String[] args) { | |
| int stopSign = 4; | |
| for(int i = 1; i < 10; i++) { | |
| if(i == stopSign) break; | |
| System.out.print(i + " "); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Point { | |
| private double x; | |
| private double y; | |
| Point() { | |
| x = 0.0; | |
| y = 0.0; | |
| } | |
| Point(double _x, double _y) { | |
| x = _x; | |
| y = _y; | |
| } | |
| public double getPointX() { | |
| return x; | |
| } | |
| public double getPointY() { | |
| return y; | |
| } | |
| public void setPointX(double _x1) { | |
| x = _x1; | |
| } | |
| public void setPointY(double _y1) { | |
| y = _y1; | |
| } | |
| } | |
| class Circle { | |
| private Point o; | |
| private double radius; | |
| Circle(Point p, double r) { | |
| o = p; | |
| radius = r; | |
| } | |
| Circle(double r) { | |
| o = new Point(); | |
| radius = r; | |
| } | |
| boolean containPoint(Point tp) { | |
| double x = tp.getPointX() - o.getPointX(); | |
| double y = tp.getPointY() - o.getPointY(); | |
| return ( radius*radius > x*x + y*y) ? true : false; | |
| } | |
| public void setO(double x, double y) { | |
| o.setPointX(x); | |
| o.setPointY(y); | |
| } | |
| public Point getO() { | |
| return o; | |
| } | |
| public double getRadius() { return radius; } | |
| public void setRadius(double r) { radius = r; } | |
| public double getArea() { | |
| return 3.14 * radius * radius; | |
| } | |
| } | |
| public class TestCircle { | |
| public static void main(String[] args) { | |
| Circle c1 = new Circle(new Point(1.0, 2.0), 2.0); | |
| Circle c2 = new Circle(5.0); | |
| c1.getO().setPointX(4.0); | |
| c1.setRadius(3.0); | |
| System.out.println("c1:(" + c1.getO().getPointX() + "," + c1.getO().getPointY() + ") " + c1.getRadius() + " " + c1.getArea()); | |
| System.out.println("c2:(" + c2.getO().getPointX() + "," + c2.getO().getPointY() + ") " + c2.getRadius() + " " + c2.getArea()); | |
| Point checkPoint1 = new Point(6.0, 3.0); | |
| System.out.println("Circle c1 contain the check Point(6,3)? : " + c1.containPoint(checkPoint1) ); | |
| System.out.println("Circle c2 contain the check Point(6,3)? : " + c2.containPoint(checkPoint1) ); | |
| Point checkPoint2 = new Point(3.0, 3.0); | |
| System.out.println("Circle c1 contain the check Point(3,3)? : " + c1.containPoint(checkPoint2) ); | |
| System.out.println("Circle c2 contain the check Point(3,3)? : " + c2.containPoint(checkPoint2) ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestContinue { | |
| public static void main(String[] args) { | |
| int continueSign = 4; | |
| for(int i = 1; i < 10; i++) { | |
| if(i == continueSign) continue; | |
| System.out.print(i + " "); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestDividedBy3 { | |
| public static void main(String[] args) { | |
| int num = 0; | |
| for(int i=1; i<100; i++) { | |
| if(i%3 == 0) { | |
| num ++; | |
| System.out.print(i + " "); | |
| if(num == 12) break; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestFor { | |
| public static void main(String[] args) { | |
| long result = 0; | |
| long f = 1; | |
| for (int i = 1; i <= 10; i++) { | |
| f = f * i; | |
| result += f; | |
| } | |
| System.out.println("result=" + result); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FatherClass { | |
| public int value; | |
| public void f() { | |
| value = 100; | |
| System.out.println("FatherClass.value=" + value); | |
| } | |
| } | |
| class ChildClass extends FatherClass { | |
| public int value; | |
| public void f() { | |
| super.f(); | |
| value = 200; | |
| System.out.println("ChildClass.value=" + value); | |
| System.out.println(value); | |
| System.out.println(super.value); | |
| } | |
| } | |
| public class TestInherit { | |
| public static void main(String[] args) { | |
| // FatherClass fc = new FatherClass(); | |
| // fc.f(); | |
| ChildClass cc = new ChildClass(); | |
| cc.f(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| protected int id; | |
| protected int age; | |
| Person() { | |
| id = 100; | |
| age = 1; | |
| } | |
| Person(int _id) { | |
| id = _id; | |
| } | |
| Person(int _id, int _age) { | |
| id = _id; | |
| age = _age; | |
| } | |
| void info() { | |
| System.out.println("Empty Method Called."); | |
| } | |
| void info(String t) { | |
| System.out.println(t + " 2nd Method Called."); | |
| } | |
| } | |
| public class TestOverload { | |
| public static void main(String[] args) { | |
| Person p = new Person(); | |
| Person pID = new Person(101); | |
| Person pBoth = new Person(99, 33); | |
| //p.info(); | |
| //p.info("EthanZ"); | |
| System.out.println(p.id + " " + p.age); | |
| System.out.println(pID.id + " " + pID.age); | |
| System.out.println(pBoth.id + " " + pBoth.age); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| private String name; | |
| private int age; | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public void setAge(int age) { | |
| this.age = age; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public String getInfo() { | |
| return "Name: " + name + "\n" +"Age: " + age; | |
| } | |
| } | |
| class Student extends Person { | |
| private String school; | |
| public void setSchool(String school) { | |
| this.school = school; | |
| } | |
| public String getSchool() { | |
| return school; | |
| } | |
| public String getInfo() { | |
| return "Name: " + getName() + "\n" +"Age: " + getAge() + "\n" +"School: " + getSchool(); | |
| } | |
| } | |
| public class TestOverWrite { | |
| public static void main(String[] args) { | |
| Person ps = new Person(); | |
| Student sd = new Student(); | |
| ps.setName("none"); | |
| ps.setAge(1000); | |
| sd.setName("Kira"); | |
| sd.setAge(19); | |
| sd.setSchool("Tokyo Univercity"); | |
| System.out.println(ps.getInfo()); | |
| System.out.println(sd.getInfo()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.Ethan.JavaTest; | |
| public class Cat { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.Ethan.JavaTest.Cat; | |
| public class Dog { | |
| public static void main(String[] args) { | |
| Cat c = new Cat(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| private String name; | |
| private int age; | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public void setAge(int age) { | |
| this.age = age; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| } | |
| class Student extends Person { | |
| private String school; | |
| public void setSchool(String school) { | |
| this.school = school; | |
| } | |
| public String getSchool() { | |
| return school; | |
| } | |
| } | |
| public class TestPerson { | |
| public static void main(String[] args) { | |
| Student sd = new Student(); | |
| sd.setName("Kira"); | |
| sd.setAge(19); | |
| sd.setSchool("Tokyo Univercity"); | |
| System.out.println(sd.getName()); | |
| System.out.println(sd.getAge()); | |
| System.out.println(sd.getSchool()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Point { | |
| double x, y, z; | |
| Point(double _x, double _y, double _z) { | |
| x = _x; | |
| y = _y; | |
| z = _z; | |
| } | |
| void setX(double _x){ | |
| x = _x; | |
| } | |
| double getDistance(Point p) { | |
| return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z); | |
| } | |
| } | |
| public class TestPoint { | |
| public static void main(String[] args) { | |
| Point p1 = new Point(3.0, 4.0, 5.0); | |
| Point p2 = new Point(0.0, 0.0, 0.0); | |
| System.out.println("Done"); | |
| p1.setX(2.0); | |
| System.out.println(p1.getDistance(p2)); | |
| System.out.println(p1.getDistance(new Point(1.0, 1.0, 1.0))); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A { | |
| protected void print(String s) { | |
| System.out.println(s); | |
| } | |
| A() { print ("A()");} | |
| public void f() { print ("A:f()");} | |
| } | |
| class B extends A { | |
| B() { print("B()"); } | |
| public void f() { print ("B:f()");} | |
| } | |
| public class TestSuperFunction { | |
| public static void main(String[] args) { | |
| B b = new B(); | |
| b.f(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SuperClass { | |
| private int n; | |
| SuperClass() { | |
| System.out.println("SuperClass()"); | |
| } | |
| SuperClass(int n) { | |
| System.out.println("SuperClass(" + n +")"); | |
| this.n = n; | |
| } | |
| } | |
| class SubClass extends SuperClass { | |
| private int m; | |
| SubClass() { | |
| super(300); | |
| System.out.println("SubClass()"); | |
| // super(); 父类构造函数必须写在第一句 | |
| } | |
| SubClass(int m) { | |
| System.out.println("SubClass(" + m + ")"); | |
| this.m = m; | |
| } | |
| } | |
| public class TestSuperSub { | |
| public static void main(String[] args) { | |
| // SubClass sub1 = new SubClass(); | |
| SubClass sub2 = new SubClass(500); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestSwitch { | |
| public static void main(String[] args) { | |
| int i = 7; | |
| switch(i) { | |
| case 1: | |
| System.out.println(i+" exit."); | |
| break; | |
| case 3: | |
| System.out.println(i+" exit."); | |
| break; | |
| case 5: | |
| System.out.println(i+" exit."); | |
| break; | |
| case 7: | |
| System.out.println(i+" exit."); | |
| //break; | |
| case 9: | |
| System.out.println(i+" exit."); | |
| break; | |
| default: | |
| System.out.println("To the end. Default exit."); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| private String name; | |
| private String location; | |
| Person(String name) { | |
| this.name = name; | |
| location = "Beijing-P"; | |
| } | |
| Person(String name, String location) { | |
| this.name = name; | |
| this.location = location; | |
| } | |
| public String Info() { | |
| return "Name:" + name + " Location:" + location; | |
| } | |
| public void print(String s) { | |
| System.out.println(s); | |
| } | |
| } | |
| class Student extends Person { | |
| private String school; | |
| Student(String name, String school) { | |
| this(name, "Beijing-S", school); | |
| } | |
| Student(String name, String location, String school) { | |
| super(name, location); | |
| this.school = school; | |
| } | |
| public String Info() { | |
| return super.Info() + " School:" + school; | |
| } | |
| } | |
| class Teacher extends Student { | |
| private String title; | |
| Teacher(String name, String school, String title) { | |
| super(name, school); | |
| this.title = title; | |
| } | |
| Teacher(String name, String location, String school, String title) { | |
| super(name, location, school); | |
| this.title = title; | |
| } | |
| public String Info() { | |
| return super.Info() + " Title:" + title; | |
| } | |
| } | |
| public class TestTeacher { | |
| public static void main(String[] args) { | |
| Person p1 = new Person("Person-1"); | |
| Person p2 = new Person("Person-2","Shanghai"); | |
| p1.print(p1.Info()); | |
| p2.print(p2.Info()); | |
| Student s1 = new Student("Student-1","PKU"); | |
| Student s2 = new Student("Student-2","Xiamen","XMU"); | |
| s1.print(s1.Info()); | |
| s2.print(s2.Info()); | |
| Teacher t1 = new Teacher("Teacher-1","XMU","Professon Level 1"); | |
| Teacher t2 = new Teacher("Teacher-2","Xiamen","XMU","Professon Level 2"); | |
| t1.print(t1.Info()); | |
| t2.print(t2.Info()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestWhile { | |
| public static void main(String[] args) { | |
| int i = 0; | |
| while (i < 10) { | |
| System.out.println(i); | |
| i++; | |
| } | |
| System.out.println(""); | |
| i = 9; | |
| do { | |
| System.out.println(i); | |
| i--; | |
| } while( i >= 0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment