JAVA高级练习题.docx

上传人:b****1 文档编号:1439812 上传时间:2023-05-01 格式:DOCX 页数:30 大小:22.89KB
下载 相关 举报
JAVA高级练习题.docx_第1页
第1页 / 共30页
JAVA高级练习题.docx_第2页
第2页 / 共30页
JAVA高级练习题.docx_第3页
第3页 / 共30页
JAVA高级练习题.docx_第4页
第4页 / 共30页
JAVA高级练习题.docx_第5页
第5页 / 共30页
JAVA高级练习题.docx_第6页
第6页 / 共30页
JAVA高级练习题.docx_第7页
第7页 / 共30页
JAVA高级练习题.docx_第8页
第8页 / 共30页
JAVA高级练习题.docx_第9页
第9页 / 共30页
JAVA高级练习题.docx_第10页
第10页 / 共30页
JAVA高级练习题.docx_第11页
第11页 / 共30页
JAVA高级练习题.docx_第12页
第12页 / 共30页
JAVA高级练习题.docx_第13页
第13页 / 共30页
JAVA高级练习题.docx_第14页
第14页 / 共30页
JAVA高级练习题.docx_第15页
第15页 / 共30页
JAVA高级练习题.docx_第16页
第16页 / 共30页
JAVA高级练习题.docx_第17页
第17页 / 共30页
JAVA高级练习题.docx_第18页
第18页 / 共30页
JAVA高级练习题.docx_第19页
第19页 / 共30页
JAVA高级练习题.docx_第20页
第20页 / 共30页
亲,该文档总共30页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

JAVA高级练习题.docx

《JAVA高级练习题.docx》由会员分享,可在线阅读,更多相关《JAVA高级练习题.docx(30页珍藏版)》请在冰点文库上搜索。

JAVA高级练习题.docx

JAVA高级练习题

1、编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。

2、编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。

packagezuoye;

/**

*输出e的出现次数

*/

import;

publicclassTest2{

publicstaticvoidmain(String[]args){

Scannerinput=newScanner(System.in);

Stringstr=input.next();

intcount=0;

for(inti=0;i

{

if(str.charAt(i)=='e')

{

count++;

}

}

"e的出现次数是:

"+count);

}

}

3、生成十个0~100之间的随机数,放到数组中,然后排序输出。

packagezuoye;

import;

/**

*生成十个0~100之间的随机数,放到数组中,然后排序输出

*@authorAdministrator

*

*/

publicclassTest3{

publicstaticvoidmain(String[]args){

intarr[]=newint[10];//定义一个长度为10的int数组

for(inti=0;i

arr[i]=(int)(Math.random()*100);//取0~1的随机数*100,即为0~100的随机数

}

Arrays.sort(arr);//排序方法

for(inta:

arr)//增强for循环

{

"");

}

}

}

4、巴黎时间比北京时间晚7个小时,纽约时间比北京时间晚12个小时,试编写一程序,根据输入的北京时间输出相应的巴黎和纽约时间。

packagezuoye;

import;

import;

publicclassTest4{

publicvoidgetTime(inty,intm,intd,inth,intmi,ints){

//获取Calendar实例

Calendartime=Calendar.getInstance();

//设置巴黎时间

time.set(y,m,d,h-7,mi,s);

//输出巴黎时间

"巴黎时间是"+time.get(Calendar.YEAR)+"年"+

((time.get(Calendar.MONTH)))+"月"+

time.get(Calendar.DAY_OF_MONTH)+"日"+

time.get(Calendar.HOUR_OF_DAY)+"时"+

time.get(Calendar.MINUTE)+"分"+

time.get(Calendar.SECOND)+"秒");

//设置纽约时间

time.set(y,m,d,h-12,mi);

//输出纽约时间

"纽约时间是"+time.get(Calendar.YEAR)+"年"+

((time.get(Calendar.MONTH)))+"月"+

time.get(Calendar.DAY_OF_MONTH)+"日"+

time.get(Calendar.HOUR_OF_DAY)+"时"+

time.get(Calendar.MINUTE)+"分"+

time.get(Calendar.SECOND)+"秒");

}

publicstaticvoidmain(String[]args){

Test4time=newTest4();

//设置北京时间

time.getTime(2018,3,15,16,53,9);

}

}

5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名

如果邮箱地址不合法则显示不合法的原因

提示:

邮箱地址不合法的因素:

1)邮箱地址中不包含@或。

2)邮箱地址中含有多了@或。

3)邮箱地址中。

出现在@的前面

4)用户名里有其他字符

实现步骤:

(1)创建一个类,类名:

mailtest

类图如下:

(类名和方法名必须与要求一样。

区分大小写)

packageyouxiang;

import;

publicclassMailTest{

publicstaticbooleantestMail(){

Scannerinput=newScanner(System.in);

Strings=input.next();

if(s.indexOf("@")==-1||s.indexOf(".")==-1)

{

"邮箱地址中不包含@或.");

returnfalse;

}

if(s.indexOf("@")!

=s.lastIndexOf("@")||s.indexOf(".")!

=s.lastIndexOf("."))

{

"邮箱地址中含有多余的@或.");

returnfalse;

}

if(s.indexOf("@")>s.lastIndexOf("."))

{

"邮箱地址中.出现在@的前面");

returnfalse;

}

for(inti=0;i

{

if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z'))

{

}else

{

"用户名里有其他字符");

returnfalse;

}

}

returntrue;

}

}

packageyouxiang;

publicclassTest{

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

if(MailTest.testMail()){

"邮箱格式合法");

}else{

"邮箱格式不合法");

}

}

}

6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。

packagezuoye;

import;

/**

*控制台输入一段字符串,再查询一段子字符串出现的次数

*

*/

publicclassTest6{

publicstaticvoidmain(String[]args){

Scannerinput=newScanner(System.in);

Stringa=input.next();

Stringb=input.next();

intcount=0;//计数器

for(inti=0;i

{

if(a.indexOf(b,i)!

=-1)

{

count++;

}

}

;

}

}

集合类

1、请使用LinkedList来模拟一个队列(先进先出的特性):

1)拥有放入对象的方法voidput(Objecto)

2)取出对象的方法Objectget()

3)判断队列当中是否为空的方法booleanisEmpty();并且,编写测试代码,验证你的队列是否正确。

publicclassLinkeds{

Listl;

Linkeds(){

l=newLinkedList();

}

publicvoidput(Objecto){

l.add(o);

}

publicObjectget(){

Objecto=l.get(0);

l.remove(0);

returno;

}

publicbooleanisEmpty(){

if(l.isEmpty()){

returntrue;

}

returnfalse;

}

publicstaticvoidmain(String[]args){

//定义一个空队列

Linkedsl=newLinkeds();

//往队列中放入对象

l.put("Tom1");

l.put("John2");

l.put("Mary3");

//如果队列不为空,依次输出队列中的元素

while(!

l.isEmpty()){

;

}

}

}

2、假设顺序列表ArrayList中存储的元素是整型数字1~5,遍历每个元素,将每个元素顺序输出。

packagezuoye;

import;

import;

import;

publicclassTest1{

publicstaticvoidmain(String[]args){

Listlist=newArrayList<>();

Numbern1=newNumber

(1);

Numbern2=newNumber

(2);

Numbern3=newNumber(3);

Numbern4=newNumber(4);

Numbern5=newNumber(5);

list.add(n1);

list.add(n2);

list.add(n3);

list.add(n4);

list.add(n5);

Iteratorit=list.iterator();

while(it.hasNext())

{

;

}

}

}

3、在一个列表中存储以下元素:

apple,grape,banana,pear

1)返回集合中的最大的和最小的元素

2)将集合进行排序,并将排序后的结果打印在控制台上

packagezuoye;

publicclassFruit{

privateStringfruit;

publicFruit()

{

}

publicFruit(Stringfruit)

{

this.fruit=fruit;

}

publicStringgetFruit(){

returnfruit;

}

publicvoidsetFruit(Stringfruit){

this.fruit=fruit;

}

}

packagezuoye;

import;

import;

import;

publicclassFruitTest{

publicstaticvoidmain(String[]args){

Listfruit=Arrays.asList("applegradebananapear".split(""));

"最大值是:

"+Collections.max(fruit));

"最小值是:

"+Collections.min(fruit));

}

}

4、编写一个程序,创建一个 HashMap对象,用于存储银行储户的信息(其中储户的主要信息有储户的ID,姓名和余额)。

另外,计算并显示其中某个储户的当前余额。

packagezuoye;

publicclassBank{

privateStringid;

privateStringname;

privatedoublemoney;

publicBank()

{

}

publicBank(Stringid,Stringname,doublemoney)

{

this.id=id;

this.name=name;

this.money=money;

}

publicStringgetId(){

returnid;

}

publicvoidsetId(Stringid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicdoublegetMoney(){

returnmoney;

}

publicvoidsetMoney(doublemoney){

this.money=money;

}

}

packagezuoye;

import;

import;

import;

import;

publicclassBankTest{

publicstaticvoidmain(String[]args){

Mapmap=newHashMap();

map.put("用户1",newBank("1","张三",Math.random()*100000));

map.put("用户2",newBank("2","李四",Math.random()*100000));

map.put("用户3",newBank("3","王五",Math.random()*100000));

map.put("用户4",newBank("4","赵六",Math.random()*100000));

SetkeySet=map.keySet();

Iteratorit=keySet.iterator();

doublesum=0;

while(it.hasNext()){

Objectkey=it.next();

Banka=map.get(key);

"账户"+a.getId()+"的余额:

"+a.getMoney());

//计算所有帐户的余额之和

sum+=a.getMoney();

}

"所有账户余额:

"+sum);

}

}

5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。

packagezuoye;

import;

import;

import;

publicclassTest5{

publicstaticvoidmain(String[]args){

Scannerinput=newScanner(System.in);

Listlist=newArrayList();

Stringstr=input.nextLine();

String[]arr=str.split("");

Stringtmp;

for(inti=0;i

for(intj=i+1;j

if(arr[i].compareToIgnoreCase(arr[j])>0){

str=arr[j];

arr[j]=arr[j+1];

arr[j+1]=str;

}

}

}

for(Stringa:

arr){

list.add(a);

;

}

}

}

IO流

1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:

-

路径名:

c:

\temp\def.txt

package作业;

import;

publicclassTest1{

publicstaticvoidmain(String[]args){

Filefile=newFile("E:

\\新建文件夹");

String[]files=file.list();

for(Stringo:

files){

"文件名:

"+o);

"路径:

"+file.getAbsolutePath()+"\\"+o);

"--------------------------------------");

}

}

}

2、编写一个java程序实现文件复制功能,要求将d:

/io/copysrc.doc中的内容复制到d:

/io/copydes.doc中。

package作业;

import;

import;

import;

/**

*文件的拷贝

*/

publicclassTest2{

publicstaticvoidmain(String[]args){

Filefile=newFile("file\\abc.txt");

Filefile_back=newFile("file\\abc_back.txt");

try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){

intb=-1;

while((b=fi.read())!

=-1){

fo.write(b);

}

}catch(Exceptione){

e.printStackTrace();

}

"拷貝成功!

");

}

}

3、创建c:

/test.txt文件并在其中输入"helloworld"

创建一个输入流读取该文件中的文本

并且把小写的l变成大写L再利用输出流写入到d:

\test.txt中

实现步骤:

1.在本地硬盘C盘下创建一个文件test.txt

2.创建一个包含main()方法的类,并在main中编写代码

3.运行代码并且测试结果

实现过滤器的功能

效果显示:

package作业;

import;

import;

import;

import;

import;

import;

/**

*替换文件中的小写字母l为大写字母L

*/

publicclassTest3{

publicstaticvoidmain(String[]args){

FileReaderf;

Strings="";

BufferedReaderbr=null;

try{

f=newFileReader("F:

\\test.txt");

br=newBufferedReader(f);

s=br.readLine();

"源文件"+s);

s=s.replace("l","L");

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}finally{

try{

br.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

BufferedWriterbw=null;

;

try{

FileWriterfw=newFileWriter("F:

\\test.txt");

bw=newBufferedWriter(fw);

bw.write(s);

}catch(IOExceptione){

e.printStackTrace();

}finally{

try{

bw.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

4、在程序中创建一个Student类型的对象,并把对象信息保存到d:

/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:

package作业;

import;

publicclassStudent{

privateintid;

privateStringname;

privateDatebirth;

publicStudent(){

super();

}

publicStudent(intid,Stringname,Datebirth){

super();

this.id=id;

this.name=name;

this.birth=birth;

}

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicDategetBirth(){

returnbirth;

}

publicvoidsetBirth(Datebirth){

this.birth=birth;

}

@Override

publicStringtoString(){

return"Student[id="+id+",name="+name+",birth="+birth+"]";

}

}

publicclassSerializationExe

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2