JAVA上机之一维数组和二维数组 第2页

Test1:

package fox.math.kmust;

 

public final class Test1 {

   private static void print(Object obj) {

      if(obj==null){

         System.out.println("null");

         return;

      }

      if (Object[].class.isInstance(obj)) {

         for (Object temp : (Object[]) obj) {

            print(temp);

         }

         return;

      }

      if (String.class.isInstance(obj)) {

         String temp=(String) obj;

         System.out.println(temp+"\t--Lower Case--\t"+temp.toLowerCase());

      } else {

         System.out.println(obj.toString());

      }

   }

  

   public static void main(String[] args){

      String[] stringArray1[]=new String[2][];

      stringArray1[0]=new String[13];

      stringArray1[1]=new String[13];

      for(int i=0,j=stringArray1[0].length;i<j;i++){

         stringArray1[0][i]=((char)('A'+i))+"";

         stringArray1[1][i]=((char)('A'+i+13))+"";

      }

      print(stringArray1);

   }

}

 

Test2:

package fox.math.kmust;

 

public class Test2 {

   private static class Rectangle {

      private double length = 0.0;

      private double width = 0.0;

 

      public Rectangle(double length, double width) {

         if (length < 0) {

            length = 0.0;

         }

         if (width < 0) {

            width = 0.0;

         }

         this.length = length;

         this.width = width;

      }

 

      public double getArea() {

         return length * width;

      }

 

      public Rectangle() {

      }

 

      public double getGirth() {

         return 2 * (length + width);

      }

   }

 

   public static void main(String[] args) {

      Rectangle rectangle = new Rectangle(2.0, 4.0);

      System.out.printf("Area:%.2f\n", rectangle.getArea());

      System.out.printf("Girth:%.2f\n", rectangle.getGirth());

   }

}

 

Test3:

package fox.math.kmust;

 

public class Test3 {

   private static class Circle {

      private double radius = 0.0;

 

      public Circle() {

      }

 

      public double getArea() {

         return Math.PI * radius * radius;

      }

 

      public Circle(double radius) {

         if (radius < 0) {

            radius = 0.0;

         }

         this.radius = radius;

      }

 

      public double getGirth() {

         return 2 * Math.PI * radius;

      }

   }

 

   public static void main(String[] args) {

      Circle circle = new Circle(3.0);

      System.out.printf("Area:%.2f\n", circle.getArea());

      System.out.printf("Girth:%.2f\n", circle.getGirth());

   } 

}

上一页  [1] [2] [3] 下一页

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有