Chapter 3 Problems
Chapter 3 Problems
Chapter 3 Problems
import java.util.Scanner;
PROGRAM 3.2
import java.util.Scanner;
System.out.println(
digit1 + " + " + digit2 + " + " + digit3 + " = " + answer + " is " +
(digit1 + digit2 + digit3 == answer));
}
}
PROGRAM 3.3
import java.util.Scanner;
PROGRAM 3.4
PROGRAM 3.5
import java.util.Scanner;
// Prompt the user to enter an integer for today's day of the week
System.out.print("Enter today’s day: ");
int day = input.nextInt();
System.out.print("Today is ");
switch (day)
{
case 0: System.out.print("Sunday"); break;
case 1: System.out.print("Monday"); break;
case 2: System.out.print("Tuesday"); break;
case 3: System.out.print("Wednesday"); break;
case 4: System.out.print("Thursday"); break;
case 5: System.out.print("Friday"); break;
case 6: System.out.print("Saturday");
}
PROGRAM 3.6
import java.util.Scanner;
// Compute BMI
weight *= KILOGRAMS_PER_POUND;
double height = (inches += feet / FEET_PER_INCH) * METERS_PER_INCH;
double bmi = weight / (Math.pow(height, 2));
// 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");
}
}
PROGRAM 3.7
import java.util.Scanner;
// Display results
System.out.println("Your amount " + amount + " consists of");
System.out.println(" " + numberOfDollars +
(numberOfDollars == 1 ? " dollar" : " dollars"));
System.out.println(" " + numberOfQuarters +
(numberOfQuarters == 1 ? " quarter" : " quarters"));
System.out.println(" " + numberOfDimes +
(numberOfDimes == 1 ? " dime" : " dimes"));
System.out.println(" " + numberOfNickels +
(numberOfNickels == 1 ? " nickel" : " nickels"));
System.out.println(" " + numberOfPennies +
(numberOfPennies == 1 ? " pennie" : " pennies"));
}
}
PROGRAM 3.8
import java.util.Scanner;
// Sort numbers
int temp;
if (number2 < number1 || number3 < number1)
{
if (number2 < number1)
{
temp = number1;
number1 = number2;
number2 = temp;
}
if (number3 < number1)
{
temp = number1;
number1 = number3;
number3 = temp;
}
}
if (number3 < number2)
{
temp = number2;
number2 = number3;
number3 = temp;
}
PROGRAM 3.9
import java.util.Scanner;
// Compute d10
int d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5
+ d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11;
PROGRAM 3.10
import java.util.Scanner;
// Display result
if (number1 + number2 == answer)
System.out.println("You are correct!");
else
System.out.println("You are wrong " + number1 + " + " + number2
+ " should be " + (number1 + number2));
}
}
PROGRAM 3.11
import java.util.Scanner;
PROGRAM 3.12
import java.util.Scanner;
// Display result
System.out.println(
number + ((digit1 == digit3) ? " is a " : " is not a ") + "palindrome");
}
}
PROGRAM 3.13
import java.util.Scanner;
// Compute tax
double tax = 0;
switch (status)
{
case 0 : // Compute tax for single filers
tax += (income <= 8350) ? income * 0.10 : 8350 * 0.10;
if (income > 8350)
tax += (income <= 33950) ? (income - 8350) * 0.15 :
25600 * 0.15;
if (income > 33950)
tax += (income <= 82250) ? (income - 33950) * 0.25 :
48300 * 0.25;
if (income > 82250)
tax += (income <= 171550) ? (income - 82250) * 0.28 :
89300 * 0.28;
if (income > 171550)
tax += (income <= 372950) ? (income - 171550) * 0.33 :
201400 * 0.33;
if (income > 372950)
tax += (income - 372950) * 0.35;
break;
case 1 : // Compute tax for married file jointly or qualifying widow(er)
tax += (income <= 16700) ? income * 0.10 : 16700 * 0.10;
if (income > 16700)
tax += (income <= 67900) ? (income - 16700) * 0.15 :
(67900 - 16700) * 0.15;
if (income > 67900)
tax += (income <= 137050) ? (income - 67900) * 0.25 :
(137050 - 67900) * 0.25;
if (income > 137050)
tax += (income <= 208850) ? (income - 137050) * 0.28 :
(208850 - 137050) * 0.28;
if (income > 208850)
tax += (income <= 372950) ? (income - 208850) * 0.33 :
(372950 - 208850) * 0.33;
if (income > 372950)
tax += (income - 372950) * 0.35;
break;
case 2 : // Compute tax for married separately
tax += (income <= 8350) ? income * 0.10 : 8350 * 0.10;
if (income > 8350)
tax += (income <= 33950) ? (income - 8350) * 0.15 :
(33950 - 8350) * 0.15;
if (income > 33950)
tax += (income <= 68525) ? (income - 33950) * 0.25 :
(68525 - 33950) * 0.25;
if (income > 68525)
tax += (income <= 104425) ? (income - 68525) * 0.28 :
(104425 - 68525) * 0.28;
if (income > 104425)
tax += (income <= 186475) ? (income - 104425) * 0.33 :
(186475 - 104425) * 0.33;
if (income > 186475)
tax += (income - 186475) * 0.35;
break;
case 3 : // Compute tax for head of household
tax += (income <= 11950) ? income * 0.10 : 11950 * 0.10;
if (income > 11950)
tax += (income <= 45500) ? (income - 11950) * 0.15 :
(45500 - 11950) * 0.15;
if (income > 45500)
tax += (income <= 117450) ? (income - 45500) * 0.25 :
(117450 - 45500) * 0.25;
if (income > 117450)
tax += (income <= 190200) ? (income - 117450) * 0.28 :
(190200 - 117450) * 0.28;
if (income > 190200)
tax += (income <= 372950) ? (income - 190200) * 0.33 :
(372950 - 190200) * 0.33;
if (income > 372950)
tax += (income - 372950) * 0.35;
break;
default : System.out.println("Error: invalid status");
System.exit(1);
}
// Display the result
System.out.println("Tax is " + (int)(tax * 100) / 100.0);
}
}
PROGRAM 3.14
import java.util.Scanner;
// Display result
System.out.println(((guess == coin) ? "Correct" : "Incorrect") + " guess.");
}
}
PROGRAM 3.15
import java.util.Scanner;
}
}
}
Program 3.16
// Display coordinate
System.out.println("Random coordinate in rectangle centered at (0,0)");
System.out.println(
"with width 100 and height 200: (" + width + ", " + height + ")");
}
}
PROGRAM 3.17
import java.util.Scanner;
// Display result
if (computer == user)
System.out.println(" too. It is a draw");
else
{
boolean win = (user == 0 && computer == 2) ||
(user == 1 && computer == 0) ||
(user == 2 && computer == 1);
if (win)
System.out.println(". You won");
else
System.out.println(". You lose");
}
}
}
PROGRAM 3.18
import java.util.Scanner;
PROGRAM 3.19
import java.util.Scanner;
if (!valid)
{
System.out.println("Input is invalid.");
System.exit(1);
}
PROGRAM 3.20
import java.util.Scanner;
// Display result
System.out.println("The wind chill index is " + windChill);
}
}
Program 3.21
import java.util.Scanner;
// Prompt the user to enter a year, month, and day of the month.
System.out.print("Enter year: (e.g., 2012): ");
int year = input.nextInt();
System.out.print("Enter month: 1-12: ");
int month = input.nextInt();
System.out.print("Enter the day of the month: 1-31: ");
int dayOfMonth = input.nextInt();
// Display reslut
System.out.print("Day of the week is ");
switch(dayOfWeek)
{
case 0: System.out.println("Saturday"); break;
case 1: System.out.println("Sunday"); break;
case 2: System.out.println("Monday"); break;
case 3: System.out.println("Tuesday"); break;
case 4: System.out.println("Wednesday"); break;
case 5: System.out.println("Thursday"); break;
case 6: System.out.println("Friday");
}
}
}
PROGRAM 3.22
import java.util.Scanner;
// Display results
System.out.println("Point (" + x + ", "+ y + ") is " +
((withinCircle) ? "in " : "not in ") + "the circle");
}
}
PROGRAM 3.23
import java.util.Scanner;
PROGRAM 3.24
PROGRAM 3.25
import java.util.Scanner;
public class Exercise_03_25 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);// Create Scanner object
// Display results
if (a * d - b * c == 0)
{
System.out.println("The two lines are parallel");
}
else
{
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
System.out.println("The intersecting point is at (" + x + ", " + y + ")");
}
}
}
PROGRAM 3.26
import java.util.Scanner;
PROGRAM 3.27
import java.util.Scanner;
// Get the intersecting point with the hypotenuse side of the triangle
// of a line that starts and points (0, 0) and touches the user points
double intersectx = (-x * (200 * 100)) / (-y * 200 - x * 100);
double intersecty = (-y * (200 * 100)) / (-y * 200 - x * 100);
// Display results
System.out.println("The point " + ((x > intersectx || y > intersecty)
? "is not " : "is " ) + "in the triangle");
}
}
PROGRAM 3.28
import java.util.Scanner;
PROGRAM 3.29
import java.util.Scanner;
// Prompt the user to enter the center coordinates and radii of two circles
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();
PROGRAM 3.30
import java.util.Scanner;
// Display results
System.out.println(
"Current time is " + ((currentHour > 12) ? currentHour - 12 :
currentHour) + ":" + currentMinute + ":" + currentSecond +
((currentHour > 12) ? " PM" : " AM"));
}
}
PROGRAM 3.31
import java.util.Scanner;
// Prompt the user to enter the exchange rate from USD to RMB
System.out.print("Enter the exchange rate from dollars to RMB: ");
double rate = input.nextDouble();
PROGRAM 3.32
import java.util.Scanner;
// Prompt the user to enter the three points for p0, p1, and p2
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();
// Display result
System.out.print("(" + x2 + ", " + y2 + ") is on the ");
if (position > 0)
System.out.print("left side of the ");
if (position < 0)
System.out.print("right side of the ");
System.out.println("line from (" + x0 + ", " + y0 +
") to (" + x1 + ", " + y1 + ")");
}
}
PROGRAM 3.33
import java.util.Scanner;
// Prompt the user to enter the weight and price of each package
System.out.print("Enter weight and price for package 1: ");
double weight1 = input.nextDouble();
double price1 = input.nextDouble();
System.out.print("Enter weight and price for package 2: ");
double weight2 = input.nextDouble();
double price2 = input.nextDouble();
import java.util.Scanner;
// Prompt the user to enter the three points for p0, p1, and p2
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();
// Display result
System.out.print("(" + x2 + ", " + y2 + ") is ");
if (!online)
System.out.print("not ");
System.out.println("on the line segment from (" + x0 + ", " + y0 +
") to (" + x1 + ", " + y1 + ")");
}
}