实验5 JAVA常用类_第1页
实验5 JAVA常用类_第2页
实验5 JAVA常用类_第3页
实验5 JAVA常用类_第4页
实验5 JAVA常用类_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、山西大学计算机与信息技术学院实验报告姓 名学 号专业班级软件工程一班课程名称 Java实验实验日期成 绩指导教师批改日期实验名称实验5 JAVA常用类一实验目的:(1)掌握常用的String,StringBuffer(StringBuilder)类的构造方法的使用;(2)掌握字符串的比较方法,尤其equals方法和=比较的区别;(3)掌握String类常用方法的使用;(4)掌握字符串与字符数组和byte数组之间的转换方法;(5)Date,Math, PrintWriter,Scanner类的常用方法。二实验内容1.二进制数转换为十六进制数(此程序参考例题249页9.2.13) 程序源代码imp

2、ort java.util.*;public class BinaryToHex public static void main(String args) System.out.print("Enter a binary number:");Scanner input = new Scanner(System.in);String binary = input.nextLine();String h = String.valueOf(binaryToHex(binary);System.out.println("Value of hex:" + h);p

3、ublic static String binaryToHex(String binary) char hex = new char(int) Math.ceil(binary.length() / 4);int hexValue = new int(int) Math.ceil(binary.length() / 4);for (int i = binary.length() - 1; i >= 0; i = i - 4) int binaryChar1 = binaryIntToChar(binary.charAt(i);int binaryChar2 = binaryIntToCh

4、ar(binary.charAt(i - 1);int binaryChar3 = binaryIntToChar(binary.charAt(i - 2);int binaryChar4 = binaryIntToChar(binary.charAt(i - 3);if (i + 1) % 4 = 0) hexValue(int) Math.ceil(i + 1) / 4) - 1 = (int) (binaryChar1+ binaryChar2 * 2 + binaryChar3 * 4 + binaryChar4 * 8);elsei = i - 4;for (int i = 0; i

5、 < hex.length; i+)hexi = bianaryToHexChar(hexValuei);String s = String.valueOf(hex);return s;public static char bianaryToHexChar(int a) char b = (char) (a + 48);char c = (char) (a + 55);if (a >= 0 && a <= 9)return b;elsereturn c;public static int binaryIntToChar(char a) if (a = '

6、;1')return 1;else if (a = '0')return 0;elsereturn -1;程序运行结果贴图2.将十进制转换为二进制程序源代码import java.util.*;public class DecimalToBinary public static void main(String args) Scanner input = new Scanner(System.in);System.out.print("Enter a decimal number:");String decimal = input.nextLine(

7、);int binary = new intdecimal.length()4;for (int i = 0; i < decimal.length(); i+) int charDecimal = (int) decimal.charAt(i) - 48;for (int j = 0; j < 4; j+) binaryij = (int) (charDecimal / Math.pow(2, 3 - j) % 2);System.out.print("Value of binary is:");for (int i = 0; i < decimal.l

8、ength(); i+) for (int j = 0; j < 4; j+)System.out.print(binaryij);程序运行结果贴图3. 一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否是合法的密码。假设密码规则如下:(1)密码必须至少有8个字符。(2)密码只能包括字母和数字。(3)密码必须至少有2个数字。编写一个程序,提示用户输入密码,如果该密码符合规则就显示“Valid Password”,否则显示“Invalid Password”。程序源代码import java.util.*;public class Password public stati

9、c void main(String args) Scanner input = new Scanner(System.in);System.out.print("Please input password:");String passward = input.nextLine();int lettersNum = 0;/密码中的字母个数int numbersNum = 0;/密码中的数字个数int NotLettersAndNumbersNum = 0;/密码中的非字母非数字个数for (int i = 0; i < passward.length(); i+) c

10、har passwardChar = passward.charAt(i);if (passwardChar >= '0' && passwardChar <= '9')numbersNum+;else if (passwardChar >= 'A' && passwardChar <= 'Z')| (passwardChar >= 'a' && passwardChar <= 'z')lettersNum+;els

11、eNotLettersAndNumbersNum+;if (passward.length() < 8 | NotLettersAndNumbersNum != 0| numbersNum+ < 2)System.out.println("Invalid Password");elseSystem.out.println("Valid Password");程序运行结果贴图4.使用下面的方法头编写一个方法,找出某个指定字符在字符串中出现的次数:public static int count(String str,char a)例如,count

12、(“Welcome”,e)返回2.编写一个测试程序 ,提示用户输入一个字符串,在该字符串后紧跟着一个字符,然后显示这个字符在字符串中出现的次数。程序源代码import java.util.*;public class CharOfStringNum public static void main(String args) System.out.print("Please input string and char:");Scanner input = new Scanner(System.in);String string1 = input.nextLine();Strin

13、g string2 = string1.substring(0, string1.length() - 2);char ch = string1.charAt(string1.length() - 1);System.out.println("Characters '" + ch + "' in the string ""+ string1.substring(0, string1.length() - 2)+ "" appeared in number is " + count(string2,

14、ch);public static int count(String str, char a) int count = 0;for (int i = 0; i < str.length(); i+) char stringChar = str.charAt(i);if (stringChar = a)count+;return count;程序运行结果贴图5. Java 提供了3 个日期类:Date、Calendar 和DateFormat。其中,Date 类主要用于创建日期对象并获取日期,Calendar 类可获取和设置日期,DateFormat 类用来设置日期的格式。Java 语言规

15、定的基准日期为1970.1.1 00:00:00 格林威治(GMT)标准时间,当前日期是由基准日期开始所经历的毫秒数转换出来的。程序源代码如下,手工输入,认真分析并运行程序,掌握java日期相关类的用法。import 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 时

16、mm 分ss 秒 a EEEEE");System.out.println("当前日期和时间: "+sdf.format(today);long hms=System.currentTimeMillis(); /当前时间的毫秒数System.out.println("当前时间的毫秒数="+hms);Date tomorrow = new Date(hms+24*60*60*1000);System.out.println("明天是"+sdf.format(tomorrow);Calendar now = Calendar.g

17、etInstance();int year =now.get(Calendar.YEAR); /年份int month=now.get(Calendar.MONTH)+1; /月份int day = now.get(Calendar.DATE); /日期System.out.print("今天是"+year+"年"+month+"月"+day+"日");int week = now.get(Calendar.DAY_OF_WEEK); /星期switch (week)case 1: System.out.print

18、ln(" 星期日");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(" 星期五");break;case 7: System.out

19、.println(" 星期六");break;编译并运行程序程序运行结果贴图6 Math 是一个最终类,含有基本数学运算函数。创建使用Math 类的应用程序,程序中使用如指数运算、对数运算、求平方根、三角函数、随机数等,可以直接在程序中加Math.前缀调用。 程序源代码import java.util.*;public class MathValue public static void main(String args) Scanner input = new Scanner(System.in);System.out.print("绝对值运算(请输入一个数):

20、");double abs = input.nextDouble();System.out.println("abs(" + abs + ")=" + Math.abs(abs);System.out.print("n指数运算(请输入两个数):");double pow1 = input.nextDouble();double pow2 = input.nextDouble();System.out.print("pow(" + pow1 + "," + pow2 + ")=

21、"+ Math.pow(pow1, pow2) + "nn求以e为底数的指数运算(请输入一个数):");double exp = input.nextDouble();System.out.println("exp(" + exp + ")=" + Math.exp(exp);System.out.print("n对数运算(请输入一个数):");double log = input.nextDouble();System.out.print("log(" + log + ")

22、=" + Math.log(log);System.out.println(",log10" + log + ")=" + Math.log10(log);System.out.print("n求平方根(请输入一个数):");double sqrt = input.nextDouble();System.out.println("sqrt(" + sqrt + ")=" + Math.sqrt(sqrt);System.out.println("n三角函数(请输入一个数):&

23、quot;);double s = input.nextDouble();System.out.println("toDegrees(" + s + ")=" + Math.toDegrees(s)+ "ntoRadians(" + s + ")=" + Math.toRadians(s) + "nsin(" + s+ ")=" + Math.sin(s) + "ncos(" + s + ")=" + Math.cos(s)+ &quo

24、t;ntan(" + s + ")=" + Math.tan(s) + "nasin(" + s + ")="+ Math.asin(s) + "nacos(" + s + ")=" + Math.acos(s)+ "natan(" + s + ")=" + Math.atan(s) + Math.sinh(s)+ "natan(" + s + ")=" + Math.sinh(s);System.out.

25、println("n产生随机数(该值大于等于 0.0 且小于 1.0):" + Math.random()+ "n产生随机数(该值大于等于 0.0 且小于 10000.0):" + Math.random() * 10000);System.out.print("n符号函数(请输入一个数):");float signum = input.nextFloat();System.out.println("signum(" + signum + ")=" + Math.signum(signum);S

26、ystem.out.print("n余数运算(请输入两个数):");double IEEEremainder1 = input.nextDouble();double IEEEremainder2 = input.nextDouble();System.out.println("IEEEremainder(" + IEEEremainder1 + ","+ IEEEremainder1 + ")="+ Math.IEEEremainder(IEEEremainder1, IEEEremainder2);程序运行结果

27、贴图课后作业题 p267 9.5 9.8 p272 9.19 9.219.5String类中没有可以改变字符串内容的方法。9.8第二行声明了一个私有类型变量text,又在第五行重新声明此变量为一个局部变量。局部变量被指定的字符串传递给构造函数,但数据域仍然是空的。所以第十行中的test.text是空的,使得当调用toLowerCase()时出现异常。 9.19程序如下:import java.io.*;import java.util.*;public class FileWrite public static void main(String args) throws Exception F

28、ile file = new File("Exercise9_18.txt");if (file.exists() System.out.println("File already exists");PrintWriter output = new PrintWriter(file);output.println("随机数:");Random random = new Random();int rand = new int100;for (int i = 0; i < 100; i+) randi = random.nextInt();if (i % 10 = 0 && i != 0) output.println();output.printf("%12d", randi); elseoutput.printf("%12d", randi);output.println();System.out.println("文件Exercise9_18.txt中读入的100个随机整数排序后为:");int s = sort(rand);for (int i = 0; i < 100; i+) if (i % 10 = 0 &a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论