




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、山西大學(xué)計算機與信息技術(shù)學(xué)院實驗報告姓 名 學(xué) 號專業(yè)班級課程名稱 Java實驗實驗日期成 績指導(dǎo)教師批改日期實驗名稱實驗5 JAVA常用類一實驗?zāi)康模海?)掌握常用的String,StringBuffer(StringBuilder)類的構(gòu)造方法的使用;(2)掌握字符串的比較方法,尤其equals方法和=比較的區(qū)別;(3)掌握String類常用方法的使用;(4)掌握字符串與字符數(shù)組和byte數(shù)組之間的轉(zhuǎn)換方法;(5)Date,Math, PrintWriter,Scanner類的常用方法。二實驗內(nèi)容1.二進(jìn)制數(shù)轉(zhuǎn)換為十六進(jìn)制數(shù)(此程序參考例題249頁9.2.13) 程序源代碼import j
2、ava.util.*; public class BinToHexConversion /二進(jìn)制轉(zhuǎn)化為十六進(jìn)制的方法 public static String binToHex(String bin) int temp; /二進(jìn)制轉(zhuǎn)化為十六進(jìn)制的位數(shù) if(bin.length()%4=0) temp = bin.length()/4; else temp = bin.length()/4 + 1; char hex = new chartemp; /十六進(jìn)制數(shù)的字符形式 int hexDec = new inttemp;/十六進(jìn)制數(shù)的十進(jìn)制數(shù)形式 int j = 0; for(int i=0
3、;i<bin.length();i+) char binChar = bin.charAt(i); hexDecj = hexDecj*2 + (binChar-'0'); if(bin.length()-1-i)%4=0) hexj = decToHexChar(hexDecj); j+; return String.valueOf(hex); /十進(jìn)制015轉(zhuǎn)化為十六進(jìn)制的方法 public static char decToHexChar(int dec) if(dec>=0&&dec<10) return (char)('0
4、39;+dec-0); else if(dec>=10&&dec<=15) return (char)('A'+dec-10); else return '' /測試方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println("請輸入一個二進(jìn)制數(shù)(11100011):"); String bin = input.nextLine(); String hex = binToHex(
5、bin); System.out.println("二進(jìn)制數(shù):"+bin+"轉(zhuǎn)化為的十六進(jìn)制為:"+hex); 程序運行結(jié)果貼圖2.將十進(jìn)制轉(zhuǎn)換為二進(jìn)制程序源代碼import java.util.*; public class DecToBinConversion /十進(jìn)制轉(zhuǎn)化為二進(jìn)制的方法 public static String DecToBin(int dec) int j=0;/轉(zhuǎn)化為二進(jìn)制的位數(shù) for(long temp=1;temp<=dec;j+) temp =temp *2; char bin = new charj; while
6、(dec!=0) binj-1 = (char)('0'+(dec%2)-0); dec=dec/2; j-; return String.valueOf(bin); /測試方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println("請輸入一個十進(jìn)制數(shù):"); int dec = input.nextInt(); String bin = DecToBin(dec); System.out.println("十
7、進(jìn)制數(shù)"+dec+"轉(zhuǎn)化為的二進(jìn)制數(shù)為:"+bin); 程序運行結(jié)果貼圖3. 一些網(wǎng)站設(shè)定了一些制定密碼的規(guī)則。編寫一個方法,檢驗一個字符串是否是合法的密碼。假設(shè)密碼規(guī)則如下:(1)密碼必須至少有8個字符。(2)密碼只能包括字母和數(shù)字。(3)密碼必須至少有2個數(shù)字。編寫一個程序,提示用戶輸入密碼,如果該密碼符合規(guī)則就顯示“Valid Password”,否則顯示“Invalid Password”。程序源代碼import java.util.*; public class CheckPassword /檢查password是否合法的方法 public static
8、 boolean isPassword(String password) boolean b=true; /password 少于8個字符 if(password.length()<8) return b=false; int cout=0;/統(tǒng)計字符串中數(shù)字的個數(shù) for(int i=0;i<password.length();i+) char pChar = password.charAt(i); /判斷字符串中的非法字符 if(pChar<'0'|pChar>'9')&&(pChar<'A'|p
9、Char>'Z')&&(pChar<'a'|pChar>'z') return b=false; if(pChar>='0'&&pChar<='9') cout+; if(cout<2) return b=false; return b; /測試方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(&quo
10、t;請輸入密碼password:"); String password = input.nextLine(); Boolean b = isPassword(password); if(b) System.out.println("Valid Password!"); else System.out.println("Invalid Password!"); 程序運行結(jié)果貼圖4.使用下面的方法頭編寫一個方法,找出某個指定字符在字符串中出現(xiàn)的次數(shù):public static int count(String str,char a)例如,count
11、(“Welcome”,e)返回2.編寫一個測試程序 ,提示用戶輸入一個字符串,在該字符串后緊跟著一個字符,然后顯示這個字符在字符串中出現(xiàn)的次數(shù)。程序源代碼import java.util.*; public class CoutChar /統(tǒng)計字符的方法 public static int cout(String str,char a) int cout=0; for(int i=0;i<str.length();i+) char strChar = str.charAt(i); if(strChar-a=0) cout+; return cout; /測試方法 public stati
12、c void main(String args) Scanner input = new Scanner(System.in); System.out.println("請輸入要統(tǒng)計的字符串(string)和字符(a):"); String str = input.nextLine(); String strA = input.next(); char a = strA.charAt(0); System.out.println("字符'"+a+"'在字符串""+str+""中出現(xiàn)的次數(shù)為
13、:t"+cout(str,a); 程序運行結(jié)果貼圖5. Java 提供了3 個日期類:Date、Calendar 和DateFormat。其中,Date 類主要用于創(chuàng)建日期對象并獲取日期,Calendar 類可獲取和設(shè)置日期,DateFormat 類用來設(shè)置日期的格式。Java 語言規(guī)定的基準(zhǔn)日期為1970.1.1 00:00:00 格林威治(GMT)標(biāo)準(zhǔn)時間,當(dāng)前日期是由基準(zhǔn)日期開始所經(jīng)歷的毫秒數(shù)轉(zhuǎn)換出來的。程序源代碼如下,手工輸入,認(rèn)真分析并運行程序,掌握java日期相關(guān)類的用法。import java.util.*;import java.text.*;public class
14、 KY5_10public static void main (String args)Date today = new Date(); /當(dāng)前日期和時間SimpleDateFormat sdf;sdf= new SimpleDateFormat("yyyy 年MM 月dd 日hh 時mm 分ss 秒 a EEEEE");System.out.println("當(dāng)前日期和時間: "+sdf.format(today);long hms=System.currentTimeMillis(); /當(dāng)前時間的毫秒數(shù)System.out.println(&quo
15、t;當(dāng)前時間的毫秒數(shù)="+hms);Date tomorrow = new Date(hms+24*60*60*1000);System.out.println("明天是"+sdf.format(tomorrow);Calendar now = Calendar.getInstance();int year =now.get(Calendar.YEAR); /年份int month=now.get(Calendar.MONTH)+1; /月份int day = now.get(Calendar.DATE); /日期System.out.print("今天
16、是"+year+"年"+month+"月"+day+"日");int week = now.get(Calendar.DAY_OF_WEEK); /星期switch (week)case 1: System.out.println(" 星期日");break;case 2: System.out.println(" 星期一");break;case 3: System.out.println(" 星期二");break;case 4: System.out.prin
17、tln(" 星期三");break;case 5: System.out.println(" 星期四");break;case 6: System.out.println(" 星期五");break;case 7: System.out.println(" 星期六");break;編譯并運行程序程序運行結(jié)果貼圖6 Math 是一個最終類,含有基本數(shù)學(xué)運算函數(shù)。創(chuàng)建使用Math 類的應(yīng)用程序,程序中使用如指數(shù)運算、對數(shù)運算、求平方根、三角函數(shù)、隨機數(shù)等,可以直接在程序中加Math.前綴調(diào)用。 程序源代碼public
18、class TestMath public static void main(String args) System.out.println("-1的絕對值為:"+Math.abs(-1); System.out.println("asin(1) = "+Math.asin(1); System.out.println("sin(PI/2) = "+Math.sin(Math.PI/2); System.out.println("角度90度對應(yīng)的弧度為:"+Math.toRadians(90); System.out.println("弧度PI/3對應(yīng)的角度為"+Math.toDegrees(Math.PI/3)+"度"); System.out.println("e的23次方為:"+Math.exp(23); System.out.println("log以e為底e的對數(shù)為:"+Math.log(Math.E); System.out.println("log以10為底100的對數(shù)為:"+Math.log10(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國新材料產(chǎn)業(yè)需求形勢及投資戰(zhàn)略決策研究報告
- 2025-2030年中國整車制造行業(yè)規(guī)模分析及投資可行性預(yù)測研究報告
- 2025年部編新版教材一年級教師培訓(xùn)計劃
- 人保財險支公司綜合部年度總結(jié)與下一步工作計劃
- 物業(yè)管理更新協(xié)議書(2篇)
- 小學(xué)音樂評估與反饋機制計劃
- 制造業(yè)2025年生產(chǎn)效率總結(jié)與改進(jìn)計劃
- 股權(quán)轉(zhuǎn)讓協(xié)議書
- 智能樂器交易平臺研發(fā)合作協(xié)議
- 棉花貨物運輸合同協(xié)議書
- 頸椎功能障礙指數(shù),Neck Disabilitv Index,NDI
- 天地萬物一體 的整體觀念
- 大班音樂游戲《郵遞馬車》課后反思
- 2022新高考卷小說《江上》 答案+評點
- 裝配式擋墻專項施工方案
- 污水廠設(shè)備管理培訓(xùn)(共110頁).ppt
- 雍琦版-《法律邏輯學(xué)》課后習(xí)題答案(共78頁)
- 20-5T雙梁橋式起重機設(shè)計(全套圖紙)
- 潛水式排污泵檢驗報告(共8頁)
- 供應(yīng)商管理制度與流程
- 數(shù)控機床裝調(diào)維修工考工練習(xí)試題題庫
評論
0/150
提交評論