Skip to content

Commit

Permalink
feat(2.8.3): Finished Unit 2 Lesson 8 Activity 3
Browse files Browse the repository at this point in the history
- Using Math API
--- Math.round()
--- Math.abs()
- finding the difference between two numbers and rounding it
  • Loading branch information
101zh committed Sep 29, 2023
1 parent 2bb4738 commit 75cf7b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"configurations": [
{
"type": "java",
"name": "U2_L8_Activity_Three",
"request": "launch",
"mainClass": "Unit2.U2_L8_Activity_Three",
"projectName": "my-app"
},
{
"type": "java",
"name": "U2_L8_Activity_Two",
Expand Down
25 changes: 13 additions & 12 deletions a.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
Write a program that calculates the slope between two coordinates: (x1, y1) and (x2, y2). The program should take four decimal inputs from the user, in the order of x1, x2, y1, y2. Then print the slope.
Write a program which calculates the difference between two doubles which are entered by the user, then round the difference to the nearest integer. The answer should be a positive integer regardless of the order in which the doubles are entered.

Note: slope formula looks like this:
Hint: The Math.round() function can be used to round a double value to the nearest integer.

Sample run:
Sample run 1:

Enter the first x-coordinate:
3.9
Enter the second x-coordinate:
5.1
Enter the first y-coordinate:
4.8
Enter the second y-coordinate:
1.2
Slope: -3.0000000000000004
Enter two doubles:
>2.2
>9.6
Difference: 7
Sample run 2:

Enter two integers:
>-4.5
>12.56
Difference: 17
20 changes: 20 additions & 0 deletions src/main/java/Unit2/U2_L8_Activity_Three.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Unit2;

import java.util.Scanner;

public class U2_L8_Activity_Three {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter two doubles:");
System.out.print(">");
double num1= scanner.nextDouble();
System.out.print(">");
double num2= scanner.nextDouble();

int difference = (int) Math.round(Math.abs(num2-num1));

System.out.println("Difference: "+difference);
scanner.close();
}
}

0 comments on commit 75cf7b6

Please sign in to comment.