1、Java实验报告实验一【实验名称】实验1 Java程序设计基础【实验目标】1、学习和掌握Java程序的基本结构。 2、学习和掌握Java程序的基本开发过程和方法。 3、学习和掌握程序设计的基本开发步骤。 4、学习和掌握Java程序的基本输入、输出方法。 5、学习和掌握Java程序设计的基本技.【实验日期和时间记录】2014.4.18-2014.4.26 1.创建、编译和执行Welcome.java。【实验预习】练习利用Java输出简单语句。【实验过程及结果记录】public class Welcomepublic static void main(String args)System.out.
2、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 pub
3、lic static void main(String args) /input section System.out.print(Enter employees 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 ho
4、urs 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=i
5、nput.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 employees name System.out.println(工作小时数:
6、 +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(州税+
7、(+staterate*100+%+)+: +$+workhour*payrate*staterate); System.out.printf(总纳税: +$); System.out.printf(%.2fn,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/
8、12 */import javax.swing.JOptionPane;public class ComputeAndShowSolaryWithGUI public static void main(String args) /input section String namestring=JOptionPane.showInputDialog(Enter employees name:); String hoursstring=JOptionPane.showInputDialog(Enter numbers of hours worked in a week:); int hours=I
9、nteger.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 st
10、atestring=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 州税(
11、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,基于纳税人的身份和可征税收入,编写程序,计算某个纳税人的纳税额。比如,计算一个收入美元的单身纳税人的纳税额。程序的运行效果见图3。 说明:题目来源,编程练习3.13(P85)和程序清单3-6(P68)。程序设计的基本步骤,参见2.2小节(P18)
12、和教材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); in
13、t 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)
14、*0.15+(income-33950)*0.25; else if(82251=income&income=) tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28; else if(=income&income=) tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(-82250)*0.28+(income-)*0.33; else if(=income) tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+
15、(-82250)*0.28+(- )*0.33+(income-)*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=) tax=16700*0.1+(67900-16700)*0.15+(income-67900)*0.25; else if(=income&income=) tax=16700*0.1+(67900-16700
16、)*0.15+(-67900)*0.25+(income-13751)*0.28; else if(=income&income=) tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(-13751)*0.28+ (income-)*0.33; else if(=income) tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(-13750)*0.28+ (-)*0.33+(income-)*0.35; if(2=choice) if(0=income&income=8350) tax=income*0.
17、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=) tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(income-68525)*0.28; else if(=income&income=) tax=8350*0.1+(339
18、50-8350)*0.15+(68525-33950)*0.25+(-68525)*0.28+(income- )*0.33; else if(=income) tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(-68525)*0.28+(- )*0.33+(income-)*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
19、 if(45501=income&income=) tax=11951*0.1+(45500-11950)*0.15+(income-45500)*0.25; else if(=income&income=) tax=11951*0.1+(45500-11950)*0.15+(-45500)*0.25+(income-)*0.28; else if(=income&income=) tax=11951*0.1+(45500-11950)*0.15+(-45500)*0.25+(-)*0.28+(income-)*0.33; else if(=income) tax=8350*0.1+(3395
20、0-8350)*0.15+(82250-33950)*0.25+(-82250)*0.28+(- )*0.33+(income-)*0.35; System.out.println(Tax is +tax); 4、(财务应用程序:打印税表)在问题3的基础上,使用下面的方法头部,编写一个计算税款的方法: public static double computeTax()int status,double taxableIncome) 使用这个方法编写程序,打印可征税收入从50000美元到60000美元,收入间隔50美元的所有四种纳税人的纳税表。 【实验预习】练习if语句,以及方法的使用【实验过程
21、及结果记录】/* * (#)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&
22、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=) tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28; else if(=income&income=) tax=8350*0.1+(33950-8350)*0.15+(82250-33
23、950)*0.25+(-82250)*0.28+(income-)*0.33; else if(=income) tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(-82250)*0.28+(-)*0.33+(income-)*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=)
24、 tax=16700*0.1+(67900-16700)*0.15+(income-67900)*0.25; else if(=income&income=) tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(income-13751)*0.28; else if(=income&income=) tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(-13751)*0.28+(income-)*0.33; else if(=income) tax=16700*0.1+(67900-16700)*0.15+
25、(-67900)*0.25+(-13750)*0.28+(-)*0.33+(income-)*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=) tax=8350*
26、0.1+(33950-8350)*0.15+(68525-33950)*0.25+(income-68525)*0.28; else if(=income&income=) tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(-68525)*0.28+(income-)*0.33; else if(=income) tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(-68525)*0.28+(-)*0.33+(income-)*0.35; if(3=choice) if(0=income&in
27、come=11950) tax=income*0.1; else if(11951=income&income=45500) tax=11951*0.1+(income-11950)*0.15; else if(45501=income&income=) tax=11951*0.1+(45500-11950)*0.15+(income-45500)*0.25; else if(=income&income=) tax=11951*0.1+(45500-11950)*0.15+(-45500)*0.25+(income-)*0.28; else if(=income&income=) tax=1
28、1951*0.1+(45500-11950)*0.15+(-45500)*0.25+(-)*0.28+(income-)*0.33; else if(=income) tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(-82250)*0.28+(- )*0.33+(income-)*0.35; System.out.println(choice+ +income+ +tax); return 0; public static void main(String args) for(int status=0;status4;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, 16700,67900, 8350,33950,68525, 11950,45500, ; 【实验预习】练习数组的输入输出以及处理数组内的数据。【实验过程及结果记录】/*