1. (简答题)Solve Quadratic Equations:
import java.util.Scanner;
import static java.lang.Math.pow;
public class Demo {
public static void main(String[] args) {
double a, b, c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a , b , c:");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
double r1 = (-b+pow(b*b-4*a*c,0.5))/2*a;
double r2 = (-b-pow(b*b-4*a*c,0.5))/2*a;
double s = b*b-4*a*c;
if(s>0){
System.out.println("The equation has two roots : "+(int)(r1*1000000)/1000000.0+" and "+(int)(r2*100000)/100000.0);
}else if (s == 0 ){
System.out.println("The equation has one root : "+r1);
}else if(s<0){
System.out.println("The equation has no real roots");
}
}
}
2. (简答题)Random month:
import java.util.Random;
public class Demo2 {
public static void main(String[] args) {
Random r = new Random();
int month = r.nextInt(11) + 1;
switch (month) {
case 1:
System.out.println(month + " " + "January");
break;
case 2:
System.out.println(month + " " + "February");
break;
case 3:
System.out.println(month + " " + "March");
break;
case 4:
System.out.println(month + " " + "April");
break;
case 5:
System.out.println(month + " " + "May");
break;
case 6:
System.out.println(month + " " + "June");
break;
case 7:
System.out.println(month + " " + "July");
break;
case 8:
System.out.println(month + " " + "August");
break;
case 9:
System.out.println(month + " " + "September");
break;
case 10:
System.out.println(month + " " + "October");
break;
case 11:
System.out.println(month + " " + "November");
break;
case 12:
System.out.println(month + " " + "December");
break;
}
}
}
3. (简答题)Sort three integers:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter three numbers:");
int a, b, c;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
int temp1 = (a > b) ? a : b;
temp1 = (temp1 > c) ? temp1 : c;
int temp2;
int temp3;
if (temp1 == a) {
if (b > c) {
temp2 = b;
temp3 = c;
} else {
temp2 = c;
temp3 = b;
}
} else if (temp1 == b) {
if (a > c) {
temp2 = a;
temp3 = c;
} else {
temp2 = c;
temp3 = a;
}
} else {
if (a > b) {
temp2 = a;
temp3 = b;
} else {
temp2 = b;
temp3 = a;
}
}
System.out.println("The order of the three numbers is" + " " + temp3 + " " + temp2 + " " + temp1);
}
}
4. (简答题)Business:check ISBN-10:
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
System.out.println("Enter the first 9 digits of an IBSN as integer:");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int d9 = number % 10;
int d8 = number / 10 % 10;
int d7 = number / 100 % 10;
int d6 = number / 1000 % 10;
int d5 = number / 10000 % 10;
int d4 = number / 100000 % 10;
int d3 = number / 1000000 % 10;
int d2 = number / 10000000 % 10;
int d1 = number / 100000000 % 10;
int d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9)%11;
if(d10==10){
String d = "X";
System.out.println("The ISBN-10 number is"+" "+"0"+number+d);
}else{
System.out.println("The ISBN-10 number is"+" "+"0"+number+d10);
}
}
5. (简答题)Palindrome number:
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a three-digit integer:");
int number = sc.nextInt();
int a = number % 10;
int c = number / 100 % 10;
if (a == c) {
System.out.println(number + " is a palindrome");
} else {
System.out.println(number + " is not a palindrome");
}
}
}
6. (简答题)Game: lottery:
import java.util.Random;
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) {
Random r = new Random();
int lotteryNumber = r.nextInt(900) + 100;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a three-digit number:");
int a = sc.nextInt();
int unit1 = a % 10;
int tens1 = a / 10 % 10;
int kilobit1 = a / 100 % 10;
int unit2 = lotteryNumber % 10;
int tens2 = a / 10 % 10;
int kilobit2 = a / 100 % 10;
System.out.println("The Lottery number is " + lotteryNumber);
if (unit1 == unit2 && tens1 == tens2 && kilobit1 == kilobit2) {
System.out.println("The award is $10,000");
} else if (unit1 == tens2 && tens1 == kilobit2 && kilobit1 == unit2) {
System.out.println("The award is $3,000");
} else if (unit1 == tens2 && tens1 == unit2 && kilobit1 == kilobit2) {
System.out.println("The award is $3,000");
} else if (unit1 == kilobit2 && tens1 == unit2 && kilobit1 == tens2) {
System.out.println("The award is $3,000");
} else if (unit1 == kilobit2 && tens1 == tens2 && kilobit1 == unit2) {
System.out.println("The award is $3,000");
} else if (unit1 == unit1 && tens1 == kilobit2 && kilobit1 == tens2) {
System.out.println("The award is $3,000");
} else if (unit1==unit2||unit1==tens2||unit1==kilobit2||tens1==unit2||tens1==tens2||tens1==kilobit2||kilobit1==unit2||kilobit1==tens2||kilobit1==kilobit2) {
System.out.println("The award is $1,000");
}else{
System.out.println("Sorry,you don't get an award");
}
}
}
7. (简答题)
Financial application: compute taxes.
Listing 3.5, ComputeTax.java, gives the source code to compute taxes for single filers. Complete Listing 3.5 to compute the taxes for all filing statuses by using a switch statement.
import java.util.Scanner;
public class Demo7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("(0-single filer ,1-married jointly or" + "qualifying widow(er),2-married separately,3-head of" + "household)Enter the filing status:");
int status = sc.nextInt();
System.out.println("Enter the taxable income:");
double income = sc.nextDouble();
double tax = 0;
switch (status) {
case 0:
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;
}
break;
case 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;
}
break;
case 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;
}
break;
case 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 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
}
break;
default:
System.out.println("Error : invalid status");
}
System.out.println("Tax is " + (int)(tax*100)/100.0);
}
}