CS 111 Homework No 3 (Java).doc
上传人:sy****28 上传时间:2024-09-12 格式:DOC 页数:4 大小:38KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

CS 111 Homework No 3 (Java).doc

CS111HomeworkNo3(Java).doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

CS111HomeworkNo.3(Java)SolutionNotethatyoumayhavedifferent,yetcorrectsolutions!(Problem3.4)WriteaJavaapplicationthatcreatesandprintsarandomphonenumberoftheformxxx-xxx-xxxx.Includethedashesintheoutput.Donotletthefirstdigitscontainan8or9(butdon’tbemorerestrictivethanthat),andmakesurethatthesecondsetofthreedigitsisnotgreaterthan742.Hint:Thinkthroughtheeasiestwaytoconstructthephonenumber.Eachdigitdoesnothavetobedeterminedseparately.importjava.util.Random;publicclassHwk3_Q1/*phonenumberxxx-xxx-xxxx*/{publicstaticvoidmain(String[]args){Randomgenerator=newRandom();/*generatefirstpartwhichdoesnotcontaina8or9*/intnum1,num2,num3;num1=generator.nextInt(8);/*generatearandomintbetween0-7*/num2=generator.nextInt(8);/*generatearandomintbetween0-7*/num3=generator.nextInt(8);/*generatearandomintbetween0-7*/intpart1=num1*100+num2*10+num3;/*generatesecondpartwhichisnotgreaterthan742*/intpart2=generator.nextInt(743);/*generatethirdpartof4digits*/intpart3=generator.nextInt(1000);/*printtheresultsintherequiredformat:printpreceding0'salso*/System.out.printf("%03d-%03d-%04d\n",part1,part2,part3);}}(Problem5.15)Designandimplementanapplicationthatreadsastringfromtheuser,thendeterminesandprintshowmanyofeachlowercasevowel(a,e,I,o,andu)appearintheentirestring.Haveaseparatecounterforeachvowel.Alsocountandprintthenumberofnon-vowelcharacters.importjava.util.*;publicclassHwk3_Q2{publicstaticvoidmain(String[]args){Scanneroku=newScanner(System.in);/*readonelineofstring*/System.out.println("Pleasetypeyourtextandpresstheenterkey:");Stringtxt=oku.nextLine();/*defineandinitializethesixcounters*/intcnt_a=0;intcnt_e=0;intcnt_i=0;intcnt_o=0;intcnt_u=0;intcnt_others=0;//counterfornon-vowelcharacters/*defineaintegerforholdingthenumberofcharactersinthestring*/intcnt_all=txt.length();/*checkallchar'sintxtandupdatethe6countersappropriately*/for(intk=0;k<cnt_all;k++){charc=txt.charAt(k);if(c=='a')cnt_a++;els