第五章例题

一,5.1
 

package england. people;
import american.people.Son;
import japan. people. Grandson;
public class Example5_1 {
    public static void main(String args[]){
        Son son = new Son();
        Grandson grandson = new Grandson();
        son. height = 180;
        son. hand =“一双大手”;
        grandson. height = 155;
        grandson. hand =“一双小手”;
        grandson. foot ="一双小脚";
        String str = son.getHand();
        System.out.printf("儿子:*s,* d\n",str, son. height);
        System.out.printf("孙子:*s,*s,*d\n",str,grandson. foot,grandson. height);
        str = grandson. getHand();
       }
}

 5.3

class People {
    public double x;
    public void setX(double x){
        this.X=x;
    }
    public double getDoublex(){
        return x;
    }
}
class Student extends People {
    int x;
    public int getX(){
        return x;
    }
}
public class Example5_3 {
    public static void main(String args[]){
        Student stu= new Student();
        stu.x= 98;
        System. out. println("对象 stu 的x的值是:"+ stu.getX());
        stu.setX(98.98);
        double m = stu. getDoubleX();
        System, out. println("对象 stu隐藏的x的值是:"+m);
    }
}

5.4

class A {
    double f(float x,float y)(
        return X+y;
    }
    public int g(int x, int y){
        return x + y;
    }
}
class B extends A {
    double f(float x,float y){
        return X* y;
    }
}
public class Example5_4 {
        public static void main(String args[]){
        B b= new B();
        double result = b.f(5,6);
        System.out.println("调用重写方法得到的结果:"+ result);
        int m= b.g(3,5);
        System.out. println("调用继承方法得到的结果:"+m);
    }
}

5.6

public class Example5_6 {
    public static void main(String args[]){
        UniverStudent zhang = newUniverStudent(20111,"张三",false);
        int number = zhang. getNumber();
        String name = zhang. getName();
        boolean marriage=zhang.getIsMarriage();
        System. out. println(name +"的学号是:"+ number);
        if(marriage == true){
            System. out. println(name +"已婚");
        }
        elset{
            System.out. println(name +"未婚");
            }
        }
}

5.7

public class Bxamples_7 {
    public static void main(String args[]) {
        Average aver = new Average();
        aver. n= 100.5678;
        double result1=aver.f();
        double result2= aver.g();
        System.out.println("result1="+ result1);
        System., out.println("result2="+ result2);
    }
}

5.9

public class Example5_9 (
    public static void main(String args[]) {
        People people= new People();
        Anthropoid monkey = people;
        monkey. crySpeak("I love this game");
        //monkey.n = 100;
        //monkey.computer(12,19);
        System. out.println(monkey.m);
        System.out.println(people.m);
        People zhang =(People)monkey;
        zhang.computer(55,33);
        zhang.m ='T';
        System. out. println(zhang.m);
    }
}

5.11

abstract class GirlFriend { 
    abstract void speak();
    abstract void cooking();
}
class ChinaGirlFriend extends GirlFriend {
    void speak(){
        System.out.println("你好");
    }
    void cooking(){
        System.out.println("水煮鱼");
        }
}
class AmericanGirlFriend extends GirlFriend {
    void speak(){
        System.out.println("hello");
    }
    void cooking(){
        System.out.println("roast beef");    
    }
}
class Boy {
    GirlFriend friend;
    void setGirlfriend(GirlFriend f){
        friend = f;
    }
    void showGir1Friend(){
        friend.speak();
        friend.cooking();
    }
}
public class Example5_11 {
    public static void main(String args[]){
        GirlFriend girl = new ChinaGirlFriend();
        Boy boy = new Boy();
        boy.setGirlfriend(girl);
        boy, showGirlFriend();
        girl=_new AmericanGirlFriend();
        boy.setGirlfriend(girl);
        boy.showGirlFriend();
    }
}

5.13

public class Example5_13 {
    public static void main(String args[]){
        Simulator simulator = new Simulator();
        simulator.playSound(new Dog());
        simulator.playSound(new Cat());
    }
}

5.14

Public class Example5_14 {
    publ ic static void main(String args[]){
        System.out. println("接口中的常量"+ AAA.MAX);
        AAA a = new AAA();
        System.out. println("调用 on 方法(重写的):");
        a.on();
        System. out.println("调用 sum 方法(重写的):"+ a. sum(12,18));
        System. out. println("调用接口提供的 default 方法"+ a.max(12,78));
        Printable.f();
    }
}

第五题

(1)

class A {
    public final void f(){
        char cStart ='a',cEnd='z';
        for(char c= cStart;c<= cEnd;c++){
        System.out.print(""+c);
        }
    }
}
class B extends A {
    public void g(){
        char cStart ='a',cEnd='w';
            for(char c =cStart;c<= cEnd;c++){
                System. out.print(""+c);
        }
    }
}
public class E {
    publ ic static void main (String args[ ]) {
        B b = new B();
        b.f();
        b.g();
    }
}

(2)

class A {
    public final void f(){
        char cStart ='a',cEnd='z';
        for(char c=cStart;c<= cEnd;c++){
            System.out. print(""+c);
        }
    }
}
class B extends A {
    public void g(){
        char eStart ='a', cEnd ='w';
        for(char c=cStart;c<= cEnd;c++){
            System.out.print(""+c);
        }
    }
}
public class B{
    public static void main (String args[ ]) {
        B b= new B();
        b.f();
        b.g();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是第五版《代码答案形式的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、付费专栏及课程。

余额充值