1、实验5 JAVA常用类xxxxx大学计算机与信息技术学院实验报告姓 名学 号专业班级课程名称 Java实验实验日期成 绩指导教师批改日期实验名称实验5 JAVA常用类一实验目的:(1)掌握常用的String,StringBuffer(StringBuilder)类的构造方法的使用;(2)掌握字符串的比较方法,尤其equals方法和=比较的区别;(3)掌握String类常用方法的使用;(4)掌握字符串与字符数组和byte数组之间的转换方法;(5)Date,Math, PrintWriter,Scanner类的常用方法。二实验内容1.二进制数转换为十六进制数(此程序参考例题249页9.2.13)
2、程序源代码package T5;import java.util.*;public class BiaToHex public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(请输入二进制数:); String s = input.next(); System.out.print(转换后的16进制值为:); chang(chaiString(ping(s); public static String ping(String st1) if (st1.length()
3、 % 4 = 0) return st1; else if (st1.length() % 4 = 1) String st2 = null; st2 = 000 + st1; return st2; else if (st1.length() % 4 = 2) String st2 = null; st2 = 00 + st1; return st2; else String st2 = null; st2 = 0 + st1; return st2; public static String chaiString(String str1) String str2 = new Strings
4、tr1.length(); for (int i = 0; i str1.length(); i += 4) str2i = str1.substring(i, i + 4); return str2; public static void chang(String str3) for (int i = 0; i str3.length; i += 4) switch (Integer.parseInt(str3i) case 0: System.out.print(0); break; case 1: System.out.print(1); break; case 10: System.o
5、ut.print(2); break; case 11: System.out.print(3); break; case 100: System.out.print(4); break; case 101: System.out.print(5); break; case 110: System.out.print(6); break; case 111: System.out.print(7); break; case 1000: System.out.print(8); break; case 1001: System.out.print(9); break; case 1010: Sy
6、stem.out.print(A); break; case 1011: System.out.print(B); break; case 1100: System.out.print(C); break; case 1101: System.out.print(D); break; case 1110: System.out.print(E); break; case 1111: System.out.print(F); break; 程序运行结果贴图2.将十进制转换为二进制程序源代码package T5;public class Shujingzhi private int shi; pr
7、ivate int a1 = new int200; public int j; Shujingzhi(int shi) this.shi = shi; public int exchange() int a = shi; for (int i = 0; i+) if (a = 0; i-) System.out.print(shi1.getA1()i); 程序运行结果贴图3. 一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否是合法的密码。假设密码规则如下:(1)密码必须至少有8个字符。(2)密码只能包括字母和数字。(3)密码必须至少有2个数字。编写一个程序,提示用户输入密码
8、,如果该密码符合规则就显示“Valid Password”,否则显示“Invalid Password”。程序源代码package T5;import java.util.*;public class Password public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(请输入密码:); String str1 = input.next(); String str2 = str1.toLowerCase(); if (str1.length() = 8)
9、int j = 0; char a1 = str2.toCharArray(); for (int i = 0; i a1.length; i+) if (a1i - 0 = 0) | (a1i - a = 0 & a1i - a = 26) if (a1i - 0 = 0) j+; else System.out.println(Valid Password); System.exit(0); if (j 2) System.out.println(Valid Password); else System.out.println(Invalid Password); else System.
10、out.println(Valid Password); 程序运行结果贴图4.使用下面的方法头编写一个方法,找出某个指定字符在字符串中出现的次数:public static int count(String str,char a)例如,count(“Welcome”,e)返回2.编写一个测试程序 ,提示用户输入一个字符串,在该字符串后紧跟着一个字符,然后显示这个字符在字符串中出现的次数。程序源代码package T5;import java.util.*;public class LX5_3 public static void main(String args) Scanner input
11、= new Scanner(System.in); System.out.println(请输入一个字符串); String str1 = input.next(); System.out.println(请输入一个字符); String str2 = input.next(); char a1 = str2.charAt(0); System.out.println(字符 + a1 + 出现的次数为 + count(str1, a1); public static int count(String str, char a) char a1 = str.toCharArray(); int j
12、 = 0; for (int i = 0; i a1.length; i+) if (a1i - a = 0) j+; return j; 程序运行结果贴图5. Java 提供了3 个日期类:Date、Calendar 和DateFormat。其中,Date 类主要用于创建日期对象并获取日期,Calendar 类可获取和设置日期,DateFormat 类用来设置日期的格式。Java 语言规定的基准日期为1970.1.1 00:00:00 格林威治(GMT)标准时间,当前日期是由基准日期开始所经历的毫秒数转换出来的。程序源代码如下,手工输入,认真分析并运行程序,掌握java日期相关类的用法。im
13、port java.util.*;import java.text.*;public class KY5_10public static void main (String args)Date today = new Date(); /当前日期和时间SimpleDateFormat sdf;sdf= new SimpleDateFormat(yyyy 年MM 月dd 日hh 时mm 分ss 秒 a EEEEE);System.out.println(当前日期和时间: +sdf.format(today);long hms=System.currentTimeMillis(); /当前时间的毫秒
14、数System.out.println(当前时间的毫秒数=+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(今天是+year
15、+年+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.println( 星期三);break;case 5: System.out.println( 星期四);break;case 6: System.out.println( 星
16、期五);break;case 7: System.out.println( 星期六);break; 编译并运行程序程序运行结果贴图6 Math 是一个最终类,含有基本数学运算函数。创建使用Math 类的应用程序,程序中使用如指数运算、对数运算、求平方根、三角函数、随机数等,可以直接在程序中加Math.前缀调用。 程序源代码package T5;import java.util.*;public class LX5_4 public static void main(String args) Scanner input = new Scanner(System.in); System.out.p
17、rintln(指数运算,请输入a,b:); System.out.println(结果为: + Math.pow(input.nextDouble(), input.nextDouble(); System.out.println(对数运算,请输入值(e为底):); System.out.println(结果为: + Math.log(input.nextDouble(); System.out.println(对数运算,请输入值(10为底):); System.out.println(结果为: + Math.log10(input.nextDouble(); System.out.print
18、ln(求平方根,请输入值:); System.out.println(结果为 + Math.sqrt(input.nextDouble(); System.out.println(求正弦,请输入角度(弧度):); System.out.println(结果为: + Math.sin(input.nextDouble(); System.out.println(求余弦,请输入角度(弧度)); System.out.println(结果为: + Math.cos(input.nextDouble(); System.out.println(求正切,请输入角度值(弧度):); System.out.
19、println(结果为: + Math.tan(input.nextDouble(); System.out.println(求反正弦,请输入角度(弧度):); System.out.println(结果为: + Math.asin(input.nextDouble(); System.out.println(求反余弦,请输入角度(弧度)); System.out.println(结果为: + Math.acos(input.nextDouble(); System.out.println(求反正切,请输入角度值(弧度):); System.out.println(结果为: + Math.at
20、an(input.nextDouble(); System.out.println(产生随机数(01) + Math.random(); 程序运行结果贴图课后作业题 p267 9.5 9.8 p272 9.19 9.219.5 可以用toLowerCase();toUpperCase();等改变字符串,转换大小写。9.8因为构造函数的text为局部变量,并不是类里定义的private text这个变量,所以无法将大写变成小写字母。9.19程序代码:package T5;import java.io.*;import java.util.*;public class LX5_5 public s
21、tatic void main(String args) throws Exception creatFile(); print(paixu(read(); public static void creatFile() throws Exception File file = new File(Exercis9_19.txt); if (!file.exists() System.out.println(文件不存在,创建文件:); else System.out.println(文件已存在!); PrintWriter output = new PrintWriter(file);/ 也可以直
22、接写文件名,须加“”,内位文件名的字符串,不能是路径; for (int i = 0; i 100; i+) int a = (int) (1 + Math.random() * 999); output.print(a + ); output.close(); System.out.println(创建完成!); public static int read() throws Exception int a = new int100; File file = new File(Exercis9_19.txt);/ 须创建文件提供读取 Scanner input = new Scanner(f
23、ile);/ 若Exercis9_19.txt是指从Exercis9_19.txt的字符串创建扫描器,而不是从文件读取数据。 for (int i = 0; i 100; i+) ai = input.nextInt(); if (!input.hasNext() input.close(); break; return a; public static int paixu(int list) throws Exception for (int i = 0; i list.length - 1; i+) int currentMin = listi; int currentMinIndex =
24、 i; for (int j = i + 1; j listj) currentMin = listj; currentMinIndex = j; if (currentMinIndex != i) listcurrentMinIndex = listi; listi = currentMin; return list; public static void print(int list1) for (int i = 0; i list1.length; i+) System.out.printf(%-4d, list1i); if (i + 1) % 10 = 0) System.out.p
25、rintln(); 运行贴图:9.21程序代码:package T5;import java.io.*;import java.util.*;public class LX5_6 public static void main(String args) throws Exception File file = new File(Exercise9_21.txt);/ 不会创建一个文件 if (!file.exists() System.out.println(文件不存在!); System.exit(0); String a1 = null; System.out.println(请输入要删除的字符串:); Scanner input = new Scanner(System.in); a1 = input.next(); deletString(a1); public static boolean search(String a) throws Exception int x; File file = new
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1