Java基本類庫和集合框架的使用(java的集合框架有哪些)
目錄
一、實驗目的
二、實驗代碼
1. 按照身份證判斷是否過生日
2.提取字符串里面的數字
3. 身份證隱藏信息
4.字符串實現大小寫轉換
5.字符串加密與解密
6.銀行卡卡號格式化
7. 編寫程序,輸入一個英文句子,將每個單詞的首字符改成大寫后再輸出。
8. 編寫程序,產生隨機電話號碼,電話號碼的前五位是15923,后六位隨機產生。
9. 編寫程序,隨機產生一個4位的驗證碼,驗證碼可以包含英文字符的大小和數字。
10. 運用面向對象的程序設計思想,把表1中的學生信息存儲在List集合(ArrayList或者LinkedList)中。編寫程序實現下列功能:
11. 編程模擬用戶注冊功能
12. 利用Map存放用戶名和密碼,請給其中的某個用戶修改密碼,請輸出所有的賬號和其對應的密碼。
每文一語
官方Java幫助手冊文檔,點擊這里下載
一、實驗目的
1. 掌握String類和StringBuffer類的使用;
2. 掌握包裝類的使用;
3. 掌握集合List、Set和Map的使用;
4. 掌握迭代器Iterator的使用;
5. 了解泛型的作用以及基本使用方法。
二、實驗代碼
1. 按照身份證判斷是否過生日
編寫程序:輸入一個18位的身份證號碼,然后根據其中的出生日期信息判斷是否已過生日?并輸出以下三個信息中的一個。
This year's birthday has passed.
This year's birthday has not passed.
Today is birthday.
package 作業(yè)練習.test3;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Birthday {
public static void main(String[] args) {
System.out.println("輸入您的身份證號:");
Scanner reader = new Scanner(System.in);
String str = reader.nextLine();
String[] sNum ={str};
int[] birthTime = new int[1];
birthTime[0] = Integer.parseInt(sNum[0].substring(10, 14));
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");//設置格式
Date date = new Date();//表示當前時間,無法單獨輸出
int now = Integer.parseInt(sdf.format(date));//方法
System.out.println(now);
// 輸出
for (int i = 0; i < 1; i++) {
System.out.println(sNum[i]);
if (now > birthTime[i]) {
System.out.println("This year's birthday has passed.");
} else if (now < birthTime[i]) {
System.out.println("This year's birthday has not passed.");
} else if (now == birthTime[i]) {
System.out.println("Today is birthday.");
}
}
}
}
2.提取字符串里面的數字
編寫程序剔除一個字符串中的全部非數字字符,例如:將形如“A1BC2you3”中的非數字字符全部剔除,得到字符串“123”,并將“123”轉換為int型并輸出,如果輸入字符串中不含數字,則輸出“無數字”。
package 作業(yè)練習.test3;
import java.util.Scanner;
public class StringToInt {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String str = reader.nextLine();
String newStr = "\D+";//\d 是一個正字表達式,標識所有數字及0-9
str = str.replaceAll(newStr, "");
if (str.length() > 0) {
int n = Integer.parseInt(str);//parseInt() 方法用于將字符串參數作為有符號的十進制整數進行解析。
System.out.println(n);
} else {
System.out.println("無數字");
}
}
}
3. 身份證隱藏信息
編寫程序將18位的身份號證號碼中表示年份的信息顯示為*,其余的字符保持不變(例如:將身份證號碼10013319961213602X轉換為100133****1213602X顯示)。
注意:請分別采用StringBuffer類和String類的方法實現。
package 作業(yè)練習.test3;
import java.util.Scanner;
public class Shenfenzheng {
public static void main(String[] args) {
System.out.println("輸入您的身份證號:");
Scanner reader = new Scanner(System.in);
String str = reader.nextLine();
StringBuilder sb = new StringBuilder(str);
sb.replace(6, 10, "****");
System.out.println(sb.toString());
}
}
4.字符串實現大小寫轉換
編寫程序實現,大小寫字母的切換,其他字符不變(例如:輸入字符串“I love Java!”則輸出為“i LOVE jAVA!”)。
package 作業(yè)練習.test3;
import java.util.Scanner;
public class Upper {
public static void main(String [] args)
{
System.out.println("輸入字符:");
String str1 = new Scanner(System.in).nextLine(); //輸入字符串
String str2 = new String();
char ch;
for(int i=0;i { if(Character.isUpperCase(str1.charAt(i))) { ch = Character.toLowerCase(str1.charAt(i)); str2+=ch; }else if(Character.isLowerCase(str1.charAt(i))) { ch =Character.toUpperCase(str1.charAt(i)); str2+=ch; }else { ch =str1.charAt(i); str2+=ch; } } System.out.println(str2); } } 5.字符串加密與解密 請設計一個類,封裝兩個靜態(tài)方法,分別是字符串加密方法、字符串解密方法。要求: 在對一個指定的字符串加密之后,利用解密方法數能夠對密文解密,顯示明文信息。加密的方式是將字符串中每個字符加上它在字符串中的位置和一個偏移值 5。以字符串“china”為例,第一個字符“c”在字符串中的位置為 0,那么它對應的密文是“'c'+0+5",即 h。按照這個規(guī)則字符串“china”加密為“hnpvj”。 package 作業(yè)練習.test3; import java.util.Scanner; public class Jiami { public static void main(String [] args) { Scanner scan =new Scanner(System.in); char secret ='8';//加密文字符 System.out.println("請輸入你要加密的內容"); String pass =scan.next(); System.out.println("原字符串的內容" +pass); String secrerult=secret(pass,secret); System.out.println("加密后的內容"+secrerult); String uncrypresult = secret(secrerult,secret); System.out.println("解密后的內容"+uncrypresult); } public static String secret(String value,char secret) { byte[] bt =value.getBytes();//將其轉化為字符數組 for(int i=0;i { bt[i]=(byte)(bt[i]^(int)secret); //兩次位或運算返回 } String newresult =new String(bt,0,bt.length); return newresult; } } 6.銀行卡卡號格式化 編寫程序,實現輸入銀行卡號,以每4個字符加一個空格顯示。例如,假如銀行卡號是62220630108589564785130,則輸出為6222 0630 1085 8956 4785 130。 package 作業(yè)練習.test3; import java.util.*; public class str6 { public static void main(String[] args) { System.out.println("請輸入你的銀行卡號:"); String bank = new Scanner(System.in).next(); char[] id = bank.toCharArray(); String changeid = new String(); for (int i = 0; i < id.length; i++) { if (i % 4 == 0 && i > 0) { changeid += ' '; } changeid += id[i]; } System.out.println(changeid); } } 7. 編寫程序,輸入一個英文句子,將每個單詞的首字符改成大寫后再輸出。 package 作業(yè)練習.test3; import java.util.Scanner; public class Exer07 { public static void main(String[]args) { System.out.println("輸入一個英文句子"); String in =new Scanner(System.in).nextLine(); String [] s=in.split(" "); String str2 =new String(); for(int i=0;i { s[i]=s[i].substring(0,1).toUpperCase()+s[i].substring(1); if(i==s.length-1) { str2=str2+s[i]; }else { str2=str2+s[i]+' '; } } System.out.println(str2); } } 8. 編寫程序,產生隨機電話號碼,電話號碼的前五位是15923,后六位隨機產生。 package 作業(yè)練習.test3; import java.util.Random; public class Exer08 { public static void main(String[] args) { String firtst_five = "15923"; String lastsix =new String(); String Phone =new String(); Random rnd =new Random(); for(int i=0;i<6;i++) { int nmber =rnd.nextInt(10); lastsix += nmber; } Phone =firtst_five+lastsix; System.out.println(Phone); } } 9. 編寫程序,隨機產生一個4位的驗證碼,驗證碼可以包含英文字符的大小和數字。 package 作業(yè)練習.test3; import java.util.Random; class Random_1 { public String string() { String ZiMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890"; String result = ""; Random random = new Random(); for (int i = 0; i < 4; i++) { int index = random.nextInt(ZiMu.length()); char c = ZiMu.charAt(index); result += c; } return result; } public static void main(String[] args) { Random_1 test2 = new Random_1(); System.out.println(test2.string()); } } 10. 運用面向對象的程序設計思想,把表1中的學生信息存儲在List集合(ArrayList或者LinkedList)中。編寫程序實現下列功能: 表1? 學生信息 學號 姓名 出生日期 201644001 張三 1997年2月3日 201644002 李四 1998年11月11日 201644003 王五 1996年3月2日 201644004 趙六 1996年12月12日 201644005 周正 1997年10月10日 (1)創(chuàng)建Student學生類,類中包括三個成員變量,分別為學號、姓名、出生日期,添加不帶參數的構造方法,添加含以上三個參數的構造方法,添加各成員變量的 Getter和Setter方法,改寫toString()方法(返回字符串格式:學號 姓名 出生日期) (2)將表1中的學生依次添加到List中; (3)輸出所有的學生信息(使用迭代器Iterator); (4)有學生(學號:201644006,姓名:李明,出生日期:1998年1月12日)中途入學,將其添加到List集合中; (5)有學生“王五”中途退學,將其從List集合中刪除; (6)再一次遍歷輸出所有的學生信息(使用foreach語句)。 (7)輸出年齡最小的學生的學號、姓名和出生日期(考慮最小年齡的學生不止一個的情況 package 作業(yè)練習.test3; import java.util.*; public class Students { public String id;//學號 public String name;//姓名 public String birth;//出生日期 Students(){} //不帶參數的構造方法 Students(String id, String name, String birth) //帶三個參數的構造方法 { this.id = id; this.name = name; this.birth = birth; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirth() { return birth; } public void setBirth(String birth) { this.birth = birth; } public static void addStudent(List { Scanner sc = new Scanner(System.in); boolean flag = false; String newStudentid; while (true) { System.out.println("請輸入學號:"); newStudentid = sc.nextLine(); for (int i = 0; i < list.size(); i++) { Students s = list.get(i); if (s.getId().equals(newStudentid)) { flag = true; break; } } if (flag == true) { System.out.println("輸入的學生已存在,請重新輸入"); } else { break; } } System.out.println("請輸入學生的姓名"); String newStudentname = sc.nextLine(); System.out.println("請輸入學生的生日"); String newStudentbirth = sc.nextLine(); Students newStudent = new Students(); newStudent.setId(newStudentid); newStudent.setBirth(newStudentbirth); newStudent.setName(newStudentname); list.add(newStudent); } public static void print1(List { Iterator while (it.hasNext()) { Students st = it.next(); System.out.println("學號: " + st.id + "姓名: " + st.name + "生日: " + st.birth); } } public static void print2(List { for (Students obj : list) { Students st = obj; System.out.println("學號: " + st.id + "姓名: " + st.name + "生日: " + st.birth); } } public static void remove(List System.out.println("請輸入你要移除的學生的姓名"); String movename = new Scanner(System.in).nextLine(); int index = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).equals(movename)) { index = i; } } list.remove(index); } public static void main(String[] args) { List while (true) { System.out.println("-----------歡迎進入學生信息管理系統-----------"); System.out.println("請選擇相關選項對學生信息進行管理...\n"); System.out.println("1.查看所有學生信息"); System.out.println("2.添加學生信息"); System.out.println("3.刪除學生信息"); System.out.println("4.退出系統\n"); Scanner sc = new Scanner(System.in); // 接收選項數據 System.out.println("請輸入你的選項:"); String option = sc.nextLine(); switch (option) { case "1": print2(list); break; case "2": addStudent(list); break; case "3": remove(list); break; case "4": System.out.println("退出系統成功!"); System.exit(0); break; default: System.out.println("\n輸入錯誤,請重新選擇!\n"); break; } } } } 11. 編程模擬用戶注冊功能 要求:利用Set集合存放用戶信息(含用戶名、昵稱、密碼),實現用戶注冊功能,如果用戶名已經存在,則無法注冊。用戶注冊成功或者失敗,請輸出相應的提示信息。注意:用戶名不區(qū)分大小寫。 假定已經存在以下用戶信息: 用戶名 密碼 Tim 112233 Barton 123456 Mary 654321 package 作業(yè)練習.test3; import java.util.HashSet; import java.util.Iterator; class Exer09 { private String userName; private String passWord; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } @Override public String toString() { return "userName: " + getUserName() + " passWord: " + getPassWord(); } } class Set { public static void main(String[] args) { // 添加初始信息 Exer09 user1 = new Exer09(); user1.setUserName("Tim"); user1.setPassWord("112233"); Exer09 user2 = new Exer09(); user2.setUserName("Barton"); user2.setPassWord("123456"); Exer09 user3 = new Exer09(); user3.setUserName("Mary"); user3.setPassWord("654321"); // 創(chuàng)建hash對象 HashSet user.add(user1); user.add(user2); user.add(user3); // 輸出 Iterator while (hashIter.hasNext()) System.out.println(hashIter.next()); // 新用戶 Exer09 newUser = new Exer09(); newUser.setUserName("tim"); newUser.setPassWord("112273"); System.out.println("新的信息:" + newUser.toString()); // 測試 if (!user.add(newUser)) System.out.println("已經注冊"); else System.out.println("注冊成功"); // 二次迭代 hashIter = user.iterator(); while (hashIter.hasNext()) System.out.println(hashIter.next()); } } 12. 利用Map存放用戶名和密碼,請給其中的某個用戶修改密碼,請輸出所有的賬號和其對應的密碼。 要求至少含有4個及以上的用戶。 1.請利用Map按表2提供的信息創(chuàng)建一個“專業(yè)-學院”對應關系表,編程實現:當用戶輸入專業(yè)名稱時,輸出所屬學院名稱。要求:允許用戶輸入專業(yè)時可以有前導或后導空格。 表3-2 學院與專業(yè)對照表 學院名稱 專業(yè) 信息學院 計算機科學與技術 物聯網工程 軟件工程 電氣自動化 法政與經貿學院 國際經濟與貿易 社會工作 資源與環(huán)境經濟學 石油工程學院 石油鉆井技術 油氣開采技術 石油與天然氣地質勘探技術 油氣儲運技術 package 作業(yè)練習.test3; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Map2 { public static void main(String[] args) { Map hm.put("計算機科學與技術","智能技術與工程"); hm.put("物聯網工程","智能技術與工程"); hm.put("軟件工程","智能技術與工程"); hm.put("智能科學與技術","智能技術與工程"); hm.put("物流管理","工商管理"); hm.put("酒店管理", "工商管理"); hm.put("人力資源管理", "工商管理"); hm.put("英語", "外國語"); hm.put("西班牙語", "外國語"); Scanner reader = new Scanner(System.in); String key = reader.nextLine(); String key2 = reader.nextLine(); String nkey = key.trim(); //去掉字符串兩端的多余的空格 String nkey2 = key2.trim(); hm.put(nkey,nkey2); //map輸出方法; System.out.println(hm); hm.clear(); } } 每文一語 多想跨過距離去擁抱你,而不是抱著手機說想你。 本文幸運色是嫩綠色喲!希望你們都可以熬過每一段距離的煎熬!~! Java
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發(fā)現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。