《Java基础程序设计》编程题Word格式文档下载.docx
《《Java基础程序设计》编程题Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《《Java基础程序设计》编程题Word格式文档下载.docx(25页珍藏版)》请在冰豆网上搜索。
for(inti=1;
i<
array.length;
i++){
if(array[i]>
max){
max=array[i];
数组中最大的数是:
"
+max);
//最小数
intmin=array[0];
if(array[i]<
min){
min=array[i];
}
数组中最小的数是:
+min);
第三章
1、编写一个程序,要求创建一个Student类,添加name和age属性,为该属性自动添加相应的getter和setter方法,并给出有参和无参的构造方法。
publicclassStudent{
privateStringname;
privateintage;
publicStudent(){
publicStudent(Stringname,intage){
super();
this.name=name;
this.age=age;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
publicintgetAge(){
returnage;
publicvoidsetAge(intage){
2、编写一个类,类中定义一个静态方法,用于求两个整数的和。
请按照以下要求设计一个测试类Demo01,并进行测试。
要求如下:
1)Demo01类中有一个静态方法get(inta,intb)该方法用户返回参数a、b两个整数的和;
2)在main()方法中调用get方法并输出计算结果。
publicclassDemo01{
publicstaticintgetSum(inta,intb){
returna+b;
intresult=Demo01.getSum(2,3);
System.out.println(result);
第四章
1、定义一个抽象类Car,在该类中包含一个抽象方法run()。
分别定义一个Bike类和Bus类继承自Car,在重写的run()方法中分别输出一句话。
定义测试类,调用Bike类和Bus类中的方法。
abstractclassCar{
abstractvoidrun();
classBikeextendsCar{
voidrun(){
自行车在行驶"
}
classBusextendsCar{
公交车在行驶"
Bikebike=newBike();
bike.run();
Busbus=newBus();
bus.run();
}
2、编写一个程序,模拟计算机的PCI插槽以及各种插卡。
主板上的插槽就是计算机中的接口,它可以把显卡、网卡、声卡等都插在PCI插槽上。
在计算机启动主板时,这些插槽中的卡也随之启动;
关机时,这些卡也随之停止工作。
//PCI接口
interfacePCI{
voidstart();
voidstop();
//显卡
classGraphicsimplementsPCI{
publicvoidstart(){
显卡已开启"
publicvoidstop(){
显卡已停止"
//网卡
classNetworkCardimplementsPCI{
网卡已开启"
网卡已停止"
//声卡
classSoundCardimplementsPCI{
声卡已开启"
声卡已停止"
//主板
classMainBoard{
publicvoidPCICardStart(PCIp){
p.start();
publicvoidPCICardStop(PCIp){
p.stop();
//电脑
classComputer{
privatePCI[]pciArr=newPCI[4];
//电脑上的PCI插槽
publicvoidadd(PCIusb){//向电脑上安装一个PCI设备
for(inti=0;
pciArr.length;
i++){//循环遍历所有插槽
if(pciArr[i]==null){//如果发现一个空的
pciArr[i]=usb;
//将usb设备装在这个插槽上
break;
//装上之后结束循环
publicvoidturnOn(){//电脑的开机功能
if(pciArr[i]!
=null){//如果发现有设备
pciArr[i].start();
//将PCI设备启动
电脑开机成功"
publicvoidturnOff(){//电脑的开机功能
pciArr[i].stop();
电脑关机成功"
Computerc=newComputer();
c.add(newGraphics());
c.add(newNetworkCard());
c.add(newSoundCard());
c.turnOn();
c.turnOff();
第五章
1、编写一个程序,获取一个已知文件的扩展名。
publicstaticvoidmain(String[]args){
System.out.println(getExtname("
Person.java"
));
publicstaticStringgetExtname(Stringfilename){
intindex=filename.lastIndexOf("
."
Stringextname=filename.substring(index+1);
returnextname;
2、编写一个程序,接收一个字符串,将字符串中每个单词的首字母改为大写。
StringBuffersbn=newStringBuffer("
hellowworldandhappynewyear"
StringBufferss=newStringBuffer("
Strings=sbn.toString();
String[]sb=s.split("
"
sb.length;
sb[i]=sb[i].substring(0,1).toUpperCase()+sb[i].substring
(1);
ss.append(sb[i]);
ss.append("
System.out.println(ss);
第六章
1、编写一个程序,向ArrayList集合中添加5个对象,然后使用迭代器输出集合中的对象。
Listlist=newArrayList();
list.add("
zhangsan"
lisi"
wangwu"
zhaoliu"
Iteratorit=list.iterator();
while(it.hasNext()){
Objectobject=(Object)it.next();
System.out.println(object);
2、编写一个程序,向Properties集合存入5个配置项,并迭代出所有的配置项。
Propertiesprops=newProperties();
props.setProperty("
username"
"
password"
123456"
email"
zhangsan@"
Enumeratione=props.propertyNames();
while(e.hasMoreElements()){
Stringname=(String)e.nextElement();
Stringvalue=props.getProperty(name);
System.out.println(name+"
="
+value);
第七章
1、编写一个程序,使用定义数组的方式将D盘中的程序拷贝到E盘中。
publicstaticvoidmain(String[]args)throwsIOException{
//创建输入流与源文件相关联
InputStreamin=newFileInputStream("
D:
\\jdk-7u60-windows-i586.exe"
//创建输出流与目标文件相关联
OutputStreamout=
newFileOutputStream("
E:
longstart=System.currentTimeMillis();
copyByBuffer(in,out);
longend=System.currentTimeMillis();
耗时:
+(end-start)+"
毫秒"
in.close();
out.close();
//定义byte数组作为缓冲区进行拷贝
privatestaticvoidcopyByBuffer(InputStreamin,OutputStreamout)
throwsIOException{
byte[]buffer=newbyte[1024];
intlen;
while((len=in.read(buffer))!
=-1){
out.write(buffer,0,len);
2、编写一个程序,遍历出指定目录下所有的.java文件,并将其绝对路径存入一个list集合中输出。
publicstaticvoidmain(String[]args){
//创建一个File对象封装路径d:
Filedir=
newFile("
\\eclipseWorkspace\\JavaBasicWorkspace\\test\\src"
//创建一个List集合用于存放路径
List<
String>
list=newArrayList<
();
//调用方法
listAllJavaFiles(dir,list);
//输出
for(Stringfilename:
list)
System.out.println(filename);
staticvoidlistAllJavaFiles(Filedir,List<
list){
//获得dir目录中所有的子文件
File[]files=dir.listFiles();
//如果数组为null说明dir是不可打开的目录或者不是目录
if(files==null){
return;
//遍历数组获得子文件
for(Filefile:
files){
//判断
if(file.isDirectory()){
//说明文件是目录需要递归调用
listAllJavaFiles(file,list);
}else{
//说明是标准文件
//判断是不是java文件如果是存入list
if(file.getName().endsWith("
.java"
)){
list.add(file.getAbsolutePath());
}
第八章
1、设计一个窗体,窗体中有一个按钮,当单击按钮时,可以添加其它按钮,并按数字依次出现,当单击数字按钮时,被单击按钮消失,此窗体带关闭功能。
staticintnum=1;
//初始化一个frame
Frameframe=newFrame("
myframe"
//设置大小
frame.setSize(300,300);
//设置位置
frame.setLocation(100,100);
//设置布局管理
frame.setLayout(newFlowLayout());
//添加一个按钮
Buttonbtn=newButton("
按钮"
frame.add(btn);
frame.setVisible(true);
//添加事件监听器监听窗口事件
//通过继承WindowAdapter(适配器)来实现WindowListener
frame.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
e.getWindow().dispose();
});
//为按钮添加事件鼠标事件事件源是按钮
btn.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
//鼠标单击了
//获得事件源btn
Buttonbtn=(Button)e.getComponent();
//获得btn所在的容器frame
Frameframe=(Frame)btn.getParent();
//添加一个新的btn
ButtonnewBtn=newButton("
+num++);
frame.add(newBtn);
//刷新frame的显示
frame.setVisible(true);
//为newBtn添加事件
newBtn.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
Buttonbtn=(Button)e.getComponent();
btn.getParent().remove(btn);
}
});
2、编写一个小游戏:
设计一窗体,窗体中上下有两个名称为“你来点我啊!
!
”的按钮,当鼠标移动到上面按钮时,上面按钮消失,下面的显示;
移动到下面时,下面消失,上面的显示。
finalFrameframe=newFrame("
finalButtonbtn1=newButton("
你来点我啊!
frame.add(btn1,BorderLayout.NORTH);
finalButtonbtn2=newButton("
frame.add(btn2,BorderLayout.SOUTH);
btn2.setVisible(false);
//添加事件监听器,监听窗口事件,
//通过继承WindowAdapter(适配器)来实现WindowListener
btn1.addMouseListener(newMouseAdapter(){
publicvoidmouseEntered(MouseEvente){
btn1.setVisible(false);
btn2.setVisible(true);
btn2.addMouseListener(newMouseAdapter(){
btn2.setVisible(false);
btn1.setVisible(true);
第九章
1、已知在数据库jdbc中有一个名称为user的表,表中包含三个字段