// 自己练习的
// 第三章课后题基本完成,除了一些需要改编的没写(太懒了),而且题目难度不是很大。唉,人就是喜欢给自己的懒惰找借口。
// 题目基本是独立完成,除3.27,3.28参考了第八版课后习题外。
// 拖的时间太长了,晚上时间利用低效,不专注
// 2015/10/9 夜
import java.util.Scanner;
public class Code_Practice_3 {
public static void main(String[] args) {
// 3.1
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c:");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double discriminant = (b * b) - (4 * a * c);
if (discriminant > 0) {
double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
System.out.println("The equation has two roots " + r1 + " and " + r2);
}
else if (discriminant == 0){
double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
System.out.println("The equation has one root " + r1);
}
else
System.out.println("The equation has no real roots");
// 3.4
int number = (int)(Math.random() * 12) + 1;
switch (number) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February "); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December");
}
// 3.5
Scanner input = new Scanner(System.in);
System.out.print("Enter today's day:");
int today = input.nextInt();
System.out.print("Enter the number of elpased since today:");
int elpased = input.nextInt();
switch (today) {
case 0: System.out.print("Today is Sunday"); break;
case 1: System.out.print("Today is Monday"); break;
case 2: System.out.print("Today is Tuesday"); break;
case 3: System.out.print("Today is Wednesday"); break;
case 4: System.out.print("Today is Thursday"); break;
case 5: System.out.print("Today is Friday"); break;
case 6: System.out.print("Today is Saturday");
}
int next = (today + elpased) % 7;
switch (next) {
case 0: System.out.println(" and the future day is Sunday"); break;
case 1: System.out.println(" and the future day is Monday"); break;
case 2: System.out.println(" and the future day is Tuesday"); break;
case 3: System.out.println(" and the future day is Wednesday"); break;
case 4: System.out.println(" and the future day is Thursday"); break;
case 5: System.out.println(" and the future day is Friday"); break;
case 6: System.out.println(" and the future day is Saturday");
}
// 3.8
Scanner input = new Scanner(System.in);
System.out.print("Enter three integers:");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
System.out.println(num1 + " " + num2 + " " + num3);
// 3.9
Scanner input = new Scanner(System.in);
System.out.print("Enter the first 9 digits of an ISBN as integer:");
int integer = input.nextInt();
int d1 = integer / 100000000;
// System.out.println(d1); test
int d2 = integer / 10000000 % 10;
int d3 = integer / 1000000 % 10;
int d4 = integer / 100000 % 10;
int d5 = integer / 10000 % 10;
int d6 = integer / 1000 % 10;
int d7 = integer / 100 % 10;
int d8 = integer / 10 % 10;
int d9 = integer % 10;
int check = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 +
d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11;
if (check == 10)
System.out.println(d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 +
"" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + "X");
else
System.out.println(d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 +
"" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + check);
// 3.11Scanner input = new Scanner(System.in);
System.out.print("Enter a month and a year:");
int month = input.nextInt();
int year = input.nextInt();
if ((year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0)) {
switch(month) {
case 1:System.out.println("Jan. has 31 days."); break;
case 2:System.out.println("Feb. has 29 days."); break;
case 3:System.out.println("Mar. has 31 days."); break;
case 4:System.out.println("Apr. has 30 days."); break;
case 5:System.out.println("May has 31 days."); break;
case 6:System.out.println("Jun. has 30 days."); break;
case 7:System.out.println("Jul. has 31 days."); break;
case 8:System.out.println("Aug. has 31 days."); break;
case 9:System.out.println("Sep. has 30 days."); break;
case 10:System.out.println("Oct. has 31 days."); break;
case 11:System.out.println("Nov. has 30 days."); break;
case 12:System.out.println("Dec. has 31 days.");
}
}
else {
switch(month) {
case 1:System.out.println("Jan. has 31 days."); break;
case 2:System.out.println("Feb. has 28 days."); break;
case 3:System.out.println("Mar. has 31 days."); break;
case 4:System.out.println("Apr. has 30 days."); break;
case 5:System.out.println("May has 31 days."); break;
case 6:System.out.println("Jun. has 30 days."); break;
case 7:System.out.println("Jul. has 31 days."); break;
case 8:System.out.println("Aug. has 31 days."); break;
case 9:System.out.println("Sep. has 30 days."); break;
case 10:System.out.println("Oct. has 31 days."); break;
case 11:System.out.println("Nov. has 30 days."); break;
case 12:System.out.println("Dec. has 31 days.");
}
}// 3.12
Scanner input = new Scanner(System.in);System.out.print("Enter a three-digit integer:");
int integer = input.nextInt();
if ((integer / 100) == (integer % 10))
System.out.println(integer + " is a palindrome");
else
System.out.println(integer + " is not a palindrome");// 3.14
Scanner input = new Scanner(System.in);
System.out.print("Enter 0 or 1, 0 stand for head, 1 stand for tail:");
int guess = input.nextInt();
int coin = (int)(Math.random() * 2);
if (guess == coin)
System.out.println("Your guess is correct, the coin is " + coin);
else
System.out.println("Your guess is incorrect, the coin is " + coin);
// 3.15
int lottery = (int)(100 + Math.random() * 900);
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (three digits):");
int guess = input.nextInt();
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = lottery / 10 % 10;
int lotteryDigit3 = lottery % 10;
int guessDigit1 = guess / 100;
int guessDigit2 = guess / 10 % 10;
int guessDigit3 = guess % 10;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if ((lotteryDigit1 == guessDigit1 && lotteryDigit2 == guessDigit3 && lotteryDigit3 == guessDigit2)
|| (lotteryDigit1 == guessDigit3 && lotteryDigit2 == guessDigit2 && lotteryDigit3 == guessDigit1)
|| (lotteryDigit1 == guessDigit2 && lotteryDigit2 == guessDigit3 && lotteryDigit3 == guessDigit1)
|| (lotteryDigit1 == guessDigit3 && lotteryDigit2 == guessDigit1 && lotteryDigit3 == guessDigit3)
|| (lotteryDigit1 == guessDigit2 && lotteryDigit2 == guessDigit1 && lotteryDigit3 == guessDigit2))
System.out.println("Match all digits: you win $3,000");
else if (guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3
|| guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2
|| guessDigit2 == lotteryDigit3
|| guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry,no match");
// 3.16
int lengthDot = (int)(Math.random() * 100);
int heightDot = (int)(Math.random() * 200);
System.out.println("The coordinate is " + "(" +
lengthDot + "," + heightDot + ")");
// 3.17
Scanner input = new Scanner(System.in);
System.out.print("scissor (0), rock(1), paper(2):");
int numByUser = input.nextInt();
int numByCom = (int)(Math.random() * 3);
if (numByUser == numByCom){
switch(numByUser) {
case 0:System.out.println("The computer is scissor. You are scissor too.It is draw."); break;
case 1:System.out.println("The computer is rock. You are rock too.It is draw."); break;
case 2:System.out.println("The computer is paper. You are paper too.It is draw."); break;
}
}
else if ((numByUser == 0 && numByCom == 2)
|| (numByUser == 1 && numByCom == 0)
|| (numByUser == 2 && numByCom == 1)){
switch(numByUser) {
case 0:System.out.println("The computer is paper. You are scissor.You win!"); break;
case 1:System.out.println("The computer is scissor. You are rock.You win!"); break;
case 2:System.out.println("The computer is rock. You are paper.You win!"); break;
}
}
else if ((numByUser == 0 && numByCom == 1)
|| (numByUser == 1 && numByCom == 2)
|| (numByUser == 2 && numByCom == 0)
){
switch(numByUser) {
case 0:System.out.println("The computer is rock. You are scissor.You lose!"); break;
case 1:System.out.println("The computer is paper. You are rock.You lose!"); break;
case 2:System.out.println("The computer is scissor. You are paper.You lose!"); break;
}
}
// 3.18
Scanner input = new Scanner(System.in);
System.out.print("Enter the weight of package:");
int weight = input.nextInt();
if (weight > 0 && weight <= 1)
System.out.println("The cost is 3.5");
else if (weight > 1 && weight <= 3)
System.out.println("The cost is 5.5");
else if (weight > 3 && weight <= 10)
System.out.println("The cost is 8.5");
else if (weight > 10 && weight <= 20)
System.out.println("The cost is 10.5");
else
System.out.println("The package cannot be shipped");
// 3.19
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides:");
int side1 = input.nextInt();
int side2 = input.nextInt();
int side3 = input.nextInt();
if ((side1 + side2 > side3) && (side1 + side3 > side2)
&& (side2 + side3 > side1))
System.out.println("The circle is " + (side1 + side2 + side3));
else
System.out.println("The side is not legal.");
// 3.21
Scanner input = new Scanner(System.in);
System.out.print("Enter year: (e.g., 2012):");
int year = input.nextInt();
System.out.print("Enter month:");
int month = input.nextInt();
System.out.print("Enter the day of the month:");
int day = input.nextInt();
int week = 0;
if (month == 1) {
month = 13;
year -= 1;
week = (day + 26 * (month + 1) / 10 + (year % 100) +
(year % 100) / 4 + (year / 100) / 4 + (year / 100) * 5) % 7;
}
else if (month == 2) {
month = 14;
year -= 1;
week = (day + 26 * (month + 1) / 10 + (year % 100) +
(year % 100) / 4 + (year / 100) / 4 + (year / 100) * 5) % 7;
}
else
week = (day + 26 * (month + 1) / 10 + (year % 100) +
(year % 100) / 4 + (year / 100) / 4 + (year / 100) * 5) % 7;
switch(week) {
case 0:System.out.println("Day of week is Saturday"); break;
case 1:System.out.println("Day of week is Sunday"); break;
case 2:System.out.println("Day of week is Monday"); break;
case 3:System.out.println("Day of week is Tuesday"); break;
case 4:System.out.println("Day of week is Wednesday"); break;
case 5:System.out.println("Day of week is Thursday"); break;
case 6:System.out.println("Day of week is Friday"); break;
}
// 3.22
Scanner input = new Scanner(System.in);
System.out.print("Enter a point with two coordinates:");
int point1 = input.nextInt();
int point2 = input.nextInt();
double sum = point1 * point1 + point2 * point2;
if (Math.pow(sum, 0.5) < 10)
System.out.println("Point (" + point1 + ", " + point2 +
") is in the circle");
else
System.out.println("Point (" + point1 + ", " + point2 +
") is not in the circle");
// 3.23
Scanner input = new Scanner(System.in);
System.out.print("Enter a point with two coordinates:");
double point1 = input.nextDouble();
double point2 = input.nextDouble();
if (point1 <= 5 && point2 <= 2.5)
System.out.println("Point (" + point1 + ", " + point2 +
") is in the rectangle");
else
System.out.println("Point (" + point1 + ", " + point2 +
") is not in the rectangle");
// 3.24
int num = (int)(1 + Math.random() * 13);
switch(num) {
case 1:System.out.print("The card of you picked is Ace "); break;
case 2:System.out.print("The card of you picked is 2 "); break;
case 3:System.out.print("The card of you picked is 3 "); break;
case 4:System.out.print("The card of you picked is 4 "); break;
case 5:System.out.print("The card of you picked is 5 "); break;
case 6:System.out.print("The card of you picked is 6 "); break;
case 7:System.out.print("The card of you picked is 7 "); break;
case 8:System.out.print("The card of you picked is 8 "); break;
case 9:System.out.print("The card of you picked is 9 "); break;
case 10:System.out.print("The card of you picked is 10 "); break;
case 11:System.out.print("The card of you picked is Jack "); break;
case 12:System.out.print("The card of you picked is Queen "); break;
case 13:System.out.print("The card of you picked is King "); break;
}
int color = (int)(1 + Math.random() * 4);
switch(color) {
case 1:System.out.println("of Hearts"); break;
case 2:System.out.println("of Diamonds"); break;
case 3:System.out.println("of Spades"); break;
case 4:System.out.println("of Clubs"); break;
}
// 3.26
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer:");
int integer = input.nextInt();
boolean div1 = (integer % 5 == 0) && (integer % 6 == 0);
boolean div2 = (integer % 5 == 0) || (integer % 6 == 0);
boolean div3 = (integer % 5 == 0) ^ (integer % 6 == 0);
System.out.println("Is " + integer + " divisible by 5 and 6? " + div1);
System.out.println("Is " + integer + " divisible by 5 or 6? " + div2);
System.out.println("Is " + integer + " divisible by 5 or 6, but not both? " + div3);*/
// 3.27
Scanner input = new Scanner(System.in);
System.out.print("Enter a point's x- and y-coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
if (x > 200 || x < 0 || y > 100 || y < 0)
System.out.println("The point is not in the triangle");
else {
double slope = (200.0 - 0) / (0 - 100.0);
double x1 = x + -y * slope;
if (x1 <= 200)
System.out.println("The point is in the triangle");
else
System.out.println("The point is not in the triangle");
}
// 3.28
Scanner input = new Scanner(System.in);
System.out.print("Enter r1's center x-, y-coordinates, width, and height: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double w1 = input.nextDouble();
double h1 = input.nextDouble();
System.out.print("Enter r2's center x-, y-coordinates, width, and height: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double w2 = input.nextDouble();
double h2 = input.nextDouble();
double xDistance = x1 - x2 >= 0 ? x1 - x2 : x2 - x1;
double yDistance = y1 - y2 >= 0 ? y1 - y2 : y2 - y1;
if (xDistance <= (w1 - w2) / 2 && yDistance <= (h1 - h2) / 2)
System.out.println("r2 is inside r1");
else if (xDistance <= (w1 + w2) / 2 && yDistance <= (h1 + h2) / 2)
System.out.println("r2 overlaps r1");
else
System.out.println("r2 does not overlap r1");
// 3.29
Scanner input = new Scanner(System.in);
System.out.print("Enter circle1's center x-, y-coordinates, and radius:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double r1 = input.nextDouble();
System.out.print("Enter circle2's center x-, y-coordinates, and radius:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double r2 = input.nextDouble();
double temp = Math.pow((x1 - x2), 2) + Math.pow((y2 - y1), 2);
double distance = Math.pow(temp, 0.5);
if (distance <= Math.abs(r1 - r2))
System.out.println("circle2 is inside circle1");
else if (distance <= (r1 + r2))
System.out.println("circle2 overlaps circle1");
else
System.out.println("circle2 does not overlap circle1");
// 3.31
Scanner input = new Scanner(System.in);
System.out.print("Enter teh exchange rate from dollars to RMB:");
double rate = input.nextDouble();
System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa:");
int choice = input.nextInt();
if (choice == 0) {
System.out.print("Enter the dollars amount:");
double dollar = input.nextDouble();
double rmb = dollar * rate;
System.out.println("$" + dollar + " is " + rmb + " yuan");
}
else if (choice == 1) {
System.out.print("Enter the RMB amount:");
double rmb = input.nextDouble();
double dollar = (int)(rmb / rate * 100) / 100.0;
System.out.println(rmb + " yuan is " + "$" + dollar);
}
else
System.out.println("Enter a correct number");
// 3.32
Scanner input = new Scanner(System.in);
System.out.print("Enter three points for p0, p1 and p2:");
double x0 = input.nextDouble();
double y0 = input.nextDouble();
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double result = (x1-x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
System.out.print("(" + x2 + ", " + y2 + ")" +
" is on the ");
if (result > 0)
System.out.println("left side of the line from (" +
x0 + ", " + y0 + ") to (" + x1 + ", " + y1 + ")");
else if (result == 0)
System.out.println("line from (" +
x0 + ", " + y0 + ") to (" + x1 + ", " + y1 + ")");
else
System.out.println("right side of the line from (" +
x0 + ", " + y0 + ") to (" + x1 + ", " + y1 + ")");
// 3.33
Scanner input = new Scanner(System.in);
System.out.print("Enter weight and price for package1:");
double w1 = input.nextDouble();
double p1 = input.nextDouble();
System.out.print("Enter weight and price for package2:");
double w2 = input.nextDouble();
double p2 = input.nextDouble();
double amount1 = w1 * p1;
double amount2 = w2 * p2;
if (amount1 > amount2)
System.out.println("Package 2 has a better price.");
else if (amount2 > amount1)
System.out.println("Package 1 has a better price.");
else
System.out.println("Two packages have the same price.");}
}