From 75cf7b62e54fc08ab7019fefd52a027820d7aa1c Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Fri, 29 Sep 2023 21:09:11 +0000 Subject: [PATCH] feat(2.8.3): Finished Unit 2 Lesson 8 Activity 3 - Using Math API --- Math.round() --- Math.abs() - finding the difference between two numbers and rounding it --- .vscode/launch.json | 7 ++++++ a.txt | 25 ++++++++++--------- src/main/java/Unit2/U2_L8_Activity_Three.java | 20 +++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 src/main/java/Unit2/U2_L8_Activity_Three.java diff --git a/.vscode/launch.json b/.vscode/launch.json index a1643aa..0f4b102 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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", diff --git a/a.txt b/a.txt index bcec2a1..becfa7a 100644 --- a/a.txt +++ b/a.txt @@ -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 \ No newline at end of file +Enter two doubles: +>2.2 +>9.6 +Difference: 7 +Sample run 2: + +Enter two integers: +>-4.5 +>12.56 +Difference: 17 \ No newline at end of file diff --git a/src/main/java/Unit2/U2_L8_Activity_Three.java b/src/main/java/Unit2/U2_L8_Activity_Three.java new file mode 100644 index 0000000..82ee631 --- /dev/null +++ b/src/main/java/Unit2/U2_L8_Activity_Three.java @@ -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(); + } +}