diff --git a/section3/ContinueBreak.java b/section3/ContinueBreak.java new file mode 100644 index 0000000..121a50e --- /dev/null +++ b/section3/ContinueBreak.java @@ -0,0 +1,18 @@ + + +public class ContinueBreak { + public static void main(String[] args) { + int count = 0; + + while(count < 10) { + + if(count == 5) { + count++; + break; + } + System.out.print(count + "\t"); + + count++; + }//end while + }//end main +} diff --git a/section3/ControlStatementsIntro.java b/section3/ControlStatementsIntro.java new file mode 100644 index 0000000..6d4d281 --- /dev/null +++ b/section3/ControlStatementsIntro.java @@ -0,0 +1,20 @@ + +public class ControlStatementsIntro { + public static void main(String[] args) { + int age = 19; + + if(age >= 16) { + System.out.println("You can drive!"); + } + else { + System.out.println("You cannot drive yet!"); + }//end if-else + + System.out.println(); //add some newline space + + for(int i = 1; i <= age; i++) { + System.out.println("Happy Birthday " + i); + }//end for + + }//end main +} diff --git a/section3/DiceSimulation.java b/section3/DiceSimulation.java new file mode 100644 index 0000000..c610815 --- /dev/null +++ b/section3/DiceSimulation.java @@ -0,0 +1,14 @@ +import java.util.Random; + +public class DiceSimulation { + public static void main(String[] args) { + Random random = new Random(); + + int diceVal; + + for(int i = 0; i < 10; i++) { + diceVal = random.nextInt(6) + 1; + System.out.println(diceVal); + }//end for + }//end main +} diff --git a/section3/DivisibleByThree.java b/section3/DivisibleByThree.java new file mode 100644 index 0000000..9365630 --- /dev/null +++ b/section3/DivisibleByThree.java @@ -0,0 +1,19 @@ +import java.util.Scanner; + +public class DivisibleByThree { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + int input; + + System.out.println("Please enter your integer"); + input = keyboard.nextInt(); + + if(input % 3 == 0) { + System.out.println(input + " is divisible by 3"); + } + else { + System.out.println(input + " is NOT divisible by 3"); + } + + }//end main +} diff --git a/section3/EvenOnly.java b/section3/EvenOnly.java new file mode 100644 index 0000000..a5aac2e --- /dev/null +++ b/section3/EvenOnly.java @@ -0,0 +1,17 @@ + +public class EvenOnly { + public static void main(String[] args) { + int count = 0; + + while(count < 10) { + + if(count % 2 != 0) { + count++; + continue; + } + System.out.println(count); + + count++; + }//end while + }//end main +} diff --git a/section3/Fraternity.java b/section3/Fraternity.java new file mode 100644 index 0000000..b99488d --- /dev/null +++ b/section3/Fraternity.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class Fraternity { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + int age; + char gender; + + System.out.print("Enter your age:\t"); + age = keyboard.nextInt(); + + System.out.print("Enter your gender:\t"); + gender = keyboard.next().charAt(0); + + if(age >= 19 && gender == 'M') { + System.out.println("You can join the fraternity."); + } + else { + System.out.println("Sorry, you cannot join the fraternity."); + }//end if-else + }//end main +} diff --git a/section3/GradeFun.java b/section3/GradeFun.java new file mode 100644 index 0000000..0abaf7b --- /dev/null +++ b/section3/GradeFun.java @@ -0,0 +1,40 @@ +import java.util.Scanner; + +public class GradeFun { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + + char grade; + + System.out.println("Enter a grade"); + grade = keyboard.next().charAt(0); + + switch(grade) { + case 'A': + case 'a': + System.out.println("Great job!"); + break; + case 'B': + case 'b': + System.out.println("Good job."); + break; + case 'C': + case 'c': + System.out.println("You can do better."); + break; + case 'D': + case 'd': + System.out.println("You're getting pretty close to failing"); + break; + case 'F': + case 'f': + System.out.println("You are failing the course!"); + break; + default: + System.out.println("You have entered an invalid grade."); + }//end switch + + + + }//end main +} diff --git a/section3/Proj3_1_LearningPackages.java b/section3/Proj3_1_LearningPackages.java new file mode 100644 index 0000000..b20df3f --- /dev/null +++ b/section3/Proj3_1_LearningPackages.java @@ -0,0 +1,47 @@ +import java.util.Scanner; + +public class Proj3_1_LearningPackages { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + int packageNumber; + int howManyCourses; + int baseCost; + int costPerCourse; + int numIncluded; + int totalCost; + + System.out.println("Which of the packages do you want? 1,2, or 3?"); + packageNumber = keyboard.nextInt(); + + System.out.println("How many courses did you enroll in this month?"); + howManyCourses = keyboard.nextInt(); + + if(packageNumber == 1) { + baseCost = 10; + costPerCourse = 6; + numIncluded = 2; + } + else if(packageNumber == 2) { + baseCost = 12; + costPerCourse = 4; + numIncluded = 4; + } + else { + //package 3 + baseCost = 15; + costPerCourse = 3; + numIncluded = 6; + }//end if-else chain + + //calculate total cost + if(howManyCourses > numIncluded) { + totalCost = baseCost + (howManyCourses - numIncluded) * costPerCourse; + } + else { + totalCost = baseCost; + } + + System.out.println("Total cost is $" + totalCost); + + }//end main +} diff --git a/section3/Proj3_3_GuessTheNumber.java b/section3/Proj3_3_GuessTheNumber.java new file mode 100644 index 0000000..975be0b --- /dev/null +++ b/section3/Proj3_3_GuessTheNumber.java @@ -0,0 +1,43 @@ +import java.util.Random; +import java.util.Scanner; + +public class Proj3_3_GuessTheNumber { + + public static void main(String[] args) { + int ourGuess; + int computerNumber; + int guessCount = 0; + Random random = new Random(); + boolean guessedNumber = false; + Scanner keyboard = new Scanner(System.in); + + computerNumber = random.nextInt(100) + 1; + + while(!guessedNumber) { + System.out.println("Enter your integer guess"); + ourGuess = keyboard.nextInt(); + guessCount++; + + if(ourGuess >= 1 && ourGuess <= 100) { + if(ourGuess == computerNumber) { + System.out.println("Congratulations! You guessed the number in " + + guessCount + " guesses! Thanks for playing!"); + + guessedNumber = true; + } + else if(ourGuess > computerNumber) { + System.out.println("Your guess was too high!"); + } + else { //too low! + System.out.println("Your guess was too low!"); + } + } + else { + //invalid guess + System.out.println("That was a wasted guess! You must pick a number between 1 and 100, inclusive!"); + } + + }//end while + + }//end main +} diff --git a/section3/RandomFun.java b/section3/RandomFun.java new file mode 100644 index 0000000..18aaa8f --- /dev/null +++ b/section3/RandomFun.java @@ -0,0 +1,19 @@ +import java.util.Random; + +public class RandomFun { + public static void main(String[] args) { + Random random = new Random(); + int myRandomNumber; + + myRandomNumber = random.nextInt(); + System.out.println("number is:\t" + myRandomNumber); + + myRandomNumber = random.nextInt(1000); //0 - 999 + System.out.println("0 through 999?\t" + myRandomNumber); + + //shifting + myRandomNumber = random.nextInt(1000) + 1; //1 - 1000 + System.out.println("1 through 1000?\t" + myRandomNumber); + + }//end main +} diff --git a/section3/RepetitionFun.java b/section3/RepetitionFun.java new file mode 100644 index 0000000..fa46d0f --- /dev/null +++ b/section3/RepetitionFun.java @@ -0,0 +1,48 @@ +import java.util.Scanner; + +public class RepetitionFun { + public static void main(String[] args) { + + Scanner keyboard = new Scanner(System.in); + int input; + + //priming read + System.out.println("Enter a non-negative integer"); + System.out.println("Or negative to exit"); + input = keyboard.nextInt(); + + while(input >= 0) { + System.out.println(input); + + System.out.println("Enter a non-negative integer"); + System.out.println("Or negative to exit"); + input = keyboard.nextInt(); + }//end while + + System.out.println("Done!"); + + + +// int count = 0; +// +// while(count < 10) { +// System.out.println(count); +// count++; +// }//end while + +// int count2 = 15; +// +// do { +// System.out.println(count2); +// count2++; +// } +// while(count2 < 10); + +// for(int i = 0; i < 10; i++) { +// System.out.println(i); +// }//end for + + + + }//end main +} diff --git a/section3/SelectionFun.java b/section3/SelectionFun.java new file mode 100644 index 0000000..6630f2c --- /dev/null +++ b/section3/SelectionFun.java @@ -0,0 +1,27 @@ +import java.util.Scanner; + +public class SelectionFun { + public static void main(String[] args) { + int age; + Scanner keyboard = new Scanner(System.in); + + System.out.println("Welcome to the Pub and Grille."); + + System.out.println("Please enter your age"); + age = keyboard.nextInt(); + + if(age >= 21) { + System.out.println("Here, have a beer."); + } + else if(age >= 16) { + System.out.println("Here have a Coke!"); + System.out.println("At least you can drive!"); + } + else { + System.out.println("Here have a Coke!"); + } + + System.out.println("Thanks for coming to the Pub and Grille!"); + + } +} diff --git a/section3/SumFun.java b/section3/SumFun.java new file mode 100644 index 0000000..0c30c39 --- /dev/null +++ b/section3/SumFun.java @@ -0,0 +1,25 @@ +import java.util.Scanner; + +public class SumFun { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + int sum = 0; + int input; + + //priming read + System.out.println("Enter non-negative to add to sum"); + System.out.println("Enter a negative to exit!"); + input = keyboard.nextInt(); + + while(input >= 0) { + sum += input; //sum = sum + input; + System.out.println("Enter non-negative to add to sum"); + System.out.println("Enter a negative to exit!"); + input = keyboard.nextInt(); + }//end while + + System.out.println("Sum is " + sum); + + + }//end main +}