java中no1_Java程序设计实验(NO.1).doc
Java程序設計實驗(NO.1)
1、實驗目的:使用Java的String類操作字符串和子串。
寫一個程序可以對兩個字符串進行測試,判斷第一個字符串是否包含在第二個字符串中,例如字符串“op”包含在字符串“interoperabilityop”中。當第一個字符串包含在第二個字符串中時,顯示第一個字符串在第二個字符串中的起始位置;如果被多次包含,則一一列出各個起始位置;如果不包含,也給出相應的提示信息。
提示:利用API幫助文檔,查找并使用下面方法:
boolean regionMatches(int toffset, String other, int ooffset, int len)
public class StringTest {
public static void main(String[] arg){
String big="interoerabilityo";
String small=new String("o");
// String small2="a";
int i;
boolean tag;
boolean matchtag=false;
// if(small=="a") System.out.println("琵琶");
for(i=0;i
tag=big.regionMatches(i, small, 0, small.length());
if(tag) {
matchtag=true;
System.out.println("匹配的index為"+i);
}
}
if(matchtag==false) System.out.println("沒有匹配的");
}
}
2、實驗目的:掌握java中數組的使用。
實驗要求:
創建一個字符數組測試類 ArrayTest,在其main方法中:將5個元素的數組a 拷貝至8個元素的數組b中。數組a和b初始值均為空,a數組通過命令行輸入進行初始化,通過提示信息“準備把a 拷貝到b 數組中,請按任意鍵開始”,進行數組拷貝,完畢后,輸出b數組的內容。
提示:第一行寫import java.util.*; 利用Scanner 類的new Scanner(System.in) 和nextInt();
import java.util.*;
public class ArrayTest {
public static void main(String[] arg){
int [] a=new int[5];
int [] b=new int[8];
Scanner input=new Scanner(System.in);
System.out.println("請輸入數組a的元素,共計5個");
for(int i=0;i<5;i++){
a[i]=input.nextInt();
}
System.out.println("準備把a copy 到b 數組中,請按任意鍵開始");
for(int i=0;i<5;i++){
b[i]=a[i];
}
for(int i=0;i<5;i++){
System.out.println(b[i]+" ");
}
}
}
Java程序設計實驗(NO.2)
實驗目的:掌握類的定義、實例對象的創建、封裝、方法的使用,權限控制、static、final的使用。
1、 設計一個日期類(Date),它包含了3個變量:year、month、day,進行年、月、日的設置和讀取(讀取輸出格式:年/月/日)。注意:如果是閏年,則2月有29號。如果為非有效日期,則自動重置為1月或1日,并輸出錯誤提示信息。
要求:理解和填充下面程序框架,用3個日期對象進行程序測試:1997年13月4號,1999年2月29日,2000年2月29日。
public class Date {
private int year;
private int month;
private int day;
public Date(int year,int month,int day){//構造方法,以年、月、日為參數
setDate(year,month,day);
}
public void setDate(int year,int month,int day){ //設置date的年、月、日
this.year=year;
this.month=checkMonth(month);
this
總結
以上是生活随笔為你收集整理的java中no1_Java程序设计实验(NO.1).doc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android checkbox监听另一
- 下一篇: java 反射.问题_Java知识点总结