Lab 6 (Year 3 Sem 1)

Lab 6 is the seventh lab in semester 1 of year 3, and the seventh lab in the total of year 3. It is a lab for Java API, and is considered to be easy.

Description
No description was provided for this lab.

Unlocked after completion

 * OCD (Score at least 60% for Lab 6: Java API )
 * API Guru (Score at least 80% for Lab 6: Java API )

Question
Write a program named SSN that prompts the user to enter a Staff Security Number (SSN) in the format DDD-CC-DDD-X, where D is a digit, C is a character (A to Z) and X is a character (M to V inclusive). Your program should be able to handle both uppercase and lowercase entries and check whether the input is valid.

Your program should have a method isValidSSN which takes in an input parameter ssn of String data type. The method will return true if the ssn is valid, false otherwise.

Sample Output 1:

Enter a SSN: 232-AP-543-S 232-AP-543-S is a valid staff security number

Sample Output 2:

Enter a SSN: 232-AP-543-A 232-AP-543-A is an invalid staff security number

Sample Output 3:

Enter a SSN: 232-AP-5A3-6 232-AP-5A3-6 is an invalid staff security number

Sample Output 4:

Enter a SSN: 2K2-9P-543-Y 2K2-9P-543-Y is an invalid staff security number

Solution
use regex

Question
Write a program named CountOccurrence that prompts the user to enter a string followed by a character and displays the number of occurrences of the character in the string.

Your program should have a method that finds the number of occurrences of a specified character in a string using the following header:

public static int count(String str, char a)

For example, count("Welcome", 'e') returns 2.

Note: count should be case-sensitive.

Sample Output:

Enter a string: The President of the United States of America Enter a character: e The number of occurrences of 'e' in "The President of the United States of America" is 7

Solution
for loop

to take char input use  where i is your Scanner object

Question
The great circle distance is the shortest distance between two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographical latitude and longitude of two points. The great circle distance between the two points can be computed using the following formula:

d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2))

Write a method getCircleDistance which will take in radius, x1, y1, x2, y2 as input parameters (where x1, y1, x2, y2 are taken in as degrees), compute and return the great circle distance.

Write a program named CircleDistance that prompts the user to enter the latitude and longitude of two points on the earth  in degrees  and displays the great circle distance between the two points. The average earth radius is 6,371.01 km. Note that you need to convert the degrees into radians using the Math.toRadians method since the Java trigonometric methods use radians.

The latitude and longitude degrees in the formula are for north and west. Use negative to indicate south and east degrees.

Format your output to 3 decimal places.

Sample Output:

Enter point 1 (latitude and longitude) in degrees: 39.55 -116.25 Enter point 2 (latitude and longitude) in degrees: 41.5 87.37 The distance between the two points is 10691.792 km

Solution
getCircleDistance takes in 5 parameters (radius, x1, y1, x2, y2 in that order)

use builtin radians method

Question
Write a program named ScissorRockPaper 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.

Your program should have a method named getWinner which takes in two input integers representing player and computer choices. The method will return a String "You win", "You lose" or "Draw" depending on the scenario.

Sample Output:

scissor (0), rock (1), paper (2): 0 Computer is scissor. Draw

Solution
remember to import random, use  of the java.util.Random class to generate a random integer between 0 and 2 both inclusive