0% found this document useful (0 votes)
45 views26 pages

Java Math Exercises and Solutions

This document contains 11 Java programs (PROGRAM 3.1 through PROGRAM 3.11) that demonstrate various programming concepts such as: - Computing the roots of a quadratic equation - Generating random numbers and testing user input - Solving linear equations - Using switch statements to output month names - Calculating future days of the week from inputs - Computing BMI from weight and height inputs - Converting monetary amounts to coins and bills - Sorting numbers in ascending order - Validating 10-digit ISBN numbers - Generating addition problems and checking answers - Determining number of days in a month based on leap year logic The programs get various inputs from users, perform calculations, and output

Uploaded by

M. Hamza Akhtar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views26 pages

Java Math Exercises and Solutions

This document contains 11 Java programs (PROGRAM 3.1 through PROGRAM 3.11) that demonstrate various programming concepts such as: - Computing the roots of a quadratic equation - Generating random numbers and testing user input - Solving linear equations - Using switch statements to output month names - Calculating future days of the week from inputs - Computing BMI from weight and height inputs - Converting monetary amounts to coins and bills - Sorting numbers in ascending order - Validating 10-digit ISBN numbers - Generating addition problems and checking answers - Determining number of days in a month based on leap year logic The programs get various inputs from users, perform calculations, and output

Uploaded by

M. Hamza Akhtar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM 3.

import [Link];

public class Exercise_03_01 {


public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner([Link]);

// Prompt the user to enter values for a, b and c.


[Link]("Enter a, b, c: ");
double a = [Link]();
double b = [Link]();
double c = [Link]();

// Compute the discriminant of the quadriatic equation.


double discriminant = [Link](b, 2) - 4 * a * c;

// Compute the real roots of the quadriatic equation if any.


[Link]("The equation has ");
if (discriminant > 0)
{
double root1 = (-b + [Link](discriminant, 0.5)) / (2 * a);  
double root2 = (-b - [Link](discriminant, 0.5)) / (2 * a);  
[Link]("two roots " + root1 + " and " + root2);
}
else if (discriminant == 0)
{
double root1 = (-b + [Link](discriminant, 0.5)) / (2 * a);
[Link]("one root " + root1);
}
else
[Link]("no real roots");
}
}

PROGRAM 3.2

import [Link];

public class Exercise_03_02 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Generate three random integers


int digit1 = (int)([Link]() * 10);
int digit2 = (int)([Link]() * 10);
int digit3 = (int)([Link]() * 10);

// Prompt user to enter the sum of three integers


[Link](
"What is " + digit1 + " + " + digit2 + " + " + digit3 + "? ");
int answer = [Link]();

[Link](
digit1 + " + " + digit2 + " + " + digit3 + " = " + answer + " is " +
(digit1 + digit2 + digit3 == answer));
}
}

PROGRAM 3.3

import [Link];

public class Exercise_03_03 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter a, b, c, d, e, and f.


[Link]("Enter a, b, c, d, e, f: ");
double a = [Link]();
double b = [Link]();
double c = [Link]();
double d = [Link]();
double e = [Link]();
double f = [Link]();

// Solve the linear equation


if (a * d - b * c == 0)
[Link]("The equation has no solution.");
else
{
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
[Link]("x is " + x + " and y is " + y);
}
}
}

PROGRAM 3.4

public class Exercise_03_04 {


public static void main(String[] args) {
// Generate an integer between 1 and 12.
int month = (int)(([Link]() * 12) + 1);

// Display the English month name


switch (month)
{
case 1: [Link]("January"); break;
case 2: [Link]("February"); break;
case 3: [Link]("March"); break;
case 4: [Link]("April"); break;
case 5: [Link]("May"); break;
case 6: [Link]("June"); break;
case 7: [Link]("July"); break;
case 8: [Link]("August"); break;
case 9: [Link]("September"); break;
case 10: [Link]("October"); break;
case 11: [Link]("November"); break;
case 12: [Link]("December");
}
}
}

PROGRAM 3.5

import [Link];

public class Exercise_03_05 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter an integer for today's day of the week
[Link]("Enter today’s day: ");
int day = [Link]();

// Prompt the user to enter the number of days after today


[Link]("Enter the number of days elapsed since today: ");
int daysElapsed = [Link]();

// Calculate future day


int futureDay = (day + daysElapsed) % 7;

[Link]("Today is ");
switch (day)
{
case 0: [Link]("Sunday"); break;
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
case 4: [Link]("Thursday"); break;
case 5: [Link]("Friday"); break;
case 6: [Link]("Saturday");
}

[Link](" and the future day is ");


switch (futureDay)
{
case 0: [Link]("Sunday"); break;
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
case 4: [Link]("Thursday"); break;
case 5: [Link]("Friday"); break;
case 6: [Link]("Saturday");
}
}
}

PROGRAM 3.6

import [Link];

public class Exercise_03_06 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter weight, feet and inches


[Link]("Enter weight in pounds: ");
double weight = [Link]();
[Link]("Enter feet: ");
double feet = [Link]();
[Link]("Enter inches: ");
double inches = [Link]();

final double KILOGRAMS_PER_POUND = 0.45359237; // Constant


final double METERS_PER_INCH = 0.0254;   // Constant
final double FEET_PER_INCH = 0.0833333;  // Constant

// Compute BMI
weight *= KILOGRAMS_PER_POUND;
double height = (inches += feet / FEET_PER_INCH) * METERS_PER_INCH;
double bmi = weight / ([Link](height, 2));

// Display result
[Link]("BMI is " + bmi);
if (bmi < 18.5)
[Link]("Underweight");
else if (bmi < 25)
[Link]("Normal");
else if (bmi < 30)
[Link]("Overweight");
else
[Link]("Obese");
}
}

PROGRAM 3.7

import [Link];

public class Exercise_03_07 {


public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner([Link]);

// Receive the amount


[Link](
"Enter an amount in double, for example 11.56: ");
double amount = [Link]();

int remainingAmount = (int)(amount * 100);

// Find the number of one dollars


int numberOfDollars = remainingAmount / 100;
remainingAmount %= 100;

// Find the number of quarters in the remaining amount


int numberOfQuarters = remainingAmount / 25;
remainingAmount %= 25;

// Find the number of dimes in the remaining amount


int numberOfDimes = remainingAmount / 10;
remainingAmount %= 10;

// Find the number of nickels in the remaining amount


int numberOfNickels = remainingAmount / 5;
remainingAmount %= 5;

// Find the number of pennies in the remaining amount


int numberOfPennies = remainingAmount;

// Display results
[Link]("Your amount " + amount + " consists of");
[Link](" " + numberOfDollars +
(numberOfDollars == 1 ? " dollar" : " dollars"));
[Link](" " + numberOfQuarters +
(numberOfQuarters == 1 ? " quarter" : " quarters"));
[Link](" " + numberOfDimes +
(numberOfDimes == 1 ? " dime" : " dimes"));
[Link](" " + numberOfNickels +
(numberOfNickels == 1 ? " nickel" : " nickels"));
[Link](" " + numberOfPennies +
(numberOfPennies == 1 ? " pennie" : " pennies"));
}
}

PROGRAM 3.8

import [Link];

public class Exercise_03_08 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter three integers


[Link]("Enter three integers: ");
int number1 = [Link]();
int number2 = [Link]();
int number3 = [Link]();

// 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;
}

// Display numbers in accending order


[Link](number1 + " " + number2 + " " + number3);
}
}

PROGRAM 3.9

import [Link];

public class Exercise_03_09 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter the first 9 digits of a 10-digit ISBN


[Link]("Enter the first 9 digits of an ISBN as integer: ");
int isbn = [Link]();

// Extract the digits of the ISBN


int d1 = isbn / 100000000;
int remainingDigits = isbn % 100000000;
int d2 = remainingDigits / 10000000;
remainingDigits %= 10000000;
int d3 = remainingDigits / 1000000;
remainingDigits %= 1000000;
int d4 = remainingDigits / 100000;
remainingDigits %= 100000;
int d5 = remainingDigits / 10000;
remainingDigits %= 10000;
int d6 = remainingDigits / 1000;
remainingDigits %= 1000;
int d7 = remainingDigits / 100;
remainingDigits %= 100;
int d8 = remainingDigits / 10;
remainingDigits %= 10;
int d9 = remainingDigits;

// Compute d10
int d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5
+ d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11;

// Display the 10-digit ISBN


[Link]("The ISBN-10 number is " + d1 + d2 + d3 + d4 + d5
+ d6 + d7 + d8 + d9);
if (d10 == 10)
[Link]("X");
else
[Link](d10);
}
}

PROGRAM 3.10
import [Link];

public class Exercise_03_10 {


public static void main(String[] agrs) {
Scanner input = new Scanner([Link]);

// Generate tow integers less than 100


int number1 = (int)([Link]() * 100);
int number2 = (int)([Link]() * 100);

// Prompt the user to enter an answer


[Link](
"What is " + number1 + " + " + number2 + "? ");
int answer = [Link]();

// Display result
if (number1 + number2 == answer)
[Link]("You are correct!");
else
[Link]("You are wrong " + number1 + " + " + number2
+ " should be " + (number1 + number2));
}
}

PROGRAM 3.11

import [Link];

public class Exercise_03_11 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt user to enter the month an year


[Link]("Enter the month as integer: ");
int month = [Link]();
[Link]("Enter the year as integer: ");
int year = [Link]();
boolean leapYear =
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
// Display the number of days in the month
switch (month)
{
case 1: [Link](
"January " + year + " had 31 days"); break;
case 2: [Link]("February " + year + " had" +
 ((leapYear) ? " 29 days" : " 28 days")); break;
case 3: [Link](
"March " + year + " had 31 days"); break;
case 4: [Link](
"April " + year + " had 30 days"); break;
case 5: [Link](
"May " + year + " had 31 days"); break;
case 6: [Link](
"June " + year + " had 30 days"); break;
case 7: [Link](
"July " + year + " had 31 days"); break;
case 8: [Link](
"August " + year + " had 31 days"); break;
case 9: [Link](
"September " + year + " had 30 days"); break;
case 10: [Link](
"October " + year + " had 31 days"); break;
case 11: [Link](
"November " + year + " had 30 days"); break;
case 12: [Link](
"December " + year + " had 31 days");
}
}
}

PROGRAM 3.12

import [Link];

public class Exercise_03_12 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter a three-digit integer


[Link]("Enter a three-digit integer: ");
int number = [Link]();
// Test for palindrome
int digit1 = (int)(number / 100);
int remaining = number % 100;
int digit3 = (int)(remaining % 10);

// Display result
[Link](
number + ((digit1 == digit3) ? " is a " : " is not a ") + "palindrome");
}
}

PROGRAM 3.13

import [Link];

public class Exercise_03_13 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter filing status


[Link]("(0-single filter, 1-married jointly or " +
"qualifying widow(er), 2-married separately, 3-head of " +
"houshold) Enter the filing status: ");
int status = [Link]();

// Prompt the user to enter taxable income


[Link]("Enter the taxable income: ");
double income = [Link]();

// 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 : [Link]("Error: invalid status");
[Link](1);
}
// Display the result
[Link]("Tax is " + (int)(tax * 100) / 100.0);
}
}

PROGRAM 3.14

import [Link];

public class Exercise_03_14 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Generate a random integer 0 or 1


int coin = (int)([Link]() * 2);

// Prompt the user to enter a guess


[Link]("Enter a guess 0-head or 1-tail: ");
int guess = [Link]();

// Display result
[Link](((guess == coin) ? "Correct" : "Incorrect") + " guess.");
}
}

PROGRAM 3.15

import [Link];

public class Exercise_03_15 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Generate a random three-digit number


int lottery = (int)([Link]() * 1000);
// Prompt the user to enter a three-digit number
[Link]("Enter a three-digit number: ");
int guess = [Link]();

// Extract digits from lottery


int lotteryDigit1 = lottery / 100;
int remainingDigits = lottery % 100;
int lotteryDigit2 = remainingDigits / 10;
int lotteryDigit3 = remainingDigits % 10;

// Extract digits from guess


int guessDigit1 = guess / 100;
int remainingDigits = guess % 100;
int guessDigit2 = remainingDigits / 10;
int guessDigit3 = remainingDigits % 10;

[Link]("The lottery number is " + lottery);

// Check the guess


if (guess == lottery)
[Link]("Exact match: you win $10,000");
if (guessDigit1 == lotteryDigit2)
{

}
}
}

Program 3.16

public class Exercise_03_16 {


public static void main(String[] args) {
// Generate random width and height
int width = (int)(([Link]() * (50 + 50)) -50);
int height = (int)(([Link]() * (100 + 100)) -100);

// Display coordinate
[Link]("Random coordinate in rectangle centered at (0,0)");
[Link](
"with width 100 and height 200: (" + width + ", " + height + ")");
}
}
PROGRAM 3.17

import [Link];

public class Exercise_03_17 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Generate a random integer 0, 1, or 2


int computer = (int)([Link]() * 3);

// Prompt the user to enter a number 0, 1, or 2


[Link]("scissor (0), rock (1), paper (2): ");
int user = [Link]();

[Link]("The computer is ");


switch (computer)
{
case 0: [Link]("scissor."); break;
case 1: [Link]("rock."); break;
case 2: [Link]("paper.");
}

[Link](" You are ");


switch (user)
{
case 0: [Link]("scissor"); break;
case 1: [Link]("rock"); break;
case 2: [Link]("paper ");
}

// Display result
if (computer == user)
[Link](" too. It is a draw");
else
{
boolean win = (user == 0 && computer == 2) ||
 (user == 1 && computer == 0) ||
 (user == 2 && computer == 1);
if (win)
[Link](". You won");
else
[Link](". You lose");
}
}
}
PROGRAM 3.18

import [Link];

public class Exercise_03_18 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter the weight of the package


[Link]("Enter the weight of the package: ");
double weight = [Link]();

// Calculate cost of shipping


if (weight > 50)
[Link]("The package cannot be shipped.");
else
{
double costPerPound;
if (weight > 0 && weight <= 1)
costPerPound = 3.5;
else if (weight <= 3)
costPerPound = 5.5;
else if (weight <= 10)
costPerPound = 8.5;
else //if (weight <= 20)
costPerPound = 10.5;
[Link]("Shipping cost of package is $" +
costPerPound * weight);
}
}
}

PROGRAM 3.19

import [Link];

public class Exercise_03_19 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter three edges for a triangle


[Link]("Enter three edges for a triangle:");
[Link](" Edge 1 points x, y: ");
double x1 = [Link]();
double y1 = [Link]();
[Link](" Edge 2 points x, y: ");
double x2 = [Link]();
double y2 = [Link]();
[Link](" Edge 3 points x, y: ");
double x3 = [Link]();
double y3 = [Link]();

// Test if input is valid


boolean valid = (x1 + y1 > x3 + y3 && x2 + y2 > x3 + y3) ||
(x1 + y1 > x2 + y2 && x3 + y3 > x2 + y2) ||
(x3 + y3 > x1 + y1 && x2 + y2 > x1 + y1);

if (!valid)
{
[Link]("Input is invalid.");
[Link](1);
}

// Compute the sides of the triangle


double side1 = [Link]([Link](x2 - x1, 2) + [Link](y2 - y1, 2), 0.5);
double side2 = [Link]([Link](x3 - x2, 2) + [Link](y3 - y2, 2), 0.5);
double side3 = [Link]([Link](x1 - x3, 2) + [Link](y1 - y3, 2), 0.5);

// Display the perimeter of the triangle


[Link]("perimeter of triangle is " + (side1 + side2 + side3));
}
}

PROGRAM 3.20

import [Link];

public class Exercise_03_20 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter a temperature and a wind speed


[Link]("Enter the temperature in Fahrenheit " +
"between -58F and 41F: ");
double temperature = [Link]();
[Link]("Enter the wind speed (>= 2) in miles per hour: ");
double speed = [Link]();

if (temperature <= -58 || temperature >= 41 || speed < 2)


{
[Link]("The ");
if (temperature <= -58 || temperature >= 41)
[Link]("temperature ");
if ((temperature <= -58 || temperature >= 41) && speed < 2)
[Link]("and ");
if (speed < 2)
[Link]("wind speed ");
[Link]("is invalid");
[Link](1);
}

// Compute the wind chill index


double windChill = 35.74 + 0.6215 * temperature -
35.75 * [Link](speed, 0.16) +
0.4275 * temperature * [Link](speed, 0.16);

// Display result
[Link]("The wind chill index is " + windChill);
}
}
Program 3.21

import [Link];

public class Exercise_03_21 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter a year, month, and day of the month.
[Link]("Enter year: (e.g., 2012): ");
int year = [Link]();
[Link]("Enter month: 1-12: ");
int month = [Link]();
[Link]("Enter the day of the month: 1-31: ");
int dayOfMonth = [Link]();

// Convert January and February to months 13 and 14 of the previous year


if (month == 1 || month == 2)
{
month = (month == 1) ? 13 : 14;
year--;
}

// Calculate day of the week


int dayOfWeek = (dayOfMonth + (26 * (month + 1)) / 10 + (year % 100)
+ (year % 100) / 4 + (year / 100) / 4 + 5 * (year / 100)) % 7;

// Display reslut
[Link]("Day of the week is ");
switch(dayOfWeek)
{
case 0: [Link]("Saturday"); break;
case 1: [Link]("Sunday"); break;
case 2: [Link]("Monday"); break;
case 3: [Link]("Tuesday"); break;
case 4: [Link]("Wednesday"); break;
case 5: [Link]("Thursday"); break;
case 6: [Link]("Friday");
}
}
}

PROGRAM 3.22

import [Link];

public class Exercise_03_22 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter a point


[Link]("Enter a point with two coordinates: ");
double x = [Link]();
double y = [Link]();

// Check whether the point is within the circle


boolean withinCircle =
([Link]([Link](x, 2) + [Link](y, 2), 0.5) <= 10);

// Display results
[Link]("Point (" + x + ", "+ y + ") is " +
((withinCircle) ? "in " : "not in ") + "the circle");
}
}

PROGRAM 3.23

import [Link];

public class Exercise_03_23 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Prompt the user to enter a point (x, y)


[Link]("Enter a point with two coordinates: ");
double x = [Link]();
double y = [Link]();

// Check whether the point is within the rectangle


// centered at (0, 0) with width 10 and height 5
boolean withinRectangle = ([Link]([Link](x, 2), 0.5) <= 10 / 2 ) ||
 ([Link]([Link](y, 2), 0.5) <= 5.0 / 2);
// Display results
[Link]("Point (" + x + ", " + y + ") is " +
((withinRectangle) ? "in " : "not in ") + "the rectangle");
}
}

PROGRAM 3.24

public class Exercise_03_24 {


public static void main(String[] args) {
// Generate a random integer 1 - 13
int rank = (int)(([Link]() * (14 - 1)) + 1);

// Generate a random integer 1 - 4


int suit = (int)([Link]() * 4);

// Display card picked from deck


[Link]("The card you picked is ");
switch(rank) // Get rank
{
case 1: [Link]("Ace"); break;
case 2: [Link](rank); break;
case 3: [Link](rank); break;
case 4: [Link](rank); break;
case 5: [Link](rank); break;
case 6: [Link](rank); break;
case 7: [Link](rank); break;
case 8: [Link](rank); break;
case 9: [Link](rank); break;
case 10: [Link](rank); break;
case 11: [Link]("Jack"); break;
case 12: [Link]("Queen"); break;
case 13: [Link]("King");
}
[Link](" of ");
switch (suit) // Get suit
{
case 0: [Link]("Clubs"); break;
case 1: [Link]("Diamonds"); break;
case 2: [Link]("Hearts"); break;
case 3: [Link]("Spades");
}
}
}

PROGRAM 3.25

import [Link];
public class Exercise_03_25 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);// Create Scanner object

// Prompt the user to enter four points


[Link]("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");
double x1 = [Link]();
double y1 = [Link]();
double x2 = [Link]();
double y2 = [Link]();
double x3 = [Link]();
double y3 = [Link]();
double x4 = [Link]();
double y4 = [Link]();

// Calculate the intersecting point


// Get a, b, c, d, e, f
double a = y1 - y2;
double b = -1 * (x1 - x2);
double c = y3 - y4;
double d = -1 * (x3 - x4);
double e = (y1 - y2) * x1 - (x1 - x2) * y1;
double f = (y3 - y4) * x3 - (x3 - x4) * y3;

// Display results
if (a * d - b * c == 0)
{
[Link]("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);
[Link]("The intersecting point is at (" + x + ", " + y + ")");
}
}
}

PROGRAM 3.26

import [Link];

public class Exercise_03_26 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);// Create Scanner object

// Prompt user to an integer


[Link]("Enter an integer: ");
int number = [Link]();
// Determine whether it is divisible by 5 and 6
// Display results
[Link]("Is 10 divisible by 5 and 6? " +
((number % 5 == 0) && (number % 6 == 0)));
[Link]("Is 10 divisible by 5 or 6? " +
((number % 5 == 0) || (number % 6 == 0)));
[Link]("Is 10 divisible by 5 of 6, but not both? " +
((number % 5 == 0) ^ (number % 6 == 0)));
}
}

PROGRAM 3.27

import [Link];

public class Exercise_03_27 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]); // Create a Scanner object

// Prompt the user to enter a point with x and y coordinates


[Link]("Enter a point's x- and y-coordinates: ");
double x = [Link]();
double y = [Link]();

// Determine whether the point is inside the triangle


// getting the point of ina line that starts at point

// 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
[Link]("The point " + ((x > intersectx || y > intersecty)
? "is not " : "is " ) + "in the triangle");
}
}

PROGRAM 3.28

import [Link];

public class Exercise_03_28 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);// Create Scanner object

// Prompt the user to enter the center x, y coorginates,


// width, and height of two rectangles
[Link]("Enter r1's center x-, y-coordinates, width and height: ");
double r1x = [Link]();
double r1y = [Link]();
double r1Width = [Link]();
double r1Height = [Link]();
[Link]("Enter r2's center x-, y-coordinates, width and height: ");
double r2x = [Link]();
double r2y = [Link]();
double r2Width = [Link]();
double r2Height = [Link]();

// Determine whether the second rectangle is inside the first


if(([Link]([Link](r2y - r1y, 2), .05) + r2Height / 2 <= r1Height / 2) &&
([Link]([Link](r2x - r1x, 2), .05) + r2Width / 2 <= r1Width / 2) &&
(r1Height / 2 + r2Height / 2 <= r1Height) &&
(r1Width / 2 + r2Width / 2 <= r1Width))
[Link]("r2 is inside r1");
else if ((r1x + r1Width / 2 > r2x - r2Width) ||
(r1y + r1Height / 2 > r2y - r2Height))
[Link]("r2 overlaps r1");
else
[Link]("r2 does not overlap r1");
}
}

PROGRAM 3.29

import [Link];

public class Exercise_03_29 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]); // Create a new Scanner

// Prompt the user to enter the center coordinates and radii of two circles
[Link]("Enter circle1's center x-, y-coordinates, and radius: ");
double x1 = [Link]();
double y1 = [Link]();
double r1 = [Link]();
[Link]("Enter circle2's center x-, y-coordinates, and radius: ");
double x2 = [Link]();
double y2 = [Link]();
double r2 = [Link]();

if ([Link]([Link](x2 - x1, 2) + [Link](y2 - y1, 2), 0.5)


<= [Link](r1 - r2))
[Link]("circle2 is inside circle1");
else if ([Link]([Link](x2 - x1, 2) + [Link](y2 - y1, 2), 0.5)
<= r1 + r2)
[Link]("circle2 overlaps circle1");
else
[Link]("circle2 does not overlap circle1");
}
}

PROGRAM 3.30

import [Link];

public class Exercise_03_30 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]); // Create a Scanner

// Prompt user to enter the time offset of GMT


[Link]("Enter the time zone offset to GMT: ");
int offset = [Link]();

// Obtain the total milliseconds since midnight, Jan 1, 1970


long totalMilliseconds = [Link]();

// Obtain the total seconds since midnight, Jan 1, 1970


long totalSeconds = totalMilliseconds / 1000;

// Compute the current second in the minute in the hour


long currentSecond = totalSeconds % 60;

// Obtain the total minutes


long totalMinutes = totalSeconds / 60;

// Compute the current minute in the hour


long currentMinute = totalMinutes % 60;

// Obtain the total hours


long totalHours = totalMinutes / 60;

// Compute the current hour


long currentHour = totalHours % 24;
currentHour = currentHour + offset;

// Display results
[Link](
"Current time is " + ((currentHour > 12) ? currentHour - 12 :
currentHour) + ":" + currentMinute + ":" + currentSecond +
((currentHour > 12) ? " PM" : " AM"));
}
}

PROGRAM 3.31

import [Link];

public class Exercise_03_31 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);// Create a Scanner object

// Prompt the user to enter the exchange rate from USD to RMB
[Link]("Enter the exchange rate from dollars to RMB: ");
double rate = [Link]();

// Prompt the user to enter 0 to convert from USD to RMB


// and 1 to convert from RMB to USD
[Link]("Enter 0 to convert dollars to RMB and 1 vice versa: ");
int option = [Link]();

// Prompt the user to enter the amount in USD or RMB


// to convert it to RMB or USD respectively
double amount;
switch(option)
{
case 0: [Link]("Enter the dollar amount: ");
 amount = [Link]();
 [Link]("$" + amount + " is " +
 (amount * rate) + " yuan"); break;
case 1: [Link]("Enter the RMB amount: ");
 amount = [Link]();
 [Link](amount + " yuan is $" +
 ((int)((amount * 100) / rate)) / 100.0); break;
default: [Link]("Incorrect input");
}
}
}

PROGRAM 3.32

import [Link];

public class Exercise_03_32 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);// Create Scanner object

// Prompt the user to enter the three points for p0, p1, and p2
[Link]("Enter three points for p0, p1, and p2: ");
double x0 = [Link]();
double y0 = [Link]();
double x1 = [Link]();
double y1 = [Link]();
double x2 = [Link]();
double y2 = [Link]();

// Calculate point position


double position = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);

// Display result
[Link]("(" + x2 + ", " + y2 + ") is on the ");
if (position > 0)
[Link]("left side of the ");
if (position < 0)
[Link]("right side of the ");
[Link]("line from (" + x0 + ", " + y0 +
") to (" + x1 + ", " + y1 + ")");
}
}

PROGRAM 3.33

import [Link];

public class Exercise_03_33 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);// Create Scanner object

// Prompt the user to enter the weight and price of each package
[Link]("Enter weight and price for package 1: ");
double weight1 = [Link]();
double price1 = [Link]();
[Link]("Enter weight and price for package 2: ");
double weight2 = [Link]();
double price2 = [Link]();

if (price1 / weight1 < price2 / weight2)


[Link]("Package 1 has a better price.");
else if (price1 / weight1 > price2 / weight2)
[Link]("Package 2 has a better price.");
else
[Link]("Two packages have the same price.");
}
}

PROGRAM 3.34 (Last Program)

import [Link];

public class Exercise_03_34 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);// Create Scanner

// Prompt the user to enter the three points for p0, p1, and p2
[Link]("Enter three points for p0, p1, and p2: ");
double x0 = [Link]();
double y0 = [Link]();
double x1 = [Link]();
double y1 = [Link]();
double x2 = [Link]();
double y2 = [Link]();

// Calculate point in on line segment


boolean online =  
!(((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) > 0 ||
((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) < 0 ||
(x2 < x0) || (y2 < y0) || (x2 > x1) || (y2 > y1));

// Display result
[Link]("(" + x2 + ", " + y2 + ") is ");
if (!online)
[Link]("not ");
[Link]("on the line segment from (" + x0 + ", " + y0 +
") to (" + x1 + ", " + y1 + ")");
}
}

Common questions

Powered by AI

The BMI calculator converts weight from pounds to kilograms and height from feet and inches to meters, then calculates the Body Mass Index (BMI) by dividing weight by the square of the height. Based on the resulting BMI value, it classifies weight status into categories: 'Underweight' for BMI lower than 18.5, 'Normal' for 18.5 to less than 25, 'Overweight' for 25 to less than 30, and 'Obese' for BMI 30 and above. These categories help in evaluating the health risks associated with different weight levels.

The currency conversion program handles multiple transaction types—converting from dollars to RMB and vice versa—by prompting the user to choose one of these options. The switch statement processes the user's choice and executes the corresponding conversion calculation. This structure provides clarity and organization, allowing the program to efficiently handle the different conversion paths based on user input. It simplifies user interaction by clearly delineating distinct actions under user-determined conditions.

If a package's weight exceeds 50 pounds, the program deems it unshippable and outputs a message to that effect. Handling invalid weights is crucial for user usability because it prevents customers from attempting to ship packages outside of the service's limitations, avoiding futile shipping attempts and ensuring that only feasible shipments are processed, which enhances efficiency and customer satisfaction.

In the integer summation program, Math.random() is used to generate three random integers between 0 and 9. This provides a dynamic and unpredictable component to the problem posed to the user, requiring them to calculate the sum of these integers. The user then inputs their solution, and the program checks this input against the actual sum of the generated numbers, providing immediate feedback on the correctness of their answer.

The program computes the position of a point relative to a line using the mathematical determinant derived from the coordinates of the line’s endpoints and the point. It evaluates the expression (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0). A positive result indicates the point is on the left, while a negative result indicates the right. This distinction is vital in computational geometry for determining point orientation, which is essential in pathfinding, graphics rendering, and various geometric algorithms.

The modulo operation is used to compute future days by wrapping the count of future days around a week's cycle. By taking the sum of the current day and the elapsed days' modulo 7, the program determines the future day's position within the week. This approach ensures that calculations remain within the valid range of 0 to 6, corresponding directly to days of the week, thus maintaining correct and logical calendar calculations without exceeding the week’s boundary.

The linear equation solver checks the determinant of the coefficient matrix, calculated as (a * d - b * c), to determine if the system has no solution. If this determinant equals 0, the program concludes that the system of equations is either dependent or parallel, leading it to output 'The equation has no solution.' This condition is significant as it prevents the calculation of meaningless solutions when the equations do not form intersecting lines.

The discriminant of a quadratic equation, given by b^2 - 4ac, determines the nature of the roots. If the discriminant is greater than 0, the equation has two distinct real roots. If it equals 0, there is one repeated real root. If it is less than 0, there are no real roots, implying the roots are complex.

The program checks if a number is a palindrome by extracting the first and last digits and comparing them. For a three-digit integer, it divides the number by 100 to get the first digit and uses the modulus operator to determine the last digit. If both are equal, the number is a palindrome. This step-by-step comparison is significant because it directly checks the symmetry of the integer around the center, which is the defining property of palindromes.

Checking for a leap year is crucial because February has 29 days in a leap year instead of the typical 28. The program evaluates the year using leap year conditions: a year is a leap year if it is divisible by 4 but not by 100, except when it is divisible by 400. Correctly identifying leap years ensures that the program provides accurate information about the number of days in February each year, affecting the calendar's overall accuracy.

You might also like