《Java基础程序设计》_编程题

上传人:飞****9 文档编号:127629362 上传时间:2020-04-04 格式:DOC 页数:29 大小:33.17KB
返回 下载 相关 举报
《Java基础程序设计》_编程题_第1页
第1页 / 共29页
《Java基础程序设计》_编程题_第2页
第2页 / 共29页
《Java基础程序设计》_编程题_第3页
第3页 / 共29页
《Java基础程序设计》_编程题_第4页
第4页 / 共29页
《Java基础程序设计》_编程题_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《《Java基础程序设计》_编程题》由会员分享,可在线阅读,更多相关《《Java基础程序设计》_编程题(29页珍藏版)》请在金锄头文库上搜索。

1、第一章 1、请使用Eclipse编写一个程序,程序运行后,在控制台输出“这是我的第一个Java程序”。public class FirstJava public static void main(String args) System.out.println(这是我的第一个Java程序);第二章 1、请编写一个程序,计算100以内所有奇数的和。提示:1) 使用循环语句实现自然数199的遍历。2) 在遍历过程中,通过条件判断当前遍历的数是否为偶数,如果是,就continue,如果是奇数进行叠加运算。public class Demo01 public static void main(Strin

2、g args) int sum = 0;for (int x = 1; x 100; x+) if (x % 2 = 0) continue;sum += x;System.out.println(sum = + sum);2、定义一个函数,找出数组中的最大数或最小数。public class Demo02 public static void main(String args) int array = 5,10,-8,-2,-500,50,200;/最大数int max = array0;for (int i = 1; i max)max = arrayi;System.out.printl

3、n(数组中最大的数是:+max);/最小数int min = array0;for (int i = 1; i array.length; i+) if(arrayi min)min = arrayi;System.out.println(数组中最小的数是:+min);第三章1、编写一个程序,要求创建一个Student类,添加name和age属性,为该属性自动添加相应的getter和setter方法,并给出有参和无参的构造方法。public class Student private String name;private int age;public Student() public Stu

4、dent(String name, int age) super();this.name = name;this.age = age;public String getName() return name;public void setName(String name) this.name = name;public int getAge() return age;public void setAge(int age) this.age = age;2、编写一个类,类中定义一个静态方法,用于求两个整数的和。请按照以下要求设计一个测试类Demo01,并进行测试。要求如下:1)Demo01类中有一

5、个静态方法get(int a,int b)该方法用户返回参数a、b两个整数的和;2)在main()方法中调用get方法并输出计算结果。public class Demo01 public static int getSum(int a, int b) return a + b;public static void main(String args) int result = Demo01.getSum(2, 3);System.out.println(result);第四章1、定义一个抽象类Car,在该类中包含一个抽象方法run()。分别定义一个Bike类和Bus类继承自Car,在重写的run

6、()方法中分别输出一句话。定义测试类,调用Bike类和Bus类中的方法。abstract class Carabstract void run();class Bike extends Carvoid run() System.out.println(自行车在行驶);class Bus extends Carvoid run() System.out.println(公交车在行驶);public class Demo01 public static void main(String args) Bike bike = new Bike();bike.run();Bus bus = new Bu

7、s();bus.run();2、编写一个程序,模拟计算机的PCI插槽以及各种插卡。主板上的插槽就是计算机中的接口,它可以把显卡、网卡、声卡等都插在PCI插槽上。在计算机启动主板时,这些插槽中的卡也随之启动;关机时,这些卡也随之停止工作。/ PCI接口interface PCI void start();void stop();/ 显卡class Graphics implements PCI public void start() System.out.println(显卡已开启);public void stop() System.out.println(显卡已停止);/ 网卡class N

8、etworkCard implements PCI public void start() System.out.println(网卡已开启);public void stop() System.out.println(网卡已停止);/ 声卡class SoundCard implements PCI public void start() System.out.println(声卡已开启);public void stop() System.out.println(声卡已停止);/ 主板class MainBoard public void PCICardStart(PCI p) p.sta

9、rt();public void PCICardStop(PCI p) p.stop();/ 电脑class Computer private PCI pciArr = new PCI4; / 电脑上的PCI插槽public void add(PCI usb) / 向电脑上安装一个PCI设备for (int i = 0; i pciArr.length; i+) / 循环遍历所有插槽if (pciArri = null) / 如果发现一个空的pciArri = usb; / 将usb设备装在这个插槽上break; / 装上之后结束循环public void turnOn() / 电脑的开机功能

10、for (int i = 0; i pciArr.length; i+) / 循环遍历所有插槽if (pciArri != null) / 如果发现有设备pciArri.start(); / 将PCI设备启动System.out.println(电脑开机成功);public void turnOff() / 电脑的开机功能for (int i = 0; i pciArr.length; i+) / 循环遍历所有插槽if (pciArri != null) / 如果发现有设备pciArri.stop(); / 将PCI设备启动System.out.println(电脑关机成功);public s

11、tatic void main(String args) Computer c = new Computer();c.add(new Graphics();c.add(new NetworkCard();c.add(new SoundCard();c.turnOn();c.turnOff();第五章1、编写一个程序,获取一个已知文件的扩展名。public class Demo01 public static void main(String args) System.out.println(getExtname(Person.java);public static String getExtn

12、ame(String filename)int index = filename.lastIndexOf(.);String extname = filename.substring(index+1);return extname;2、编写一个程序,接收一个字符串,将字符串中每个单词的首字母改为大写。public class Demo02 public static void main(String args) StringBuffer sbn = new StringBuffer(hellow world and happy new year);StringBuffer ss = new S

13、tringBuffer();String s = sbn.toString();String sb = s.split( );for (int i = 0; i sb.length; i+) sbi = sbi.substring(0, 1).toUpperCase() + sbi.substring(1);for (int i = 0; i sb.length; i+) ss.append(sbi);ss.append( );System.out.println(ss);第六章1、编写一个程序,向ArrayList集合中添加5个对象,然后使用迭代器输出集合中的对象。public class Demo01 pu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 管理论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号