Java实验报告实验一.docx
《Java实验报告实验一.docx》由会员分享,可在线阅读,更多相关《Java实验报告实验一.docx(17页珍藏版)》请在冰点文库上搜索。
![Java实验报告实验一.docx](https://file1.bingdoc.com/fileroot1/2023-5/21/8b918634-195b-406c-8cc9-04bb32820890/8b918634-195b-406c-8cc9-04bb328208901.gif)
Java实验报告实验一
【实验名称】实验1Java程序设计基础
【实验目标】
1、学习和掌握Java程序的基本结构。
2、学习和掌握Java程序的基本开发过程和方法。
3、学习和掌握程序设计的基本开发步骤。
4、学习和掌握Java程序的基本输入、输出方法。
5、学习和掌握Java程序设计的基本技.
【实验日期和时间记录】2014.4.18-2014.4.26
1.创建、编译和执行Welcome.java。
【实验预习】
练习利用Java输出简单语句。
【实验过程及结果记录】
publicclassWelcome{
publicstaticvoidmain(String[]args){
System.out.println(“WlecometoJava!
”);
}
}
2、(财务应用程序:
工资单)编写程序,读入工资基本信息并
打印工资单。
编写两种版本的程序,依次使用控制台实现输入\输出和GUI实现输入\输出。
运行效果可参见图1和图2。
【实验预习】
练习GUI输入输出,键盘输入,存在一个变量里面,整体输出。
【实验过程及结果记录】
控制台界面
/**
*@(#)ComputeAndShowSolary.java
*
*
*@author
*@version1.002014/4/12
*/
importjava.util.Scanner;//
publicclassComputeAndShowSolary{
publicstaticvoidmain(String[]args){
//inputsection
System.out.print("Enteremployee'sname:
");//Promttheusertoenteraname
Scannerinput=newScanner(System.in);
Stringname=input.nextLine();//inputthename
System.out.print("Enternumberofhoursworkedinaweek:
");//Promttheusertoenternumberofhours
doubleworkhour=input.nextDouble();//inputthehour
System.out.print("Enterhourlypayrate:
");//Promttheusertoentertherate
doublepayrate=input.nextDouble();//inputtherate
System.out.print("EnterfederaltaxWithholdingrate:
");//Promttheusertoenterthefederaltaxrate
doublefederalrate=input.nextDouble();//inputthefederaltaxrate
System.out.print("EnterstatetaxWithholdingrate:
");//promttheusertoenterthestatetaxrate
doublestaterate=input.nextDouble();//inoutthestatetaxrate
//outputsection
System.out.println("雇员姓名:
"+name);//outputemployee'sname
System.out.println("工作小时数:
"+workhour);//outputhoursworked
System.out.println("每小时工资数:
"+"$"+payrate);//outputpayrate
System.out.println("工资总收入:
"+"$"+payrate*workhour);//outputpayrate
System.out.println("所纳税款:
");//outputDeductions
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
*@version1.002014/4/12
*/
importjavax.swing.JOptionPane;
publicclassComputeAndShowSolaryWithGUI{
publicstaticvoidmain(String[]args){
//inputsection
Stringnamestring=JOptionPane.showInputDialog("Enteremployee'sname:
");
Stringhoursstring=JOptionPane.showInputDialog("Enternumbersofhoursworkedinaweek:
");
inthours=Integer.parseInt(hoursstring);
Stringpayratestring=JOptionPane.showInputDialog("Enterhourlypayrate:
");
doublepayrate=Double.parseDouble(payratestring);
Stringfederalstring=JOptionPane.showInputDialog("Enterfederaltaxwithholdingrate:
");
doublefederal=Double.parseDouble(federalstring);
Stringstatestring=JOptionPane.showInputDialog("Enterstatetaxwithholdingrate:
");
doublestate=Double.parseDouble(statestring);
//outputsection
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,基于纳税人的身份和可征税收入,编写程序,计算某个纳税人的纳税额。
比如,计算一个收入美元的单身纳税人的纳税额。
程序的运行效果见图3。
说明:
⑴题目来源,编程练习3.13(P85)和程序清单3-6(P68)。
⑵程序设计的基本步骤,参见2.2小节(P18)和教材P84给出的教学注意。
⑶解决方案的选择模型,参见授课ppt文稿。
【实验预习】
练习if语句和基本运算语句
【实验过程及结果记录】
importjava.util.Scanner;
publicclassComputeTax{
publicstaticvoidmain(String[]args){
System.out.println("(0-单身纳税人。
1-已婚共同纳税人或证实的鳏寡,2-已婚单独纳税人,3-家庭户主纳税人)");
System.out.print("Enterthefilingstatus:
");
Scannerinput=newScanner(System.in);
intchoice=input.nextInt();
System.out.print("Enterthetaxableincome:
");
doubleincome=input.nextDouble();
doubletax=0;
if(0==choice){
if(0<=income&&income<=8350){
tax=income*0.1;
}
elseif(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
elseif(33951<=income&&income<=82250){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
elseif(82251<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28;
}
elseif(<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(-82250)*0.28+(income-)*0.33;
}
elseif(<=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;
}
elseif(16701<=income&&income<=67900){
tax=16700*0.1+(income-16700)*0.15;
}
elseif(67901<=income&&income<=){
tax=16700*0.1+(67900-16700)*0.15+(income-67900)*0.25;
}
elseif(<=income&&income<=){
tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(income-13751)*0.28;
}
elseif(<=income&&income<=){
tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(-13751)*0.28+(income-)*0.33;
}
elseif(<=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.1;
}
elseif(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
elseif(33951<=income&&income<=67525){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
elseif(68526<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(income-68525)*0.28;
}
elseif(<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(-68525)*0.28+(income-)*0.33;
}
elseif(<=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;
}
elseif(11951<=income&&income<=45500){
tax=11951*0.1+(income-11950)*0.15;
}
elseif(45501<=income&&income<=){
tax=11951*0.1+(45500-11950)*0.15+(income-45500)*0.25;
}
elseif(<=income&&income<=){
tax=11951*0.1+(45500-11950)*0.15+(-45500)*0.25+(income-)*0.28;
}
elseif(<=income&&income<=){
tax=11951*0.1+(45500-11950)*0.15+(-45500)*0.25+(-)*0.28+(income-)*0.33;
}
elseif(<=income){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(-82250)*0.28+(-)*0.33+(income-)*0.35;
}
}
System.out.println("Taxis"+tax);
}
}
4、(财务应用程序:
打印税表)在问题3的基础上,使用下面的方法头部,编写一个计算税款的方法:
publicstaticdoublecomputeTax()intstatus,doubletaxableIncome)
使用这个方法编写程序,打印可征税收入从50000美元到60000美元,收入间隔50美元的所有四种纳税人的纳税表。
【实验预习】
练习if语句,以及方法的使用
【实验过程及结果记录】
/**
*@(#)NewComputeTax.java
*
*
*@author
*@version1.002014/5/4
*/
publicclassNewComputeTax{
publicstaticdoublecomputeTax(intstatus,doubletaxableIncome){
doubletax=0;
doubleincome=taxableIncome;
intchoice=status;
if(0==choice){
if(0<=income&&income<=8350){
tax=income*0.1;
}
elseif(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
elseif(33951<=income&&income<=82250){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
elseif(82251<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28;
}
elseif(<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(-82250)*0.28+(income-)*0.33;
}
elseif(<=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;
}
elseif(16701<=income&&income<=67900){
tax=16700*0.1+(income-16700)*0.15;
}
elseif(67901<=income&&income<=){
tax=16700*0.1+(67900-16700)*0.15+(income-67900)*0.25;
}
elseif(<=income&&income<=){
tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(income-13751)*0.28;
}
elseif(<=income&&income<=){
tax=16700*0.1+(67900-16700)*0.15+(-67900)*0.25+(-13751)*0.28+(income-)*0.33;
}
elseif(<=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.1;
}
elseif(8351<=income&&income<=33950){
tax=8350*0.1+(income-8350)*0.15;
}
elseif(33951<=income&&income<=67525){
tax=8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
}
elseif(68526<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(income-68525)*0.28;
}
elseif(<=income&&income<=){
tax=8350*0.1+(33950-8350)*0.15+(68525-33950)*0.25+(-68525)*0.28+(income-)*0.33;
}
elseif(<=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;
}
elseif(11951<=income&&income<=45500){
tax=11951*0.1+(income-11950)*0.15;
}
elseif(45501<=income&&income<=){
tax=11951*0.1+(45500-11950)*0.15+(income-45500)*0.25;
}
elseif(<=income&&income<=){
tax=11951*0.1+(45500-11950)*0.15+(-45500)*0.25+(income-)*0.28;
}
elseif(<=income&&income<=){
tax=11951*0.1+(45500-11950)*0.15+(-45500)*0.25+(-)*0.28+(income-)*0.33;
}
elseif(<=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);
return0;
}
publicstaticvoidmain(String[]args){
for(intstatus=0;status<4;status++){
for(doubleincome=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,,,}
};
【实验预习】
练习数组的输入输出以及处理数组内的数据。
【实验过程及结果记录】
/**