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

下载本文档

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

文档简介

山西大学计算机与信息技术学院实验报告姓 名 学 号专业班级、课程名称 Java实验实验日期成 绩指导教师批改日期实验名称实验5 JAVA常用类一实验目的:(1)掌握常用的String,StringBuffer(StringBuilder)类的构造方法的使用;(2)掌握字符串的比较方法,尤其equals方法和=比较的区别;(3)掌握String类常用方法的使用;(4)掌握字符串与字符数组和byte数组之间的转换方法;(5)Date,Math, PrintWriter,Scanner类的常用方法。二实验内容1.二进制数转换为十六进制数(此程序参考例题249页9.2.13) 程序源代码import java.util.*;public class BinToHexConversion/二进制转化为十六进制的方法 public static String binToHex(String bin) int temp; /二进制转化为十六进制的位数 if(bin.length()%4=0) temp = bin.length()/4; else temp = bin.length()/4 + 1; char hex = new chartemp; /十六进制数的字符形式 int hexDec = new inttemp;/十六进制数的十进制数形式 int j = 0; for(int i=0;i=0&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(请输入一个二进制数(11100011):); String bin = input.nextLine(); String hex = binToHex(bin); System.out.println(二进制数:+bin+转化为的十六进制为:+hex); 程序运行结果贴图:图一2.将十进制转换为二进制程序源代码:import java.util.*;public class DecToBinConversion/十进制转化为二进制的方法 public static String DecToBin(int dec) int j=0;/转化为二进制的位数 for(long temp=1;temp=dec;j+) temp =temp *2; char bin = new charj; while(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(请输入一个十进制数:); int dec = input.nextInt(); String bin = DecToBin(dec); System.out.println(十进制数+dec+转化为的二进制数为:+bin); 程序运行结果贴图:图二3. 一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否是合法的密码。假设密码规则如下:(1)密码必须至少有8个字符。(2)密码只能包括字母和数字。(3)密码必须至少有2个数字。编写一个程序,提示用户输入密码,如果该密码符合规则就显示“Valid Password”,否则显示“Invalid Password”。程序源代码:import java.util.*;public class CheckPassword/检查password是否合法的方法 public static boolean isPassword(String password) boolean b=true; /password 少于8个字符 if(password.length()8) return b=false; int cout=0;/统计字符串中数字的个数 for(int i=0;ipassword.length();i+) char pChar = password.charAt(i); /判断字符串中的非法字符 if(pChar9)&(pCharZ)&(pCharz) return b=false; if(pChar=0&pChar=9) cout+; if(cout2) return b=false; return b; /测试方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(请输入密码password:); String password = input.nextLine(); Boolean b = isPassword(password); if(b) System.out.println(Valid Password!); else System.out.println(Invalid Password!); 程序运行结果贴图:图三4.使用下面的方法头编写一个方法,找出某个指定字符在字符串中出现的次数:public static int count(String str,char a)例如,count(“Welcome”,e)返回2.编写一个测试程序 ,提示用户输入一个字符串,在该字符串后紧跟着一个字符,然后显示这个字符在字符串中出现的次数。程序源代码import java.util.*;public class CoutChar/统计字符的方法 public static int cout(String str,char a) int cout=0; for(int i=0;istr.length();i+) char strChar = str.charAt(i); if(strChar-a=0) cout+; return cout; /测试方法 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(请输入要统计的字符串(string)和字符(a):); String str = input.nextLine(); String strA = input.next(); char a = strA.charAt(0); System.out.println(字符+a+在字符串+str+中出现的次数为:t+cout(str,a); 程序运行结果贴图图五5. Java 提供了3 个日期类:Date、Calendar 和DateFormat。其中,Date 类主要用于创建日期对象并获取日期,Calendar 类可获取和设置日期,DateFormat 类用来设置日期的格式。Java 语言规定的基准日期为1970.1.1 00:00:00 格林威治(GMT)标准时间,当前日期是由基准日期开始所经历的毫秒数转换出来的。程序源代码如下,手工输入,认真分析并运行程序,掌握java日期相关类的用法。import java.util.*;import java.text.*;public class KY5_10 public 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();/当前时间的毫秒数 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);/月份 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.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(星期五);break; case 7: System.out.println(星期六);break; 编译并运行程序程序运行结果贴图图六6 Math 是一个最终类,含有基本数学运算函数。创建使用Math 类的应用程序,程序中使用如指数运算、对数运算、求平方根、三角函数、随机数等,可以直接在程序中加Math.前缀调用。 程序源代码public 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度对应的弧度为:+Math.toRadians(90); System.out.println(弧度PI/3对应的角度为+Math.toDegrees(Math.PI/3)+度); System.out.println(e的23次方为:+Math.exp(23); System.out.println(log以e为底e的对数为:+Math.log(Math.E); System.out.println(log以10为底100的对数为:+Math.log10(100); System.out.println(2的3次方为:+Math.pow(2,3); System.out.println(4的平方根为:-+Math.sqrt(4)+和+Math.sqrt(4); System.out.println(系统随机产生的09之间的随机整数为:+(int)(Math.random(); System.out.println(系统随机产生的2035之间的整数为+(int)(20+Math.random()*16); 程序运行结果贴图 图七题9.5题目:String类中是否有可以改变字符串内容的方法?解答:没有,String类对象一旦被初始化就不能改变。虽然String类中包含replace、toLowerCase 等方法,但其均为在堆内存新创建的对象。 eg:String str =” java”; str = str.replace(”a”,”o”) ; 其中str先后指向的为不同的内存空间,原先指向的对象”java”已经丢失,现在指向的为 新的对象。toLowerCase等方法也类似。题9.8代码错误:构造方法Test中重新定义了text,其只在该方法块中有效,即构造方法并为将类Test 的数据成员text初始化,故在下面main方法中使用NullPointerException异常。代码修改:public class Test private String text; public Test(String s) text = s; public static void main(String args) Test test = new Test(ABC); System.out.println(test.text.toLowerCase(); 运行结果:图八题9.19 参考代码:import java.io.*;import java.util.*;public class T9_19 public static void main(String args) throws Exception File file = new File(H:/java实验23/Exercise9_19.txt); if(file.exists() System.out.println(File alresdy exists); System.exit(0); PrintWriter output = new PrintWriter(file); for(int i=0;i100;i+) int temp = (int)(Math.random()*1000); output.print(temp); output.print( ); if(i%10=0&i!=0) output.print(n); output.close(); Scanner input = new Scanner(file); int i = 0; while(input.hasNext() System.out.print(input.nextInt()+ )

温馨提示

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

评论

0/150

提交评论