Initial Commit

This commit is contained in:
2024-05-14 18:45:10 -04:00
commit c90b3a95fe
10 changed files with 421 additions and 0 deletions

59
CS5 - Demo/Main.java Normal file
View File

@@ -0,0 +1,59 @@
import java.util.Random;
/*
CS5 Demo
For Loops and Random Numbers
This demo goes over how to write for loops and how
there generally used. This demo also includes Java's
Random class, so we can generate random values while using for
loops.
*/
public class Main {
public static void main(String[] args) {
//This for loop prints numbers from 0 through 9 (inclusive)
//A for loop in this instance does what is known as "iterating"
//over a series of values, in this case, we are iterating over
//a range of numbers. In order to use a for loop in this way
//we need to define three pieces inside the parenthesis
//int i = 0; This first piece (initialization) defines
//what value you are using to iterate your loop. This can
//actually be left out (unlike the next two pieces) if you're
//using a variable already defined elsewhere in your code
//i < 10; This is your condition, what determines if the loop
//should continue to run. So long as the variable i is < 10,
//the loop runs.
//i++ - This is our modifier, the thing that is changing to
//ensure that at some point our loop will exit (and avoid an
//infinite loop). i++ just means to add 1 to i. Each time the
//loop finishes a run of whatever is inside the brackets, i++
//will get called to update the value of i to i = i + 1;
//For this loop, all we're doing is printing the value of
//i as it changes while the loop runs.
for(int i = 0; i < 10; i++) {
System.out.println("Index Number: " + i);
}
//Similar to Scanner, we can create other types of objects
//and in this case, we are creating one called Random.
//This just gives us access to a (pseudo) random value generator.
//This wouldn't be good if you wanted truely random values, but
//it'll serve out purposes well enough for now.
Random random = new Random();
//With this for loop, we're trying to print 5 of each type of
//primitive value (int, double, and boolean) that we've seen so
//far. If all goes well, this will print the following for each
//variable type
//ValueName Value i+1: randomValue
//Where ValueName is the current random value type we're trying to create
//i is the current for loop i value
//randomValue is the value generated by the Random object for
//that specific type of variable.
for(int i = 0; i < 5; i++) {
System.out.println("Int Value " + (i + 1) + ": " + random.nextInt());
System.out.println("Double Value " + (i + 1) + ": " + random.nextDouble());
System.out.println("Boolean Value " + (i + 1) + ": " + random.nextBoolean());
}
}
}