好文档 - 专业文书写作范文服务资料分享网站

2015河南理工大学java期末考试编程题

天下 分享 时间: 加入收藏 我要投稿 点赞

2.1public class Test01 {

public static void main(String[] args) { int sum = 0;

for (inti = 1; i< 100; i++) { if (i % 2 != 0) sum += i; }

System.out.println(sum);}} 2、public class Test02 {

public static void main(String args[]) { int y = function(0); System.out.println(y);

} public static int function(int x) { int y;

if (x > 0) { y = x + 3;

} else if (x == 0) { y = 0; } else {

y = x * x - 1; }return y; }}

3、public class Test03 {

public static void main(String[] args) { int[] arr = { 25, 24, 12, 76, 101, 96, 28 }; for (inti = 0; i

for (int j = 0; j arr[j + 1]) { // 比较相邻元素 // 下面的三行代码用于交换两个元素 int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;}} }

for (inti = 0; i

System.out.print(arr[i] + \打印元素和空格 }}}

3.1class Student { private String name; private double grade; public Student() {}

public Student(String name, double grade) {

this.name = name; this.grade = grade; }

public String getName() { return name;

}public void setName(String name) { this.name = name;}

public double getGrade() { return grade; }

public void setGrade(double grade) { this.grade = grade; }} public class Test01 {

public static void main(String[] args) { Student stu1 = new Student(); stu1.setName(\stu1.setGrade(99);

Student stu2 = new Student(\}} 2、class Father {

private String name = \class Child {

public void introFather() {

System.out.println(\public class Test02 {

public static void main(String[] args) {

Father.Child child = new Father().new Child(); child.introFather();}} 4.1class Student { public String name; public int age;

public Student(String name,int age){ this.name=name; this.age=age;

}public void show(){

System.out.println(\class UnderGraduate extends Student{ public String degree;

public UnderGraduate(String name,intage,String degree){ super(name, age); this.degree=degree; }public void show(){

System.out.println(\\age: \degree: \

public class Test01{

public static void main(String[] args) {

Student student = new Student(\student.show();

UnderGraduateunderGraduate = new UnderGraduate(\20, \

underGraduate.show(); }} 2、

interface Shape {

double area(double givenValue); }class Square implements Shape{

public double area(double sideLength) { return sideLength*sideLength; }}

class Circle implements Shape{ public double area(double r) { return Math.PI*r*r; }}

public class Test02 {

public static void main(String[] args) { Shape square = new Square(); Shape circle = new Circle();

System.out.println(square.area(2)); System.out.println(circle.area(3)); }} 3、

class NoThisSongException extends Exception{ public NoThisSongException(){ super(); }

public NoThisSongException(String message){ super(message); }}

class Player{

public void play(int index)throws NoThisSongException{ if(index>10){

throw new NoThisSongException(\您播放的歌曲不存在\

}System.out.println(\正在播放歌曲\}}public class Test03 {

public static void main(String[] args) { Player player = new Player(); try {player.play(13);

} catch (NoThisSongException e) {

System.out.println(\异常信息为: \5.1public class MyThread extends Thread{ public MyThread(String name) { super(name);

}public void run() {

System.out.println(this.getName()); }public static void main(String[] args) { new MyThread(\new MyThread(\2、

public class MyRunnable implements Runnable { public void run() {

for (inti = 0; i< 50; i++) { System.out.println(\

public static void main(String[] args) { new Thread(new MyRunnable()).start(); for (inti = 0; i< 100; i++) { System.out.println(\3、

public class Test01 {

public static void main(String[] args) { Teacher t = new Teacher();

new Thread(t, \陈老师\new Thread(t, \高老师\new Thread(t, \李老师\class Teacher implements Runnable { private int notes = 80; public void run() { while (true) {

dispatchNotes(); // 调用售票方法 if (notes <= 0) { break;}}}

private synchronized void dispatchNotes() {

if (notes > 0) { try {

Thread.sleep(10); // 经过的线程休眠10毫秒 } catch (InterruptedException e) { e.printStackTrace(); }

System.out.println(Thread.currentThread().getName() + \发出的笔记\

+ notes--);}}} 4、

public class Accumulator extends Thread { private intstratNum; public static int sum;

public Accumulator(intstartNum) { this.stratNum = startNum;

}public static synchronized void add(intnum) {sum += num; }public void run() { int sum = 0;

for (inti = 0; i< 10; i++) { sum += stratNum + i; }add(sum);}

public static void main(String[] args) throws Exception { Thread[] threadList = new Thread[10]; for (inti = 0; i< 10; i++) {

threadList[i] = new Accumulator(10 * i + 1); threadList[i].start();

}for (inti = 0; i< 10; i++) { threadList[i].join();}

System.out.println(\}}

6.1public class Test01 {

public static void main(String[] args) { String str = \// 字符串转成char数组 char[] ch = str.toCharArray();

StringBuffer buffer = new StringBuffer(); for (inti = str.length() - 1; i>= 0; i--) { if (ch[i] >= 'A' &&ch[i] <= 'Z') {

buffer.append(String.valueOf(ch[i]).toLowerCase());

2015河南理工大学java期末考试编程题

2.1publicclassTest01{publicstaticvoidmain(String[]args){intsum=0;for(inti=1;i<100;i++){if(i%2!=0)sum+=i;}System.out.println(sum);}}2、publiccla
推荐度:
点击下载文档文档为doc格式
2ytr53xm9f2r4yj9c244
领取福利

微信扫码领取福利

微信扫码分享