Week 2: Data Types, Math, Client Input, Debugging
Exercise #2.1 What day is it?
Write a Java program that takes a date as input and prints the day of the week that date falls on.
Your program should take three integers: y (year), m (month), and d (date). For m use 1 for January, 2 for February, and so on. For output print 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
Use the following formulas:
Test case 1:
Input: | Output: |
---|---|
2018 12 24 |
It’s day 1 ! |
My code:
1 |
|
Exercise #2.2 Great Circle Distance
The great-circle distance is the length of the shortest path between two points (x1,y1) and (x2,y2) on the surface of a sphere.
The formula is given below.
Write a Java program that takes four double input numbers x1, y1, x2, y2, which are the latitude and longitude in degrees of two points on the surface of the Earth; uses the mean radius of the Earth
r = 6,371.0 kilometres; and prints the great-circle distance dist between them in kilometres.
Note that the input numbers are given in degrees but Java’s
trigonometric functions use radians. Use Math.toRadians()
to convert from degrees to radians.
Test case 1:
Input: | Output: |
---|---|
80.0 25.0 155.0 102.5 |
7509.440708014122 kilometres |
Test case 2:
Input: | Output: |
---|---|
10.55 39.33 21.47 -7.88 |
5169.256612492542 kilometres |
My code:
1 |
|
Exercise #2.3 Area of a Pentagon
Write a Java program that computes the area of a pentagon, given
*r*
= the length from the center of a pentagon to a vertex
(a real number).
The formula of computing the area of a pentagon is:
where *s*
is the length of a side, computed with the
formula:
Test case 1:
Input: | Output: |
---|---|
2.5 | Area = 14.86025806711178 |
Test case 2:
Input: | Output: |
---|---|
1.0 | Area = 2.377641290737884 |
My code:
1 |
|
CW1 #2.1 YIQ to RGB
There are several different formats used to represent color. You may have known the RGB format that specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255. You have learned about CMYK format in Lab 2.
There is another format called the YIQ that is used in analog TV broadcasting. It specifies the luminance (Y), in-phase (I), and quadrature (Q) on a real scale from with 0.0 ≤ Y ≤ 1.0, –0.5957 ≤ I ≤ 0.5957, and –0.5226 ≤ Q ≤ 0.5226.
.png)
Write a Java program that converts from YIQ format to RGB format using the following steps.
Step 1: Compute the values of R, G, B in double using the following formula:
%20(1).png)
Step 2: Round the double values to the nearest int values.
Step 3: Using Math.min function, make sure that the largest possible resulting value is 255; and using Math.max function, make sure that the smallest possible resulting value is 0.
Your program must take three double input numbers Y, I, and Q; compute the corresponding RGB values, and print the RGB values as in the test cases below:
Test case 1:
Input: | Output: |
---|---|
0.5 0.5 0.5 |
red = 255 green = 10 blue = 204 |
Test case 2:
Input: | Output: |
---|---|
0.0 0.25 -0.50 |
red = 0 green = 65 blue = 0 |
My code:
1 |
|