diff --git a/FinishLine/src/com/company/Die.java b/FinishLine/src/com/company/Die.java new file mode 100644 index 0000000..f1c3e80 --- /dev/null +++ b/FinishLine/src/com/company/Die.java @@ -0,0 +1,23 @@ +package com.company; + +import java.util.Random; + +public class Die +{ + private int value; + public Die() + { + roll(); + } + + public int getValue() + { + return value; + } + public void roll() + { + Random random = new Random(); + value = random.nextInt(6)+ 1; + } + +} diff --git a/FinishLine/src/com/company/DieTester.java b/FinishLine/src/com/company/DieTester.java new file mode 100644 index 0000000..8add0ce --- /dev/null +++ b/FinishLine/src/com/company/DieTester.java @@ -0,0 +1,19 @@ +package com.company; + +public class DieTester +{ + public static void main(String[] args) + { + Die die = new Die(); + Die die1 = new Die(); + + // System.out.println(die.getValue()); + die.roll(); + die1.roll(); + System.out.println(die.getValue()); + System.out.println(die1.getValue()); + + } + + +} diff --git a/FinishLine/src/com/company/FinishLine.java b/FinishLine/src/com/company/FinishLine.java new file mode 100644 index 0000000..ac9e097 --- /dev/null +++ b/FinishLine/src/com/company/FinishLine.java @@ -0,0 +1,66 @@ +package com.company; + +public class FinishLine +{ + private Peg bluePeg; + private Peg redPeg; + + private Die dieOne; + private Die dieTwo; + + public FinishLine() + { + bluePeg = new Peg(); + redPeg = new Peg(); + + dieOne = new Die(); + dieTwo = new Die(); + } + + public void play() + { + System.out.println("Starting Game Finish Line"); + + do + { + takeTurn(bluePeg); + takeTurn(redPeg); + printGameStatus(); + } + while(!bluePeg.isWinner() && !redPeg.isWinner()); + + if (bluePeg.isWinner()) + { + System.out.println("Blue Wins!"); + } + if( redPeg.isWinner()) + { + System.out.println("Red Wins!"); + } + } + + private void takeTurn(Peg peg) + { + dieOne.roll(); + dieTwo.roll(); + + if (peg.getNextPosition() == dieOne.getValue() || + peg.getNextPosition() == dieTwo.getValue()|| + peg.getNextPosition() == dieTwo.getValue()+dieOne.getValue() ) + { + peg.moveForward(); + } + } + //RoundAbout + + private void printGameStatus() + { + System.out.println("Blue peg at: "+bluePeg.getPosition()); + System.out.println("Red peg at: "+redPeg.getPosition()); + } + public void reset() + { + bluePeg.reset(); + redPeg.reset(); + } +} diff --git a/FinishLine/src/com/company/Main.java b/FinishLine/src/com/company/Main.java new file mode 100644 index 0000000..e50b3ee --- /dev/null +++ b/FinishLine/src/com/company/Main.java @@ -0,0 +1,26 @@ +package com.company; + +public class Main { + + public static void main(String[] args) + { + + FinishLine game = new FinishLine(); + + game.play(); + game.reset(); + + + System.out.println(); + System.out.println(); + + + RoundAbout game2 = new RoundAbout(); + + + game2.play(); + game2.resetToZero(); + } + + +} diff --git a/FinishLine/src/com/company/Peg.java b/FinishLine/src/com/company/Peg.java new file mode 100644 index 0000000..0ce2229 --- /dev/null +++ b/FinishLine/src/com/company/Peg.java @@ -0,0 +1,33 @@ +package com.company; + +public class Peg +{ + private int position; + public Peg() + { + reset(); + + } + + public int getPosition() + { + return position; + } + public void moveForward() + { + position++; + } + public int getNextPosition() + { + return position + 1; + } + public boolean isWinner() + { + return position >= 10; + } + public void reset() + { + position = 1; + } + +} diff --git a/FinishLine/src/com/company/Peg2.java b/FinishLine/src/com/company/Peg2.java new file mode 100644 index 0000000..4d7f5b9 --- /dev/null +++ b/FinishLine/src/com/company/Peg2.java @@ -0,0 +1,47 @@ +package com.company; + +public class Peg2 +{ + private int position; + + public Peg2() + { + resetToZero(); + + } + + public int getPosition() + { + return position; + } + + public void moveForward() + { + position++; + } + + public int getNextRollValueNeeded() + { + if (position == 0) + { + return 5; + + } + else + { + return position +1; + } + } + + public boolean isWinner() + { + return position >= 11; + } + + public void resetToZero() + { + position = 0; + } + +} + diff --git a/FinishLine/src/com/company/PegTester.java b/FinishLine/src/com/company/PegTester.java new file mode 100644 index 0000000..970810f --- /dev/null +++ b/FinishLine/src/com/company/PegTester.java @@ -0,0 +1,12 @@ +package com.company; + +public class PegTester +{ + public static void main(String[] args) + { + Peg bluePeg = new Peg(); + System.out.println(bluePeg.getPosition()); + bluePeg.moveForward(); + System.out.println(bluePeg.getPosition()); + } +} diff --git a/FinishLine/src/com/company/RoundAbout.java b/FinishLine/src/com/company/RoundAbout.java new file mode 100644 index 0000000..12ae491 --- /dev/null +++ b/FinishLine/src/com/company/RoundAbout.java @@ -0,0 +1,74 @@ +package com.company; + +public class RoundAbout +{ + private Peg2 bluePeg2; + private Peg2 redPeg2; + + private Die dieOne; + private Die dieTwo; + + public RoundAbout() + { + bluePeg2 = new Peg2(); + redPeg2 = new Peg2(); + dieOne = new Die(); + dieTwo = new Die(); + } + + public void play() + { + System.out.println("Starting Game Round About"); + + do + { + takeTurn2(bluePeg2,redPeg2); + takeTurn2(redPeg2,bluePeg2); + printGameStatus(); + } + while (!bluePeg2.isWinner() && !redPeg2.isWinner()); + + if (bluePeg2.isWinner()) + { + System.out.println("Blue Wins!"); + } + if (redPeg2.isWinner()) + { + System.out.println("Red Wins!"); + } + } + + private void takeTurn2(Peg2 peg2, Peg2 opponentPeg) + { + dieOne.roll(); + dieTwo.roll(); + + if (peg2.getNextRollValueNeeded() == dieOne.getValue() || + peg2.getNextRollValueNeeded() == dieTwo.getValue() || + peg2.getNextRollValueNeeded() == dieTwo.getValue() + dieOne.getValue()) + { + peg2.moveForward(); + if (peg2.getPosition()== opponentPeg.getPosition()) + { + System.out.println("Move it buddy!"); + opponentPeg.resetToZero(); + } + } + } + + + + private void printGameStatus() + { + System.out.println("Blue peg at: " + bluePeg2.getPosition()); + System.out.println("Red peg at: " + redPeg2.getPosition()); + } + + + public void resetToZero() + { + bluePeg2.resetToZero(); + redPeg2.resetToZero(); + } + +} diff --git a/ch02/.idea/vcs.xml b/ch02/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch02/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch02/Arithmetic.java b/ch02/Arithmetic.java new file mode 100644 index 0000000..94bb4b2 --- /dev/null +++ b/ch02/Arithmetic.java @@ -0,0 +1,18 @@ +public class Arithmetic +{ + public static void main(String[] args) + { + int num = 100; + int factor = 20; + int sum = 0; + sum = num + factor; //100+20 + System.out.println("Addition Sum: " + sum); + sum = num - factor; //100-20 + System.out.println("Subtraction sum:" + sum); + sum = num* factor; //100x 20 + System.out.println("Multiplication: "+ sum); + sum = num / factor; //100/20 + System.out.println("Division sum: " + sum); + + } +} diff --git a/ch02/Assignment.java b/ch02/Assignment.java new file mode 100644 index 0000000..73ad4ee --- /dev/null +++ b/ch02/Assignment.java @@ -0,0 +1,20 @@ +public class Assignment +{ + public static void main(String[] args) + { + String txt = "Fantastic "; + String lang = "Java"; + txt += lang; + //Assign concatenated String + System.out.println("Add & Assign Strings: "+txt); + int sum = 10; + int num= 20; + sum += num; //Assign Result(10 + 20 = 30) + System.out.println("Add & Assign Integers: "+ sum); + int factor = 5; + sum *= factor; //Assign result (30 x 5= 150) + System.out.println("Multiplication sum: "+ sum); + sum/= factor; //Assgn result ( 150 / 5 = 30) + System.out.println("Division sum: "+ sum); + } +} diff --git a/ch02/Constants.java b/ch02/Constants.java new file mode 100644 index 0000000..962b813 --- /dev/null +++ b/ch02/Constants.java @@ -0,0 +1,24 @@ + +/* A program to demonstrate constant variables +/* + */ +public class Constants +{ + public static void main (String[]args) + { + //Constant score values. + final int TOUCHDOWN = 6; + final int CONVERSION = 1; + final int FIELDGOAL = 3; + + //Calculate points scored + int td,pat,fg,total; + td=4* TOUCHDOWN; //4x6=24 + pat=3*CONVERSION; //3x1=3 + fg =2*FIELDGOAL; //2x3=6 + total = (td+pat+fg); //24+3+6=33 + + //Output calculated total. + System.out.println("Score: "+ total); + } +} diff --git a/ch02/DataTypes.java b/ch02/DataTypes.java new file mode 100644 index 0000000..6207c7d --- /dev/null +++ b/ch02/DataTypes.java @@ -0,0 +1,16 @@ +public class DataTypes +{ + public static void main(String[]args) + { + char letter = 'M'; + String title = "Java in easy steps"; + int number = 365; + float decimal = 98.6f; + boolean result = true; + System.out.println("Initial is " + letter); + System.out.println("Book is " + title); + System.out.println("Days are "+ number); + System.out.println("Temperature is " + decimal); + System.out.println("Answer is "+ result); + } +} diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..ae91d64 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,22 @@ +public class Date +{ + public static void main(String[] args) + { + String day = "Tuesday "; + String month = "April "; + int date = 16; + int year = 2018; + System.out.println("American Format:"); + System.out.print(day); + System.out.print(month); + System.out.print(date); + System.out.print(", "); + System.out.println(year); + System.out.println("European Format:"); + System.out.print(day); + System.out.print(date); + System.out.print(" "); + System.out.print(month); + System.out.println(year); + } +} diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..bc5f7fa --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,12 @@ +public class DoubleByZero +{ + public static void main (String[] args) + { + + //Divide by zero error demo + double first = 42; + double second = 0; + double result = first / second; + System.out.println(result); + } +} \ No newline at end of file diff --git a/ch02/FirstVariable.java b/ch02/FirstVariable.java new file mode 100644 index 0000000..16b78a9 --- /dev/null +++ b/ch02/FirstVariable.java @@ -0,0 +1,10 @@ +public class FirstVariable +{ + public static void main (String[] args) + { + String message = "Initial Value"; + System.out.println(message); + message = "Modified value"; + System.out.println(message); + } +} diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..16311cc --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,12 @@ +public class IntByZero +{ + public static void main (String[] args) + { + + //Divide by zero error demo + int first = 42; + int second = 0; + int result = first / second; + System.out.println(result); + } +} diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..db4ac61 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,18 @@ +public class IntExtremes +{ + public static void main(String[] args) + { + //Increase max positive int + int positiveInt = 2147483647; + System.out.println(positiveInt); + positiveInt++; + System.out.println(positiveInt); + int negativeInt = -2147483648; + + //Decrease max negative int + System.out.println(negativeInt); + negativeInt--; + System.out.println(negativeInt); + + } +} diff --git a/ch02/LoveJava.java b/ch02/LoveJava.java new file mode 100644 index 0000000..10d2d95 --- /dev/null +++ b/ch02/LoveJava.java @@ -0,0 +1,11 @@ +public class LoveJava +{ + public static void main (String[] args) + { + System.out.println("I love Java!"); + String firstWord = "I "; + String secondWord = "love "; + String thirdWord = "Java!"; + System.out.println(firstWord + secondWord + thirdWord); + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..382815b --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,42 @@ +public class Time +{ + public static void main(String[] args) + { + int hour = 15; + int minute = 46; + int second = 33; + + /*Calculate current time to seconds since midnight, 15 hours x 60 minutes x 60 seconds + + minutes x 60 seconds + seconds = 56793 + */ + int secondsSinceMidnight = (hour * 60) * 60 + (minute * 60) + second; + System.out.print("Number of seconds since midnight: "); + System.out.println(secondsSinceMidnight); + //Calculate one day into seconds + double oneDay = 24 * 60 * 60; + double secondsUntilMidnight = oneDay - secondsSinceMidnight; + System.out.println("Number of seconds until midnight: " + secondsUntilMidnight); + //Percentage of the day that has passed + System.out.print("Percent of day that has passed: " + secondsSinceMidnight * 100 / oneDay); + System.out.println("%"); + //Change hour minute and second to reflect current time + + //Place holders for computation + int x = hour; + int y= minute; + int z = second; + //Change values to "current" time + hour = 18; + minute = 58; + second = 45; + //Compute how much time has passed in hours minutes and seconds + int elapsedHour = hour - x; + int elapsedMinute = minute - y; + int elapsedSecond = second -z; + System.out.print("Elapsed Time: "+ elapsedHour); + System.out.print(" Hours "+ elapsedMinute); + System.out.print(" Minutes "+ elapsedSecond); + System.out.println(" Seconds has passed since starting exercise."); + + } +} diff --git a/ch02/Variables.java b/ch02/Variables.java index a295abf..0d46a93 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -1,10 +1,12 @@ /** * Examples from Chapter 2. */ -public class Variables { - - public static void main(String[] args) { +public class Variables +{ + public static void main(String[] args) + { + //Tim String message; int x; @@ -59,7 +61,7 @@ public static void main(String[] args) { System.out.println(0.1 * 10); System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 - + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); + + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); double balance = 123.45; // potential rounding error int balance2 = 12345; // total number of cents diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..64e6506 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,106 @@ +public class Withdrawal +{ + public static void main (String[] args) + { + //Compute $137 withdrawal into appropriate bills + int withdrawal = 137; + int twenty = 20; + int ten = 10; + int five = 5; + int one = 1; + int distributeTwenties = withdrawal / twenty; + int twentyRemainder = withdrawal % twenty; + int distributeTen = twentyRemainder / ten; + int tenRemainder = twentyRemainder % ten; + int distributeFive = tenRemainder / five; + int fiveRemainder = tenRemainder % five; + int distributeOne = fiveRemainder / one; + System.out.println("When withdrawal = 137"); + System.out.print("Distribute $20 bills = " + distributeTwenties); + System.out.print(", $10 bills = " + distributeTen); + System.out.print(", $5 bills = " + distributeFive); + System.out.print(", $1 bills = " + distributeOne); + System.out.println("\n"); + + //Compute $100 withdrawal into appropriate bills + withdrawal = 100; + distributeTwenties = withdrawal / twenty; + twentyRemainder = withdrawal % twenty; + distributeTen = twentyRemainder / ten; + tenRemainder = twentyRemainder % ten; + distributeFive = tenRemainder / five; + fiveRemainder = tenRemainder % five; + distributeOne = fiveRemainder / one; + System.out.println("When withdrawal = $100"); + System.out.print("Distribute $20 bills = " + distributeTwenties); + System.out.print(", $10 bills = " + distributeTen); + System.out.print(", $5 bills = " + distributeFive); + System.out.print(", $1 bills = " + distributeOne); + System.out.println("\n"); + + //Compute $20 withdrawal into appropriate bills + withdrawal = 20; + distributeTwenties = withdrawal / twenty; + twentyRemainder = withdrawal % twenty; + distributeTen = twentyRemainder / ten; + tenRemainder = twentyRemainder % ten; + distributeFive = tenRemainder / five; + fiveRemainder = tenRemainder % five; + distributeOne = fiveRemainder / one; + System.out.println("When withdrawal = $20"); + System.out.print("Distribute $20 bills = " + distributeTwenties); + System.out.print(", $10 bills = " + distributeTen); + System.out.print(", $5 bills = " + distributeFive); + System.out.print(", $1 bills = " + distributeOne); + System.out.println("\n"); + + //Compute $17 withdrawal into appropriate bills + withdrawal = 17; + distributeTwenties = withdrawal / twenty; + twentyRemainder = withdrawal % twenty; + distributeTen = twentyRemainder / ten; + tenRemainder = twentyRemainder % ten; + distributeFive = tenRemainder / five; + fiveRemainder = tenRemainder % five; + distributeOne = fiveRemainder / one; + System.out.println("When withdrawal = $17"); + System.out.print("Distribute $20 bills = " + distributeTwenties); + System.out.print(", $10 bills = " + distributeTen); + System.out.print(", $5 bills = " + distributeFive); + System.out.print(", $1 bills = " + distributeOne); + System.out.println("\n"); + + //Compute $15 withdrawal into appropriate bills + withdrawal = 15; + distributeTwenties = withdrawal / twenty; + twentyRemainder = withdrawal % twenty; + distributeTen = twentyRemainder / ten; + tenRemainder = twentyRemainder % ten; + distributeFive = tenRemainder / five; + fiveRemainder = tenRemainder % five; + distributeOne = fiveRemainder / one; + System.out.println("When withdrawal = $15"); + System.out.print("Distribute $20 bills = " + distributeTwenties); + System.out.print(", $10 bills = " + distributeTen); + System.out.print(", $5 bills = " + distributeFive); + System.out.print(", $1 bills = " + distributeOne); + System.out.println("\n"); + + //Compute $2 withdrawal into appropriate bills + withdrawal = 2; + distributeTwenties = withdrawal / twenty; + twentyRemainder = withdrawal % twenty; + distributeTen = twentyRemainder / ten; + tenRemainder = twentyRemainder % ten; + distributeFive = tenRemainder / five; + fiveRemainder = tenRemainder % five; + distributeOne = fiveRemainder / one; + System.out.println("When withdrawal = $2"); + System.out.print("Distribute $20 bills = " + distributeTwenties); + System.out.print(", $10 bills = " + distributeTen); + System.out.print(", $5 bills = " + distributeFive); + System.out.println(", $1 bills = " + distributeOne); + + //This was tough for the 2nd day, may be crude but it meets requirements! + } +} diff --git a/ch03/Escape.java b/ch03/Escape.java new file mode 100644 index 0000000..ef9fd93 --- /dev/null +++ b/ch03/Escape.java @@ -0,0 +1,15 @@ +public class Escape +{ + public static void main(String[] args) + { + String header = "\n\tNEWYORK 3-DAY FORCAST:\n"; + header += "\n\tDay\t\tHigh\tLow\tConditions\n" ; + header += "\t---\t\t----\t---\t----------\n" ; + + String forecast = "\tSunday\t\t68F\t48F\tSunny\n" ; + forecast += "\tMonday\t\t69F\t57F\tSunny\n" ; + forecast += "\tTuesday\t\t71F\t50F\tCloudy\n" ; + + System.out.println(header + forecast); + } +} diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 64984df..8d52862 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -1,5 +1,5 @@ import java.util.Random; - +import java.util.Scanner; /** * Starter code for the "Guess My Number" exercise. */ @@ -8,8 +8,16 @@ public class GuessStarter { public static void main(String[] args) { // pick a random number Random random = new Random(); + Scanner in = new Scanner(System.in); int number = random.nextInt(100) + 1; - System.out.println(number); + System.out.println("I'm thinking of a number between 1 and 100: "); + System.out.println("Can you guess what it is? "); + int guess = in.nextInt(); + System.out.println("Your Guess is " + guess); + System.out.println(); + System.out.println("The number I was thinking of was "+number); + int difference = guess - number; + System.out.println("You were off by "+ Math.abs(difference)); } } diff --git a/ch03/TempConvert.java b/ch03/TempConvert.java new file mode 100644 index 0000000..802c1cf --- /dev/null +++ b/ch03/TempConvert.java @@ -0,0 +1,27 @@ + +import java.util.Scanner; + /** + * Converts centimeters to feet and inches. + */ + public class TempConvert + { + + public static void main(String[] args) { + double celsius; + double fahrenheit; + + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Exactly how many cm? "); + celsius = in.nextDouble(); + + fahrenheit = (celsius * 9.0/5.0) + 32; + System.out.println("Temp in Farenheight is: " + fahrenheit); + + + + } + + } + diff --git a/ch03/TimeConverter.java b/ch03/TimeConverter.java new file mode 100644 index 0000000..79012e9 --- /dev/null +++ b/ch03/TimeConverter.java @@ -0,0 +1,28 @@ +import java.util.Scanner; +/** + * Converts centimeters to feet and inches. + */ +public class TimeConverter +{ + + public static void main(String[] args) { + + + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + + int seconds; + int minutes ; + int hours; + System.out.print("Enter the number of seconds : "); + seconds = in.nextInt(); + hours = seconds / 3600; + minutes = (seconds % 3600) / 60; + int secondsRemainder = (seconds % 3600) % 60; + + System.out.println(seconds + " Seconds = " + hours + " Hours " + minutes + " Minutes and " + secondsRemainder + " Seconds"); + + } + +} diff --git a/ch04/BigMethodSignature.java b/ch04/BigMethodSignature.java new file mode 100644 index 0000000..5f07e7d --- /dev/null +++ b/ch04/BigMethodSignature.java @@ -0,0 +1,13 @@ +public class BigMethodSignature +{ + public static void main (String[] args) + { + printSum(1,2,3,4,5,6,7,8,9,11); + printSum(11,22,33,44,55,66,77,88,99,111); + } + public static void printSum(int a, int b, int c, int d, int e, int f, int g, int h, int i,int j) + { + int k = a + b + c + d + e + f + g + h + i + j; + System.out.println("The sum is: " + k); + } +} diff --git a/ch04/DemoMath.java b/ch04/DemoMath.java new file mode 100644 index 0000000..a8ecc13 --- /dev/null +++ b/ch04/DemoMath.java @@ -0,0 +1,13 @@ +public class DemoMath +{ + public static void main(String[] args) + { + int abs = Math.abs(-4); + System.out.println(abs); + int max = Math.max(7,10); + System.out.println(max); + double pow = Math.pow(5.0, 6.6); + System.out.println(pow); + System.out.println(Math.PI); + } +} diff --git a/ch04/Employee.java b/ch04/Employee.java new file mode 100644 index 0000000..d92996b --- /dev/null +++ b/ch04/Employee.java @@ -0,0 +1,76 @@ +import java.util.Random; +import java.util.Scanner; + +public class Employee +{ + public static void main(String[] args) + { + int birthYear = 1992; + + boolean isUnionMember = false; + + String firstName = "Timothy"; + String middleName = "John"; + String lastName = "Heckler"; + + int employeeNumber; + Scanner scanner = new Scanner(System.in); + printHeader(); + System.out.println("Please enter your 5 digit employee number:"); + employeeNumber = scanner.nextInt(); + + printFullName(firstName, middleName, lastName); + printUnionStatus(isUnionMember); + printAge(birthYear); + printEvenOrOdd(employeeNumber); + printGenerateSecretPassword(employeeNumber); + } + + public static void printHeader() + { + System.out.println("Welcome to WallabyTech Employee Application"); + System.out.println("==========================================="); + } + + public static void printFullName(String firstName, String middleName, String lastName) + { + System.out.println(lastName + ", " + firstName + " " + middleName); + } + + public static void printUnionStatus(boolean unionStatus) + { + System.out.println("Your union status is: " + unionStatus); + } + + public static void printAge(int birthYear) + { + int age; + int currentYear = 2018; + age = currentYear - birthYear; + System.out.println("Your age is: " + age); + } + + public static void printEvenOrOdd(int x) + { + int isOdd = 1; + int isEven = 2; + if (x % 2 == 0) + { + System.out.println("Employee number is even/odd (1=odd, 2=even; " + isEven); + } else + { + System.out.println("Employee number is even/odd (1=odd, 2=even; " + isOdd); + } + } + + public static void printGenerateSecretPassword(int employeeNumber) + { + Random random = new Random(); + int min = 1; + int max = 10; + int randomNumber = random.nextInt(max - min + 1) + min; + int superSecretPassword = (employeeNumber + randomNumber) * 5; + System.out.println("Employee's random secret pw is: " + superSecretPassword); + + } +} diff --git a/ch04/MathUtil.java b/ch04/MathUtil.java new file mode 100644 index 0000000..d0b1ccb --- /dev/null +++ b/ch04/MathUtil.java @@ -0,0 +1,23 @@ +public class MathUtil +{ + public static void main(String[] args) + { + + printDifference(6,7); + printAbs(-1); + + } + + public static void printDifference(int a, int b) + { + int subtract = a-b; + System.out.print("The value is " +subtract); + } + + public static void printAbs(int a) + { + a = Math.abs(a); + System.out.println(" and abs value is " + a); + + } +} diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java new file mode 100644 index 0000000..bdee7fb --- /dev/null +++ b/ch04/SimpleMethods.java @@ -0,0 +1,26 @@ +public class SimpleMethods +{ + public static void main (String[] args) + { + printCount(5); + printSum(4,6); + printSum(7,2); + printBoolean(true); + printBoolean(false); + } + private static void printCount(int count) + { + System.out.println("The count is: " + count); + } + + private static void printSum(int x, int y) + { + int sum= x+y; + System.out.println(x + "+" + y); + System.out.println(x+"+"+y+"="+sum); + } + private static void printBoolean(Boolean isStudent) + { + System.out.println("I am a student: "+ isStudent); + } +} diff --git a/ch05/Comparision.java b/ch05/Comparision.java new file mode 100644 index 0000000..98c99e5 --- /dev/null +++ b/ch05/Comparision.java @@ -0,0 +1,18 @@ +public class Comparision +{ + public static void main(String[]args) + { + String txt = "Fantastic"; + String lang = "Java"; + Boolean state = (txt == lang); //Assign test result + System.out.println("String Equality Test:"+state); + state = (txt!=lang); //Assign result + System.out.println("String Inequality Test:"+state); + int dozen = 12; + int score = 20; + state = (dozen > score); // Assign result + System.out.println("Greater Than Test:" +state); + state =(dozen< score); //Assign result + System.out.println("Less Than Test:" + state); + } +} diff --git a/ch05/Condition.java b/ch05/Condition.java new file mode 100644 index 0000000..de112d4 --- /dev/null +++ b/ch05/Condition.java @@ -0,0 +1,13 @@ +public class Condition +{ + public static void main(String[]args) + { + int num1 = 1357; + int num2 = 2468; + String result; + result = (num1 % 2 != 0) ? "Odd":"Even"; + System.out.println(num1 + " is " + result); + result = (num2 % 2 != 0) ? "Odd": "Even"; + System.out.println((num2 + " is " + result)); + } +} diff --git a/ch05/CrazyEdsWholesaleStringCheese.java b/ch05/CrazyEdsWholesaleStringCheese.java new file mode 100644 index 0000000..bbfd570 --- /dev/null +++ b/ch05/CrazyEdsWholesaleStringCheese.java @@ -0,0 +1,110 @@ +import java.util.Scanner; + +public class CrazyEdsWholesaleStringCheese +{ + + public static void main(String[] args) + { + int size; + int amount; + Scanner in = new Scanner(System.in); + + System.out.println("Crazy Ed's Wholesale String Cheese offers"); + System.out.println("1 in, 2 in or 3 in, diameter string cheese"); + System.out.println("Limit: one size per order"); + System.out.println("What size cheese would you like to order? "); + + size = in.nextInt(); + if (size > 3) + { + System.out.println("Your order is too crazy, Please try a different size!"); + } + else + { + System.out.println("How many yards would you like to order?"); + amount = in.nextInt(); + System.out.println("You ordered: " + amount + " yards of " + size + " inch thick string cheese."); + + + switch (size) + { + case 1: + int price1 = 2; + int shipping1 = 2; + int withoutShipping = amount * price1; + int withShipping = (amount * price1) + shipping1; + if ((amount >= 50) && (size == 1)) + { + String header ="\n\tITEMIZED INVOICE:\n" ; + header += "\n\tSize\tCost\tAmount\tTotal\n"; + header += "\n\t----\t----\t------\t-----\n"; + String invoice = "\n\t|"+size+"in |\t| $"+price1+" | \t| "+amount+" |\t| $"+withoutShipping+" |\n"; + System.out.println(header + invoice); + //System.out.println("Your total cost will be $ " + withoutShipping); + } + else if ((amount < 50)&& (size == 1)) + { + String header ="\n\tITEMIZED INVOICE:\n" ; + header += "\n\tSize\tCost\tAmount\tshipping\tTotal\n"; + header += "\n\t----\t----\t------\t--------\t-----\n"; + String invoice = "\n\t|"+size+"in |\t| $"+price1+" | \t| "+amount+" |\t| $ "+shipping1+" |\t\t| $"+withShipping+" |\n"; + System.out.println(header + invoice); + //System.out.println("Your total cost will be $ " + withShipping); + } + + case 2: + int price2 = 4; + int shipping2 = 2; + withoutShipping = amount * price2; + withShipping = (amount * price2) + shipping2; + + if ((amount >= 75)&&(size ==2)) + { + String header2 ="\n\tITEMIZED INVOICE:\n" ; + header2 += "\n\t Size\t Cost\t Amount\t Total\n"; + header2 += "\n\t ----\t ----\t ------\t -----\n"; + String invoice2 = "\n\t| "+size+"in|\t| $"+price2+" | \t| "+amount+" |\t| $"+withoutShipping+" |\n"; + System.out.println(header2 + invoice2); + //System.out.println("Your total cost will be $ " + withoutShipping); + } + else if ((amount < 75)&&(size == 2)) + { + String header2 ="\n\tITEMIZED INVOICE:\n" ; + header2 += "\n\t Size\tCost\tAmount\tShipping\tTotal\n"; + header2 += "\n\t----\t ----\t------\t--------\t-----\n"; + String invoice2 = "\n\t|"+size+"in |\t| $"+price2+" | \t| "+amount+" |\t| $ "+shipping2+"|\t\t| $"+withShipping+" |\n"; + System.out.println(header2 + invoice2); + //System.out.println("Your total cost will be $ " + withShipping); + } + + case 3: + int price3 = 6; + int shipping3 = 4; + withoutShipping = amount * price3; + withShipping = (amount * price3) + shipping3; + + if ((amount >= 25)&&(size ==3)) + { + String header3 ="\n\tITEMIZED INVOICE:\n" ; + header3 += "\n\tSize\t Cost\tAmount\tTotal\n"; + header3 += "\n\t----\t ----\t------\t-----\n"; + String invoice3 = "\n\t| "+size+"in |\t| $"+price3+" | \t| "+amount+" |\t| $"+withoutShipping+" |\n"; + System.out.println(header3 + invoice3); + //System.out.println("Your total cost will be $ " + withoutShipping); + } + else if ((amount < 25)&&(size ==3)) + { + String header3 ="\n\tITEMIZED INVOICE:\n" ; + header3 += "\n\tSize\tCost\tAmount\tShipping\tTotal\n"; + header3 += "\n\t----\t----\t------\t--------\t-----\n"; + String invoice3 = "\n\t| "+size+"in |\t| $"+price3+" | \t| "+amount+" |\t | $ "+shipping3+" |\t\t| $ "+withShipping+" |\n"; + System.out.println(header3 + invoice3); + //System.out.println("Your total cost will be $ " + withShipping); + } + } + } + } +} + + + diff --git a/ch05/Else.java b/ch05/Else.java new file mode 100644 index 0000000..80175f7 --- /dev/null +++ b/ch05/Else.java @@ -0,0 +1,16 @@ +public class Else +{ + public static void main( String[] args) + { + int hrs = 21; + if (hrs< 13) + { + System.out.println("Good morning: " +hrs); + } + else if (hrs < 18) + { + System.out.println("Good Afternoon: " +hrs); + } + else System.out.println("Good evening: "+hrs); + } +} diff --git a/ch05/GreenCoffeeGrowers.java b/ch05/GreenCoffeeGrowers.java new file mode 100644 index 0000000..90d6967 --- /dev/null +++ b/ch05/GreenCoffeeGrowers.java @@ -0,0 +1,108 @@ +import java.util.Scanner; + +public class GreenCoffeeGrowers +{ + public static void main(String[] args) + { + int commute; + int miles; + + + Scanner in = new Scanner(System.in); + + System.out.println("How do you commute?"); + System.out.println("Please type (1) for bus, (2) for bike, (3) for car, or (4) Don't commute, proceed to checkout "); + commute = in.nextInt(); + System.out.println("How many miles? "); + miles = in.nextInt(); + + + if (commute > 4 || commute < 1) + { + System.out.println("Invalid Selection, Please Try again!"); + } else if (commute <= 4) + { + switch (commute) + { + //Bus commute + case 1: + if (commute == 1) + { + if (miles < 21&& miles > 0) + { + System.out.println("You receive a free coffee"); + } + else if (miles >= 21 && miles < 35 ) + { + System.out.println("You receive a free coffee and a 20% discount"); + } + else if (miles >= 35 && miles <= 50 ) + { + System.out.println("You receive a free coffee and a 30% discount"); + } + + else if (miles > 50) + { + System.out.println("You receive a free coffee and 50% discount"); + } + + } + + //Bike commute + case 2: + if (commute == 2) + { + if (miles < 21 && miles < 0) + { + System.out.println("You receive a free coffee"); + } + else if (miles > 21 && miles <= 30) + { + System.out.println("You receive a free coffee and a 10% discount"); + } + + else if (miles >= 31 && miles < 50) + { + System.out.println("You receive a free coffee and a 20% discount"); + } + + else if (miles >= 50) + { + System.out.println("You receive a free coffee and 30% discount"); + } + + } + + //Trick question? you said EVERYONE included car + case 3: + if (commute == 3) + { + if (miles < 21 && miles > 0) + { + System.out.println("You receive a free coffee"); + } + else if (miles >= 21) + { + System.out.println("Your Total is... "); + } + } + //Chose not to take discount or doesn't commute + case 4: + if ( miles >= 0) + { + System.out.println("Your Total is... "); + } + else + { + System.out.println("Invalid selection! Pssst! you cant travel negative miles no matter the direction, Try again."); + } + } + } //Rest of Program +else + { + System.out.println("Your Total is... "); + } + + } + +} diff --git a/ch05/If.java b/ch05/If.java new file mode 100644 index 0000000..2ee719c --- /dev/null +++ b/ch05/If.java @@ -0,0 +1,16 @@ +public class If +{ + public static void main (String[] args) + { + if (5>1) System.out.println("Five is greater than one."); + if (2<4) + { + System.out.println("Two is less than four."); + System.out.println("Test succeeded."); + int num =10; + if(((num > 5)&&(num < 10))||(num == 12)) + System.out.println("Number is 6-9 inclusive, or 12"); + } + + } +} diff --git a/ch05/Logic.java b/ch05/Logic.java new file mode 100644 index 0000000..dead234 --- /dev/null +++ b/ch05/Logic.java @@ -0,0 +1,15 @@ +public class Logic +{ + public static void main(String[]args) + { + boolean yes = true; + boolean no = false; + System.out.println("Both YesYes True: "+ (yes && yes)); + System.out.println("Both YesNo True: "+(yes && no)); + System.out.println("Either YesYes True: "+(yes || yes)); + System.out.println("Either YesNo True: "+ (yes|| no)); + System.out.println("Either NoNo True: "+(no | no)); + System.out.println("Origiona Yes Value: "+ yes); + System.out.println("Inverse Yes Value: "+ !yes); + } +} diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..103fab0 --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,76 @@ +public class LogicMethods +{ + public static void main(String[] args) + { + printIsLarge(101); + printIsLargeOrSmall(6); + printLargest(10 ,10); + printLargestOdd(5,5); + } + public static void printIsLarge(int number) + { + if (number > 99) + { + System.out.println("The number is large"); + } + } + public static void printIsLargeOrSmall(int number) + { + if (number > 99 ) + { + System.out.println("The number is large"); + } + else if (number < 10) + { + System.out.println("The number is small"); + } + } + public static void printLargest(int number1, int number2) + { + int value; + if(number1 > number2) + { + value= number1; + System.out.println("The largest number is: " + value); + } + else if (number1 < number2) + { + value = number2; + System.out.println("the largest number is: "+value); + + } + else System.out.println("The two numbers are equal"); + } + public static void printLargestOdd(int number1, int number2) + { + int value; + if ((number1 % 2 != 0) && number1 > number2) + { + + value = number1; + System.out.println("The larges odd number is: "+ value ); + } + else if ((number2 % 2 != 0) && number2 > number1) + { + value = number2; + System.out.println("The largest odd number is: "+ value ); + } + else if ((number1 % 2 == 0) && (number2 % 2 ==0)) + { + System.out.println("Neither number is odd"); + } + + + if ((number1 % 2 != 0) && (number2 % 2 != 0 )) + { + System.out.println("Two odds make an even"); + + if ((number1 % 2 != 0) && (number2 % 2 != 0 && (number1 == number2))) + { + int sum = number1 + number2; + System.out.println("The Sum of the two odds are: " + sum); + } + } + } + +} diff --git a/ch05/Precedence.java b/ch05/Precedence.java new file mode 100644 index 0000000..e9df31b --- /dev/null +++ b/ch05/Precedence.java @@ -0,0 +1,12 @@ +public class Precedence +{ + public static void main( String[] args) + { + int sum = 32 - 8 + 16 *2; //16x32 +, +24 = 56 + System.out.println("Default order: "+ sum); + sum = (32-8+16)*2; //24+16=40,x2=80 + System.out.println("Specified order: "+ sum); + sum =(32 - (8+16)) *2; //32-24=8, * 2 =16 + System.out.println("Nested Specific order: "+ sum); + } +} diff --git a/ch05/Switch.java b/ch05/Switch.java new file mode 100644 index 0000000..0c1bc3e --- /dev/null +++ b/ch05/Switch.java @@ -0,0 +1,17 @@ +public class Switch +{ + public static void main (String[] args) + { + int month = 2; + int year = 2018; + int num = 31; + + switch (month) + { + case 4: case 6: case 11: num = 30 ;break; + case 2: num = (year % 4== 0)? 29: 28; break; + + } + System.out.println(month + "/" + year + ":" + num + " days"); + } +} diff --git a/ch05/SwitchExample.java b/ch05/SwitchExample.java new file mode 100644 index 0000000..88269bf --- /dev/null +++ b/ch05/SwitchExample.java @@ -0,0 +1,52 @@ +class SwitchExample +{ + public static void main(String[] args) + { + lastNameWinner("Lazenby"); + dayOfWeek(100); + } + + public static void lastNameWinner(String lastName) + { + switch (lastName) + { + case "smith": + System.out.println("Congratulations, grand winner"); + break; + case "jones": + System.out.println("Congratulations, grand winner"); + break; + + case "Lazenby": + System.out.println("Hey, he owes me dinner"); + break; + default: + System.out.println("Sorry, not a winner"); + } + + } + + public static void dayOfWeek(int day) + { + switch (day) + { + case 1: + System.out.println("Sunday");break; + case 2: + System.out.println("Monday");break; + case 3: + System.out.println("Tuesday");break; + case 4: + System.out.println("Wednesday");break; + case 5: + System.out.println("Thursday");break; + case 6: + System.out.println("Friday");break; + case 7: + System.out.println("Saturday");break; + } + System.out.println("Invalid value: X"); + } +} + + diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..6b349de --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,28 @@ +import java.util.Scanner; +public class TicketNumber +{ + + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + System.out.println("Enter 6 digit ticket number: "); + int ticketNumber = in.nextInt(); + int lastDigit = ticketNumber%10; + int prefix = ticketNumber / 10; + int remainder = prefix % 7; + boolean result = lastDigit == remainder; + + + if(result == true) + { + System.out.println(ticketNumber + " Is a good number"); + } + else + { + System.out.println("Bad Number, Try again"); + } + + + } + +} diff --git a/ch06/MathUtil.java b/ch06/MathUtil.java new file mode 100644 index 0000000..cd7ee24 --- /dev/null +++ b/ch06/MathUtil.java @@ -0,0 +1,25 @@ +public class MathUtil +{ + public static void main(String[] args) + { + System.out.println(absoluteSum(12,12)); + System.out.println(absoluteSum(12,12,-12)); + } + + public static int absoluteSum(int a, int b) + { + int absa = Math.abs(a); + int absb = Math.abs(b); + int absSum = absa + absb; + return absSum; + } + public static int absoluteSum(int a, int b, int c) + { + int absa = Math.abs(a); + int absb = Math.abs(b); + int absc = Math.abs(c); + int absSum = absa + absb + absc; + return absSum; + } + +} diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..50b5433 --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,16 @@ +public class Multadd +{ + public static void main (String[] args) + { + System.out.println(Multadd(1,2,3)); + } + + public static double Multadd(double a, double b, double c) + { + double math = a*b +c; + return math; + } + + //I started to look at step 4 and said to myself... not that smart + // ... couldn't attempt if it was required +} diff --git a/ch06/isDivisible.java b/ch06/isDivisible.java new file mode 100644 index 0000000..3b89fa3 --- /dev/null +++ b/ch06/isDivisible.java @@ -0,0 +1,22 @@ +public class isDivisible +{ + + public static void main(String[] args) + { + + + System.out.println(isDivisible(4, 2)); + + } + + public static boolean isDivisible(int n, int m) + { + if (n % m == 0) + { + return true; + } else + { + return false; + } + } +} diff --git a/ch06/isTriangle.java b/ch06/isTriangle.java new file mode 100644 index 0000000..7c5a210 --- /dev/null +++ b/ch06/isTriangle.java @@ -0,0 +1,24 @@ +public class isTriangle +{ + public static void main(String[]args) + { + System.out.println(isTriangle(5,10,5)); + } + + //Can Create Triangle? + public static boolean isTriangle(int left, int right, int bottom) + { + boolean noTriangle = false; + boolean yesTriangle = true; + // If any of the sides is > than the sum of the other two = no triangle + if( (bottom > left + right) || ( right > bottom + left ) || (left > bottom + right) ) + { + return noTriangle; + } + else + { + return yesTriangle; + } + + } +} diff --git a/ch07/AskForInt.java b/ch07/AskForInt.java new file mode 100644 index 0000000..0438a8c --- /dev/null +++ b/ch07/AskForInt.java @@ -0,0 +1,25 @@ +import java.util.Scanner; + +public class AskForInt +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + int answer; + int total = 0; + + + do + { + + System.out.println("Please enter a number"); + answer = in.nextInt(); + total = answer + total; + System.out.println("Your total is " + total); + } + while (total <= 1000); + + System.out.println("Your total is " + total); + + } +} diff --git a/ch07/DoWhile.java b/ch07/DoWhile.java new file mode 100644 index 0000000..ab701fb --- /dev/null +++ b/ch07/DoWhile.java @@ -0,0 +1,14 @@ +public class DoWhile +{ + public static void main(String[] args) + { + int num = 100; + do + { + System.out.println("DoWhile Countup: "+ num); + num+=10; + } + while(num < 10); + + } +} diff --git a/ch07/For.java b/ch07/For.java new file mode 100644 index 0000000..99860ed --- /dev/null +++ b/ch07/For.java @@ -0,0 +1,17 @@ +public class For +{ + public static void main(String[] args) + { + int num = 0; + for (int i = 1 ; i < 4 ; i++) + { + System.out.println(" Outer Loop i = " + i); + for (int j = 1 ; j < 4; j++) + { + System.out.print("\t Inner Loop j = " + j); + System.out.println( "\t\t Total num = " +(++num)); + } + } + } +} + diff --git a/ch07/For2.java b/ch07/For2.java new file mode 100644 index 0000000..9746118 --- /dev/null +++ b/ch07/For2.java @@ -0,0 +1,16 @@ +public class For2 +{ + public static void main(String[] args) + { + loopFor(10); + } + public static void loopFor(int n) + { + + for (int i = 1 ; i <= n ; i++) + { + System.out.println(" Outer Loop i = " + i); + + } + } +} diff --git a/ch07/ForDoWhile2.java b/ch07/ForDoWhile2.java new file mode 100644 index 0000000..3f5f9df --- /dev/null +++ b/ch07/ForDoWhile2.java @@ -0,0 +1,57 @@ +public class ForDoWhile2 +{ + public static void main(String[] args) + { + + forLoop(0); + While(10); + DoWhile(0,100); + } + public static void forLoop (int i) + { + + + for (i = 10; i <= 100; i+=10) + { + System.out.println(" Count up i = " + i); + + } + for (i = 100; i >= 10; i-=10) + { + System.out.println(" Count down i = " + i); + + } + } + public static void While(int num) + { + + while(num >=100) + { + System.out.println("While Countdown: " + num); + num -=10 ; + } + while(num <= 100) + { + System.out.println("While Countup: " + num); + num +=10 ; + } + } + public static void DoWhile(int number,int number2) + { + + do + { + System.out.println("DoWhile Countup: "+ number); + number += 10; + } + while(number <= 100); + + do + { + System.out.println("DoWhile Countdown: "+ number2); + number2 -= 10; + } + while(number2 >= 10); + } +} + diff --git a/ch07/ForDoWhile3.java b/ch07/ForDoWhile3.java new file mode 100644 index 0000000..12a4b3f --- /dev/null +++ b/ch07/ForDoWhile3.java @@ -0,0 +1,56 @@ +public class ForDoWhile3 +{ + public static void main(String[] args) + { + + forLoop(0); + While(100,-100); + DoWhile(-100,100); + } + public static void forLoop (int i) + { + + + for (i = -100; i <= 100; i+=8) + { + System.out.println(" Count up i = " + i); + + } + for (i = 100; i >= -100; i-=8) + { + System.out.println(" Count down i = " + i); + + } + } + public static void While(int num, int num2) + { + + while(num >= -100) + { + System.out.println("While Countdown: " + num); + num -=8 ; + } + while(num2 <= 100) + { + System.out.println("While Countup: " + num2); + num2 +=8 ; + } + } + public static void DoWhile(int number,int number2) + { + + do + { + System.out.println("DoWhile Countup: "+ number); + number += 8; + } + while(number <= 100); + + do + { + System.out.println("DoWhile Countdown: "+ number2); + number2 -= 8; + } + while(number2 >= -100); + } +} diff --git a/ch07/ForWhileDoWhile.java b/ch07/ForWhileDoWhile.java new file mode 100644 index 0000000..e688ce9 --- /dev/null +++ b/ch07/ForWhileDoWhile.java @@ -0,0 +1,56 @@ +public class ForWhileDoWhile +{ + public static void main(String[] args) + { + + forLoop(0); + While(10); + DoWhile(0,10); + } + public static void forLoop (int i) + { + + + for (i = 1; i <= 10; i++) + { + System.out.println(" Count up i = " + i); + + } + for (i = 10; i >= 1; i--) + { + System.out.println(" Count down = " + i); + + } + } + public static void While(int num) + { + + while(num >=1) + { + System.out.println("While Countdown: " + num); + num -=1 ; + } + while(num < 10) + { + System.out.println("While Countup: " + num); + num +=1 ; + } + } + public static void DoWhile(int number,int number2) + { + + do + { + System.out.println("DoWhile Countup: "+ number); + number += 1; + } + while(number <= 10); + + do + { + System.out.println("DoWhile Countdown: "+ number2); + number2 -= 1; + } + while(number2 >= 1); + } +} diff --git a/ch07/MultiplicationTable.java b/ch07/MultiplicationTable.java new file mode 100644 index 0000000..6a2c18a --- /dev/null +++ b/ch07/MultiplicationTable.java @@ -0,0 +1,55 @@ +public class MultiplicationTable +{ + public static void main(String[] args) + { + + int tableSize = 5; + //for loop + printMultiplicationTable(tableSize); + //space between + System.out.println(); + //while loop + printMultiplicationTable2(tableSize); + } + + + public static void printMultiplicationTable(int tableSize) + { + + for (int i = 1; i <= tableSize; i++) + { + + for (int j = 1; j <= tableSize; j++) + { + + System.out.printf("%4d\t", i * j); + + } + + System.out.println(); + + } + + + } + public static void printMultiplicationTable2(int tableSize) + { + int i = 1; + + while ( i <= tableSize ) + { + int j = 1; + while ( j <= tableSize ) + { + + System.out.printf("%4d\t", i * j); + j= j + 1; + } + i= i + 1; + System.out.println(); + + } + + + } +} diff --git a/ch07/While.java b/ch07/While.java new file mode 100644 index 0000000..05d3021 --- /dev/null +++ b/ch07/While.java @@ -0,0 +1,12 @@ +public class While +{ + public static void main(String[] args) + { + int num = 100; + while(num > 0) + { + System.out.println("While Countdown: " + num); + num -=10 ; + } + } +} diff --git a/ch07/ZeroLoop.java b/ch07/ZeroLoop.java new file mode 100644 index 0000000..950d44c --- /dev/null +++ b/ch07/ZeroLoop.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +public class ZeroLoop +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + int answer; + + do + { + System.out.println("Please enter a number"); + + answer = in.nextInt(); + + } + while (answer != 0); + System.out.println("Great Guess!"); + + } + + +} + diff --git a/ch08/Array.java b/ch08/Array.java new file mode 100644 index 0000000..a1e8452 --- /dev/null +++ b/ch08/Array.java @@ -0,0 +1,15 @@ +public class Array +{ + public static void main(String[] args) + { + String[] str = {"Much","More","Java"}; + int[]num = new int[3]; + num[0]=100; + num[1]=200; + str[1]="Better"; + System.out.println("String array length is "+str.length); + System.out.println("integer array lengthis "+ num.length); + System.out.println(num[0]+","+num[1]+","+num[2]); + System.out.println(str[0] + str[1] + str[2]); + } +} diff --git a/ch08/ArrayDemo.java b/ch08/ArrayDemo.java new file mode 100644 index 0000000..11d056f --- /dev/null +++ b/ch08/ArrayDemo.java @@ -0,0 +1,105 @@ +public class ArrayDemo +{ + public static void main(String[] args) + { + int[] value = {1, 5, 9}; + int[] moreValue = {5, 2, 9, 8, 0}; + int[] maxValues = {5, 8, 21, 19}; + double[] averageValues = {34.2, 18.0,12.5,13.1}; + int[] array = new int[10]; + array[0]=4; + array[3]=2; + array[9]=4; + String[] array2 = new String[10]; + array2[0]="Hi"; + array2[3]="Hello"; + array2[9]="Bye"; + + + printArray(value); + printArray(moreValue); + int mySum = arrayTotal(moreValue); + System.out.println("Sum of the array is " + mySum); + int myMaxIndex = arrayMaxIndex(maxValues); + System.out.println("Max index of the array is " + myMaxIndex); + int myMaxNum = arrayMax(maxValues); + System.out.println("Max number of the array is " + myMaxNum); + double myAverage = arrayAverage(averageValues); + System.out.println("Average value of the array is " + myAverage); + printArray(array); + printArray2(array2); + + } + + //print array values + public static void printArray(int[] values) + { + for (int value : values) + { + System.out.println(value); + } + } + + public static int arrayTotal(int[] values) + { + int sum = 0; + for (int value : values) + { + sum = sum + value; + + } + return sum; + } + + public static int arrayMax(int[] values) + { + int highest = 0; + for (int value : values) + { + if (value > highest) + { + highest = value; + } + + } + return highest; + } + + public static int arrayMaxIndex(int[] values) + { + int highest = 0; + for (int i = 0; i <= values.length; i++) + { + if (highest < i) + { + highest = i; + } + } + return highest; + } + + public static double arrayAverage(double[] values) + { + double length = values.length; + double average; + double sum = 0; + + for(double value : values) + { + + sum += value; + + } + average = (sum / length); + + return average; + } + + public static void printArray2(String[] values) + { + for (String value : values) + { + System.out.println(value); + } + } +} \ No newline at end of file diff --git a/ch08/Food.java b/ch08/Food.java new file mode 100644 index 0000000..916a516 --- /dev/null +++ b/ch08/Food.java @@ -0,0 +1,28 @@ +public class Food +{ + + private String name; + private int id; + + + + public Food (String name, int id) + { + this.name = name; + this.id = id; + + + } + + public String getName() + { + return name; + } + + public int getId() + { + return id; + } + + +} diff --git a/ch08/HowManyPets.java b/ch08/HowManyPets.java new file mode 100644 index 0000000..f5db6ad --- /dev/null +++ b/ch08/HowManyPets.java @@ -0,0 +1,43 @@ +import java.util.Scanner; + +public class HowManyPets +{ + + public static void main(String[] args) + { + howManyPets(); + + } + public static void howManyPets() + { + Scanner in = new Scanner(System.in); + int pets; + + String[] petNames; + String petName; + + System.out.println("How many pets do you have? "); + + pets = in.nextInt(); + in.nextLine(); + petNames = new String[pets]; + + for (int i =0; i< pets; i++) + { + System.out.println("What is the name(s)? "); + petName = in.nextLine(); + petNames[i] = petName; + + } + + printArray2(petNames); + + } + public static void printArray2(String[] values) + { + for (String value : values) + { + System.out.println("Your pets name is " + value); + } + } +} diff --git a/ch08/IntergalacticVendingMachines.java b/ch08/IntergalacticVendingMachines.java new file mode 100644 index 0000000..1455f5c --- /dev/null +++ b/ch08/IntergalacticVendingMachines.java @@ -0,0 +1,104 @@ +import java.util.Scanner; + +public class IntergalacticVendingMachines +{ + + + public static void main(String[] args) + { + run(); + } + + private static void run() + { + Scanner in = new Scanner(System.in); + String choice; + //Populate Food Array + Food driedSushi = new Food(" Freeze Dried Sushi", 0); + Food brainBlast = new Food(" Spock's Brain Blast", 1); + Food alienAsparagus = new Food(" Alien Asparagus", 2); + + Food[] meal = new Food[3]; + meal[0] = driedSushi; + meal[1] = brainBlast; + meal[2] = alienAsparagus; + + + //Counter Array + int[] counter = new int[3]; + counter[0] = 0; + counter[1] = 0; + counter[2] = 0; + do + { + //Print Food Menu Start of program + System.out.println("Please Select an Item:"); + printMenu(meal); + System.out.println(); + choice = in.nextLine(); + System.out.println(); + String[] selection = choice.split(""); + + if (choice.contains("99")) + { + + } + + else + { + + for (String number : selection) + { + + int magicNumber = Integer.parseInt(number); + int bufferVariable = Integer.parseInt(number); + if (bufferVariable < 0 || bufferVariable > meal.length-1) + {break;} + + System.out.println("Thank you for choosing: " + meal[magicNumber].getName()); + counter[magicNumber]++; + + } + System.out.println("Items sold so far:"); + printSales(counter, meal); + System.out.println(); + } + } + while(!choice.contains("99") ); + { + System.out.println(); + System.out.println("Final Total Sales"); + printSales(counter, meal); + System.out.println(); + System.out.println("GoodBye!"); + + } + + + } + + + public static void printMenu(Food[] meals) + { + //Prints Menu for selection + for (Food meal : meals) + { + System.out.println(meal.getId() + ") " + meal.getName()); + + } + } + + public static void printSales(int[] counter, Food[] meal) + { + for (int i = 0; i <= counter.length - 1; i++) + { + + + System.out.println(counter[i] + " " + meal[i].getName()); + + + } + } + + +} diff --git a/ch09/StringUtil.java b/ch09/StringUtil.java new file mode 100644 index 0000000..b3f7123 --- /dev/null +++ b/ch09/StringUtil.java @@ -0,0 +1,188 @@ +import com.sun.xml.internal.fastinfoset.util.CharArray; +import com.sun.xml.internal.fastinfoset.util.CharArrayString; + +public class StringUtil +{ + public static void main(String[] args) + { + System.out.println("Welcome to StringUtil"); + System.out.println(); + getFirstCharacter("Goodbye"); + getFirstCharacter("Hello"); + System.out.println(); + getLastCharacter("Hello"); + getLastCharacter("Goodbye"); + System.out.println(); + System.out.println(getFirstTwoCharacters("Hello")); + System.out.println(getFirstTwoCharacters("Goodbye")); + System.out.println(); + System.out.println(getLastTwoCharacters("Goodbye")); + System.out.println(getLastTwoCharacters("Hello")); + System.out.println(); + System.out.println(getFirstThreeCharacters("Hello")); + System.out.println(getFirstThreeCharacters("Goodbye")); + System.out.println(); + System.out.println(printCharacters("Hello")); + System.out.println(printCharacters("Goodbye")); + System.out.println(); + printPhoneNumber("501-555-0100"); + printPhoneNumber2("5015550100"); + System.out.println(); + findFirstE("Hello"); + findFirstE("Goodbye"); + System.out.println(); + System.out.println(isFinn("Finn")); + System.out.println(isFinn("Jake")); + System.out.println(); + System.out.println(reverse("Finn")); + System.out.println(); + System.out.println(isPalindrome("Finn")); + System.out.println(isPalindrome("dad")); + System.out.println(); + String[] wordArray = new String[4]; + wordArray[0]="pickle"; + wordArray[1]="learn"; + wordArray[2]="education"; + wordArray[3]="coding"; + + System.out.println("The result is "+allLetters(wordArray)); + + } + + private static String getFirstCharacter(String value) + { + String word = "Hello"; + String newWord = "Goodbye"; + System.out.println(word.substring(0, 1)); + System.out.println(newWord.substring(0, 1)); + return value.substring(0, 1); + } + + private static String getLastCharacter(String value) + { + int startIndex = value.length() - 1; + return value.substring(startIndex, startIndex + 1); + } + + private static String getFirstTwoCharacters(String value) + { + return value.substring(0, 2); + + + } + + private static String getLastTwoCharacters(String value) + { + int startIndex = value.length() - 2; + return value.substring(startIndex, startIndex + 2); + } + + private static String getFirstThreeCharacters(String value) + { + return value.substring(0, 3); + } + + private static String printCharacters(String value) + { + for (int i = 0; i < value.length(); i++) + { + char letter = value.charAt(i); + System.out.println(letter + ":" + i); + } + return value; + } + + private static void printPhoneNumber(String value) + { + + + System.out.print("Area Code: " + value.substring(0, 3) + " "); + System.out.print("Exchange: " + value.substring(4, 7) + " "); + System.out.println("Line Number: " + value.substring(8, 12)); + System.out.println(); + + } + + //9-S-1 + private static void printPhoneNumber2(String value2) + { + + + System.out.print("Area Code: " + value2.substring(0, 3) + " "); + System.out.print("Exchange: " + value2.substring(3, 6) + " "); + System.out.println("Line Number: " + value2.substring(6, 10)); + System.out.println(); + + } + + private static void findFirstE(String e) + { + e = "Hello"; + String e2 = "Goodbye"; + int index = e.indexOf("e"); + int index2 = e2.indexOf("e"); + System.out.println("First e in " + e + " is at position: " + index); + System.out.println("First e in " + e2 + " is at position: " + index2); + } + + private static boolean isFinn(String value) + { + + if (value.equals("Finn")) + { + return true; + } else + { + return false; + } + } + + // 9-S-2 + private static String reverse(String word) + { + + String reverse = new StringBuffer(word).reverse().toString(); + + return reverse; + + + } + + // 9-S-3 + private static boolean isPalindrome(String value) + { + String reverse = new StringBuffer(value).reverse().toString(); + if (value.equals(reverse)) + { + return true; + } else + { + return false; + } + } + +///don't think this is working correctly + private static boolean allLetters(String[] wordArray) + { + + String[] word = "learn".split(""); + + for (int i = 0; i < wordArray.length - 1; i++) + { + for (String words : wordArray) + { + if (words.startsWith(word.toString())) + { + return true; + } else + { + return false; + } + + } + + } + return false; + } +} + diff --git a/ch10/Student.java b/ch10/Student.java new file mode 100644 index 0000000..06bbb67 --- /dev/null +++ b/ch10/Student.java @@ -0,0 +1,29 @@ +public class Student +{ + private String name; + private int studentId; + private double gpa; + + + public Student(String name, int studentId) + { + this.name = name; + this.studentId = studentId; + + } + + public String getName() + { + return name; + } + + public int getStudentId() + { + return studentId; + } + + public double getGpa() + { + return gpa; + } +} diff --git a/ch10/StudentTester.java b/ch10/StudentTester.java new file mode 100644 index 0000000..22fd6cd --- /dev/null +++ b/ch10/StudentTester.java @@ -0,0 +1,42 @@ +public class StudentTester +{ + public static void main (String[] args) + { + // System.out.println("vomit some code here" ); + Student maggie = new Student("Maggie", 1); + // System.out.println(maggie.getName()); + + Student tim = new Student("Tim",2); + //System.out.println(tim.getName()); + + Student joseph = new Student("Joseph",3); + // System.out.println(joseph.getName()); + Student josiah = new Student("Josiah",4); + Student christy = new Student("Christy",5); + Student luke = new Student("Luke",6); + + Student[] frontRowStudents= new Student[3]; + frontRowStudents[0]= maggie; + frontRowStudents[1]= tim; + frontRowStudents[2]= joseph; + + Student[] backRowStudents= new Student[3]; + backRowStudents[0]= josiah; + backRowStudents[1]= christy; + backRowStudents[2]= luke; + + + printStudents(frontRowStudents); + System.out.println(); + printStudents(backRowStudents); + + } + private static void printStudents(Student[] students) + { + for (Student student: students) + { + // System.out.println(student.getName()); + System.out.println(student.getName()); + } + } +} diff --git a/ch11/Date.java b/ch11/Date.java new file mode 100644 index 0000000..3ddab83 --- /dev/null +++ b/ch11/Date.java @@ -0,0 +1,41 @@ +public class Date +{ + private int day; + private int month; + private int year; + + public Date(int day, int month, int year) + { + this.day = day; + this.month = month; + this.year = year; + } + + public int getDay() + { + return day; + } + + public int getMonth() + { + return month; + } + + public int getYear() + { + return year; + } + + public String getFormattedDate() + { + if (month < 10) + { + return getYear() + "-0" + getMonth() + "-" + getDay(); + } + else if (day < 10) + return getYear() + "-" + getMonth() + "-0" + getDay(); + + else + return getYear() + "-" + getMonth() + "-" + getDay(); + } +} diff --git a/ch11/DateTester.java b/ch11/DateTester.java new file mode 100644 index 0000000..2adfebd --- /dev/null +++ b/ch11/DateTester.java @@ -0,0 +1,14 @@ +public class DateTester +{ + public static void main(String[] args) + { + Date currentDate; + + currentDate = new Date(27,04,2018); + + + System.out.println(currentDate.getFormattedDate()); + } + + +} diff --git a/ch11/Planet.java b/ch11/Planet.java new file mode 100644 index 0000000..6d5f10c --- /dev/null +++ b/ch11/Planet.java @@ -0,0 +1,13 @@ +public class Planet +{ + private String name; + public Planet (String name) + { + this.name = name; + } + + public String getName() + { + return name; + } +} diff --git a/ch11/PlanetTester.java b/ch11/PlanetTester.java new file mode 100644 index 0000000..025dfdf --- /dev/null +++ b/ch11/PlanetTester.java @@ -0,0 +1,37 @@ +public class PlanetTester +{ + + public static void main(String[] args) + { + Planet mercury = new Planet("Mercury"); + Planet venus = new Planet("Venus"); + Planet earth = new Planet("Earth"); + Planet mars = new Planet("Mars"); + Planet saturn = new Planet("Saturn"); + Planet jupiter = new Planet("Jupiter"); + Planet uranus = new Planet("Uranus"); + Planet neptune = new Planet("Neptune"); + Planet pluto = new Planet("Pluto"); + + + Planet[] planets = new Planet[9]; + planets[0] = mercury; + planets[1] = venus; + planets[2] = earth; + planets[3] = mars; + planets[4] = saturn; + planets[5] = jupiter; + planets[6] = uranus; + planets[7] = neptune; + planets[8] = pluto; + printPlanets(planets); + } + + public static void printPlanets(Planet[] planets) + { +for (Planet planet: planets) +{ + System.out.println(planet.getName()); +} + } +} diff --git a/ch11/Player.java b/ch11/Player.java new file mode 100644 index 0000000..3010483 --- /dev/null +++ b/ch11/Player.java @@ -0,0 +1,31 @@ +public class Player +{ + private String name; + private int score; + + public Player(String name, int score) + { + score = 0; + this.name = name; + this.score = score; + } + + public int increaseScore() + { + return score = score+1; + } + public int resetScore() + { + return score = 0; + } + + public int getScore() + { + return score; + } + + public String getName() + { + return name; + } +} diff --git a/ch11/PlayerTest.java b/ch11/PlayerTest.java new file mode 100644 index 0000000..a2bd34b --- /dev/null +++ b/ch11/PlayerTest.java @@ -0,0 +1,11 @@ +public class PlayerTest +{ + public static void main(String[]args) + { + Player player; + player = new Player("Josh",4); + System.out.println(player.getName()+" entered the game with score at "+player.getScore()); + System.out.println(player.getName()+" scored a point! increased score to "+player.increaseScore()); + System.out.println(player.getName()+" cheated, score is now reset to "+player.resetScore()); + } +} diff --git a/ch11/Time2.java b/ch11/Time2.java new file mode 100644 index 0000000..e92efed --- /dev/null +++ b/ch11/Time2.java @@ -0,0 +1,76 @@ +public class Time2 +{ + private int hour; + private int minute; + private boolean PM; + + public Time2(int hour, int minute) + { + this.hour = hour; + this.minute = minute; + } + public Time2(int hour, int minute, boolean PM) + { + this.hour = hour; + this.minute = minute; + this.PM = PM; + + } + + + public String getMilitaryTime() + { + if (hour < 10 && minute < 10) + { + return "0"+getHour() + ":0" + getMinute(); + } + else if (hour < 10) + { + return "0"+getHour() + ":" + getMinute(); + } + else if (minute < 10) + { + return getHour() + ":0" + getMinute(); + } + else + { + return getHour() + ":" + getMinute(); + } + } + public String getTime() + { + if (hour > 12 && PM) + { + return "Civilian Time: "+ (getHour() - 12) + ":" + getMinute() + " PM"; + } + else if (hour <= 12) + { + return "Civilian Time: "+"0" + getHour() + ":" + getMinute() + " AM"; + } + else if (minute < 10) + { + return "Civilian Time: "+getHour() + ":0" + getMinute() + " AM"; + } + else + return "Civilian Time: "+getHour() + ":" + getMinute() + " AM"; + } + + + + public int getHour() + { + return hour; + } + + public int getMinute() + { + return minute; + } + + public boolean PM() + { + return PM; + } + + +} diff --git a/ch11/Time2Tester.java b/ch11/Time2Tester.java new file mode 100644 index 0000000..c16e904 --- /dev/null +++ b/ch11/Time2Tester.java @@ -0,0 +1,23 @@ +public class Time2Tester +{ + public static void main(String[]args) + { + + Time2 currentTime; + currentTime = new Time2 (9,45,true); + System.out.println(currentTime.getTime()); + System.out.println(); + + System.out.println("Military Time: "+currentTime.getMilitaryTime()); + System.out.println(); + + + currentTime = new Time2 (14,30,true); + + System.out.println("Military Time: "+currentTime.getMilitaryTime()); + System.out.println(); + System.out.println(currentTime.getTime()); + + } + +}