当前位置:课程学习>>第五章 常用类>>实践活动




同学们,实践是检验真理问题的唯一标准,运用你们学到的知识实践检验一下能力提升了吗?

实践主题1:数组的用法

实践目标1:掌握数组的常用方法。

实践要求1:编写一个Java应用程序(YangHui.java),输出杨辉三角形,输出的行数由程序的参数给定,如:java J_Test 10,则输出杨辉三角形的前10行。杨辉三角形的第1行共有两个数,均为1。杨辉三角形的第i(i=2,3,……)行共有(i+1)个数,其首尾两个数均为1。中间的数(j)均为前一行(i-1)相邻的两个数(j-1与j)的和。下面给出杨辉三角形的前5行:

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

教师分析:

这个实践主要是加强对Java数组的掌握。程序考核的是Java数组的使用。参考代码如下:

// 数组的用法  YangHui.java

public class YangHui {

  public static void main(String[] args) {

   int num = 1;

   int i, j;

   if (args.length < 1) {

      System.out.println("请在命令行中输入杨辉三角形的行数,例如:java YangHui 10");

      System.exit(0);

   }

   try {

     num = Integer.parseInt(args[0]);

     if (num < 1) {

       System.out.println("行数请输入一个大于0的数!");

       System.exit(0);

     }

   } catch (Exception e) {

     System.out.println("请输入数字!");

     System.exit(0);

   }

   int[] firstList = new int[num + 1];

   int[] lastList = new int[num + 2];

   for (i = 0; i <= 1; i++)

     firstList[i] = 1;

     lastList[0] = 1;

   for (i = 1; i <= num; i++) {

     for (j = 0; j <= i; j++) {

       System.out.print(String.format("%6d", firstList[j]));

     }

     System.out.println();

   for (j = 1; j <= num; j++) {

     lastList[j] = firstList[j - 1] + firstList[j];

    }

     lastList[num] = 1;

     System.arraycopy(lastList, 0, firstList, 0, num + 1);

   }

  }

}

 

实践主题2:String 类的常用方法

实践目标2:掌握String类的常用方法。

实践任务2:按模板要求,将[代码]处替换为Java程序代码

实践任务2:编写一个Java应用程序,判断两个字符串是否相同,判断字符串的前缀、后缀是否与某个字符串相同,按照字典序比较两个字符串的大小关系,字符串检索,创建子字符串,将数字型字符串转化为数字,将字符串存放到数组中,用字符数组创建字符串。

StringExample.java

   class StringExample {

     public static void main(String[] args) {

       String s1=new String("you are a student"),s2=new String("how are you");

       if( [代码1] ) //判断s1与s2是否相同

         System.out.println("s1与s2相同");

       else

         System.out.println("s1与s2不相同");

      String s3=new String("22030219851022024");

      if( [代码2] ) //判断s3的前缀是否为"220302"

         System.out.println("吉林省的身份证");

         String s4=new String("你"),s5=new String("我");

      if( [代码3] ) //按照字典序s4大于s5的表达式

         System.out.println("按字典序s4大于s5");

      else

         System.out.println("按字典序s4小于s5");

      int position=0;

      String path="d:\\myjava\\jsp\\A.java";

      position=[代码4] //获取path中最后出现的目录分隔符号的位置

      System.out.println("d:\\myjava\\jsp\\A.java中最后出现\\的位置:"+position);

      String fileName=[代码5] //获取path中A.java子字符串

      System.out.println("d:\\myjava\\jsp\\A.java中含有的文件名:"+fileName);

      String s6=new String("1000"),s7=new String("123.678");

      int n1=[代码6] //将s6转化成int型数据

      double n2=[代码7] //将s7转化成double型数据

      double n=n1+n2;

      System.out.println(n);

      String s8=new String("ABCDEF");

      char a[]=[代码8] //将s8存放到数组a中

      for(int i=a.length-1;i>=0;i--)

        System.out.print(" "+a[i]);

   }

 }   

教师分析:

这个实践主要是加强对String类的使用。程序考核的是String方法的使用。参考代码如下:

if( s1.equals(s2) )             //[代码1]

if( s3.startsWith("220302") )    //[代码2]

if( s4.compareTo(s5)>0 )       //[代码3]

position=path.lastIndexOf("\\");  //[代码4]

String fileName=path.substring(position+1);   //[代码5]

int n1=Integer.parseInt(s6);                 //[代码6]

double n2=Double.parseDouble(s7);          //[代码7]

char a[]=s8.toCharArray();                 //[代码8]

 

实践主题3:向量。

实践目标3:掌握向量的常用方法。

实践要求3:编写一个Java应用程序VectorTest.java,要求使用向量Vector来实现统计字符出现的次数:由程序的参数给定一个字符串,然后由程序统计并输出在该字符串中每个字符出现的次数。

教师分析:

这个实践主要是加强对Vector类的使用。参考代码如下:

package com.shengsiyuan3;

import java.util.Vector;

public class VectorTest

{

   public static void main(String[] args)

   {

      String str="hello world";

      Vector vector = new Vector();

      for(int i=0;i<str.length;i++)

          vector.add(str[i]);

      for(int i = 0; i < vector.size(); i++)

      {

         System.out.println(vector.get(i));

      }

   }

}

进入拓展资源