day016 Java小作业
赶上课程布置的作业,更新几个java小程序,老师要求还要写伪代码,前面更新了如何写伪代码但是写的还需要再锻炼就不展示了。
1.计算税费
Problem: Computing Taxes
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 vary every year. Table 3.2 shows the rates for 2009. If you are, say, single with a taxable income of $10,000, the first $8,350 is taxed at 10% and the other $1,650 is taxed at 15%. So, your tax is $1,082.5
you are to write a program to compute personal income tax. Your program should prompt the user to enter the filing status and taxable income and compute the tax. Enter 0 for single filers, 1 for married filing jointly, 2 for married filing separately, and 3 for head of household. Your program computes the tax for the taxable income based on the filing status.
下面是这个程序的完整代码:
/*
*@zzhao
*/
package HelloJava;
import java.util.Scanner;
public class ComputingTaxes
{
public static void main(String[] args)
{
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter filing status
System.out.print(
"(0-single filer, 1-married jointly or qualifying widow(er), "
+ "\n2-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 tax
double tax = 0;
if (status == 0) { // Compute tax for single filers
if (income <= 8350)
tax = income * 0.10;
else if (income <= 33950)
tax = 8350 * 0.10 + (income - 8350) * 0.15;
else if (income <= 82250)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(income - 33950) * 0.25;
else if (income <= 171550)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (income - 82250) * 0.28;
else if (income <= 372950)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(income - 171550) * 0.33;
else
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(372950 - 171550) * 0.33 + (income - 372950) * 0.35;
}
else if (status == 1) {
if(income <= 16700)
tax = income * 0.10;
else if(income <= 67900)
tax = 16700 * 0.10 + (income-16700) *0.15;
else if(income <= 137050)
tax = 16700 * 0.10 + (67900 -16700) * 0.15 + (income - 67900) * 0.25;
else if(income <= 208850)
tax = 16700 * 0.10 + (67900 -16700) * 0.15 + (137050 - 67900) * 0.25 +
(income -137050) * 0.28;
else if(income <= 372950)
tax = 16700 * 0.10 + (67900 -16700) * 0.15 + (137050 - 67900) * 0.25 +
(208850 - 137050) * 0.28 + (income - 208850 ) * 0.33;
else
tax =16700 * 0.10 + (67900 -16700) * 0.15 + (137050 - 67900) * 0.25 +
(208850 - 137050) * 0.28 + (372950 - 208850 ) * 0.33 + (income - 372950) * 0.35;
}
else if (status == 2) {
if(income <= 8350)
tax = income * 0.10;
else if(income <= 33950)
tax = 8350 * 0.10 + (income - 8350 ) * 0.15;
else if(income <= 68525)
tax = 8350 * 0.10 + (33950 - 8350 ) * 0.15 +
(income - 33950) * 0.25;
else if(income <= 104425)
tax = 8350 * 0.10 + (33950 -8350 ) * 0.15 +
(68525 - 33950) * 0.25 + (income - 68525) * 0.28;
else if(income <= 186475)
tax = 8350 * 0.10 + (33950 -8350 ) * 0.15 +
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28+
(income - 104425) * 0.33;
else
tax = 8350 * 0.10 + (33950 -8350 ) * 0.15 +
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28+
(186475 - 104425) * 0.33 + (income - 186475) * 0.35;
}
else if (status == 3) {
if(income <= 11950)
tax = income * 0.10;
else if(income <= 45500)
tax = 11950 * 0.10 + (income -11950) * 0.15;
else if(income <= 117450)
tax = 11950 * 0.10 + (45500 -11950) * 0.15 +
(income - 45500) * 0.25;
else if(income <= 190200)
tax = 11950 * 0.10 + (45500 -11950) * 0.15 +
(117450 - 45500) * 0.25 + (income - 117450) * 0.28;
else if(income <= 372950)
tax = 11950 * 0.10 + (45500 -11950) * 0.15 +
(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28+
(income - 190200) * 0.33;
else
tax = 11950 * 0.10 + (45500 -11950) * 0.15 +
(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28+
(370950 - 190200) * 0.33 + (income - 370950) * 0.35;
}
else {
System.out.println("Error: invalid status");
System.exit(1);
}
// Display the result
System.out.println("Tax is " + (int)(tax * 100) / 100.0);
}
}
2.预测学费
Problem: Predicating the Future Tuition
Suppose that the tuition for a university is $10,000 this year and tuition increases 7% every year. In how many years will the tuition be doubled?
Before you can write a program to solve this problem, first consider how to solve it byhand. The tuition for the second year is the tuition for the first year * 1.07. The tuition for a future year is the tuition of its preceding year * 1.07.
下面是完整代码:
/*
*@zzhao
*/
package HelloJava;
public class PretFT
{
public static void main(String[] args)
{
double Current_Amount=10000;
int year = 0;
while (Current_Amount<20000)
{
Current_Amount += Current_Amount*1.07 - Current_Amount ;
year += 1;
}
System.out.println(year);
System.out.println(Current_Amount);
}
}
就是使用一个while循环计算几年后学费将翻一番,同时输出多年后的具体学费。
3.计算贷款
write a program that computes loan payments. The loan can be a car loan, astudent loan, or a home mortgage loan. The program lets the user enter the interest rate, number of years, and loan amount, and displays the monthly and total payments.
The formula to compute the monthly payment is as follows:
You don’t have to know how this formula is derived. Nonetheless, given the monthly interest rate, number of years, and loan amount, you can use it to compute the monthly payment.
Here are the steps in developing the program:
1.Prompt the user to enter the annual interest rate, number of years, and loan amount.
2. Obtain the monthly interest rate from the annual interest rate.
3. Compute the monthly payment using the preceding formula.
4. Compute the total payment, which is the monthly payment multiplied by 12 and Multiplied by the number of years.
5. Display the monthly payment and total payment.
下面是完整代码:
/*
*@zzhao
*/
package HelloJava;
import java.util.Scanner;
public class ComputeLoan {
public static void main(String args[]) {
Scanner input=new Scanner(System.in);
System.out.println("请输入贷款的年利率:");
double yearlyRate=input.nextDouble();
System.out.println("请输入贷款总额:");
double loanAmount=input.nextDouble();
System.out.println("请选择贷款年数:");
double numberOfYears=input.nextDouble();
double monthlyRate=yearlyRate/12;
double monthlyPayment=(loanAmount*monthlyRate)/(1-1/Math.pow(1+monthlyRate, numberOfYears*12));
double totalPayment = monthlyPayment * numberOfYears * 12;
System.out.println("月支付额为:"+monthlyPayment);
System.out.println("总付款额为:"+totalPayment);
}
}
4.体重指标
Problem: Computing Body Mass Index
Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. The interpretation of BMI for people 16 years or older is as follows:
Write a program that prompts the user to enter a weight in pounds and height in inches and display the BMI. Note that one pound is 0.45359237 kilograms and one inch is 0.0254 meters.
下面是完整代码:
/*
*@zzhao
*/
package HelloJava;
import java.util.Scanner;
public class ComputeAndInterpretBMI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter weight in pounds
System.out.print("Enter weight in pounds: ");
double weight = input.nextDouble();
// Prompt the user to enter height in inches
System.out.print("Enter feet: ");
double heightFeet = input.nextDouble();
System.out.print("Enter inches :");
double heightInche = input.nextDouble();
final double KILOGRAMS_PER_POUND = 0.45359237; // Constant 1磅=0.45359237米
final double METERS_PER_INCH = 0.0254; // Constant 1英寸=0.0254米
final double METERS_PER_FOOT = 0.3048;//Constant 1英尺=12英寸=0.3048米
// Compute BMI
double weightInKilograms = weight * KILOGRAMS_PER_POUND; //英镑的单位转换
double heightInMeters = heightFeet * METERS_PER_FOOT + heightInche * METERS_PER_INCH; //英尺+英寸的单位转换
double bmi = weightInKilograms / (heightInMeters * heightInMeters);//BMI计算
// Display result
System.out.println("BMI is " + bmi);
if (bmi < 18.5)
System.out.println("Underweight");
else if (bmi < 25)
System.out.println("Normal");
else if (bmi < 30)
System.out.println("Overweight");
else
System.out.println("Obese");
}
}