Skip to content

Commit

Permalink
feat(2.8.1): finished Unit 2 Lesson 8 Activity one
Browse files Browse the repository at this point in the history
- Math class and static methods
  • Loading branch information
101zh committed Sep 29, 2023
1 parent 029d440 commit 7dd4309
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 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_One",
"request": "launch",
"mainClass": "Unit2.U2_L8_Activity_One",
"projectName": "my-app"
},
{
"type": "java",
"name": "U2_L7_Activity_Two",
Expand Down
24 changes: 11 additions & 13 deletions a.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
Debug the code provided in the starter file so it does the following:
Write code that asks for a positive integer n, then prints 3 random integers from 2 to n+2 inclusive using Math.random().

creates two Integer objects x and y, and initializes them as null
prints the values of x and y (should result in the output "null null")
sets x and y to inputs entered by the user
finds the average of the two values and stores this in a Double value avg
prints a sentence as shown in the sample run with the values of x, y and the average
Hint - just as with Strings (see Unit 2: Lesson 1) we can explicitly assign any class-type variable as null just by typing = null
Note #1: In the starter code for this exercise the line "import testing.Math;" appears. You should not remove this line from your code as it is required to correctly grade your code. Also make sure that your code outputs exactly 3 numbers (be particularly careful there aren't any extra numbers in your prompt for user input).

Sample run:
Note #2: Make sure your minimum output is 2 or more, and make sure your maximum output is only up to n + 2 (so if user inputs 5, the maximum output should only be 7).

null null
Enter values:
>5
>12
Average of 5 and 12 is 8.5
Hint: Knowing your PEMDAS will help a lot.

Sample Run:
Enter a positive integer:
6
7
2
5
18 changes: 18 additions & 0 deletions src/main/java/Unit2/U2_L8_Activity_One.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Unit2;

import java.util.Scanner;
// import testing.Math;

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

System.out.println("Enter a positive integer:");
int num1 = scanner.nextInt();
num1++;
for (int i=0; i<=2; i++){
System.out.println((int)(num1*Math.random())+2);
}
scanner.close();
}
}

0 comments on commit 7dd4309

Please sign in to comment.