《Java编程语言:原理与范例》课后实验源代码 下载本文

内容发布更新时间 : 2024/5/5 13:33:11星期一 下面是文章的全部内容请认真阅读。

第一章 实验一

package ch01;

import java.text.SimpleDateFormat; import java.util.Date;

class Timer extends Thread {

private SimpleDateFormat sdf = new SimpleDateFormat(\年MM月dd日 HH:mm:ss\

public void run() { while (true) {

System.out.print(\现在时间是:\ Date now = new Date();

System.out.print(sdf.format(now)); try {

sleep(1000);

} catch (InterruptedException e) { e.printStackTrace(); } } } }

public class Clock {

public static void main(String[] args) { Timer timer = new Timer(); timer.start(); } }

实验二

package ch01;

import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random;

import javax.swing.JButton; import javax.swing.JFrame;

public class MagicButton extends MouseAdapter { JFrame win;

JButton button = new JButton(\你点不到我\ Random rand = new Random();

void initUI() {

win = new JFrame(); win.setLayout(null);

button.setSize(100, 40);

button.addMouseListener(this);

win.add(button);

win.setSize(400, 300); win.setResizable(false);

win.setLocationRelativeTo(null);

win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); }

public static void main(String[] args) {

MagicButton demo = new MagicButton(); demo.initUI(); }

public void mouseEntered(MouseEvent e) { int mouseX = button.getX() + e.getX(); int mouseY = button.getY() + e.getY();

while (true) {

int buttonX = rand.nextInt(win.getWidth() - button.getWidth()); int buttonY = rand.nextInt(win.getHeight() - button.getHeight()); button.setLocation(buttonX, buttonY);

if (!button.getBounds().contains(mouseX, mouseY)) { break; } } } }

第二章 实验一

/*********************************

2. 交换两个变量的值(不允许使用中间变量)。 **********************************/ package ch03;

public class Exp2_2 { public static void main(String[] args) { int a = 2, b = 3; int s = a * b; a = s / a; b = s / a; System.out.println(\ } }

实验二

/*********************************

3. 逆序输出一个7位整数,如8639427输出为7249368(不允许使用循环语句)。 **********************************/ package ch03;

public class Exp2_3 { public static void main(String[] args) { long a = 8639427; System.out.print(a % 10); System.out.print(a / 10 % 10); System.out.print(a / 100 % 10); System.out.print(a / 1000 % 10); System.out.print(a / 10000 % 10); System.out.print(a / 100000 % 10); System.out.print(a / 1000000 % 10); } }

实验三

/*********************************

4. 对于int型变量a,以最快的速度计算34×a的值。 **********************************/ package ch03;

public class Exp2_4 { public static void main(String[] args) { int a = 3; int b = (a << 5) + (a << 1);

System.out.println(a + \ } }

实验四

/*********************************

5. 字符型变量ch中存放着一个大小写未知的英文字母,判断其大小写后,将ch的值转为小写或大写字母(不允许使用加减运算符和if语句)。 **********************************/ package ch03;

public class Exp2_5 { public static void main(String[] args) { char ch = 'E'; ch = (char) ((ch & 32) == 0 ? ch | 32 : ch & (Integer.MAX_VALUE - 32)); System.out.println(\ } }

实验5

/*********************************

6. 使用嵌套的条件运算符,求a、b、c中的最大者。 **********************************/ package ch03;

public class Exp2_6 { public static void main(String[] args) { int a = 2, b = 4, c = 3; int max = (a > b ? a : b) > c ? (a > b ? a : b) : c; System.out.println(\ } }

第三章 实验一

/*********************************

2. 使用循环结构逆序输出任意位数的整数。 **********************************/ package ch04;

import java.util.Scanner;

public class Exp3_2 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println(\输入整数:\ long n = s.nextLong(); while (n > 0) { System.out.print(n % 10); n /= 10; } } }

实验二

/*********************************

3. 输出以下由数字组成的菱形(要求将输出行数存放于变量中以便随时更改)。 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1

**********************************/ package ch04;

import java.util.Scanner;

public class Exp3_3 { public static void main(String[] args) { int rows; Scanner s = new Scanner(System.in); System.out.print(\输入行数:\ rows = s.nextInt(); for (int i = -rows / 2; i <= rows / 2; i++) {