《Java基礎(chǔ)程序設(shè)計(jì)》編程題_第1頁
《Java基礎(chǔ)程序設(shè)計(jì)》編程題_第2頁
《Java基礎(chǔ)程序設(shè)計(jì)》編程題_第3頁
《Java基礎(chǔ)程序設(shè)計(jì)》編程題_第4頁
《Java基礎(chǔ)程序設(shè)計(jì)》編程題_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第一章

1.請使用Eclipse編寫一個程序,程序運(yùn)行后,在控制臺輸出“這是我的第?個Java程序”。

publicclassFirstJava{

publicstaticvoidmain(String[]args){

System.out.printIn("這是我的第?個Java程序”);

第二章

1.請編寫一個程序,計(jì)算100以內(nèi)所有奇數(shù)的和。

提示:

1)使用循環(huán)語句實(shí)現(xiàn)自然數(shù)1?99的遍歷。

2)在遍歷過程中,通過條件判斷當(dāng)前遍歷的數(shù)是否為偶數(shù),如果是,就continue,如果是奇數(shù)進(jìn)

行會加運(yùn)算。

publicclassDemo01{

publicstaticvoidmain(String[]args){

intsum-0;

for(intx=1;x<100;x++)(

if(x%2==0){

continue;

}

sum+=x;

)

System,out.printIn(*'sum="+sum);

)

}

2.定義一個函數(shù),找出數(shù)組中的最大數(shù)或最小數(shù)。

publicclassDemo02{

publicstaticvoidmain(String[]args){

int[]array={5,L0,-8,-2,-500,50,200};

//最大數(shù)

intmax=array[0|;

for(inti=1;i<array.length;i++){

if(array[i]>max){

max=array[i];

}

)

System.out.printIn("數(shù)組中最大的數(shù)是:"+max);

〃最小數(shù)

intmin=array[0|;

for(inti■1;i<array.length;i++){

if(array[i]<min){

min=array[i];

}

}

System.out.printIn("數(shù)組中最小的數(shù)是:"+min);

}

}

第三章

1.編寫?個程序,要求創(chuàng)建?個Student類,添加name和age屬性,為該屬性自動添加相應(yīng)的

getter和setter方法,并給出有參和無參的構(gòu)造方法。

publicclassStudent{

privateStringname;

privateintage;

publicStudent(){

)

publicStudent(Stringname,intage)(

super();

=name;

this.age"age;

)

publicStringgetNameO(

returnname;

)

publicvoidsetName(Stringname){

-name;

)

publicintgetAge(){

returnage;

)

publicvoidsetAge(intage){

this.age=age;

)

)

2.編寫一個類,類中定義一個靜態(tài)方法,用于求兩個整數(shù)的和。

請按照以下要求設(shè)計(jì)一個測試類DemoOl,并進(jìn)行測試。要求如下:

1)DcmoOl類中有一個靜態(tài)方法gct(in(b)該方法用戶返回參數(shù)a、b兩個整數(shù)的和;

2)在main()方法中調(diào)用get方法并輸出計(jì)算結(jié)果。

publicclassDemo01{

publicstaticintgetSumdnta,intb){

returna+b;

}

publicstaticvoidmain(String[]args){

intresult=DemoOl.getSum(2,3);

System.out.printin(result);

}

)

第四章

1.定義一個抽象類Car,在該類中包含一個抽象方法run()o分別定義一個Bike類和Bus類維承自

Car,在重寫的run()方法中分別輸出一句話。定義測試類,調(diào)用Bike類和Bus類中的方法。

abstractclassCar(

abstractvoidrun();

)

classBikeextendsCar{

voidrun()(

System.out.printIn("自行車在行駛");

)

)

classBusextendsCar{

voidrun()(

System.out.printin("公交車在行駛”);

}

)

publicclassDemoOl{

publicstaticvoidmain(String[]args){

Bikebike=newBLke();

bike.run();

Busbus=newBus();

bus.run();

)

)

classMainBoard{

publicvoidPCICardStart(PCIp){

p.start();

)

publicvoidPCICardstop(PCIp){

p.stopO;

)

}

//電腦

classComputer{

privatePCI(]pciArr=newPCI[4];//電腦上的PCI插槽

publicvoidadd(PCIusb){//向電腦上安裝一個PCI設(shè)備

for(inti=0;i<pciArr.length;i++)(〃循環(huán)遍歷所有插槽

if(pciArr[i)-null)(//如果發(fā)現(xiàn)一個空的

pciArr[i]=usb;//將usb設(shè)備裝在這個插槽上

break;//裝上之后結(jié)束循環(huán)

)

}

)

publicvoidturnOn(){//電腦的開機(jī)功能

for(inti=0;i<pciArr.length;i++){//循環(huán)遍歷所有插槽

if(pciArr[i]!=null){//如果發(fā)現(xiàn)有設(shè)備

pciArr[i].start();//將PCI設(shè)備啟動

}

)

System.out.printin("電腦開機(jī)成功");

)

publicvoidturnOff(){//電腦的開機(jī)功能

for(inti=0;i<pciArr.length;i++){//搐環(huán)遍歷所有插槽

if(pciArr[i]!=null)(//如果發(fā)現(xiàn)有設(shè)備

pciArr[i].stop();〃將PCI設(shè)備啟動

)

}

System.out.printIn("電腦關(guān)機(jī)成功“);

)

publicstaticvoidmain(String[]args){

Computerc=newComputer();

c.add(newGraphics());

c.add(nevjNetxvorkCard());

c.add(newSoundCard());

c.turnOn();

c.turnOff();

)

第五章

1.編寫一個程序,獲取一個已知文件的擴(kuò)展名。

publicclassDemo01{

publicstaticvoidmain(String[]args){

System.out.printin(getExtname("Person.java"));

}

publicstaticStringgetExtname(Stringfilename){

intindex=filename.lastlndexOf

Stringextname=filename.substring(index+1);

returnextname;

}

}

2.編寫一個程序,接收一個字符串,將字符串中每個單詞的苜字母改為大寫。

publicclassDemo02{

publicstaticvoidmain(String[]args){

StringBuffersbn=newStringBuffer("hellowworldandhappynewyear");

StringBufferss=newStringBuffer;

Strings=sbn.toString();

String[]sb-s.split("");

for(inti=0;i<sb.longth;i++){

sb[i]=sb[i].substring(0,1).toUpperCaseO+sb[i].substring(1);

)

for(inti=0;i<sb.length;i++){

ss.append(sb[i]);

ss.append("":;

}

System.out.printIn(ss);

}

}

第八草

1.編寫一個程序,向ArrayList集合中添加5個對象,然后使用迭代器輸出集合中的對象.

publicclassDemo01{

publicstaticvoidmain(String[]args){

Listlist=newArrayList();

list.add(?'zhangsan");

list.addC'lisi");

list.add("wangwu"t;

list.add("zhaoliu',);

Iteratorit=liss.iterator();

while(it.hasNext()){

Objectobject=(Object)it.next();

System.out.printIn(object);

)

2.編寫一個程序,向Properties集合存入5個配置項(xiàng),并迭代出所有的配置項(xiàng)。

publicclassDemo02{

publicstaticvoidmain(String[]args){

Propertiesprops=newProperties();

props.setProperty("username","zhangsan");

props.setProperty("password'*,"1234560);

props.setProperty("email","zhangsan@");

Enumeratione=pertyNames();

while(e.hasMoreElements()){

Stringname=(String)e.nextElement():

Stringvalue=props.getProperty(name);

System.out.prmtln(name+"="+value);

)

)

)

第七章

編寫一個程序,使用定義數(shù)組的方式將D盤中的程序拷貝到E盤中。

publicclassDemo01{

publicstaticvoidmain(String[]args)throwslOException{

//創(chuàng)建輸入流與源文件相關(guān)聯(lián)

Inputstreamin=newFileInputstream("D:\\jdk-7u60-windows-i586.exe")

//創(chuàng)建輸出流與目標(biāo)文件相關(guān)聯(lián)

Outputstreamout■

newFileOutputStream("E:\\jdk-7u60-windows-i586.exe");

longstart=System.currentTimeMillis();

copyByBuffer(in,out);

longend=System.currentTimeMillis();

System.out.printin("耗時:"+(end-start)+"玄秒");

in.close();

out.close();

}

//定義byte數(shù)組作為緩沖區(qū)進(jìn)行拷貝

privatestaticvoidcopyByBuffer(Inputstreamin,Outputstreamout)

throwslOException{

byte[]buffer=newbyte[1024];

intlen;

while((len?in.read(buffer))!?-l){

out.write{buffer,0,len);

}

}

}

2.編寫一個程序,遍歷出指定目錄下所有的.java文件,并將其絕對路徑存入一個list集合中輸

出。

publicclassDemo02(

publicstaticvoidmain(String[]args){

//創(chuàng)建一個File對象封裝路徑d:

Filedir=

newFile("D:WeclipseWorkspaceWJavaBasicWorkspace\\test\\srcH);

//創(chuàng)建一個List集合用于存放路徑

List<String>list=newArrayList<String>();

//調(diào)用方法

listAllJavaFiles(dir,list);

//輸出

for{Stringfilename:list)

System.out.printIn(filename);

}

staticvoidlistAllJavaFiles(Filedir,List<String>list){

//獲得dir目錄中所有的子文件

File[]files=dir.listFiles();

//如果數(shù)組為null說明dir是不可打開的目錄或者不是目錄

if(files==null)(

return;

}

//遍歷數(shù)組獲得子文件

for(Filefile:files){

//判斷

if(file.isDirectory()){

//說明文件是目錄需要遞歸調(diào)用

listAllJavaFiles(file,list);

}else{

//說明是標(biāo)準(zhǔn)文件

//判斷是不是java文件如果是存入list

if(file.getName().endsWith(".java"))

list.add(file.getAbsolutePath());

)

)

)

)

)

第八章

1.設(shè)計(jì)一個窗體,窗體中有一個按鈕,當(dāng)單擊按鈕時,可以添加其它按鈕,并按數(shù)字依次出

現(xiàn),當(dāng)單擊數(shù)字按鈕時,被單擊按鈕消失,此窗體帶關(guān)閉功能。

publicclassDemo01{

staticintnum=1;

publicstaticvoidmain(String[]args){

//初始化一個frame

Frameframe=newFrame("myframe");

//設(shè)置大小

frame.setsize(300.300);

//設(shè)苴位置

frame.setLocation(100,100);

//設(shè)置布局管理

frame.setLayout(newFlowLayout());

//添加一個按鈕

Buttonbtn=newButton("按鈕");

frame.add(btn);

frame.setvisible(二rue);

//添加事件監(jiān)聽器監(jiān)聽窗口事件

//通過繼承WindowAdapter(適自己淵)來實(shí)現(xiàn)WindowListener

frame.addVIindowListener(newWindowAdapter(){

publicvoidw.ndowClosing(WindowEvente){

e.getWindow().dispose();

)

});

//為按鈕添加事件鼠標(biāo)事件事件源是按鈕

btn.addMouseListener(newMouseAdapter(){

publicvoidmousedicked(MouseEvente)(

//鼠標(biāo)雅擊了

//獲得事件源btn

Buttonbtn=(Button)e.getComponent(:;

//獲得btn所在的容器frame

Frameframe"(Frame)btn.getParent();

//添加一個新的btn

ButtonnewBtn=newButton("按鈕”+num++);

frame.add:newBtn);

//刷新frame的顯示

frame.setvisible(true);

//為newBtn添加事件

newDtn.addMouseListener(newMouseAdapter(){

publicvoidmouseClicked(MouseEvente){

Buttonbtn"(Button)e.getComponent();

btn.getParent().remove(btn);

}

!);

}

1);

}

}

2.編寫一個小游戲:設(shè)計(jì)一餐體,窗體中上下有兩個名稱為“你來點(diǎn)我?。?!”?。。。?!”的按鈕,當(dāng)

鼠標(biāo)移動到上面按鈕時,上面按鈕消失,下面的顯示;移動到下面時,下面消失,上面的顯示。

publicclassDemo02(

staticintnum-1;

publicstaticvoidmain(String[]args){

//初始化一個frame

finalFrameframe-newFrame("myframe");

//設(shè)置大小

frame.setSize(300.300);

//設(shè)置位置

frame.setLocation(100,100);

//添加一個按鈕

finalButtonbtnl=newButton("你來點(diǎn)我?。。。。。。。。?!");

frame.add(btnl,BorderLayout.NORTH);

finalButtonbtn2=newButton("你來點(diǎn)我?。。。。。。。。?!");

frame.add(btn2,BorderLayout.SOUTH);

btn2.setVisible(false);

frame.setVisible(Lrue);

//添加事件監(jiān)聽器,監(jiān)聽窗口事件,

〃通過繼承WindoivAdapter(適配器)來實(shí)現(xiàn)WindowListeier

frame.addWindowListener(newWindowAdapter()(

publicvoidwmdowClosing(WindowEvente){

e.getWindow().dispose();

}

!);

btnl.addMouseListener(newMouseAdapter(){

publicvoidmouseEntered(MouseEvente)(

btnl.setVisible(false);

btn2.setVisible(true);

frame.setVisible(true);

}

});

btn2.addMouseListener(newMouseAdapter(){

publicvoidmouseEntered(MouseEvente){

btn2.setVisible(false);

btnl.setVisible(true);

frame.setVisible(true);

}

});

}

)

第九章

1.已知在數(shù)據(jù)庫jdbc中有一個名稱為user的表,表中包含三個字段id,name,password0要求

使用JDBCUlils工具類編寫一個程序,使程序執(zhí)行后,可以向user表中插入一條數(shù)據(jù)。

(1)JDBCUtils的代碼如下:

importjava.sql.

/**

*工具類

publicclassJDBCUtils{

//加載驅(qū)動,并建立數(shù)據(jù)庫連接

publicstaticConnectiongetConnection()throwsSQLException,

ClassNotFoundException{

Class.forName("con.mysql.jdbc.Driver'1);

Stringurl="jdbc:mysql://localhost:3306/jdb3";

Stringusername-"root";

Stringpassword="itcast";

Connectionconn=DriverManager.getConnection(urlzusername,password);

returnconn;

)

//關(guān)閉數(shù)據(jù)庫連接,擇放資源

publicstaticvoidrelease(Statementstmt,Connectionconn){

if(stmtJ=null)(

try{

stmt.close();

}catch(SQLExceptione)(

e.printStackTrace();

}

stmt=null;

}

if(conn!-null)(

try{

conn.close();

}catch(SQLExceptione){

e.printStackTrace();

}

conn=null;

}

}

publicstaticvoidrelease{ResultSetrs,Statementstmt.Connectionconn)(

if(rs!=null)(

try{

rs.close();

}catch(SQLExceptione){

e.printStackTrace();

}

rs=null;

)

release(stmt,conn);

)

)

(2)實(shí)體類User的代碼如下:

publicclassUser(

privateintid;

privateStringname;

privateStringpassword;

publicintgetld(){

returnid;

}

publicvoidsetld(intid){

this.id=id;

)

publicStringgetName(){

returnname;

)

publicvoidsetName(Stringname){

=name;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

)

<3)UnuuDau的代碼如下;

importjava.sql.

publicclassUserDao{

//添加數(shù)據(jù)

publicvoidaddUser(Useruser){

Connectionconn=null;

Statementstmt=null;

try(

//獲得數(shù)據(jù)的連接

conn=JDBCUtils.getConnection();

//獲得Statement對象

stmt=conn.createStatement();

//發(fā)送SQL語句

Stringsql="INSERTINTOuser(id,name,password)"VALUES("

+user.getld()+",'+user.getName()+

+user.getPassword()+"')";

intnum=stmt.executeUpdate(sql);

if(num>0){

System.out.printin("插入數(shù)據(jù)成功!");

}

}catch(Exceptione){

e.printStackTrace();

}finally{

JDBCUtils.release(stmt,conn);

)

}

(4)測試類UserTest的代碼如下:

publicclassUserTest{

publicstaticvoidmain(String[]args){

UserDaouserDao=newUserDao();

Useruser-newUser();

user.setld(l);

user.setName("itcast");

user.setPassword(*1234");

userDao.addUser(user);

}

)

第十章

1.編寫?個程序,創(chuàng)建兩個線程,要求分別輸出26個字母。在輸結(jié)果時,要顯示是哪個線程輸出

的字母。

publicclassDemo01(

publicstaticvoidmain(String[]args){

Testtl-newTes二("線程一");

Testt2=newTes二("線程二”);

tl.start();

t2.start();

}

)

classTestextendsThread(

privatecharch-'a,;

publicTest(Stringname)(

setName(name);

}

publicvoidrun(){

chartemp;

for(inti-0;i<26;i++)(

temp=(char)(ch+i);

System.out.prmtln(this.getName()+":n+二emp);

)

)

)

2.編寫?個程序,使用Runnable接口的方式創(chuàng)建三個線程,分別輸出從0到10的數(shù),每個

線程之間延遲500毫秒,要求渝出的結(jié)果如下所示:

線程一:0,1,2,3,4,5,6,7,8,9,10

線程三:0,1,2,3,4.5.6,7,8,9.10

線程二:0,1,2,3,4,567,8,9,10

classMyThreadimplementsRunnable{

Objectlock-newObject();//定義任意一個對象,用作同步代碼塊的鎖

publicvoidrun(){

synchronized(lock)(//定義同步代碼塊

System.out.println();

try{

System.out.print(Thread.currentThreadO.getName()+

for(inti=0;i<=10;i++){

System.out.print(i);

if(i!=10){

System.out.print

}

)

Thread.sleep(500);//經(jīng)過的線程休眠500烹秒

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

)

)

publicclassDemo02{

publicstaticvoidmain(String[]args){

MyThreadmyThread-newMyThread();

newThread(myThread,*").start();

newThread(myThread,"線程二").start;

newThread(myThread,"線程三").start();

)

第十一章

1.簡述TCP/IP協(xié)議的層次結(jié)構(gòu)。

TCP/IP協(xié)議的層次結(jié)構(gòu)比較簡單,共分為四層,分別是鏈路層、網(wǎng)絡(luò)層、傳輸層和應(yīng)用層。

其中鏈路層也稱為網(wǎng)絡(luò)接口層,該層負(fù)責(zé)監(jiān)視數(shù)據(jù)在主機(jī)和網(wǎng)絡(luò)之間的交換。網(wǎng)絡(luò)層也稱網(wǎng)

絡(luò)互聯(lián)層,是整個TCP/IP協(xié)議的核心,它主要用于將傳輸?shù)臄?shù)據(jù)進(jìn)行分組,將分組數(shù)據(jù)發(fā)送

到目標(biāo)計(jì)算機(jī)或者網(wǎng)絡(luò)。傳輸層主要使網(wǎng)絡(luò)程序進(jìn)行通信,在進(jìn)行網(wǎng)絡(luò)通信時,可以采用

TCP協(xié)議,也可以采用UDP協(xié)議。應(yīng)用層主要負(fù)責(zé)應(yīng)用程序的協(xié)議,例如HTTP協(xié)議、FTP

協(xié)議等。

2.簡述你對IP地址的認(rèn)識。

要想使網(wǎng)絡(luò)中的計(jì)算機(jī)能夠進(jìn)行通信,必須為每臺計(jì)算機(jī)指定一個標(biāo)識號,通過這個標(biāo)識號

來指定接收數(shù)據(jù)的計(jì)算機(jī)或者發(fā)送數(shù)據(jù)的計(jì)算機(jī)。在TCP/IP協(xié)議中,這個標(biāo)識號就是IP地

址,它可以唯一標(biāo)識一臺計(jì)算機(jī),目前,1P地址廣泛使用的版本是IPv4,它是由4個字節(jié)大小

的二進(jìn)制數(shù)來表示,由于二進(jìn)制形式表示的IP地址非常不便記憶和處理,因此通常會將IP

地址寫成卜進(jìn)制的形式,每個字節(jié)用一個卜進(jìn)制數(shù)字(0-255)表示,數(shù)字間用符號分開,如

“"。

1.編寫一個程序,實(shí)現(xiàn)多線程的UDP網(wǎng)絡(luò)通信。要求如下:

1)編寫數(shù)據(jù)接收類ReceiveThread,該類實(shí)現(xiàn)了Runnable接口,重寫run()方法實(shí)現(xiàn)不斷

接受客戶端發(fā)送數(shù)據(jù)的功能。

2)編寫數(shù)據(jù)發(fā)送類SendThread.該類同樣實(shí)現(xiàn)了Runnable接口,重寫run。方法實(shí)現(xiàn)通過

鍵盤錄入數(shù)據(jù),將數(shù)據(jù)向接收端發(fā)送的功能。

3)編寫測試類TcsiUDP.在main。方法中,同時啟動接收端和發(fā)送端程序。

(1)數(shù)據(jù)接收類的代碼如下所示:

importjava.io.lOExcoption;

import.Datagrampacket;

import.DatagramSocket;

publicclassReceiveT上readimplementsRunnable{

publicvoidrun(){

try(

//創(chuàng)建接收端Socket對象

DatagramSocketds=newDatagramsocket(10086);

//創(chuàng)建數(shù)據(jù)包

while(true){

byte[]bys-newbyte[1024];

DatagramPacketdp=newDatagramPacket(bys,bys.length);

//接收數(shù)據(jù)

ds.receive(dp);

//解析數(shù)據(jù)

Stringip-dp.getAddress().getHostAddress();

Strings=newString(dp.getData(),0,dp.getLength());

System.out.println("接收端:從"+ip+"主機(jī)接收到的數(shù)據(jù)是:"+s

if("bye".equals(s))(

System.out.printin(”******聊天室關(guān)閉******");

ds.close();

break;

)

}

}catch(lOExceptione){

e.printStackTrace();

}

}

}

<2)數(shù)據(jù)發(fā)送類的代碼如下所示:

importjava.io.BufferedReader;

importjava.io.lOException;

importjava.io.InputStreamReader;

import.DatagramPacket;

import.Datagramsocket;

import.InetAcdress;

publicclassSendThreadimplementsRunnable(

publicvoidrun(){

try(

//創(chuàng)建發(fā)送端Sccket對象

Datagramsocketds=newDatagramsocket();

//封裝鍵盤錄入

BufferedReaderbr=newBufferedReader(newInputStreamReader(

System.in));

//創(chuàng)建數(shù)據(jù),并可包

Stringline=null;

while((line=br.readLine())!=null){

byte[]bys■line.getBytes();

DatagramPacketdp=newDatagramPacket(bys,bys.length,

IretAddress.getByName("localhost"),10086);

ds.send(dp);

if("bye".equals(line)){

//糅放資源

br.close();

ds.close();

break;

}

//釋放資源

br.close();

ds.close();

}catch(lOExceptione){

e.printStackTrace();

}

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論