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

Chapter 3 Problems

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)
148 views26 pages

Chapter 3 Problems

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
You are on page 1/ 26

PROGRAM 3.

import java.util.Scanner;

public class Exercise_03_01 {


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

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


System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();

// Compute the discriminant of the quadriatic equation.


double discriminant = Math.pow(b, 2) - 4 * a * c;

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


System.out.print("The equation has ");
if (discriminant > 0)
{
double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);  
double root2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);  
System.out.println("two roots " + root1 + " and " + root2);
}
else if (discriminant == 0)
{
double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
System.out.println("one root " + root1);
}
else
System.out.println("no real roots");
}
}

PROGRAM 3.2

import java.util.Scanner;

public class Exercise_03_02 {


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

// Generate three random integers


int digit1 = (int)(Math.random() * 10);
int digit2 = (int)(Math.random() * 10);
int digit3 = (int)(Math.random() * 10);

// Prompt user to enter the sum of three integers


System.out.print(
"What is " + digit1 + " + " + digit2 + " + " + digit3 + "? ");
int answer = input.nextInt();

System.out.println(
digit1 + " + " + digit2 + " + " + digit3 + " = " + answer + " is " +
(digit1 + digit2 + digit3 == answer));
}
}

PROGRAM 3.3

import java.util.Scanner;

public class Exercise_03_03 {


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

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


System.out.print("Enter a, b, c, d, e, f: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double d = input.nextDouble();
double e = input.nextDouble();
double f = input.nextDouble();

// Solve the linear equation


if (a * d - b * c == 0)
System.out.println("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);
System.out.println("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)((Math.random() * 12) + 1);

// Display the English month name


switch (month)
{
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");
}
}
}

PROGRAM 3.5

import java.util.Scanner;

public class Exercise_03_05 {


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

// 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();

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


System.out.print("Enter the number of days elapsed since today: ");
int daysElapsed = input.nextInt();

// Calculate future day


int futureDay = (day + daysElapsed) % 7;

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");
}

System.out.print(" and the future day is ");


switch (futureDay)
{
case 0: System.out.println("Sunday"); break;
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
case 4: System.out.println("Thursday"); break;
case 5: System.out.println("Friday"); break;
case 6: System.out.println("Saturday");
}
}
}

PROGRAM 3.6

import java.util.Scanner;

public class Exercise_03_06 {


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

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


System.out.print("Enter weight in pounds: ");
double weight = input.nextDouble();
System.out.print("Enter feet: ");
double feet = input.nextDouble();
System.out.print("Enter inches: ");
double inches = input.nextDouble();

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

public class Exercise_03_07 {


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

// Receive the amount


System.out.print(
"Enter an amount in double, for example 11.56: ");
double amount = input.nextDouble();

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

public class Exercise_03_08 {


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

// Prompt the user to enter three integers


System.out.print("Enter three integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
int number3 = input.nextInt();

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


System.out.println(number1 + " " + number2 + " " + number3);
}
}

PROGRAM 3.9

import java.util.Scanner;

public class Exercise_03_09 {


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

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


System.out.print("Enter the first 9 digits of an ISBN as integer: ");
int isbn = input.nextInt();

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


System.out.println("The ISBN-10 number is " + d1 + d2 + d3 + d4 + d5
+ d6 + d7 + d8 + d9);
if (d10 == 10)
System.out.println("X");
else
System.out.println(d10);
}
}

PROGRAM 3.10
import java.util.Scanner;

public class Exercise_03_10 {


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

// Generate tow integers less than 100


int number1 = (int)(Math.random() * 100);
int number2 = (int)(Math.random() * 100);

// Prompt the user to enter an answer


System.out.print(
"What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();

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

public class Exercise_03_11 {


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

// Prompt user to enter the month an year


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

PROGRAM 3.12

import java.util.Scanner;

public class Exercise_03_12 {


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

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


System.out.print("Enter a three-digit integer: ");
int number = input.nextInt();
// Test for palindrome
int digit1 = (int)(number / 100);
int remaining = number % 100;
int digit3 = (int)(remaining % 10);

// Display result
System.out.println(
number + ((digit1 == digit3) ? " is a " : " is not a ") + "palindrome");
}
}

PROGRAM 3.13

import java.util.Scanner;

public class Exercise_03_13 {


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

// Prompt the user to enter filing status


System.out.print("(0-single filter, 1-married jointly or " +
"qualifying widow(er), 2-married separately, 3-head of " +
"houshold) 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;
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;

public class Exercise_03_14 {


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

// Generate a random integer 0 or 1


int coin = (int)(Math.random() * 2);

// Prompt the user to enter a guess


System.out.print("Enter a guess 0-head or 1-tail: ");
int guess = input.nextInt();

// Display result
System.out.println(((guess == coin) ? "Correct" : "Incorrect") + " guess.");
}
}

PROGRAM 3.15

import java.util.Scanner;

public class Exercise_03_15 {


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

// Generate a random three-digit number


int lottery = (int)(Math.random() * 1000);
// Prompt the user to enter a three-digit number
System.out.print("Enter a three-digit number: ");
int guess = input.nextInt();

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

System.out.println("The lottery number is " + lottery);

// Check the guess


if (guess == lottery)
System.out.println("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)((Math.random() * (50 + 50)) -50);
int height = (int)((Math.random() * (100 + 100)) -100);

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

public class Exercise_03_17 {


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

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


int computer = (int)(Math.random() * 3);

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


System.out.print("scissor (0), rock (1), paper (2): ");
int user = input.nextInt();

System.out.print("The computer is ");


switch (computer)
{
case 0: System.out.print("scissor."); break;
case 1: System.out.print("rock."); break;
case 2: System.out.print("paper.");
}

System.out.print(" You are ");


switch (user)
{
case 0: System.out.print("scissor"); break;
case 1: System.out.print("rock"); break;
case 2: System.out.print("paper ");
}

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

public class Exercise_03_18 {


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

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


System.out.print("Enter the weight of the package: ");
double weight = input.nextDouble();

// Calculate cost of shipping


if (weight > 50)
System.out.println("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;
System.out.println("Shipping cost of package is $" +
costPerPound * weight);
}
}
}

PROGRAM 3.19

import java.util.Scanner;

public class Exercise_03_19 {


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

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


System.out.println("Enter three edges for a triangle:");
System.out.print(" Edge 1 points x, y: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print(" Edge 2 points x, y: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
System.out.print(" Edge 3 points x, y: ");
double x3 = input.nextDouble();
double y3 = input.nextDouble();

// 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)
{
System.out.println("Input is invalid.");
System.exit(1);
}

// Compute the sides of the triangle


double side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
double side2 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
double side3 = Math.pow(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2), 0.5);

// Display the perimeter of the triangle


System.out.println("perimeter of triangle is " + (side1 + side2 + side3));
}
}

PROGRAM 3.20

import java.util.Scanner;

public class Exercise_03_20 {


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

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


System.out.print("Enter the temperature in Fahrenheit " +
"between -58F and 41F: ");
double temperature = input.nextDouble();
System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
double speed = input.nextDouble();

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


{
System.out.print("The ");
if (temperature <= -58 || temperature >= 41)
System.out.print("temperature ");
if ((temperature <= -58 || temperature >= 41) && speed < 2)
System.out.print("and ");
if (speed < 2)
System.out.print("wind speed ");
System.out.println("is invalid");
System.exit(1);
}

// Compute the wind chill index


double windChill = 35.74 + 0.6215 * temperature -
35.75 * Math.pow(speed, 0.16) +
0.4275 * temperature * Math.pow(speed, 0.16);

// Display result
System.out.println("The wind chill index is " + windChill);
}
}
Program 3.21

import java.util.Scanner;

public class Exercise_03_21 {


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

// 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();

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

public class Exercise_03_22 {


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

// Prompt the user to enter a point


System.out.print("Enter a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();

// Check whether the point is within the circle


boolean withinCircle =
(Math.pow(Math.pow(x, 2) + Math.pow(y, 2), 0.5) <= 10);

// Display results
System.out.println("Point (" + x + ", "+ y + ") is " +
((withinCircle) ? "in " : "not in ") + "the circle");
}
}

PROGRAM 3.23

import java.util.Scanner;

public class Exercise_03_23 {


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

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


System.out.print("Enter a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();

// Check whether the point is within the rectangle


// centered at (0, 0) with width 10 and height 5
boolean withinRectangle = (Math.pow(Math.pow(x, 2), 0.5) <= 10 / 2 ) ||
 (Math.pow(Math.pow(y, 2), 0.5) <= 5.0 / 2);
// Display results
System.out.println("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)((Math.random() * (14 - 1)) + 1);

// Generate a random integer 1 - 4


int suit = (int)(Math.random() * 4);

// Display card picked from deck


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

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

// Prompt the user to enter four points


System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double x4 = input.nextDouble();
double y4 = input.nextDouble();

// 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)
{
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;

public class Exercise_03_26 {


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

// Prompt user to an integer


System.out.print("Enter an integer: ");
int number = input.nextInt();
// Determine whether it is divisible by 5 and 6
// Display results
System.out.println("Is 10 divisible by 5 and 6? " +
((number % 5 == 0) && (number % 6 == 0)));
System.out.println("Is 10 divisible by 5 or 6? " +
((number % 5 == 0) || (number % 6 == 0)));
System.out.println("Is 10 divisible by 5 of 6, but not both? " +
((number % 5 == 0) ^ (number % 6 == 0)));
}
}

PROGRAM 3.27

import java.util.Scanner;

public class Exercise_03_27 {


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

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


System.out.print("Enter a point's x- and y-coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();

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

PROGRAM 3.28

import java.util.Scanner;

public class Exercise_03_28 {


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

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


// width, and height of two rectangles
System.out.print("Enter r1's center x-, y-coordinates, width and height: ");
double r1x = input.nextDouble();
double r1y = input.nextDouble();
double r1Width = input.nextDouble();
double r1Height = input.nextDouble();
System.out.print("Enter r2's center x-, y-coordinates, width and height: ");
double r2x = input.nextDouble();
double r2y = input.nextDouble();
double r2Width = input.nextDouble();
double r2Height = input.nextDouble();

// Determine whether the second rectangle is inside the first


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

PROGRAM 3.29

import java.util.Scanner;

public class Exercise_03_29 {


public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a new 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();

if (Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5)


<= Math.abs(r1 - r2))
System.out.println("circle2 is inside circle1");
else if (Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5)
<= r1 + r2)
System.out.println("circle2 overlaps circle1");
else
System.out.println("circle2 does not overlap circle1");
}
}

PROGRAM 3.30

import java.util.Scanner;

public class Exercise_03_30 {


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

// Prompt user to enter the time offset of GMT


System.out.print("Enter the time zone offset to GMT: ");
int offset = input.nextInt();

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


long totalMilliseconds = System.currentTimeMillis();

// 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
System.out.println(
"Current time is " + ((currentHour > 12) ? currentHour - 12 :
currentHour) + ":" + currentMinute + ":" + currentSecond +
((currentHour > 12) ? " PM" : " AM"));
}
}

PROGRAM 3.31

import java.util.Scanner;

public class Exercise_03_31 {


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

// 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();

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


// and 1 to convert from RMB to USD
System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");
int option = input.nextInt();

// 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: System.out.print("Enter the dollar amount: ");
 amount = input.nextDouble();
 System.out.println("$" + amount + " is " +
 (amount * rate) + " yuan"); break;
case 1: System.out.print("Enter the RMB amount: ");
 amount = input.nextDouble();
 System.out.println(amount + " yuan is $" +
 ((int)((amount * 100) / rate)) / 100.0); break;
default: System.out.println("Incorrect input");
}
}
}

PROGRAM 3.32

import java.util.Scanner;

public class Exercise_03_32 {


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

// 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();

// Calculate point position


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

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

public class Exercise_03_33 {


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

// 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();

if (price1 / weight1 < price2 / weight2)


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

PROGRAM 3.34 (Last Program)

import java.util.Scanner;

public class Exercise_03_34 {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);// Create 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();

// 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
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 + ")");
}
}

You might also like