![實(shí)驗(yàn)5 JAVA常用類_第1頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-3/7/acbd3e3b-5e6b-4f4d-9132-b87415430d79/acbd3e3b-5e6b-4f4d-9132-b87415430d791.gif)
![實(shí)驗(yàn)5 JAVA常用類_第2頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-3/7/acbd3e3b-5e6b-4f4d-9132-b87415430d79/acbd3e3b-5e6b-4f4d-9132-b87415430d792.gif)
![實(shí)驗(yàn)5 JAVA常用類_第3頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-3/7/acbd3e3b-5e6b-4f4d-9132-b87415430d79/acbd3e3b-5e6b-4f4d-9132-b87415430d793.gif)
![實(shí)驗(yàn)5 JAVA常用類_第4頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-3/7/acbd3e3b-5e6b-4f4d-9132-b87415430d79/acbd3e3b-5e6b-4f4d-9132-b87415430d794.gif)
![實(shí)驗(yàn)5 JAVA常用類_第5頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-3/7/acbd3e3b-5e6b-4f4d-9132-b87415430d79/acbd3e3b-5e6b-4f4d-9132-b87415430d795.gif)
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、山西大學(xué)計(jì)算機(jī)與信息技術(shù)學(xué)院實(shí)驗(yàn)報(bào)告姓 名 學(xué) 號(hào)專業(yè)班級(jí)課程名稱 Java實(shí)驗(yàn)實(shí)驗(yàn)日期成 績(jī)指導(dǎo)教師批改日期實(shí)驗(yàn)名稱實(shí)驗(yàn)5 JAVA常用類一實(shí)驗(yàn)?zāi)康模海?)掌握常用的String,StringBuffer(StringBuilder)類的構(gòu)造方法的使用;(2)掌握字符串的比較方法,尤其equals方法和=比較的區(qū)別;(3)掌握String類常用方法的使用;(4)掌握字符串與字符數(shù)組和byte數(shù)組之間的轉(zhuǎn)換方法;(5)Date,Math, PrintWriter,Scanner類的常用方法。二實(shí)驗(yàn)內(nèi)容1.二進(jìn)制數(shù)轉(zhuǎn)換為十六進(jìn)制數(shù)(此程序參考例題249頁(yè)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 '' /測(cè)試方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println("請(qǐng)輸入一個(gè)二進(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); 程序運(yùn)行結(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); /測(cè)試方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println("請(qǐng)輸入一個(gè)十進(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); 程序運(yùn)行結(jié)果貼圖3. 一些網(wǎng)站設(shè)定了一些制定密碼的規(guī)則。編寫一個(gè)方法,檢驗(yàn)一個(gè)字符串是否是合法的密碼。假設(shè)密碼規(guī)則如下:(1)密碼必須至少有8個(gè)字符。(2)密碼只能包括字母和數(shù)字。(3)密碼必須至少有2個(gè)數(shù)字。編寫一個(gè)程序,提示用戶輸入密碼,如果該密碼符合規(guī)則就顯示“Valid Password”,否則顯示“Invalid Password”。程序源代碼import java.util.*; public class CheckPassword /檢查password是否合法的方法 public static
8、 boolean isPassword(String password) boolean b=true; /password 少于8個(gè)字符 if(password.length()<8) return b=false; int cout=0;/統(tǒng)計(jì)字符串中數(shù)字的個(gè)數(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; /測(cè)試方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(&quo
10、t;請(qǐng)輸入密碼password:"); String password = input.nextLine(); Boolean b = isPassword(password); if(b) System.out.println("Valid Password!"); else System.out.println("Invalid Password!"); 程序運(yùn)行結(jié)果貼圖4.使用下面的方法頭編寫一個(gè)方法,找出某個(gè)指定字符在字符串中出現(xiàn)的次數(shù):public static int count(String str,char a)例如,count
11、(“Welcome”,e)返回2.編寫一個(gè)測(cè)試程序 ,提示用戶輸入一個(gè)字符串,在該字符串后緊跟著一個(gè)字符,然后顯示這個(gè)字符在字符串中出現(xiàn)的次數(shù)。程序源代碼import java.util.*; public class CoutChar /統(tǒng)計(jì)字符的方法 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; /測(cè)試方法 public stati
12、c void main(String args) Scanner input = new Scanner(System.in); System.out.println("請(qǐng)輸入要統(tǒng)計(jì)的字符串(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); 程序運(yùn)行結(jié)果貼圖5. Java 提供了3 個(gè)日期類:Date、Calendar 和DateFormat。其中,Date 類主要用于創(chuàng)建日期對(duì)象并獲取日期,Calendar 類可獲取和設(shè)置日期,DateFormat 類用來(lái)設(shè)置日期的格式。Java 語(yǔ)言規(guī)定的基準(zhǔn)日期為1970.1.1 00:00:00 格林威治(GMT)標(biāo)準(zhǔn)時(shí)間,當(dāng)前日期是由基準(zhǔn)日期開始所經(jīng)歷的毫秒數(shù)轉(zhuǎn)換出來(lái)的。程序源代碼如下,手工輸入,認(rèn)真分析并運(yù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)前日期和時(shí)間SimpleDateFormat sdf;sdf= new SimpleDateFormat("yyyy 年MM 月dd 日hh 時(shí)mm 分ss 秒 a EEEEE");System.out.println("當(dāng)前日期和時(shí)間: "+sdf.format(today);long hms=System.currentTimeMillis(); /當(dāng)前時(shí)間的毫秒數(shù)System.out.println(&quo
15、t;當(dāng)前時(shí)間的毫秒數(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;編譯并運(yùn)行程序程序運(yùn)行結(jié)果貼圖6 Math 是一個(gè)最終類,含有基本數(shù)學(xué)運(yùn)算函數(shù)。創(chuàng)建使用Math 類的應(yīng)用程序,程序中使用如指數(shù)運(yùn)算、對(duì)數(shù)運(yùn)算、求平方根、三角函數(shù)、隨機(jī)數(shù)等,可以直接在程序中加Math.前綴調(diào)用。 程序源代碼public
18、class TestMath public static void main(String args) System.out.println("-1的絕對(duì)值為:"+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度對(duì)應(yīng)的弧度為:"+Math.toRadians(90); System.out.println("弧度PI/3對(duì)應(yīng)的角度為"+Math.toDegrees(Math.PI/3)+"度"); System.out.println("e的23次方為:"+Math.exp(23); System.out.println("log以e為底e的對(duì)數(shù)為:"+Math.log(Math.E); System.out.println("log以10為底100的對(duì)數(shù)為:"+Math.log10(
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 公司預(yù)支申請(qǐng)書
- 導(dǎo)游業(yè)務(wù)-2025導(dǎo)游資格證導(dǎo)游業(yè)務(wù)考試模擬題
- 退出學(xué)校志愿者申請(qǐng)書
- 補(bǔ)助申請(qǐng)書范文
- 4s店申請(qǐng)書范文
- 電表開戶申請(qǐng)書
- 停薪留職后上崗申請(qǐng)書
- 提高旅游景點(diǎn)的服務(wù)質(zhì)量標(biāo)準(zhǔn)
- 高考理綜化學(xué)試題和答案全國(guó)卷I
- DB2201-T 32-2023 梅花鹿全混合日糧加工技術(shù)規(guī)范
- DBJ∕T 15-129-2017 集中空調(diào)制冷機(jī)房系統(tǒng)能效監(jiān)測(cè)及評(píng)價(jià)標(biāo)準(zhǔn)
- 閩教版(2020版)六年級(jí)下冊(cè)信息技術(shù)整冊(cè)教案
- 物業(yè)管理應(yīng)急預(yù)案工作流程圖
- (高清正版)T_CAGHP 003—2018抗滑樁治理工程設(shè)計(jì)規(guī)范 (試行)
- 裝飾裝修工程施工合理化建議和降低成本措施提要:完整
- 畢業(yè)論文論財(cái)務(wù)管理是企業(yè)管理的核心
- 清潔化施工無(wú)土化安裝施工方案
- 物業(yè)小區(qū)常規(guī)保潔工作程序
- 食管癌化療臨床路徑(最全版)
- 失業(yè)保險(xiǎn)知識(shí)PPT課件
- 急危重癥患者優(yōu)先處置制度
評(píng)論
0/150
提交評(píng)論