Monday, 6 June 2016

Chapter 3 Exercise 5, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*3.5 (Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, …, and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. Here is a sample run:



import java.util.Scanner;
 
public class ProgrammingEx3_5 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter today's day:");
  int today = input.nextInt();
  System.out.print("Enter the number of days elapsed since today:");
  int elapsed = input.nextInt();
   
  String dayString = "";
   
  switch(today%7){
   case 1:dayString= "Monday";break;
   case 2:dayString= "Tuesday";break;
   case 3:dayString= "Wednesday";break;
   case 4:dayString= "Thursday";break;
   case 5:dayString= "Friday";break;
   case 6:dayString= "Saturday";break;
   case 0:dayString= "Sunday";break;
  }
  System.out.print("Today is " + dayString + " and the future day is " );
   
  int day = (today + elapsed);
   
  switch(day%7){
   case 1:dayString= "Monday";break;
   case 2:dayString= "Tuesday";break;
   case 3:dayString= "Wednesday";break;
   case 4:dayString= "Thursday";break;
   case 5:dayString= "Friday";break;
   case 6:dayString= "Saturday";break;
   case 0:dayString= "Sunday";break;
  }
  System.out.print(dayString);
 }
 
}

Chapter 3 Exercise 4, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**3.4 (Random month) Write a program that randomly generates an integer between 1 and 12 and displays the English month name January, February, …, December for the number 1, 2, …, 12, accordingly.


public class ProgrammingEx3_4 {
 
 public static void main(String[] args) {
  int number1 = (int) (System.currentTimeMillis() % 12 + 1);
  String month = "";
   
  switch(number1){
  case 1: month = "January"; break;
  case 2: month = "February"; break;
  case 3: month = "March"; break;
  case 4: month = "April"; break;
  case 5: month = "May"; break;
  case 6: month = "June"; break;
  case 7: month = "July"; break;
  case 8: month = "August"; break;
  case 9: month = "September"; break;
  case 10: month = "October"; break;
  case 11: month = "November"; break;
  case 12: month = "December"; break;
  }
   
  System.out.println(month);
 
 }
 
}

Chapter 3 Exercise 3, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*3.3 (Algebra: solve 2 * 2 linear equations) A linear equation can be solved using Cramer’s rule given in Programming Exercise 1.13. Write a program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that “The equation has no solution.”



import java.util.Scanner;
 
public class ProgrammingEx3_3 {
 
 public static void main(String[] args) {
 
  Scanner input = new Scanner(System.in);
 
  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();
 
  if (a * d - b * c == 0) {
   System.out.println("The equation has no solution.");
   System.exit(0);
  }
 
  double x = (e * d - b * f) / (a * d - b * c);
  double y = (a * f - e * c) / (a * d - b * c);
 
  System.out.print("x is " + x);
  System.out.println(" and y is " + y);
 
 }
 
}

Chapter 3 Exercise 2, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

3.2 (Game: add three numbers) The program in Listing 3.1, AdditionQuiz.java, generates two integers and prompts the user to enter the sum of these two integers. Revise the program to generate three single-digit integers and prompt the user to enter the sum of these three integers.


import java.util.Scanner;
 
public class ProgrammingEx3_2 {
 public static void main(String[] args) {
  int number1 = (int) (System.currentTimeMillis() % 10);
  int number2 = (int) (System.currentTimeMillis() * 7 % 10);
  int number3 = (int) (System.currentTimeMillis() * 13 % 10);
  // Create a Scanner
  Scanner input = new Scanner(System.in);
 
  System.out.print("What is " + number1 + " + " + number2 + " + "
    + number3 + "? ");
 
  int answer = input.nextInt();
 
  System.out.println(number1 + " + " + number2 + " + " + number3 + " = " + answer
    + " is " + (number1 + number2 + number3 == answer));
 }
}

Chapter 3 Exercise 1, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*3.1 (Algebra: solve quadratic equations) The two roots of a quadratic equation ax2 + bx + c = 0 can be obtained using the following formula:

b2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. Write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots”. Note that you can use Math.pow(x, 0.5) to compute 2x. Here are some sample runs.




import java.util.Scanner;
 
public class ProgrammingEx3_1 {
 
 public static void main(String[] args) {
  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 = Math.pow(b, 2) - 4 * a * c;
 
  if (discriminant < 0) {
   System.out.println("The equation has no real roots");
   System.exit(0);
  }
 
  double r1 = (-b + Math.sqrt(discriminant)) / (2 * a);
  double r2 = (-b - Math.sqrt(discriminant)) / (2 * a);
 
  if (discriminant > 0) {
   System.out.println("The equation has two roots " + r1 + " and "
     + r2);
  } else {
   System.out.println("The equation has one root " + r1);
  }
 
 }
}

Chapter 2 Exercise 23, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*2.23 (Cost of driving) Write a program that prompts the user to enter the distance to drive, the fuel efficiency of the car in miles per gallon, and the price per gallon, and displays the cost of the trip. Here is a sample run:


import java.util.Scanner;
 
public class ProgrammingEx2_23 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter the driving distance:");
  double distance = input.nextDouble();
  System.out.print("Enter miles per gallon:");
  double fuelEff = input.nextDouble();
  System.out.print("Enter price per gallon:");
  double pricePerGallon = input.nextDouble();
  double cost = distance / 25.5 * pricePerGallon;
  System.out.print("The cost of driving is $" + cost);
 
 }
 
}

Chapter 2 Exercise 22, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*2.22 (Financial application: monetary units) Rewrite Listing 2.10, ComputeChange .java, to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the cents. For example, the input 1156 represents 11 dollars and 56 cents.


import java.util.Scanner;
 
public class ProgrammingEx2_22 {
 public static void main(String[] args) {
  // Create a Scanner
  Scanner input = new Scanner(System.in);
 
  // Receive the amount
  System.out
    .print("Enter the input as an integer whose last two digits represent the cents: ");
  int amount = input.nextInt();
 
  int remainingAmount = amount;
 
  // Find the number of one dollars
  int numberOfOneDollars = remainingAmount / 100;
  remainingAmount = remainingAmount % 100;
 
  // Find the number of quarters in the remaining amount
  int numberOfQuarters = remainingAmount / 25;
  remainingAmount = remainingAmount % 25;
 
  // Find the number of dimes in the remaining amount
  int numberOfDimes = remainingAmount / 10;
  remainingAmount = remainingAmount % 10;
 
  // Find the number of nickels in the remaining amount
  int numberOfNickels = remainingAmount / 5;
  remainingAmount = 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("    " + numberOfOneDollars + " dollars");
  System.out.println("    " + numberOfQuarters + " quarters ");
  System.out.println("    " + numberOfDimes + " dimes");
  System.out.println("    " + numberOfNickels + " nickels");
  System.out.println("    " + numberOfPennies + " pennies");
 }
}