Showing posts with label Algorithms Data Structures. Show all posts
Showing posts with label Algorithms Data Structures. Show all posts

Saturday, 11 June 2016

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

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

**3.23 (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle, as shown in Figure 3.7b. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to 10 / 2 and its vertical distance to (0, 0) is less than or equal to 5.0 / 2. Test your program to cover all cases.) Here are two sample runs.
Enter a point with two coordinates: 2 2
Point (2.0, 2.0) is in the rectangle

Enter a point with two coordinates: 6 4
Point (6.0, 4.0) is not in the rectangle 




import java.util.Scanner;
 
public class ProgrammingEx3_23 {
 
 public static void main(String[] args) {
 
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter a point with two coordinates:");
  double x = input.nextDouble();
  double y = input.nextDouble();
 
  String s = " ";
 
  if (Math.abs(x) > 5 || Math.abs(y) > 2.5) {
   s = " not ";
  }
 
  System.out.print("Point (" + x + ", " + y + ") is" + s
    + "in the rectangle");
 
 }
 
}

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

(Geometry: point in a circle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the circle, as shown in Figure 3.7a. (Hint: A point is in the circle if its distance to (0, 0) is less than or equal to 10. The formula for computing the distance is
(x2x1)2+(y2y1)2
 . Test your program to cover all cases.) Two sample runs are shown below.



import java.util.Scanner;
 
 
 
public class ProgrammingEx3_22 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter a point with two coordinates:");
  double x = input.nextDouble();
  double y = input.nextDouble();
 
  double d = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  String s = " ";
 
  if (d >= 10) {
   s = " not ";
  }
 
  System.out.print("Point " + x + ", " + y + " is" + s
    + "in the circle");
 
 }
 
}

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

**3.21 (Science: day of the week) Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is 
where ■ h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday). ■ q is the day of the month. ■ m is the month (3: March, 4: April, …, 12: December). January and February are counted as months 13 and 14 of the previous year. ■ j is the century (i.e., year 100 ). ■ k is the year of the century (i.e., year % 100). Note that the division in the formula performs an integer division. Write a program that prompts the user to enter a year, month, and day of the month, and displays the name of the day of the week. Here are some sample runs:

Enter year: (e.g., 2012): 2015
Enter month: 1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Sunday

Enter year: (e.g., 2012): 2012 Enter month: 1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday

(Hint: January and February are counted as 13 and 14 in the formula, so you need to convert the user input 1 to 13 and 2 to 14 for the month and change the year to the previous year.) 



import java.util.Scanner;
 
public class ProgrammingEx3_21 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter year: (e.g., 2012):");
  int y = input.nextInt();
  System.out.print("Enter month: 1-12:");
  int m = input.nextInt();
  System.out.print("Enter the day of the month: 1-31:");
  int q = input.nextInt();
 
  // dealing with Jan and Feb
  if (m == 1 || m == 2) {
   m = m + 12;
   y = y - 1;
  }
 
  int j = y / 100;
  int k = y % 100;
  int h = (q + 26 * (m + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;
  String day = "";
 
  switch (h) {
 
  case 0:
   day = "Saturday";
   break;
  case 1:
   day = "Sunday";
   break;
  case 2:
   day = "Monday";
   break;
  case 3:
   day = "Tuesday";
   break;
  case 4:
   day = "Wednesday";
   break;
  case 5:
   day = "Thursday";
   break;
  case 6:
   day = "Friday";
   break;
 
  }
 
  System.out.print("Day of the week is " + day);
 
 }
}

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

*3.20 (Science: wind-chill temperature) Programming Exercise 2.17 gives a formula to compute the wind-chill temperature. The formula is valid for temperatures in the range between −58ºF and 41ºF and wind speed greater than or equal to 2. Write a program that prompts the user to enter a temperature and a wind speed. The program displays the wind-chill temperature if the input is valid; otherwise, it displays a message indicating whether the temperature and/or wind speed is invalid.


import java.util.Scanner;
 
public class ProgrammingEx3_20 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out
    .print("Enter the temperature in Fahrenheit between -58°F and 41°F:");
  double ta = input.nextDouble();
  System.out.print("Enter the wind speed (>=2) in miles per hour:");
  double v = input.nextDouble();
  double twc = 0;
 
  if (ta < -58 || ta > 41) {
   System.out.println("The temperature input is incorrect.");
   twc = -1;
  }
 
  if (v < 2) {
   System.out.println("The wind speed input is incorrect.");
   twc = -1;
  }
 
  if (twc == -1) {
   System.exit(0);
  }
 
  twc = 35.74 + 0.6215 * ta - 35.75 * Math.pow(v, 0.16) + 0.4275 * ta
    * Math.pow(v, 0.16);
 
  System.out.print("The wind chill index is " + twc);
 
 }
}

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

**3.19 (Compute the perimeter of a triangle) Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge.


import java.util.Scanner;
 
public class ProgrammingEx3_19 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter three edges of the triangle:");
  double s1 = input.nextDouble();
  double s2 = input.nextDouble();
  double s3 = input.nextDouble();

  if (s1+s2>s3 && s1+s3>s2 && s2+s3>s1) {
   System.out.print("The perimeter is " + (s1 + s2 + s3));
  }
  else 
  {
    System.out.print("The input is invalid");
    System.exit(0);
  } 
 
 }
 
}

Tuesday, 7 June 2016

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

*3.18 (Cost of shipping) A shipping company uses the following function to calcu-
late the cost (in dollars) of shipping based on the weight of the package (in
pounds).

c(w)=3.5, if 0  <w< = 1
c(w)=5.5, if 1 <w < = 3
c(w)=8.5, if 3 <w< = 10
c(w)=10.5, if 10<w< = 20

Write a program that prompts the user to enter the weight of the package and display the shipping cost. If the weight is greater than 50, display a message “the package cannot be shipped.


import java.util.Scanner;
 
public class ProgrammingEx3_18 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter the weight of the package in pound:");
  double w = input.nextDouble();
  double cost = 0;
 
  if (w > 0 && w <= 1) {
   cost = 3.5;
  } else if (w > 1 && w <= 3) {
   cost = 5.5;
  } else if (w > 3 && w <= 10) {
   cost = 8.5;
  } else if (w > 10 && w <= 20) {
   cost = 10.5;
  }
 
  if (cost == 0) {
   System.out.print("the pakage cannot be shipped.");
   System.exit(0);
  }
 
  System.out.print("The cost of shipping is " + cost);
 
 }
 
}

Monday, 6 June 2016

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

*3.17 (Game: scissor, rock, paper) Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0 , 1 , or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0 , 1 , or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs:
 scissor (0), rock (1), paper (2): 1
    The computer is scissor. You are rock. You won
    scissor (0), rock (1), paper (2): 2
    The computer is paper. You are paper too. It is a draw

public class ProgrammingEx3_17 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("scissor (0), rock (1), paper (2):");
  int guess = input.nextInt();
  int computer = (int) (Math.random() * 3);
  String strComputer = "";
 
  switch (computer) {
  case 0:
   strComputer = "scissor";
   break;
  case 1:
   strComputer = "rock";
   break;
  case 2:
   strComputer = "paper";
   break;
  }
 
  String strGuess = "";
  switch (guess) {
  case 0:
   strGuess = "scissor";
   break;
  case 1:
   strGuess = "rock";
   break;
  case 2:
   strGuess = "paper";
   break;
  default:
   System.out.print("Invalid input.");
   System.exit(0);
  }
 
  System.out.print("The computer is " + strComputer + ". You are "
    + strGuess);
 
  if (computer == guess) {
   System.out.print(" too. It is a draw");
  } else if (computer - guess == 1 || computer - guess == -2) {
   System.out.print(". Computer won.");
  } else if (computer - guess == -1 || computer - guess == 2) {
   System.out.print(". You won.");
  }
 
 }
 
}

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

3.16 (Random point) Write a program that displays a random coordinate in a rectangle.
The rectangle is centered at (0, 0) with width 100 and height 200.


public class ProgrammingEx3_16 {
 
 public static void main(String[] args) {
  double x = Math.random() * 100-50;
  double y = Math.random() * 200-100;
   
  System.out.print(x + "," + y);
 }
 
}