《Java编程语言:原理与范例》课后实验源代码.doc

上传人:hw****26 文档编号:3865504 上传时间:2019-08-12 格式:DOC 页数:50 大小:284.50KB
下载 相关 举报
《Java编程语言:原理与范例》课后实验源代码.doc_第1页
第1页 / 共50页
《Java编程语言:原理与范例》课后实验源代码.doc_第2页
第2页 / 共50页
《Java编程语言:原理与范例》课后实验源代码.doc_第3页
第3页 / 共50页
《Java编程语言:原理与范例》课后实验源代码.doc_第4页
第4页 / 共50页
《Java编程语言:原理与范例》课后实验源代码.doc_第5页
第5页 / 共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);tr

2、y 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;imp

3、ort 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.setRes

4、izable(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 = butt

5、on.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;p

6、ublic 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 位整数,如 8639427 输出为 7249368(不允许使用循环语句) 。*/package ch03;public class Exp2_3 public static void main(String args) long a = 8639427;System.

7、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,以最快的速度计算 34a 的值。*/package ch03;public class Exp2_4 public static v

8、oid main(String args) int a = 3;int b = (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(“输入整数:“);lo

9、ng 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);S

10、ystem.out.print(“输入行数:“);rows = s.nextInt();for (int i = -rows / 2; i ai) min = ai;return min;int findLastLocation(int a, int x) for (int i = a.length - 1; i = 0; i-) if (ai = x) return i;return -1;boolean isAsc(int a) for (int i = 0; i ai + 1) return false;return true;void reverse(int a) for (int i = 0; i x)

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

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。