JAVA方法03之动手动脑问题解决

动手动脑1.当JAVA里定义的函数中去掉static后,怎么办?(如下程序,将square()函数的static去掉)

public class SquareIntTest {

    public static void main(String[] args) {
        int result;

        for (int x = 1; x <= 10; x++) {
            result = square(x);
            // Math库中也提供了求平方数的方法
             //result=(int)Math.pow(x,2);
            System.out.println("The square of " + x + " is " + result + "\n");
        }
    }

    // 自定义求平方数的静态方法
    //问:如果不加static,怎么办?
    public static int square(int y) {
        return y * y;
    }
}

因为static 是静态的意思,main是静态的,想加载的时候就加载了,可将函数写在一个新的类中即可,如下所示:

public class SquareIntTest {

    public static void main(String[] args) {
        int result;

        for (int x = 1; x <= 10; x++) {
            result = new Test().square1(x);//非静态
            System.out.println("The square of " + x + " is " + result + "\n");
        }
    }
}
class Test{
    public int square1(int y) {
        return y*y;
       
  }
}

动手动脑2.编写一个方法,使用纯随机数发生器生成指定数目(比如1000个)的随机整数。

(纯随机数发生器)

程序如下:

//博客园作业-编写一个方法,使用纯随机数产生器生成指定数目(比如1000个)的随机整数。
//李慧,2016.10.15

package boke;

import java.util.Scanner;

public class SuiJiShu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("(此系统用于产生制定数目n的随机整数)");
          System.out.printf("请输入n:");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        
        //纯随机数发生器
        int f1=1;
        for(int i = 1;i <= n; i++){
            f1=((7^5)*f1+0)% (2^10000);
        //输出结果
        System.out.print(f1+" ");
        if(i%10==0)
            System.out.println();
        }
    }
}

其运行结果:

  

 

动手动脑3.请看以下代码,你发现了有什么特殊之处吗?

// MethodOverload.java
// Using overloaded methods
//计算平方

public class MethodOverload {

    public static void main(String[] args) {
        System.out.println("The square of integer 7 is " + square(7));
        System.out.println("\nThe square of double 7.5 is " + square(7.5));
    }

    public static int square(int x) {
        return x * x;
    }

    public static double square(double y) {
        return y * y;
    }

此程序中出现了两个函数名相同的函数,经运行系统没有报错,答案也是正确的。这是因为,当具备①方法名相同;②参数类型不同,参数个数不同,或者是参数类型的顺序不同,这两个条件的时候,同名函数才是正确的。但是,方法的返回值并不作为方法重载的判断条件。


练习:查看一下JDK中System.out.println()方法,你发现了什么?

System是jdk自带的一个类,他有很多的方法,这些方法都是静态的,也就是static的。System.out.print()就相当于一个函数,它之所以可以不断的重复使用,是因为它可以有不同类型的参数,不同个数的参数,参数的顺序也可以不同。


课后作业1:使用计算机计算组合数

(1)使用组合数公式利用n!来计算

 程序如下:

package boke;

import java.math.BigInteger;
import java.util.Scanner;

public class ZuHeShu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("........求组合数C(n,k).........n为下标........");
        System.out.print("请输入n,k:");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
if(n<k)
    System.out.print("ERROR!");
   else
//n!=calculateN2(n); System.out.print("C("+n+","+k+")="+cN(n)/cN(k)/cN(n-k)); } public static long cN(int n) { if(n==1 || n==0){ return 1; } return n*cN(n-1); } }

运行结果是:

(2)使用递推的方法用杨辉三角形计算

package boke;

import java.util.Scanner;

public class ZuHeShu2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("(杨辉三角做法)........求组合数C(n,k).........n为下标........");
        System.out.print("请输入n,k:");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        if(n<k)
            System.out.print("ERROR!");
        else{
        //n!=calculateN2(n);
            //size = new Long(s);
            // long A = new Long(CN2(n+1,k)-CN2(n,k-1));
        System.out.print("C("+n+","+k+")=");
        System.out.println(CN2(n+1,k)-CN2(n,k-1));}
    }
    public static long cN(int n) {
        if(n==1 || n==0){
            return 1;
        }
        return n*cN(n-1);
    }
    
    //杨辉三角的方法递推计算
    public static long CN2(int n,int k){
        long A = cN(n)/cN(k)/cN(n-k);
        return A;
        
    }
    
}

其运行结果:

 

 (3)使用递归的方法,用组合数递推公式计算

 

package boke;

import java.util.Scanner;

public class ZuHeShu3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("(递归的方法利用公式)........求组合数C(n,k).........n为下标........");
        System.out.print("请输入n,k:");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        if(n<k || k<1)
            System.out.print("ERROR!");
        else{
        //n!=calculateN2(n);
        System.out.print("C("+n+","+k+")=");
        System.out.println(CN3(n,k));}
    }
    public static long cN(int n) {
        if(n==1 || n==0){
            return 1;
        }
        return n*cN(n-1);
    }
    
    //递归计算
    public static long CN2(int n,int k){
        if(n==1 || n==0){
            return 1;}
        return cN(n)/cN(k)/cN(n-k);    
    }
    
    public static long CN3(int n,int k){
        if(n == 1 || n == 0)
        {return 1;}
        return (CN2(n+1,k)-CN2(n,k-1));    
    }
}

其运行的结果:

       

其如果出现ERROR时,和上述两个例子是属于相同的情况。

 

 课后作业2:递归编程解决汉诺塔问题(用JAVA实现)

 

//disk是磁盘...sourcePeg源PEG...destinationPeg目标PEG...tempPeg临时PEG

package work;

public class Hanoi
{
   // 递归移动塔之间的磁盘
   public static void solveTowers( int disks, int sourcePeg, 
      int destinationPeg, int tempPeg )
   {
      // 移动的时候
      if ( disks == 1 )
      {
         System.out.printf( "\n%d ——> %d", sourcePeg, destinationPeg );
         return;
      }

      // 递归步骤--移动(磁盘1)磁盘sourcepeg
      // to tempPeg using destinationPeg
      solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );

      // 最后从sourcepeg到destinationpeg移动磁盘
      System.out.printf( "\n%d ——> %d", sourcePeg, destinationPeg );

      // 移动(磁盘1)从临时peg到目的peg磁盘
      solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
   }
   
   public static void main( String[] args )
   {
      int startPeg = 1; // 值1表示startpeg输出
      int endPeg = 3; // 值3表示endpeg输出 
      int tempPeg = 2; // 值2表示temppeg输出
      int totalDisks = 3; //有几个托盘
      
     
      //移动所有磁盘。
      solveTowers( totalDisks, startPeg, endPeg, tempPeg );
   }
}

其结果截图:

课后作业3:使用递归方式判断某个字串是否是回文( palindrome )

 

package boke;

import java.util.Scanner;

public class HuiWeiShu {
    public static void main(String[] args) {
        String S = " ";
        System.out.println("(此程序用于判断某个字符串s是否是回文)");
        System.out.println("请输入字符串S:");
        Scanner scan = new Scanner(System.in);
        S = scan.next();
        //输出结果
        System.out.println(ishui(S));
    }
    public static boolean ishui(String S){
        int L = S.length();//字符串的长度
        if(L<=1){
            return true;}//字符串是回文
        else if(S.charAt(0)!=S.charAt(L-1)){
                return false;
    }
    return ishui(S.substring(1,L-1));
}}

 

其运行结果:

经运行,其结果正确。

 

posted @ 2016-10-15 16:29  夏小暑  阅读(289)  评论(0编辑  收藏  举报