Saturday 11 June 2016

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

*3.32 (Geometry: point position) Given a directed line from point p0(x0, y0) to p1(x1, y1), you can use the following condition to decide whether a point p2(x2, y2) is on the left of the line, on the right, or on the same line (see Figure 3.11):






 Enter three points for p0, p1, and p2: 4.4 2 6.5 9.5 -5 4
    (-5.0, 4.0) is on the left side of the line from (4.4, 2.0) to (6.5, 9.5) 

    Enter three points for p0, p1, and p2: 1 1 5 5 2 2
    (2.0, 2.0) is on the line from (1.0, 1.0) to (5.0, 5.0) 

    Enter three points for p0, p1, and p2: 3.4 2 6.5 9.5 5 2.5
    (5.0, 2.5) is on the right side of the line from (3.4, 2.0) to (6.5, 9.5)


import java.util.Scanner;
 
public class ProgrammingEx3_32 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("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();
 
  String p0 = "(" + x0 + ", " + y0 + ")";
  String p1 = "(" + x1 + ", " + y1 + ")";
  String p2 = "(" + x2 + ", " + y2 + ")";
 
  double c = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
 
  if (c > 0) {
   System.out.println(p2 + " is on the left side of the line from "
     + p0 + " to " + p1);
  } else if (c == 0) {
   System.out.println(p2 + " is on the line from " + p0 + " to " + p1);
  } else {
   System.out.println(p2 + " is on the right side of the line from "
     + p0 + " to " + p1);
  }
 
 }
 
}

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

*3.31 (Financials: currency exchange) Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Chinese RMB. Prompt the user to enter 0 to convert from U.S. dollars to Chinese RMB and 1 to convert from Chinese RMB and U.S. dollars. Prompt the user to enter the amount in U.S. dollars or Chinese RMB to convert it to Chinese RMB or U.S. dollars, respectively. Here are the sample runs:

Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 0 Enter the dollar amount: 100 $100.0 is 681.0 yuan

Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 5 Enter the RMB amount: 10000 10000.0 yuan is $1468.43

Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 5 Incorrect input


public static void main(String[] args) {
 Scanner s = new Scanner(System.in);
 double exchangeRate, dollarAmount, rmbAmount;
 int userChoice;
  
 System.out.print("Enter the exchange rate from dollars to RMB:");
 exchangeRate = s.nextDouble();
  
 System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa:");
 userChoice = s.nextInt();
  
 if(userChoice == 0) {
  System.out.print("Enter the dollar amount:");
  dollarAmount = s.nextDouble();
   
  rmbAmount = dollarAmount * exchangeRate;
  System.out.print("$" + dollarAmount + " is " + rmbAmount + " yuan.");
 } else if(userChoice == 1) {
  System.out.print("Enter the RMB amount:");
  rmbAmount = s.nextDouble();
   
  dollarAmount = rmbAmount/exchangeRate;
  System.out.print(rmbAmount + " yuan is $" + dollarAmount);
 } else {
  System.out.print("Invalid choice.");
 }
}

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

*3.30 (Current time) Revise Programming Exercise 2.8 to display the hour using a 12-hour clock. Here is a sample run:
Enter the time zone offset to GMT: -5
The current time is 4:50:34 AM




import java.util.Scanner;
 
public class ProgrammingEx3_30 {
 
 public static void main(String[] args) {
 
  Scanner input = new Scanner(System.in);
  System.out.print("Enter the time zone offset to GMT:");
  int offset = input.nextInt();
  // calculate offset in seconds
  offset = offset * 60 * 60;
 
  // Obtain the total milliseconds since midnight, Jan 1, 1970
  long totalMilliseconds = System.currentTimeMillis();
 
  // Obtain the total seconds since midnight, Jan 1, 1970 and apply the
  // offset
  long totalSeconds = (totalMilliseconds / 1000) + offset;
 
  // 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;
 
  String s = " AM";
  if (currentHour >= 12) {
   s = " PM";
  }
  if (currentHour >= 13) {
   currentHour = currentHour - 12;
  }
 
  if (currentHour == 0) {
   currentHour = currentHour + 12;
  }
 
  // Display results
  System.out.println("Current time is " + currentHour + ":"
    + currentMinute + ":" + currentSecond + s);
 }
 
}

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

**3.29 (Geometry: two circles) Write a program that prompts the user to enter the center coordinates and radii of two circles and determines whether the second circle is inside the first or overlaps with the first, as shown in Figure 3.10. (Hint: circle2 is inside circle1 if the distance between the two centers <= |r1 - r2| and circle2 overlaps circle1 if the distance between the two centers <= r1 + r2. Test your program to cover all cases.) Here are the sample runs:
Enter circle1's center x-, y-coordinates, and radius: 0.5 5.1 13
Enter circle2's center x-, y-coordinates, and radius: 1 1.7 4.5
circle2 is inside circle1

Enter circle1's center x-, y-coordinates, and radius: 3.4 5.7 5.5
Enter circle2's center x-, y-coordinates, and radius: 6.7 3.5 3
circle2 overlaps circle1

Enter circle1's center x-, y-coordinates, and radius: 3.4 5.5 1
Enter circle2's center x-, y-coordinates, and radius: 5.5 7.2 1
circle2 does not overlap circle1 




import java.util.Scanner;
 
public class ProgrammingEx3_29 {
 public static void main(String[] args) {
  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 circle1's center x-, y-coordinates, and radius:");
  double x2 = input.nextDouble();
  double y2 = input.nextDouble();
  double r2 = input.nextDouble();
 
  double d = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
 
  if (d <= Math.abs(r2 - r1)) {
   if (r1 > r2) {
    System.out.print("circle2 is inside circle1");
   } else if (r2 > r1) {
    System.out.print("circle1 is inside circle2");
   } else {
    System.out.print("circle2 is indentical to circle1");
   }
  } else if (d < r2 + r1) {
   System.out.print("circle2 overlaps circle1 ");
  } else if (d >= r2 + r1) {
   System.out.print("circle2 does not overlaps circle1 ");
  }
 
 }
}

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

**3.28 (Geometry: two rectangles) Write a program that prompts the user to enter the center x-, y-coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure 3.9. Test your program to cover all cases. Here are the sample runs:
Enter r1's center x-, y-coordinates, width, and height: 2.5 4 2.5 43
Enter r2's center x-, y-coordinates, width, and height: 1.5 5 0.5 3
r2 is inside r1

Enter r1's center x-, y-coordinates, width, and height: 1 2 3 5.5
Enter r2's center x-, y-coordinates, width, and height: 3 4 4.5 5
r2 overlaps r1

Enter r1's center x-, y-coordinates, width, and height: 1 2 3 3
Enter r2's center x-, y-coordinates, width, and height: 40 45 3 2
r2 does not overlap r1







import java.util.Scanner;
 
 
 
import java.util.Scanner;
 
public class ProgrammingEx3_28 {
 public static void main(String[] args) {
  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();
  w1 = w1 / 2;
  h1 = h1 / 2;
  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();
  w2 = w2 / 2;
  h2 = h2 / 2;
 
  // Calculating range of r1 and r2
  double x1max = x1 + w1;
  double y1max = y1 + h1;
  double x1min = x1 - w1;
  double y1min = y1 - h1;
  double x2max = x2 + w2;
  double y2max = y2 + h2;
  double x2min = x2 - w2;
  double y2min = y2 - h2;
 
  if (x1max == x2max && x1min == x2min && y1max == y2max
    && y1min == y2min) {
   // Check if the two are identicle
   System.out.print("r1 and r2 are indentical");
 
  } else if (x1max <= x2max && x1min >= x2min && y1max <= y2max
    && y1min >= y2min) {
   // Check if r1 is in r2
   System.out.print("r1 is inside r2");
  } else if (x2max <= x1max && x2min >= x1min && y2max <= y1max
    && y2min >= y1min) {
   // Check if r2 is in r1
   System.out.print("r2 is inside r1");
  } else if (x1max < x2min || x1min > x2max || y1max < y2min
    || y2min > y1max) {
   // Check if the two overlap
   System.out.print("r2 does not overlaps r1");
  } else {
   System.out.print("r2 overlaps r1");
  }
 
 }
}

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

**3.27 (Geometry: points in triangle?) Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter a point with x- and y-coordinates and determines whether the point is inside the triangle. Here are the sample runs:

Enter a point's x- and y-coordinates: 100.5 25.5
The point is in the triangle Enter a point's x- and y-coordinates: 100.5 50.5
The point is not in the triangle 



import java.util.Scanner;
 
public class ProgrammingEx3_27 {
 public static void main(String[] args) {
  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();
  double y2 = -x / 2 + 100;
  String s = " ";
 
  // Check if y and x is in range and under the line
 if(( (y > 0) && (x > 0) && (x + 2*y < 200) )){
   s = " ";
  }
  else
  {
    s = " not ";
  }
 
  System.out.print("The point is" + s + "in the triangle");
 
 }
}

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

3.26 (Use the &&, || and ^ operators) Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both. Here is a sample run of this program:
Enter an integer: 10
Is 10 divisible by 5 and 6? false
Is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6, but not both? true 


import java.util.Scanner;
 
public class ProgrammingEx3_26 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter an integer:");
  int n = input.nextInt();
  boolean dby5 = false;
  boolean dby6 = false;
 
  if (n % 5 == 0) {
   dby5 = true;
  }
 
  if (n % 6 == 0) {
   dby6 = true;
  }
 
  System.out.println("Is 10 divisible by 5 and 6? " + (dby5 && dby6));
  System.out.println("Is 10 divisible by 5 or 6? " + (dby5 || dby6));
  System.out.println("Is 10 divisible by 5 or 6, but not both? "
    + (dby5 ^ dby6));
 
 }
 
}

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

*3.25 (Geometry: intersecting point) Two points on line 1 are given as (x1, y1) and (x2, y2) and on line 2 as (x3, y3) and (x4, y4), as shown in Figure 3.8a–b. The intersecting point of the two lines can be found by solving the following linear equation: (y1 - y2)x - (x1 - x2)y = (y1 - y2)x1 - (x1 - x2)y1 (y3 - y4)x - (x3 - x4)y = (y3 - y4)x3 - (x3 - x4)y3 This linear equation can be solved using Cramer’s rule (see Programming Exer- cise 3.3). If the equation has no solutions, the two lines are parallel (Figure 3.8c). Write a program that prompts the user to enter four points and displays the inter- secting point. Here are sample runs:

Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
The intersecting point is at (2.88889, 1.1111)

Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 7 6.0 4.0 2.0 -1.0 -2.0
The two lines are parallel 




import java.util.Scanner;
 
public class ProgrammingEx3_25 {
 public static void main(String[] args) {
  System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4:");
  Scanner input = new Scanner(System.in);
 
  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();
 
  double a = y1 - y2;
  double b = -(x1 - x2);
  double e = (y1 - y2) * x1 - (x1 - x2) * y1;
  double c = (y3 - y4);
  double d = -(x3 - x4);
  double f = (y3 - y4) * x3 - (x3 - x4) * y3;
 
  if (a * d - b * c == 0) {
   System.out.println("The two lines are parallel.");
   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("The intersecting point is at (" + x + ", " + y + ")");
 }
}

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

**3.24 (Game: pick a card) Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades) of the card. Here is a sample run of the program:
The card you picked is Jack of Hearts




public class ProgrammingEx3_24 {
 public static void main(String[] args) {
 
  int card = (int) (Math.random() * 52.0); // pick a card 0-51
  int rank = card / 4; // determine the rank 0-12
  int suit = card % 4; // determine the suit 0-3
  String strRank = "";
  String strSuit = "";
 
  switch (rank) {
  case 0:
   strRank = "Ace";
   break;
  case 10:
   strRank = "Jack";
   break;
  case 11:
   strRank = "Queen";
   break;
  case 12:
   strRank = "King";
   break;
  default:
   strRank = "" + (rank + 1);
   break;
  }
 
  switch (suit) {
  case 0:
   strSuit = "Clubs";
   break;
  case 1:
   strSuit = "Diamonds";
   break;
  case 2:
   strSuit = "Hearts";
   break;
  case 3:
   strSuit = "Spades";
   break;
  }
   
  System.out.print("The card you picked is " + strRank +" of " + strSuit );
 }
 
}