《《Java编程语言:原理与范例》课后实验源代码》由会员分享,可在线阅读,更多相关《《Java编程语言:原理与范例》课后实验源代码(50页珍藏版)》请在金锄头文库上搜索。
1、第一章实验一package ch01;import java.text.SimpleDateFormat;import java.util.Date;class Timer extends Thread private SimpleDateFormat sdf = new SimpleDateFormat(yyyy年MM月dd日 HH:mm:ss); public void run() while (true) System.out.print(r现在时间是:); Date now = new Date(); System.out.print(sdf.format(now); try slee
2、p(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
3、 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.se
4、tResizable(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 mo
5、useY = 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. 交换两个变量的值(不允许使用中间变量)。
6、*/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(a= + a + , b= + b);实验二/*3. 逆序输出一个7位整数,如输出为(不允许使用循环语句)。 */package ch03;public class Exp2_3 public static void main(String args) long a = ;System.out.print(a %
7、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 / % 10);System.out.print(a / % 10);实验三/*4. 对于int型变量a,以最快的速度计算34a的值。 */package ch03;public class Exp2_4 public static void main(String args) int a = 3;i
8、nt b = (a 5) + (a b ? a : b) c ? (a b ? a : b) : c;System.out.println(max= + max);第三章实验一/*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
9、 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+) System.out.printf(%- + (3 * Math.abs(i) + 1) + s, );for (int j = Math.abs(i) - rows / 2; j = rows / 2 - Math.abs(i); j+) System.out.printf(%-3d, rows / 2 + 1 - Math.abs(i) - Math.abs(j);System.out.println();实验三/*