5-1 NewDemo1.java
public class Student {
	String name;

	int age;

	String major;

	public Student(String stuName, int stuAge, String stuMajor) {
		name = stuName;
		age = stuAge;
		major = stuMajor;
	}

	public int getAge() {
		return age;
	}

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

	public String getMajor() {
		return major;
	}

	public void setMajor(String newMajor) {
		major = newMajor;
	}

	public String getName() {
		return name;
	}

	public void setName(String newName) {
		name = newName;
	}
}

public class NewDemo1 {
	public static void main(String[] args) {
		
		// һѧ
		Student aStudent = new Student("Tom", 19, "");
		
		// ʾѧϢ
		System.out.println("ѧ" + aStudent.getName());
		System.out.println("ѧ䣺" + aStudent.getAge());
		System.out.println("ѧרҵ" + aStudent.getMajor());
	}
}

5-2 FuZhi.java
public class FuZhi {

	public static void main(String[] args) {
		Student tom = new Student("Tom", 20, "");
		Student tom_Smith;
		tom_Smith = tom; // øֵ

		// tomϢ
		System.out.println("ѧtomĻϢ£");
		System.out.println("  " + tom.getName());
		System.out.println("  䣺" + tom.getAge());
		System.out.println("  רҵ" + tom.getMajor());

		// tom_SmithϢ
		System.out.println("ѧtom_SmithĻϢ£");
		System.out.println("  " + tom_Smith.getName());
		System.out.println("  䣺" + tom_Smith.getAge());
		System.out.println("  רҵ" + tom_Smith.getMajor());
	}

}

5-3 Add.java,MethodDemo1.java
public class Add {

	public int add(int a, int b) {// [1]
		int sum = 0;
		sum = a + b;
		return sum;// [2]
	}
}

public class MethodDemo1 {

	public static void main(String[] args) {
		Add ob = new Add();
		int sum = ob.add(13, 26);// [4]
		System.out.println("2ĺֵΪ" + sum);
	}
}

5-4 Arithmetic.java,MethodDemo2.java
public class Arithmetic {
	
	public int add(int a, int b) {
		int sum = 0;
		sum = a + b;
		return sum;
	}

	public int sub(int a, int b) {

		int result = 0;
		result = a - b;
		return result;
	}

	public int mul(int a, int b) {

		int result = 0;
		result = a * b;
		return result;
	}

	public double div(int a, int b) {

		double result = 0;

		result = a * 1.0 / b;

		return result;
	}

}

import javax.swing.JOptionPane;

public class MethodDemo2 {

	public static void main(String[] args) {
		Arithmetic ob = new Arithmetic();
		String strA = JOptionPane.showInputDialog("1");
		int a = Integer.parseInt(strA);
		String strB = JOptionPane.showInputDialog("2");
		int b = Integer.parseInt(strB);
		// 2Ľ
		System.out.println(a + "+" + b + "=" + ob.add(a, b));
		System.out.println(a + "-" + b + "=" + ob.sub(a, b));
		System.out.println(a + "*" + b + "=" + ob.mul(a, b));
		System.out.println(a + "/" + b + "=" + ob.div(a, b));

	}
}

5-5 MethodDemo3.java
public class MethodDemo3 {

	public static void main(String[] args) {

		MethodDemo3 ob = new MethodDemo3();
		Student jerry = new Student("Jerry", 19, "");

		ob.modify(jerry, 21);

		System.out.println("Jerryڵǣ" + jerry.getAge());

	}

	public void modify(Student stu, int newAge) {

		stu.setAge(newAge);

	}

}

5-6 ReturnDemo1.javaReturnTest1.java
public class ReturnDemo1 {

	public int absolute(int x) {

		if (x < 0) {

			return -x;
		}

		return x;
	}
}

import javax.swing.JOptionPane;

public class ReturnTest1 {

	public static void main(String[] args) {
		ReturnDemo1 ob = new ReturnDemo1();

		String strX = JOptionPane.showInputDialog("һ");

		int x = Integer.parseInt(strX);

		System.out.println(x + "ľֵΪ" + ob.absolute(x));

	}

}

5-7 ReturnDemo2.javaReturnTest2.java
public class ReturnDemo2 {
	public void div(int a, int b) {

		double result = 0;

		if (b == 0) {

			System.out.println("Ϊ0");
			return;
		}

		result = a * 1.0 / b;
		System.out.println(a + "/" + b + " = " + result);

	}

}
import javax.swing.JOptionPane;

public class ReturnTest2 {

	public static void main(String[] args) {
		ReturnDemo2 ob = new ReturnDemo2();

		String strA = JOptionPane.showInputDialog("뱻");

		int a = Integer.parseInt(strA);

		String strB = JOptionPane.showInputDialog("");

		int b = Integer.parseInt(strB);

		ob.div(a, b);

	}
}

5-8 Person.javaReturnDemo3.java
public class Person {

	String name;

	public Person(String nm) {
		name = nm;
	}

	public String getName() {
		return name;
	}

	public void setName(String nm) {
		name = nm;
	}
	
	public Person friend(String nm){		
		Person frd = new Person(nm);
		return frd;
	}

}
public class ReturnDemo3 {
	public static void main(String[] args) {
		Person tom = new Person("Tom");
		Person jerry = tom.friend("Jerry");
		System.out.println(tom.getName() + 
"" + jerry.getName() + "Ǻ");
	}
}

5-9 SameName.javaSameNameTest.java
public class SameName {

	int a=10;

	public void method() {
		int a = 20;
		System.out.println("a=" + a);
	}
}

public class SameNameTest {

	public static void main(String[] args) {
		SameName ob = new SameName();
		ob.method();
	}
}

5-10 SameName2.javaSameNameTest2.java
public class SameName2 {

	int a=10;

	public void method() {
		int a = 20;
		System.out.println("a=" + a);
		System.out.println("a=" + this.a);
	}
}

public class SameNameTest2 {

	public static void main(String[] args) {
		SameName2 ob = new SameName2();
		ob.method();
	}
}

5-11 Book.javaBookTest.java
public class Book {
	String bookName;

	String bookAuthor;

	String press;

	public Book(String bookName, String bookAuthor, String press) {
		this.bookName = bookName;
		this.bookAuthor = bookAuthor;
		this.press = press;
	}

	public String getBookAuthor() {
		return bookAuthor;
	}

	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getPress() {
		return press;
	}

	public void setPress(String press) {
		this.press = press;
	}
}

public class BookTest {

	public static void main(String[] args) {
		Book one = new Book("Java2ʵý̳", "", "廪ѧ");
		
		Book two = new Book("Java", "", "ӹҵ");

		System.out.println("ĿϢ£");		
		System.out.println("" + one.getBookName() + ",ߣ"
				+ one.getBookAuthor() + ",磺" + one.getPress());
		System.out.println("" + two.getBookName() + ",ߣ"
				+ two.getBookAuthor() + ",磺" + two.getPress());
	}

}

5-12 MyCircle.javaStaticTest1.java
public class MyCircle {
	public static double PI = 3.14;

	double radius;

	public MyCircle(double radius) {
		this.radius = radius;
	}

	public double perimeter() {
		
		return 2 * PI * radius;
	}

	public double area() {
		
		return PI * radius * radius;
	}
}

public class StaticTest1 {

	public static void main(String[] args) {

		MyCircle aCircle = new MyCircle(10);

		double perimeter1 = aCircle.perimeter();

		System.out.println("뾶Ϊ10ԲܳΪ" + perimeter1);

		double perimeter2 = 2 * MyCircle.PI * 5;	//[1]

		System.out.println("뾶Ϊ5ԲܳΪ" + perimeter2);

	}
}

5-13 StaticTest2.java
public class StaticTest2 {

	public static void main(String[] args) {
		int sum1 = add(3, 7);
		System.out.println("sum1 = " + sum1);

		int sum2 = StaticTest2.add(2, 4);
		System.out.println("sum2 = " + sum2);
	}

	public static int add(int a, int b) {
		return a + b;
	}
}

5-14 StaticTest3.java
public class StaticTest3 {

	/*static*/ int a = 10;
	/*static*/ int b = 20;

	public static void main(String[] args) {
		int sum = a+b;
		System.out.println("sum = " + sum);		
	}	
}


5-15 StaticTest4.java
public class StaticTest4 {
	public static void main(String[] args) {
		
		int sub = sub(13, 5);
		
		System.out.println("sub = " + sub);
	}

	public int sub(int a, int b) {
		return a - b;
	}
}

5-16 AddOverload.javaOverloadTest.java
public class AddOverload {

	public int add(int a, int b) {
		return a + b;
	}

	public long add(long a, long b) {
		return a + b;
	}

	public double add(double a, double b) {
		return a + b;
	}
}

public class OverloadTest {

	public static void main(String[] args) {

		AddOverload ob = new AddOverload();

		System.out.println("2intӣ" + ob.add(12, 34));
		System.out.println("2longӣ" + ob.add(123L, 456L));
		System.out.println("2doubleӣ" + ob.add(1.2, 3.4));
	}

}

5-17 ContactPerson.java, OverloadTest2.java
public class ContactPerson {
	String name;
	String selfphone;
	String email;

	// ֻ֪ϵ
	public ContactPerson(String name) {
		this.name = name;
	}

	// ֪͵绰
	public ContactPerson(String name, String selfphone) {

		this.name = name;
		this.selfphone = selfphone;
	}

	// ֪绰email
	public ContactPerson(String name, String selfphone, String email) {
		this(name, selfphone);
		this.email = email;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSelfphone() {
		return selfphone;
	}
	public void setSelfphone(String selfphone) {
		this.selfphone = selfphone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}

public class OverloadTest2 {

	public static void main(String[] args) {

		ContactPerson no1 = new ContactPerson("Tom");
		System.out.println("ֻ֪ϵˣ" + no1.getName());

		ContactPerson no2 = new ContactPerson("Jerry", "13812345678");
		System.out.println("֪绰ϵˣ" + no2.getName() + "  "
				+ no2.getSelfphone());

		ContactPerson no3 = new ContactPerson("Jane", "13887654321",
				"jane@abc.com");
System.out.println("֪绰emailϵˣ" + no3.getName() + "  "	+ no3.getSelfphone() + "  " + no3.getEmail());
	}
}

5-18 Person2.javaStudent2.java
public class Person2 {

	String name;
	String sex;
	int age;

	public Person2(String name, String sex, int age) {

		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	public void display() {
		System.out.println("This person's information : \n");

		System.out.println("Name : " + name);
		System.out.println("Sex : " + sex);
		System.out.println("Age : " + age);
	}
}

public class Student2 extends Person2{
	
	String majorClass;  //ԣרҵ༶ 
	
	public Student2(String name, String sex, int age, String majorClass){
	
		super(name,sex,age);  //øĹ췽
		
		this.majorClass = majorClass;	
	}
	
	//
	public String getMajorClass(){		
		return majorClass;
	}	
	public void setMajorClass(String newMajorClass){		
		majorClass = newMajorClass;
	}
	
	//дdisplay
	public void display(){
		System.out.println("This Student's information : \n");
		
		System.out.println("Name : " + name);		
		System.out.println("Sex : " + sex);		
		System.out.println("Age : " + age);
		
		System.out.println("Major Class : " + majorClass);			
	}	
}


5-19 Student2Test.java
public class Student2Test {
	public static void main(String[] arg) {
		Student2 jane = new Student2("Jane", "female", 19, "Computer");
		jane.display();
	}
}

5-20 SuperClass.javaSubClass.java
public class SuperClass {

	String data = "ĳԱ";

	public void method() {
		System.out.println("øķmethod()");
	}
}

public class SubClass extends SuperClass {

	String data = "ͬ"; // ˸ͬdata

	public void method() { // ˸෽method()
		System.out.println("SubClassķmethod()");
	}

	public void method2() {

		String data = "ľֲ";
		// ֲҲظͬ
		// ͬʱҲرͬԱ

		System.out.println("data is " + data);

		System.out.println("this.data is " + this.data);

		System.out.println("super.data is " + super.data);

		System.out.print("ֱӵmethod() ");
		method();

		System.out.print("this.method() ");
		this.method();

		System.out.print("super.method() ");
		super.method();
	}

	public static void main(String[] args) {

		SubClass ob = new SubClass();
		ob.method2();
	}
}

5-21 MyInterface1.javaMyCrl.javaMyRect.javaInterfaceTest1.java
public interface MyInterface1 {
	public static final double PI = 3.14;

	public double area();

	public double length();
}

public class MyCrl implements MyInterface1 {

	double radius;

	public MyCrl(double radius) {
		this.radius = radius;
	}
	public double area() {
		return PI * radius * radius;
	}
	public double length() {
		return 2 * PI * radius;
	}
}

public class MyRect implements MyInterface1 {

	double width;
	double height;	

	public MyRect(double width, double height) {		
		this.width = width;
		this.height = height;
	}
	public double area() {
		return width * height;
	}
	public double length() {
		return 2 * (width + height);
	}
}

public class InterfaceTest1 {

	public static void main(String[] args) {
		
		MyCrl circle = new MyCrl(5);
		MyRect rect = new MyRect(10, 6);

		System.out.println("Բ" + circle.area());
		System.out.println("Բܳ" + circle.length());
		System.out.println("" + rect.area());
		System.out.println("ܳ" + rect.length());
	}
}

5-22 Inter_Area_Volumn.javaInter_Color.javaMyCrl2.javaInterfaceTest2.java
public interface Inter_Area_Volumn {
	public static final double PI = 3.14159;

	public abstract double area();
	public abstract double volume();

}

public interface Inter_Color {
	public abstract void setColor(String color);
}

class MyCrl2 implements Inter_Area_Volumn, Inter_Color {

	double radius;
	String color;

	public MyCrl2(double radius) {
		this.radius = radius;
	}

	// ʵֽӿInter_Area_Volumnķ
	public double area() {
		return PI * radius * radius;
	}
	public double volume() {
		return 4 * PI * radius * radius * radius / 3;
	}

	// ʵֽӿInter_Colorķ
	public void setColor(String color) {
		this.color = color;
	}

	String getColor() {
		return color;
	}
}

public class InterfaceTest2 {
	public static void main(String args[]) {

		MyCrl2 c = new MyCrl2(4);

		System.out.println("Circle area = " + c.area());
		System.out.println("Circle volumn = " + c.volume());

		c.setColor("RED");
		System.out.println("Circle colore = " + c.getColor());
	}
}

5-23 Display.javaMyCrl3.javaInterfaceTest3.java
public class Display {
	public void heading() {
		System.out.println("ͼεĻϢ ");
	}
}

class MyCrl3 extends Display implements Inter_Area_Volumn,Inter_Color {
	
	double radius;
	String color;
	
	public MyCrl3(double radius){
	  this.radius = radius;
	}
	//ʵֽӿArea_Volumnķ
	public double area(){		
	   return PI*radius*radius;	  	
	}
	public double volume(){		
	   return 4*PI*radius*radius*radius/3;	
	}	
	//ʵֽӿColourķ
	public void setColor(String color){
		this.color = color;
	}
	String getColor(){		
		return color;
	}
}

public class InterfaceTest3 {

	public static void main(String args[]) {

		MyCrl3 c = new MyCrl3(2);

		c.heading(); // ̳Displayķ

		System.out.println("Circle area = " + c.area());
		System.out.println("Circle vloume = " + c.volume());

		c.setColor("Red");
		System.out.println("Circle colore = " + c.getColor());
	}
}

5-24 PackageDemo.java
package cn.whvcse;
public class PackageDemo {
	public void hi() {
		System.out.println("Hi~~~~");
	}
}

5-25 PackageDemo2.java
package cn.xyy;

import cn.whvcse.PackageDemo;

public class PackageDemo2 {
	public static void main(String[] args) {
		PackageDemo ob = new PackageDemo();
		ob.hi();
	}
}

5-26 PriavateDemo1.javaPrivateTest1.java
package cn.whvcse;
public class PrivateDemo1 {

	private int price = 30;
	private int num = 5;

	private int total() {
		return price * num;
	}
	int getNum() {
		return num;
	}
	void setNum(int num) {
		this.num = num;
	}
	int getTotal() {
		return total();
	}
}

package cn.whvcse;
public class PrivateTest1 {

	public static void main(String[] args) {
		PrivateDemo1 ob = new PrivateDemo1();

		// ob.price = 40; //޷ͨ
		// ob.num = 10; 	//޷ͨ
		// int total = ob.total(); //޷ͨ

		int total = ob.getTotal();

		System.out.println("ܼΪ" + total);

		ob.setNum(10);	
		//PrivateDemo1ṩdefaultsetNum()
		//PrivateDemo1ͬһеPrivateTest1пԷʵ÷

		System.out.println("޸ܼΪ" + ob.getTotal());
	}
}

5-27 DefaultClassDemo.javaDefaultClassTest.java
package cn.whvcse;

class DefaultClassDemo {

	public void hi() {

		System.out.println("Hi~~~,in DefaultClassDemo");
	}
}

package cn.xyy;

import cn.whvcse.*;

public class DefaultClassTest {

	public static void main(String[] args) {

		DefaultClassDemo ob = new DefaultClassDemo();

	}
}

5-28 ProtectedDemo.javaProtectedTest.javaProtectedTest2.java
package cn.whvcse;

public class ProtectedDemo {
	protected int a = 10;

	protected void hi() {

		System.out.println("Hi~~~,in ProtectedDemo");

		System.out.println("a = " + a);
	}
}

package cn.xyy;

import cn.whvcse.ProtectedDemo;

public class ProtectedTest extends ProtectedDemo {

	public void method() {
		super.hi();
		super.a = 20;
		System.out.println("now,in ProtectedTest , a = " + a);
	}

	public static void main(String[] args) {
		ProtectedTest ob = new ProtectedTest();
		ob.method();
	}
}

package cn.xyy;

import cn.whvcse.ProtectedDemo;

public class ProtectedTest2 {

	public static void main(String[] args) {
		ProtectedDemo ob = new ProtectedDemo();
		ob.hi();
	}
}
