JAVA实验指导书-实验一

更新时间:2024-01-13 19:10:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Java SE程序设计》

实 验 指 导 手 册

授课教师: 崔毅东 实验指导教师: 崔毅东 教学对象: 二年级本科生 开课时间: 秋季学期

北京邮电大学软件学院

1

《Java语言与编程》实验指导书

程保中 孙艺

一、实验一:Java程序编制(基础练习)

1、实验目的

学生通过使用Java语言进行基本程序的开发,掌握Java通用IDE,练习类的封装使用、Java基本类库的使用、利用UML进行简单建模。

2、实验内容(详细内容见附件一)

3、实验环境

a) Windows

b) Eclipse 或者 NetBean

4、实验要求

1) 独立完成实验内容要求

2) 熟练使用Java常用 IDE 进行编程 3) 需要上交源程序文件

4) 需要打印实验报告上交(标准格式见附件二)

5、实验步骤

1) 启动Java常用 IDE。 2)建立project。 3)编辑源程序。

4)编译、链接并执行源程序,看结果是否正确。

5)如果报错或告警,做必要修改,重复3)--5)步骤直到没有错误和告警。

2

附件一:

Project #1 A Math Learning Tool (covers Chapter 2)

CSCI 1301 Introduction to Programming Armstrong Atlantic State University

Develop a program that lets a first grader practice additions. The program

randomly generates two single-digit integers and displays a question such as “What is 7 + 9?” to the student, as shown in Figure a. After the student types the answer in the input dialog box, the program displays a message dialog box to indicate whether the answer is true or false, as shown in Figure b.

(a) (b) (c) (d)

NOTE: Write the program without using conditional statements. Use only the materials you learned in Chapter 2.

(注:写程序,无需使用条件语句。只使用你在第2章所学的材料。)

Hint: Generate the random number using the System.currentTimeMillis() introduced in Chapter 2.

提示:使用系统生成的随机数。currenttimemillis()介绍2章。 Submit the following items:

1. Analysis: Restate the problem in your own words (e.g., what is input, output if any, what needs to be computed, what data needs to be stored and their type). 1.分析:用自己的话重述问题(例如,什么是输入,输出,如果有,需要计算,哪些数据需要存储和类型)。

2. Design: Clearly describe the steps to solve this problem using English or pseudo code.

2.设计:清晰地描述用英语或伪代码来解决这个问题的步骤。 3. Coding: Implement the solution in Java.

4. Testing: Make sure that the numbers appear random. Submit the screen shots of two sample runs.

4.测试:确保数字显示随机。提交两个示例运行的屏幕截图。

3

Project #2 Tax Table (covers Chapter 3)

CSCI 1301 Introduction to Programming Armstrong Atlantic State University

The United States federal personal income tax is calculated based on filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household. The tax rates for 2005 are shown in Table (a)-(d). If you are, say, single with a taxable income of $10,000, the first $7,300 is taxed at 10% and the other $2,700 is taxed at 15%. So your tax is 730 + 2700 * 0.15.

Schedule X — Single If taxable income is over-- But not over-- $0 $7,300 $29,700 $71,950 $150,150 $326,450 $7,300 $29,700 $71,950 $150,150 $326,450 no limit The tax is: 10% of the amount over $0 $730 plus 15% of the amount over 7,300 730美元加15%的金额超过7300

$4,090.00 plus 25% of the amount over 29,700 $14,652.50 plus 28% of the amount over 71,950 $36,548.50 plus 33% of the amount over 150,150 $94,727.50 plus 35% of the amount over 326,450 Schedule Y-1 — Married Filing Jointly or Qualifying Widow(er)

If taxable income is over-- But not over-- $0 $14,600 $59,400 $119,950 $182,800 $326,450 $14,600 $59,400 $119,950 $182,800 $326,450 no limit The tax is: 10% of the amount over $0 $1,460.00 plus 15% of the amount over 14,600 $8,180 plus 25% of the amount over 59,400 $23,317.50 plus 28% of the amount over 119,950 $40,915.50 plus 33% of the amount over 182,800 $88,320.00 plus 35% of the amount over 326,450 Schedule Y-2 — Married Filing Separately If taxable income is over-- But not over-- $0 $7,300 $29,700 $59,975 $91,400 $163,225 $7,300 $29,700 $59,975 $91,400 $163,225 no limit The tax is: 10% of the amount over $0 $730 plus 15% of the amount over 7,300 $4,090 plus 25% of the amount over 29,700 $11,658.75 plus 28% of the amount over 59,975 $20,457.75 plus 33% of the amount over 91,400 $44,160.00 plus 35% of the amount over 163,225 Schedule Z — Head of Household

If taxable income is over-- But not over-- $0 $10,450 $39,800 $102,800 $166,450 $326,450 $10,450 $39,800 $102,800 $166,450 $326,450 no limit The tax is: 10% of the amount over $0 $1,045 plus 15% of the amount over 10,450 $5,447.50 plus 25% of the amount over 39,800 $21,197.50 plus 28% of the amount over 102,800 $39,019.50 plus 33% of the amount over 166,450 $91,819.50 plus 35% of the amount over 326,450 4

Develop a program that prints a tax table for taxable income from $50,000 to $60,000 with intervals of $50 for all four statuses, as follows:

写一个计程序,打印应纳税所得税表从50000美元到60000美元的所有四个状态的50个区间

taxable Income

50000 50050 ... 59950 60000

Single Married Married Head of Joint Separate a House 9846 9859

7296 7309

10398 8506 10411 8519

12532 9982 13190 11192 12546 9996 13205 11206

NOTE: the numbers in the preceding tax table are incorrect intentionally. You should print the correct values in the table.

Submit the following items:

1. Analysis: Restate the problem in your own words (e.g., what is input, output if

any, what needs to be computed, what data needs to be stored and their type).

2. Design: Clearly describe the steps to solve this problem using English or pseudo

code.

3. Coding: Implement the solution in Java.

4. Testing: Submit the screen shots for first 20 lines in the tax table. 4.测试:在税收表中提交前20行的屏幕截图。

5

Project #3 Least Common Multiple (covers Chapter 5)

CSCI 1301 Introduction to Programming Armstrong Atlantic State University

Definition: The least common multiple (LCM) of two numbers is the smallest number that is a multiple of both. For example, the LCM for 8 and 12 is 24, for 15 and 25 is 75, and for 120 and 150 is 600.

Develop a program that prompts the user to enter two integers and finds their least common multiple.

开发一个程序,提示用户输入两个整数,并找到它们的最小公倍数。

To find the LCM of two numbers, first create a prime factor table for each number. The first column of the table consists of all the prime factors and the second column tracks the occurrence of the corresponding prime factor in the number. For example, the prime factors for 120 are 2, 2, 2, 3, 5, so the prime factor table for number 120 is shown as follows:

prime factors for 120 # of occurrence 2 3 3 1 5 1 table[0][0] = 2 table[0][1] = 3 table[1][0] = 3 table[1][1] = 1 table[2][0] = 5 table[2][1] = 1

The prime factors for 150 are 2, 3, 5, 5, so the prime factor table for number 150 is shown as follows:

prime factors for 120 # of occurrence 2 1 3 1 5 2 table[0][0] = 2 table[0][1] = 1 table[1][0] = 3 table[1][1] = 1 table[2][0] = 5 table[2][1] = 2

The LCM of the two numbers consists of the factors with the largest occurrence in the two numbers. So the LCM for 120 and 150 is 2?2?2?3?5?5, where 2 appears three times in 120, 3 one time in 120, and 5 two times in 150.

Submit the following items:

1. Analysis: Restate the problem in your own words (e.g., what is input, output if

any, what needs to be computed, what data needs to be stored and their type). 2. Design: Clearly describe the steps to solve this problem using English or pseudo

code. (Hint: The prime factor table can be represented using a two-dimensional array. Write a method named getPrimeFactors(int number) that returns a two-dimensional array for the prime factor table.)

(提示:可以使用一个二维数组表示素因子表。写一个方法叫getprimefact OR(int数),返回的素因子表的二维数组。)

3. Coding: Implement the solution in Java.

4. Testing: Test your program to find the LCM for (120, 150), (7, 14), (7, 8), (1, 2),

and (345, 455)

6

Project #4 Design Classes (covers Chapter 6)

CSCI 1302 Introduction to Programming Armstrong Atlantic State University

This project consists of two separate problems.

1. Design a class named Rectangle to represent a rectangle. The class contains: ? Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ? A string data field named color that specifies the color of a rectangle.

Hypothetically, assume that all rectangles have the same color. The default color is white. ? A no-arg constructor that creates a default rectangle.

没有参数的构造函数,创建一个默认的矩形。

? A constructor that creates a rectangle with the specified width and height.

创建一个具有指定宽度和高度的矩形的构造函数

? The accessor and mutator methods for all three data fields.

所有三个数据字段的访问器方法。

? A method named getArea() that returns the area of this rectangle.

一种getarea()返回这个矩形的面积。

? A method named getPerimeter() that returns the perimeter.

一种getperimeter()返回周长。

Draw the UML diagram for the class. Implement the class.

这类画UML图。实现类。

Write a test program that creates two Rectangle objects. Assign width 4 and height 40 to the first object and width 3.5 and height 35.9 to the second object. Assign color red to all Rectangle objects. Display the properties of both objects and find their areas and perimeters.

编写一个创建两个矩形对象的测试程序。分配宽度4和高度40到第一个对象和宽度3.5和高度35.9到第二个对象。将颜色分配给所有矩形对象。 显示的对象的属性,找到他们的周长和面积。

2. Design a class named Time. The class contains:

? Data fields hour, minute, and second that represents a time. ? A no-arg constructor that creates a Time object for the current time. (The data fields value will represent the current time)

在没有参数的构造函数中创建时间为当前时间对象.(数据值将代表当前时间) ? A constructor that constructs a Time object with a specified elapse time since the middle night, Jan 1, 1970 in milliseconds. (The data fields value will represent this time)

一个构造函数构造一个时间与指定的时间从半夜对象,1970年1月1日在毫秒。(数据字段值将代表这段时间)

7

? Three get methods for the data fields hour, minute, and second, respectively.

Draw the UML diagram for the class. Implement the class. Write a test program that creates two Time objects (using new Time() and new Time(555550000)) and display their hour, minute, and second.

这类画UML图。实现类。写一个测试程序,创建两个时间对象(使用新的time()和 新时期(55555 0000)),显示小时,分钟和秒。

Hint: The current time can be obtained using System.currentTime(), as shown in Listing 2.8, ShowCurrentTime.java. The other constructor sets the hour, minute, and second for the specified elapse time. For example, if the elapse time is 555550000 milliseconds, the hour is 10, the minute is 19, and the second is 10.

提示:当前时间可以通过使用System.currenttime(),如Listing2.8所示,

ShowCurrentTime.java。其他构造函数设置小时,分钟和第二的指定时间。 例如,如果过去的时间是55555的0000毫秒,时间是10分钟,这是19,第二是10。

附件二:

北京邮电大学软件学院

实验报告

课程名称: Java SE程序设计

项目名称: Java编程(基础练习)

项目完成人:

姓名:________学号:________ 姓名:________学号:________ 姓名:________学号:________ 姓名:________学号:________ 姓名:________学号:________

指导教师:__________________

8

日 期: 年 月 日

9

一、 实验目的

(说明通过本实验希望达到的目的)

二、 实验内容

(说明本实验的内容)

三、 实验环境

(说明本实验需要的环境)

四、 实验结果

(说明实验完成情况)

五、 附录

(附上实验文档,如:问题分析、设计方案、算法、设计图、程序、仿真结果、运行结果、调试心得等,具体内容根据实验要求来

10

本文来源:https://www.bwwdw.com/article/u54o.html

Top