第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/8/21 11:41:15 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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期末考试编程题 的文档
2015河南理工大学java期末考试编程题.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2ytr53xm9f2r4yj9c244_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top