Java作业

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;

     and =".son.hand ="一双大手";

     grandson.height = 155ight = 155;

     grandson.hand = "一双小手";

     grandson.foot="一双小脚";

     String str = son.getHand();

     System.out. printf("儿子:%s,% d\n",str,son.height);

     str = grandson.getHand();

     System. out. printf("孙子:% s,%s,% d\n",str,grandson.foot,grandson.height);

     }

}

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(){

       //x=20.56; //非法,因为子类的x已经是int型,不是double型

       return x;           

    }

}

public class Example5_3{

  public static void main(String args[]) {

      Student stu=new Student();

      stu.x=98; //合法,子类对象的x是int型

      System.out.println("对象stu的x的值是:"+stu.getX());

      //stu.x=98.98; //非法,因为子类对象的x已经是int型

      stu. setX(98.98); //子类对象调用继承的方法操作隐藏的double型变量x

      double m= stu.getDoublex(); //子类对象调用继承的方法操作隐藏的 double 型变量x 

      System.out.println("对象 stu隐藏的x的值是:"+m);

  }

}

class A{

     double f(float x,float y) {

         return x+y;

     }

     public int g( int x, intint 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); //b调用重写的方法

       System.out.println("调用重写方法得到的结果:" +result);

       intm=b.g(3,5); //b调用继承的方法

       System.out.println("调用继承方法得到的结果:"+m);

     }

}

public class Example5_6{

    public static void main(String args[]){

        UniverStudent zhang = new UniverStudent(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 +"已婚");

        }

        else{

          System. out. println(name +"未婚");

        }

    }

}

public class Example5_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(1n("result1 = "+result1);

       System. out.printlr1n("result2 = "+ result2);

    }

}

public class Example5_9 {

    public static void main(String args[]) {

       People people = new People() ;

       Anthropoid monkey = people; //monkey是people对象的上转型对象

       monkey.crySpeak("I love this game"); //等同于 people调用重写的crySpeak 方法

       //monkey.n=100; //非法,因为n是子类新增的成员变量

       //monkey.computer(12,19); //非法,因为computer()是子类新增的方法

       System.out.println(monkey.m); //操作隐藏的m,不等同于 people.m

       System. out. println(people.m); //操作子类的m

       People zhang = (People)monkey; //把上转型对象强制转化为子类的对象

       zhang. computer(55,33); //zhang是子类的对象

       zhang.m = 'T'; //操作子类声明的成员的变量m

       System.out.println(zhang.m);

    }

}

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 showGirlFriend(){

       friend.speak();

       friend.cooking();

    }

}

public class Example5_11{

    public static void main(String args[ ]) {

       GirlFriend girl = new ChinaGir1Friend(); //gir1是上转型对象

       Boy boy = new Boy();

       boy. setGirlfriend(gir1);

       boy.showGirlFriend();

       girl = new AmericanGirlFriend(); //gir1是上转型对象

       boy.setGirlfriend(gir1);

       boy.showGirlFriend();

    }

}

public class Example5_13 {

    public static void main(String args[]) {

       Simulator simulator = new Simulator() ;

       simulator. playSound(new Dog()) ;

       simulator.playSound(new Cat());

    }

}

public class Example5_14{

    public static void main(String args[]) {

       AAA a = new AAA() ;

       System.out.println("接口中的常量"+ AAA. M" +AAA. MAX);

       System. out. println("调用on方法(重写的):")

       a.on();

       System.out.println("调用 sum方法(重写的):"+a.sum(12,18));

       System.out.println("调用接口提供的 default 方法"+a.max(12,78); 

       Printable.f();

    }

}

public class A {

    public void f()

    {

        for (int i = 97; i <123; i++)

        {

            System.out.print((char)i);

 

        }

        System.out.println();

    }

 

}

package zimu;

 

public class B extends A {

 

    public void g()

    {

        for (int i = 65; i <91; i++)

        {

            System.out.print((char)i);

 

        }

        System.out.println();

    }

 

}

 

public class C {

 

    public static void main(String[] args) {

    A a=new A();

    a.f();

    B b=new B();

    b.f();

    b.g();

    }

}

class A{

    public int f(int a, int b){

        if(b<a){

        int temp=0;

        temp=a;

        a=b;

        b=temp

        int r=bsa;tr=bx

        0){hile(r!=0)

        b=a;

        a=r;

        r=bs

       =b8a;

    }

    return a;

    }

}

class B extends A{

    public int f(int a,int b) {

        int division = super. f(a,b);

         return (a* b)/division;

}

public class E{

           public static void main (String args[ ]) {

               Aa=new A();

               System.out.println("最大公约数:"+a.f(36,24

               a= new B();

               System.out.println("最小公倍数:" + a.f(36,24

           }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用来完成大作业的。文档内容: 1 Java技术体系 1.1 Java语言 1.2 Java平台 1.3 Java应用领域 2 Java语言的技术特点 2.1 1 2.2 2 2.3 3 3 Java语言与C++的异同分析总结。 4 选用C和java语言时编程算法程序有什么不同,有什么优势和劣势。 5 自己编程学习的级别和状态。以及自己以后的编程学习的计划和想法。 6 下面3道题目中选一道,给出算法分析和程序。 1)“黄金分割数”在我们的生活中很常见,但是在不同的应用领域,要求的精度也不一样。 例如:三位小数是0.618 现在我们需要你能求出保留100位小数的黄金分割数,采用的算法为“分层计算法”: 黄金数= 1 --------------- 1+ 1 ------------- 1+ 1 ----------- 1+ 1 --------- ..... 注意,计算出的结果,如果第100位为0也需要保留。 2)已知一个数列: 5,2,4,3,7,6 那么,在这个数列中存在这样一些“连续数”,例如:5,2,4,3这个子数列排序后是连续的。同样2,4,3也是连续的,为了方便表示 我们使用下标来标识,这样,这个数列中存在以下“连续数”: [1,1] [1,4] [1,6] [2,2] [2,4] [3,3] [3,4] [4,4] [5,5] [5,6] [6,6] 这样,他就存在11个“连续数”。现在需要你在用户找出一个数组中所有的“连续数”。 要求: 1、用户输入一个整数N,表示下面数组的个数 2、用户每输入一行作为一个数组 如: 用户输入: 1 5,2,4,3,7,6 程序输出: 11 3)有一种数我们称之为幸运数,它的特点是这样的,首先,由自然数按顺序排列: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 … 这样,1比较特殊, 1为第一个幸运数,那么,我们移除掉序号能被2整除的数(注意:是序号,而不是数本身,每次移除后都重新排序)就剩下: 1 3 5 7 9 11 13 15 17 19… 3为第二个幸运数,那么我们需要去掉序号能被3(下一次是除4,然后是5,每次加1)整除的数,5 11 17...剩下: 1 3 7 9 13 15 19… 那么7为第三个幸运数,后面的幸运数,依此类推,移除之后剩下的数字都是幸运数。 现在我们需要你求出给定的m和n之间的幸运数的个数: 例如:给定1 20,那么个数为:5(5个幸运数分别是1,3,7,13,19) 现在要求用户输入两个数m和n(m<n<=1000*1000),输出幸运数的个数。 例如: 用户输入: 1 20 程序输出: 5 格式:小四,1.5倍行距
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值