【实验名称】实验1 Java程序设计基础
【实验目标】
1、学习和掌握Java程序的基本结构。
2、学习和掌握Java程序的基本开发过程和方法。
3、学习和掌握程序设计的基本开发步骤。
4、学习和掌握Java程序的基本输入、输出方法。
5、学习和掌握Java程序设计的基本技.
【实验日期和时间记录】2014.4.18-2014.4.26
1.创建、编译和执行Welcome.java。
【实验预习】
练习利用Java输出简单语句。
【实验过程及结果记录】
public class Welcome{
public static void main(String[] args){
System.out.println(“Wlecome to Java!”);
}
}
2、(财务应用程序:工资单)编写程序,读入工资基本信息并
打印工资单。编写两种版本的程序,依次使用控制台实现输入\输出和GUI实现输入\输出。运行效果可参见图1和图2。
【实验预习】
练习GUI输入输出,键盘输入,存在一个变量里面,整体输出。
【实验过程及结果记录】
控制台界面
/**
* @(#)ComputeAndShowSolary.java
*
*
* @author
* @version 1.00 2014/4/12
*/
import java.util.Scanner;//
public class ComputeAndShowSolary {
public static void main(String[] args) {
//input section
System.out.print("Enter employee's name:");//Promt the user to enter a name
Scanner input= new Scanner(System.in);
String name=input.nextLine();//input the name
System.out.print("Enter number of hours worked in a week:");//Promt the user to enter number of hours
double workhour=input.nextDouble();//input the hour
System.out.print("Enter hourly pay rate:");//Promt the user to enter the rate
double payrate=input.nextDouble();//input the rate
System.out.print("Enter federal tax Withholding rate:");//Promt the user to enter the federal tax rate
double federalrate=input.nextDouble();//input the federal tax rate
System.out.print("Enter state tax Withholding rate:");//promt the user to enter the state tax rate
double staterate=input.nextDouble();//inout the state tax rate
//output section
System.out.println("雇员姓名: "+name);//output employee's name
System.out.println("工作小时数: "+workhour);//output hours worked
System.out.println("每小时工资数: "+"$"+payrate);//output pay rate
System.out.println("工资总收入: "+"$"+payrate*workhour);//output payrate
System.out.println("所纳税款:");//output Deductions
System.out.println("联邦税"+"("+federalrate*100+"%"+")"+": "+"$"+workhour*payrate*federalrate);
System.out.println("州税"+"("+staterate*100+"%"+")"+": "+"$"+workhour*payrate*staterate);
System.out.printf("总纳税: "+"$");
System.out.printf("%.2f\n",workhour*payrate*(0.2+0.09));
System.out.printf("净收入: "+"$");
System.out.printf("%.2f",workhour*payrate*(1.0-0.2-0.09));
}
}
GUI界面
/**
* @(#)ComputeAndShowSolaryWithGUI.java
*
*
* @author
* @version 1.00 2014/4/12
*/
import javax.swing.JOptionPane;
public class ComputeAndShowSolaryWithGUI {
public static void main(String[] args) {
//input section
String namestring=JOptionPane.showInputDialog("Enter employee's name:");
String hoursstring=JOptionPane.showInputDialog("Enter numbers of hours worked in a week:");
int hours=Integer.parseInt(hoursstring);
String payratestring=JOptionPane.showInputDialog("Enter hourly pay rate:");
double payrate=Double.parseDouble(payratestring);
String federalstring=JOptionPane.showInputDialog("Enter federal tax withholding rate:");
double federal=Double.parseDouble(federalstring);
String statestring=JOptionPane.showInputDialog("Enter state tax withholding rate:");
double state=Double.parseDouble(statestring);
//output section
JOptionPane.showMessageDialog(null,
"雇员姓名: "+namestring+
"\n工作小时数:"+hours+
"\n每小时工资数:"+"$"+payrate+
"\n工资总收入:"+
"$"+payrate*hours+
"\n所纳税款:"+ "\n 联邦税(20.0%):"+"$"+hours*payrate*0.2+
"\n 州税(9.0%):"+"$"+(int)(hours*payrate*0.09*100)/100.0+
"\n 总纳税:"+"$"+(int)(hours*payrate*(0.2+0.09)*100)/100.00+
"\n净收入:"+"$"+(int)(hours*payrate*(1.0-0.2-0.09)*100)/100.00
);
}
}
3、(财务应用程序:计算税款)按照表1,基于纳税人的身份和可征税收入,编写程序,计算某个纳税人的纳税额。比如,计算一个收入400000美元的单身纳税人的纳税额。程序的运行效果见图3。
说明:⑴题目来源,编程练习3.13(P85)和程序清单3-6(P68)。⑵程序设计的基本步骤,参见2.2小节(P18)和教材P84给出的教学注意。⑶解决方案的选择模型,参见授课ppt文稿。
【实验预习】
练习if语句和基本运算语句
【实验过程及结果记录】
import java.util.Scanner;
public class ComputeTax{
public static void main(String[] args){
System.out.println("(0-单身纳税人。1-已婚共同纳税人或证实的鳏寡,2-已婚单独纳税人,3-家庭户主纳税人)");
System.out.print("Enter the filing status:");
Scanner input=new Scanner(System.in);
int choice=input.nextInt();
System.out.print("Enter the taxable income:");
double income=input.nextDouble();
double tax=0;
if(0==choice){
if(0<=income&&income<=8350){
tax=income*0.1;
}
else if(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
else if(33951<=income&&income<=82250){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
else if(82251<=income&&income<=171550){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28;
}
else if(171551<=income&&income<=372950){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(income-171550)*0.33;
}
else if(372951<=income){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(372950- 171550)*0.33+(income-372950)*0.35;
}
}
if(1==choice){
if(0<=income&&income<=16700){
tax=income*0.1;
}
else if(16701<=income&&income<=67900){
tax=16700*0.1+(income-16700)*0.15;
}
else if(67901<=income&&income<=137050){
tax=16700*0.1+(67900-16700)*0.15+(income-67900)*0.25;
}
else if(137051<=income&&income<=208850){
tax=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(income-13751)*0.28;
}
else if(208851<=income&&income<=372950){
tax=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(208850-13751)*0.28+ (income-208850)*0.33;
}
else if(372951<=income){
tax=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(208850-13750)*0.28+ (372950-208850)*0.33+(income-372950)*0.35;
}
}
if(2==choice){
if(0<=income&&income<=8350){
tax=income*0.1;
}
else if(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
else if(33951<=income&&income<=67525){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
else if(68526<=income&&income<=104425){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(income-68525)*0.28;
}
else if(104426<=income&&income<=186475){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(104425-68525)*0.28+(income- 171550)*0.33;
}
else if(186476<=income){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(104425-68525)*0.28+(186745- 104425)*0.33+(income-372950)*0.35;
}
}
if(3==choice){
if(0<=income&&income<=11950){
tax=income*0.1;
}
else if(11951<=income&&income<=45500){
tax=11951*0.1+(income-11950)*0.15;
}
else if(45501<=income&&income<=117450){
tax=11951*0.1+(45500-11950)*0.15+(income-45500)*0.25;
}
else if(117451<=income&&income<=190200){
tax=11951*0.1+(45500-11950)*0.15+(1175450-45500)*0.25+(income-117450)*0.28;
}
else if(190201<=income&&income<=372950){
tax=11951*0.1+(45500-11950)*0.15+(1175450-45500)*0.25+(190200-117450)*0.28+(income-171550)*0.33;
}
else if(372951<=income){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(372950- 171550)*0.33+(income-372950)*0.35;
}
}
System.out.println("Tax is "+tax);
}
}
4、(财务应用程序:打印税表)在问题3的基础上,使用下面的方法头部,编写一个计算税款的方法:
public static double computeTax()int status,double taxableIncome)
使用这个方法编写程序,打印可征税收入从50000美元到60000美元,收入间隔50美元的所有四种纳税人的纳税表。
【实验预习】
练习if语句,以及方法的使用
【实验过程及结果记录】
/**
* @(#)NewComputeTax.java
*
*
* @author
* @version 1.00 2014/5/4
*/
public class NewComputeTax {
public static double computeTax(int status,double taxableIncome){
double tax=0;
double income=taxableIncome;
int choice=status;
if(0==choice){
if(0<=income&&income<=8350){
tax=income*0.1;
}
else if(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
else if(33951<=income&&income<=82250){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
else if(82251<=income&&income<=171550){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28;
}
else if(171551<=income&&income<=372950){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(income-171550)*0.33;
}
else if(372951<=income){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(372950-171550)*0.33+(income-372950)*0.35;
}
}
if(1==choice){
if(0<=income&&income<=16700){
tax=income*0.1;
}
else if(16701<=income&&income<=67900){
tax=16700*0.1+(income-16700)*0.15;
}
else if(67901<=income&&income<=137050){
tax=16700*0.1+(67900-16700)*0.15+(income-67900)*0.25;
}
else if(137051<=income&&income<=208850){
tax=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(income-13751)*0.28;
}
else if(208851<=income&&income<=372950){
tax=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(208850-13751)*0.28+(income-208850)*0.33;
}
else if(372951<=income){
tax=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(208850-13750)*0.28+(372950-208850)*0.33+(income-372950)*0.35;
}
}
if(2==choice){
if(0<=income&&income<=8350){
tax=income*0.1;
}
else if(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
else if(33951<=income&&income<=67525){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
else if(68526<=income&&income<=104425){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(income-68525)*0.28;
}
else if(104426<=income&&income<=186475){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(104425-68525)*0.28+(income-171550)*0.33;
}
else if(186476<=income){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(104425-68525)*0.28+(186745-104425)*0.33+(income-372950)*0.35;
}
}
if(3==choice){
if(0<=income&&income<=11950){
tax=income*0.1;
}
else if(11951<=income&&income<=45500){
tax=11951*0.1+(income-11950)*0.15;
}
else if(45501<=income&&income<=117450){
tax=11951*0.1+(45500-11950)*0.15+(income-45500)*0.25;
}
else if(117451<=income&&income<=190200){
tax=11951*0.1+(45500-11950)*0.15+(1175450-45500)*0.25+(income-117450)*0.28;
}
else if(190201<=income&&income<=372950){
tax=11951*0.1+(45500-11950)*0.15+(1175450-45500)*0.25+(190200-117450)*0.28+(income-171550)*0.33;
}
else if(372951<=income){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(372950- 171550)*0.33+(income-372950)*0.35;
}
}
System.out.println(choice+" "+income+" "+tax);
return 0;
}
public static void main(String[] args){
for(int status=0;status<4;status++){
for(double income=50000;income<=60000;income+=50){
computeTax(status,income);
}
}
}
}
5、(财务应用程序:计算税款)使用数组重新编写问题3的实现。使用数组表示税率:
double[] rates = {0.10,0.15,0.25,0.28,0.33,0.35};
使用数组表示收入分段:
int[][] brackets = {
{8350,33950,82250,171550,372950},
{16700,67900,137050,208850,372950},
{8350,33950,68525,104425,186475},
{11950,45500,117450,190200,372950}
};
【实验预习】
练习数组的输入输出以及处理数组内的数据。
【实验过程及结果记录】
/**
* @(#)ComputeTaxWithArray.java
*
*
* @author
* @version 1.00 2014/5/4
*/
import java.util.Scanner;
public class ComputeTaxWithArray {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter filing status
// Prompt the user to enter filing status
System.out.print(
"(0-single filer, 1-married jointly,\n" +
"2-married separately, 3-head of household)\n" +
"Enter the filing status: ");
int status = input.nextInt();
// Prompt the user to enter taxable income
System.out.print("Enter the taxable income: ");
double income = input.nextDouble();
// Compute and display the result
System.out.println("Tax is " +
(int)(computeTax(status, income) * 100) / 100.0);
}
public static double computeTax(int status, double income) {
double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
int[][] brackets = {
{8350, 33950, 82250, 171550, 372950}, // Single filer
{16700, 67900, 137050, 20885, 372950}, // Married jointly
{8350, 33950, 68525, 104425, 186475}, // Married separately
{11950, 45500, 117450, 190200, 372950} // Head of household
};
double tax = 0; // Tax to be computed
// Compute tax in the first bracket
if (income <= brackets[status][0])
return tax = income * rates[0]; // Done
else
tax = brackets[status][0] * rates[0];
// Compute tax in the 2nd, 3rd, 4th, and 5th brackets, if needed
for (int i = 1; i < brackets[0].length; i++) {
if (income > brackets[status][i])
tax += (brackets[status][i] - brackets[status][i - 1]) *
rates[i];
else {
tax += (income - brackets[status][i - 1]) * rates[i];
return tax; // Done
}
}
// Compute tax in the last (i.e., 6th) bracket
return tax += (income - brackets[status][4]) * rates[5];
}
}
【思考题】
l 你开始“每天做一点编程练习”了吗?
已经坚持了一段时间
l 在问题3的解决方案中,你使用的是哪种选择结构模型(可以使用流程图描述)?实现这个选择结构模型的java语法结构是什么?
先判断纳税人的情况,根据纳税人情况再对其收入进行分析以确定其应缴多少的税;使用的事if选择结构
l 在问题4中,你使用了哪种循环方法?为什么?
for循环;循环开始,终止条件一目了然
l 你如何理解教材作者说的“如果能够使用循环编写程序,你就懂得如何编程了”?(语出教材P103)
利用循环可以简化语句,熟练地掌握编程语言
l 从问题3到问题5,可以使用不同的编程技术,解决同一个问题。说说你对这个过程中的编程感受和想法?
编程的方法是多种多样的,不可局限于某一种方法。