闲来无事敲代码,各种自挂东南枝(java第五章例题)

class Father
{
	private int moneyDollar=300;
	int moneyHK=200;
	int add(int x,int y)
	{
		return x+y;
	}
}
class Son extends Father{
		int moneyRMB=900;
		public void changeMoneyHK(int x){
				moneyHK=x;
			}
		public void changeMoneyRMB(int x){
				moneyRMB=x;
			}
		int subs(int x,int y){
				return x*y;
			}
	}
class GrandSon extends Son{
			int multi(int x,int y){
					return x*y;
				}
		}
	public class Example5_1{
			public static void main(String args[])
			{
					int a=5,b=3;
					Son son=new Son();
					GrandSon sunzi=new GrandSon();
					son.changeMoneyHK(666);
					son.changeMoneyRMB(5000);
					System.out.println("儿子的港币是继承属性,当前的值是:"+son.moneyHK);
					System.out.println("儿子的人民币是新增的属性,当前的值是:"+son.moneyRMB);
					System.out.printf("减法是儿子的新增功能,%d-%d等于%d\n:",a,b,son.subs(a,b));
					System.out.printf("加法是儿子的继承功能,%d+%d等于%d\n:",a,b,son.add(a,b));	
					System.out.println("孙子的港币和人民币都是继承的属性,当前的值是:");
					System.out.printf("乘法是孙子的新增功能,%d*%d等于%d\n:",a,b,sunzi.multi(a,b));
					System.out.printf("加法是孙子的新增功能,%d+%d等于%d\n:",a,b,sunzi.add(a,b));
					System.out.printf("减法是孙子的新增功能,%d-%d等于%d\n:",a,b,sunzi.subs(a,b));
			}
		}
		

class A{
		private int x=10;
		protected int y=20;
		void f(){
				y=y+x;
				System.out.printf("x=%d,y+%d\n",x,y);
			}
	}
	class B extends A{
			void g(){
					y=y+1;
					System.out.printf("y=%d\n",y);
				}
		}
		class Example5_2{
				public static void main(String args[]){
						B b=new B();
						b.g();
						b.f();
						b.g();
					}
			}
		

class A{
		public double y=11.456789;
		public void f(){
				y=y+1;
				System.out.printf("y是double型变量,y=%f\n",y);
			}
	}
class B extends A{
		int y=0;
		public void g(){
			y=y+100;
			System.out.printf("y是int型变量,y=%d\n",y);
			}
	}
class Example5_3{
		public static void main(String args[])
		{
				B b=new B();
				b.y=200;
				b.g();
				b.f();
			}
	}
	
	



class A{
		protected double x=8.0,y=0.888888;
		public void speak(){
				System.out.println("我喜欢NBA");
			}
		public void cry(){
				y=x+y;
				System.out.printf("y=%f\n",y);
			}
	}
class B extends A{
		int y=100,z;
		public void speak(){
			z=2*z;
			System.out.println("I love This Game");
			}
	}
class Example5_4{
		public static void main(String args[]){
				B b=new B();
				b.cry();
				b.speak();
			}
	}


class A{
		public int f(int x,int y)
		{
				return x+y;
			}
	
	}
class B extends A{
		public int f(byte x,int y){
				return x*y;
			}
	}
public class Example5_5{
	public static void main(String args[]){
		int z=0;
		B b=new B();
		z=b.f(10,10);
		System.out.println(z);
		z=b.f((byte)10,10);
		System.out.println(z);
		}
	}


class A{
	int x,y;
	A(){
			x=100;
			y=200;
		}
	A(int x,int y)
	{
			this.x=x;
			this.y=y;
		}
	}
class B extends A{
		int z;
		B(int x,int y)
		{
				super(x,y);
				z=300;
			}
		B()
		{
			super();
			z=800;
			}
		public void f(){
			System.out.printf("x=%d,y+%d,z=%d\n",x,y,z);	
			}
	}	
public class Example5_6{
		public static void main(String args[]){
				B b1=new B(10,20);
				b1.f();
				B b2=new B();
				b2.f();
			}
	}
	


Bank.java
public class Bank{
		int savedMoney;
		int year;
		double interest;
		public double computerInterest(){
				interest=year*0.035*savedMoney;
				System.out.printf("%d元存在银行%d年的利息:%f元\n",savedMoney,year,interest);
				return interest;
			}
	}
ConstructionBank.java
public class ConstructionBank extends Bank{
	double year;
	public double computerInterest(){
		super.year=(int)year;
		double remainNumber=year-(int)year;
		int day=(int)(remainNumber*1000);
		interest=super.computerInterest()+day*0.0001*savedMoney;
		System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
		return interest;
		}
	}
BankOfDalian.java
public class BankOfDalian extends Bank{
		double year;
		public double computerInterest(){
				super.year=(int)year;
				double remainNumber=year-(int)year;
				int day=(int)(remainNumber*1000);
				interest=super.computerInterest()+day*0.0001*savedMoney;
				System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
				return interest;
			}
	}
Example5_7.java
public class Example5_7{
	public static void main(String args[]){
			int amount=5000;
			ConstructionBank bank1=new ConstructionBank();
			bank1.savedMoney=amount;
			bank1.year=5.216;
			double interest1=bank1.computerInterest();
			BankOfDalian bank2=new BankOfDalian();
			bank2.savedMoney=amount;
			bank2.year=5.216;
			double interest2=bank2.computerInterest();
			System.out.printf("两个银行的利息相差%f元\n",interest2-interest1);
		}
	}


class A{
	double n;
	int m;
	void f(){
			System.out.printf("子类的继承方法f(),n=%f,m=%d\n",n,m);
		}
	void g(){
			System.out.printf("你好,n=%f,m=%d\n",n,m);
		}
	}
class B extends A{
	int n=12;
	void g(){
			System.out.printf("子类重写方法g(),n=%d,m=%d\n",n,m);
		}	
	void cry(){
			System.out.printf("子类新增的方法,n=%d,m=%d\n",n,m);
		}
	}
public class Example5_8{
		public static void main(String args[])
		{
			A	a;
			a=new B();
			a.n=0.618;
			a.m=200;
			a.f();
			a.g();
			B b=(B)a;
			b.n=555;
			b.cry(); 
			}
}


class Animal{
	void cry(){};
}
class Dog extends Animal{
	void cry(){
	System.out.println("Wang!Wang!....");
	}
}
class Cat extends Animal{
	void cry(){
			System.out.println("miao~~miao~~....");
		}
	}
public class Example5_9{
		public static void main(String args[]){
				Animal animal;
				animal=new Dog();
				animal.cry();
				animal=new Cat();
				animal.cry();
			}
	}



abstract class Geometry{
		public abstract double getArea();
	}
class Pillar{
		Geometry bottom;
		double height;
		Pillar(Geometry bottom,double height){
				this.bottom=bottom;
				this.height=height;
			}
		void changeBottom(Geometry bottom){
				this.bottom=bottom;
			}
		public double getVolume(){
				return bottom.getArea()*height;
			}
	}	
class Lader extends Geometry{
		double a,b,h;
		Lader(double a,double b,double h){
			this.a=a;this.b=b;this.h=h;
			}
		public double getArea(){
				return(1/2.0)*(a+b)*h;
			}
	}
class Circle extends Geometry{
	 double r;
	 Circle(double r)
	 {
	 		this.r=r;
	 	}
	 public double getArea(){
	 		return(3.14*r*r);
	 	}
	}
public class Example5_10{
		public static void main(String args[]){
				Pillar pillar;
				Geometry tuxing;
				tuxing=new Lader(12,22,100);
				System.out.println("梯形的面积"+tuxing.getArea());
				pillar=new Pillar(tuxing,58);
				System.out.println("梯形的柱体体积"+pillar.getVolume());
				tuxing=new Circle(10);
				System.out.println("半径是10的圆的面积"+tuxing.getArea());
				pillar.changeBottom(tuxing);
				System.out.println("圆形底的柱体的体积"+pillar.getVolume());
			}
	}
	



interface Computable{
		final int MAX=100;
		int f(int x);
		public abstract int g(int x,int y);
	}
class A implements Computable{
		public int f(int x){
				return x*x;
			}
		public int g(int x,int y){
				return x+y;
			}
	}
class B implements Computable{
		public int f(int x){
				return x*x*x;
			}
		public int g(int x,int y){
				return x*y;
			}
	}
public class Example5_11{
		public static void main(String args[]){
				A a=new A();
				B b=new B();
				System.out.println(a.MAX);
				System.out.println(""+a.f(10)+""+a.g(12,6));
				System.out.println(b.MAX);
				System.out.println(""+b.f(10)+""+b.g(29,2));
			}
	}
	


interface showMessage{
		void showTradeMark();
	}
class TV implements showMessage{
		public void showTradeMark(){
				System.out.println("我是电视机");
			}
	}
class PC implements showMessage{
		public void showTradeMark(){
				System.out.println("我是电脑");
			}
	}
public class Example5_12{
		public static void main(String[] args)
		{
				showMessage sm;
				sm=new TV();
				sm.showTradeMark();
				sm=new PC();
				sm.showTradeMark();
			}
	} 
	
	




interface Show{
		void show();
	}
class A implements Show{
		public void show(){
				System.out.println("I love This Game");
			}
	}
class B implements Show{
		public void show(){
				System.out.println("我喜欢NBA");
			}
	}
class C{
		public void f(Show s){
				s.show();
			}
	}
public class Example5_13{
		public static void main(String args[])
		{
				C c=new C();
				c.f(new A());
				c.f(new B());
			}
	}



Geometry.java
public interface Geometry{
		public abstract double getArea();
	}
Pillar.java
public class Pillar{
		Geometry bottom;
		double height;
		Pillar(Geometry bottom,double height){
				this.bottom=bottom;
				this.height=height;
			}
		void changeBottom(Geometry bottom){
				this.bottom=bottom;
			}
		public double getVolume(){
				return bottom.getArea()*height;
			}
	}	
Lader.java
public class Lader implements Geometry{
		double a,b,h;
		Lader(double a,double b,double h){
			this.a=a;this.b=b;this.h=h;
			}
		public double getArea(){
				return(1/2.0)*(a+b)*h);
			}
	}
Circle.java
public class Circle implements Geometry{
	 double r;
	 Circle(double r)
	 {
	 		this.r=r;
	 	}
	 public double getArea(){
	 		return(3.14*r*r);
	 	}
	}
public class Example5_14{
		public static void main(String args[]){
				Pillar pillar;
				Geometry tuxing;
				tuxing=new Lader(12,22,100);
				System.out.println("梯形的面积"+tuxing.getArea());
				pillar=new Pillar(tuxing,58);
				System.out.println("梯形的柱体体积"+pillar.getVolume());
				tuxing=new Circle(10);
				System.out.println("半径是10的圆的面积"+tuxing.getArea());
				pillar.changeBottom(tuxing);
				System.out.println("圆形底的柱体的体积"+pillar.getVolume());
			}
	}
	



class NorthEast{
		String land="黑土地";
	}
class China{
		int x=10,y=10;
		LiaoNing dalian;
		China(){
				dalian=new LiaoNing();
			}
		void f(){
				System.out.println("我是中国");
				dalian.speak();
			}
		class LiaoNing extends NorthEast{
				int z;
				void speak(){
						System.out.println("我是大连,z="+z+":"+land);
					}
				void g(){
						z=x+y;
						f();
					}	
			}
	}
	public class Example5_15{
			public static void main(String args[]){
					China china=new China();
					china.f();
					china.dalian.g();
				}
		}
		


abstract class Student{
		abstract void speak();
	}
class Teacher{
		void look(Student stu)
		{
			stu.speak();
			}
	}
public class Example5_16{
		public static void main(String args[])
		{
				Teacher zhang=new Teacher();
				zhang.look(new Student()
				{
					void speak(){
						System.out.println("这是匿名类中的方法");
					}
				}
				);
			}
	}	
	


interface Show{
		public void show();
	}
	class A{
			void f(Show s){
					s.show();
				}
		}
	public class Example5_17{
			public static void main(String args[])
			{
					A a=new A();
					a.f(new Show(){
							public void show(){
									System.out.println("这是实现了接口的匿名类");
								}
						}
					);
				}
		}
		


public class Example5_18{
		public static void main(String args[]){
				int n=0,m=0,t=0;
				try{
						t=9999;
						m=Integer.parseInt("8888");
						n=Integer.parseInt("12s3a");
						System.out.println("我没有机会输出");
					}
				catch(Exception e){
				System.out.println("发生异常");
				n=123;
				}
				System.out.println("n+"+n+",m="+m+",t="+t);	
			}
	}
	
	



class MyException extends Exception{
		String message;
		MyException(int n)
		{
				message=n+"不是整数";
			}
		public String getMessage(){
				return message;
			}
	}
class A{
		public void f(int n)throws MyException{
				if(n<0){
						MyException ex=new MyException(n);
						throw(ex);
					}
				double number=Math.sqrt(n);
				System.out.println(n+"的平方根:"+number);
			}
	}
public class Example5_19{
		public static void main(String args[])
		{
				A a=new A();
				try{
						a.f(28);
						a.f(-8);
					}
				catch(MyException e)
				{
						System.out.println(e.getMessage());
					}
			}
	}	


class Chorus<E,F>{
		void makeChorus(E person,F yueqi){
				person.toString();
				yueqi.toString();
			}
	}
class 歌手{
		public String toString(){
				System.out.println("好一对美丽的茉莉花");
				return "";
			}
	}
class 乐器{
		public String toString(){
				System.out.println("|3 35 6 116|5 56 5-|");
				return "";
			}
	}
public class Example5_20{
		public static void main(String args[]){
				Chorus<歌手,乐器>model=new Chorus<歌手,乐器>();
				歌手 pengliyuan=new 歌手();
				乐器 piano=new 乐器();
				model.makeChorus(pengliyuan,piano);
			}
	}



class 锥<E>{
		double height;
		E bottom;
		public 锥(E b)
		{
			bottom=b;
			}
		public void computerVolume(){
				String s=bottom.toString();
				double area=Double.parseDouble(s);
				System.out.println("体积是:"+1.0/3.0*area*height);
			}
	}
class Circle{
		double area,radius;
		Circle(double r)
		{
				radius=r;
			}
		public String toString(){
				area=radius*radius*Math.PI;
				return ""+area;
			}	
	}
class Rectangle{
		double sideA,sideB,area;
		Rectangle(double a,double b)
		{
				sideA=a;
				sideB=b;
			}
		public String toString()
		{
				area=sideA*sideB;
				return ""+area;
			}
	}
public class Example5_21{
		public static void main(String args[]){
				Circle circle=new Circle(10);
				锥<Circle>coneOne=new 锥<Circle>(circle);
				coneOne.height=30;
				coneOne.computerVolume();
				Rectangle rect=new Rectangle(10,20);
				锥<Rectangle>coneTwo=new 锥<Rectangle>(rect);
				coneTwo.height=10;
				coneTwo.computerVolume();
			}
	}
	



interface Computer<E,F>{
		public void makeChorus(E x,F y);
	}
class Chorus<E,F>implements Computer<E,F>{
		public void makeChorus(E x,F y){
				x.toString();
				y.toString();
			}
	}
class 乐器{
		public String toString(){
				System.out.println("|5 6 3-|5  17 56|");
				return "";
			}
	}
class 歌手{
		public String toString(){
				System.out.println("美丽的草原,我可爱的家乡");
				return "";
			} 
	}
public class Example5_22{
		public static void main(String args[]){
				Chorus<歌手,乐器>model=new Chorus<歌手,乐器>();
				歌手 pengliyuan=new 歌手();
				乐器 piano=new 乐器();
				model.makeChorus(pengliyuan,piano);
			}
	}


以下是第五版《代码答案形式的C语言程序设计》第八章的例题答案: 8.1 ```c #include <stdio.h> #define N 5 int main() { int a[N], max, i; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); max = a[0]; for (i = 1; i < N; i++) if (a[i] > max) max = a[i]; printf("max=%d\n", max); return 0; } ``` 8.2 ```c #include <stdio.h> #define N 5 int main() { int a[N], i; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); printf("Reverse array a:\n"); for (i = N - 1; i >= 0; i--) printf("%d ", a[i]); printf("\n"); return 0; } ``` 8.3 ```c #include <stdio.h> #define N 5 int main() { int a[N], i; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); printf("a[0]=%d, a[%d]=%d\n", a[0], N - 1, a[N - 1]); return 0; } ``` 8.4 ```c #include <stdio.h> #define N 5 int main() { int a[N], i, sum = 0; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); for (i = 0; i < N; i++) sum += a[i]; printf("sum=%d, average=%.2f\n", sum, (float)sum / N); return 0; } ``` 8.5 ```c #include <stdio.h> #define N 5 int main() { int a[N], i, temp; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); for (i = 0; i < N / 2; i++) { temp = a[i]; a[i] = a[N - i - 1]; a[N - i - 1] = temp; } printf("Reverse array a:\n"); for (i = 0; i < N; i++) printf("%d ", a[i]); printf("\n"); return 0; } ``` 8.6 ```c #include <stdio.h> #define N 5 int main() { int a[N], i, j, temp; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); for (i = 0; i < N - 1; i++) for (j = i + 1; j < N; j++) if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } printf("Sort array a:\n"); for (i = 0; i < N; i++) printf("%d ", a[i]); printf("\n"); return 0; } ``` 8.7 ```c #include <stdio.h> #define N 5 int main() { int a[N], i, j, temp; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); for (i = 0; i < N - 1; i++) for (j = 0; j < N - i - 1; j++) if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } printf("Sort array a:\n"); for (i = 0; i < N; i++) printf("%d ", a[i]); printf("\n"); return 0; } ``` 8.8 ```c #include <stdio.h> #define N 5 int main() { int a[N], i; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); printf("Max 5 numbers in array a:\n"); for (i = 0; i < 5; i++) printf("%d ", a[i]); printf("\n"); return 0; } ``` 8.9 ```c #include <stdio.h> #define N 5 int main() { int a[N], i, j, temp; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); for (i = 0; i < N - 1; i++) for (j = 0; j < N - i - 1; j++) if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } printf("Median number in array a:\n"); printf("%d\n", a[N / 2]); return 0; } ``` 8.10 ```c #include <stdio.h> #define N 5 int main() { int a[N], i, j, temp; printf("Enter array a:\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); for (i = 0; i < N - 1; i++) for (j = 0; j < N - i - 1; j++) if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } printf("Mode number in array a:\n"); for (i = 0; i < N; i++) { int count = 0, k; for (j = i; j < N; j++) if (a[j] == a[i]) count++; for (k = i - 1; k >= 0; k--) if (a[k] == a[i]) break; if (k < 0 && count > 1) printf("%d ", a[i]); } printf("\n"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值