CPT111-Week2

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
public class wdis {
public static void main(String[] args) {
int y,m,d,a,b,c,day;
Scanner sc=new Scanner(System.in);
y = sc.nextInt();
m = sc.nextInt();
d = sc.nextInt();
a = y - (14 - m)/12;
b = a + a/4 - a/100 + a/400;
c = m + 12 * ((14 - m)/12) - 2;
day = (d + b +(31*c)/12) % 7;
System.out.println("It's day " + day +" !");
}
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;

public class GCD {
public static void main(String[] args) {
double x1,y1,x2,y2,dist;
Scanner sc = new Scanner(System.in);
x1 = Math.toRadians(sc.nextDouble());
y1 = Math.toRadians(sc.nextDouble());
x2 = Math.toRadians(sc.nextDouble());
y2 = Math.toRadians(sc.nextDouble());
dist = 2*6371.0*Math.asin(Math.sqrt(Math.sin((x2-x1)/2)*Math.sin((x2-x1)/2) + Math.cos(x1)*Math.cos(x2)*Math.sin((y2-y1)/2)*Math.sin((y2-y1)/2)));
System.out.println(dist + " kilometres");
}
}

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). img

The formula of computing the area of a pentagon is: img

where *s* is the length of a side, computed with the formula: img

Test case 1:
Input: Output:
2.5 Area = 14.86025806711178
Test case 2:
Input: Output:
1.0 Area = 2.377641290737884
My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.*;

public class AP {
public static void main(String[] args) {
double r,s,area;
Scanner sc = new Scanner(System.in);
r = sc.nextDouble();
s = 2*r*Math.sin(Math.PI/5);
area = (5*s*s)/(4*Math.tan(Math.PI/5));
System.out.println("Area = " + area);
}
}

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.

img

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:

img

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;

public class YIQ2RGB {
public static void main(String[] args) {
double y,i,q;
int r,g,b;
Scanner sc = new Scanner(System.in);
y = sc.nextDouble();
i = sc.nextDouble();
q = sc.nextDouble();
r = Math.toIntExact(Math.round((y + 0.956 * i + 0.619 * q) * 255));
g = Math.toIntExact(Math.round((y - 0.272 * i - 0.647 * q) * 255));
b = Math.toIntExact(Math.round((y - 1.106 * i + 1.703 * q) * 255));
if(r>255) r =255;
if(r<0) r=0;
if(g>255) g =255;
if(g<0) g=0;
if(b>255) b =255;
if(b<0) b=0;
System.out.println("red = " + r);
System.out.println("green = " + g);
System.out.println("blue = " + b);
}
}