diff --git a/ArrayListFun/src/ListDemo.java b/ArrayListFun/src/ListDemo.java new file mode 100644 index 0000000..ef58414 --- /dev/null +++ b/ArrayListFun/src/ListDemo.java @@ -0,0 +1,53 @@ +import java.util.ArrayList; +import java.util.List; + +public class ListDemo +{ + private void execute() + { + ArrayList furniture = new ArrayList<>(); + + furniture.add("Couch"); + furniture.add("Table"); + furniture.add("Bed"); + + + printList(furniture); + + furniture.add(furniture.size(),"Bar Stool"); + printList(furniture); + + furniture.add(0, "Beverage Station"); + printList(furniture); + + furniture.add(2, "Chaise Lounge"); + printList(furniture); + + furniture.remove(furniture.size() - 1); + printList(furniture); + + furniture.remove(0); + printList(furniture); + + furniture.remove(1); + printList(furniture); + + } + + private void printList(ArrayList print) + { + for (int i = 0; i < print.size(); i++) + { + System.out.print(print.get(i) + " : "); + } + System.out.println(); + } + public static void main(String[] args) + { + ListDemo myFurniture = new ListDemo(); + myFurniture.execute(); + +// ListDemo myPrint = new ListDemo(); +// myPrint.execute(); + } +} diff --git a/ArrayListFun/src/ShoppingList.java b/ArrayListFun/src/ShoppingList.java new file mode 100644 index 0000000..7db7304 --- /dev/null +++ b/ArrayListFun/src/ShoppingList.java @@ -0,0 +1,127 @@ +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; + +public class ShoppingList +{ + private Scanner in = new Scanner(System.in); + private int total = 0; + private ArrayList addToList = new ArrayList<>(); + + private void run() + { + ArrayList myList = new ArrayList<>(); + Scanner in = new Scanner(System.in); + String command; + + + do { + System.out.println("Enter Command(Add, Print, Remove, Clear, Sort, Find or Exit"); + + command = in.nextLine(); + + if(command.equalsIgnoreCase("Add")) + { + add(); + //System.out.println(addToList.size()); + } + else if(command.equalsIgnoreCase("Remove")) + { + remove(); + + //System.out.println(addToList.size()); + } + else if(command.equalsIgnoreCase("Print")) + { + printList(addToList); + } + else if (command.equalsIgnoreCase("Clear")) + { + clear(); + } + else if (command.equalsIgnoreCase("Sort")) + { + sort(); + printList(addToList); + } + else if (command.equalsIgnoreCase("Find")) + { + find(); + } + else + { + messageError(); + } + //System.out.println("The current total is: " + total); + } + while(!command.equalsIgnoreCase("exit")); + { + System.out.println("Now exiting..."); + System.out.println("Goodbye"); + + } + printList(addToList); + + + } + private void add() + { + System.out.print("Enter the item to be added: "); + String item = in.nextLine(); + addToList.add(item); + System.out.println("Item added: " + item); + } + + private void remove() + { + System.out.println("Enter the item number to removed: "); + int number = in.nextInt(); + in.nextLine(); + addToList.remove(number); + + System.out.println("Item removed: " + number); + } + + private void printList(ArrayList print) + { + for(int i = 0; i < print.size(); i++) + { + System.out.println(i + ". " + print.get(i)); + } + } + + private void clear() + { + addToList.clear(); + } + + private void messageError() + { + System.out.println("Invalid entry. Please try again."); + } + + private void sort() + { + Collections.sort(addToList); + } + private void find() + { + System.out.println("Enter item to be searched: "); + String findItem = in.nextLine(); + if(addToList.contains(findItem)) + { + System.out.println("Found it!"); + } + else + { + System.out.println("No such item!"); + } + } + + public static void main(String[] args) + { + ShoppingList myShoppingList = new ShoppingList(); + myShoppingList.run(); + } +} diff --git a/ArrayListFun/src/SimpleList.java b/ArrayListFun/src/SimpleList.java new file mode 100644 index 0000000..3672f09 --- /dev/null +++ b/ArrayListFun/src/SimpleList.java @@ -0,0 +1,32 @@ +import java.util.ArrayList; + +public class SimpleList +{ + private void demo() + { + ArrayList colors = new ArrayList<>(); + + colors.add("Blue"); + colors.add("Green"); + colors.add("Indigo"); + colors.add("Black"); + colors.add("Gray"); + + for (int i = 0; i < colors.size(); i++) + { + System.out.println(colors.get(i)); + } + + for(String element: colors) + { + System.out.println(element); + } + } + + public static void main(String[] args) + { + SimpleList myColors = new SimpleList(); + myColors.demo(); + + } +} diff --git a/MapExercises/src/PetHotel.java b/MapExercises/src/PetHotel.java new file mode 100644 index 0000000..4d1e60f --- /dev/null +++ b/MapExercises/src/PetHotel.java @@ -0,0 +1,191 @@ +import java.util.*; + +public class PetHotel +{ + //instance level + + private TreeMap rooms; + private Scanner in = new Scanner(System.in); + + + private PetHotel() + { + rooms = new TreeMap<>(); + } + + private void run() + { + String command; + do { + System.out.println("Enter Command(CheckIn, " + + "CheckOut," + + "Move, " + + "Occupancy, CloseForSeason, Exit"); + + String inputLine = in.nextLine(); + String[] words = inputLine.split(" "); + + command = words[0]; //sets command as first word counted in [] + + + if (command.equalsIgnoreCase("CheckIn")) { + String name = words[1]; + int roomNumber = Integer.parseInt(words[2]); + + if(isValidRoomNumber(roomNumber)) + { + if(isRoomEmpty(roomNumber)) + { + checkIn(name, roomNumber); + } + else + { + System.out.println("Room occupied. Select another room."); + } + } + + } + else if (command.equalsIgnoreCase("CheckOut")) { + int roomNumber = Integer.parseInt(words[1]); + + checkOut(roomNumber); + + } + else if (command.equalsIgnoreCase("Move")) + { + int fromRoom = Integer.parseInt(words[1]); + int toRoom = Integer.parseInt(words[2]); + + if (isRoomOccupied(fromRoom)) + { + if (isRoomEmpty(toRoom)) + { + move(fromRoom, toRoom); + + } + else + { + System.out.println("That room is currently occupied. Please select another room."); + + } + } + + else + { + errorMessage(); + } + } + else if (command.equalsIgnoreCase("Occupancy")) + { + + occupancy(); + } + else if (command.equalsIgnoreCase("CloseForSeason")) + { + closeForSeason(); + } + } + while (!command.equalsIgnoreCase("Exit")); + { + System.out.println("Goodbye"); + } + + } + + private boolean isRoomOccupied(int roomNumber) + { + boolean occupied = false; + + if (rooms.containsKey(roomNumber)) + { + occupied = true; + } + return occupied; + } + + private boolean isRoomEmpty(int roomNumber) + { + boolean empty = true; + + if(rooms.containsKey(roomNumber)) + { + empty = false; + } + + return empty; + } + private boolean isValidRoomNumber(int roomNumber) + { + if(roomNumber >= 100 && roomNumber <= 109) + { + return true; + } + else + { + System.out.println(roomNumber + " is not a valid room number. Please try again."); + } + return false; + } + private void checkIn(String name, int roomNumber) + { + rooms.put(roomNumber, name); + System.out.println(name + " checked into room " + roomNumber); + System.out.println(rooms); + + } + + private void checkOut(int roomNumber) + { + + String name = rooms.remove(roomNumber); + + if(!isRoomEmpty(roomNumber)) + { + errorMessage(); + } + else + { + System.out.println(name + " checked out."); + + } + + + System.out.println(rooms); + + + } + + private void move(int fromRoom, int toRoom) + { + String name = rooms.remove(fromRoom); + rooms.put(toRoom, name); + + System.out.println(name + " moved from " + fromRoom + " to room " + toRoom); + System.out.println(rooms); + + } + + + private void occupancy() + { + Set> occupancyList = rooms.entrySet(); + + System.out.println(occupancyList); + } + + private void closeForSeason() + { + rooms.clear(); + System.out.println(rooms); + } + private void errorMessage() + { + System.out.println("Invalid entry. Try again."); + } + + public static void main(String[] args) + { + PetHotel petHotel = new PetHotel(); + petHotel.run(); + } +} diff --git a/MapExercises/src/SimpleMap.java b/MapExercises/src/SimpleMap.java new file mode 100644 index 0000000..c6fc06c --- /dev/null +++ b/MapExercises/src/SimpleMap.java @@ -0,0 +1,23 @@ +import java.util.HashMap; + +public class SimpleMap +{ + private void demo() + { + HashMap hashMap = new HashMap<>(); + + hashMap.put("USA", "United States"); + hashMap.put("MEX", "Mexico"); + hashMap.put("CAN", "Canada"); + + System.out.println("Value at USA is " + hashMap.get("USA")); + } + + + public static void main(String[] args) + { + SimpleMap simpleMap = new SimpleMap(); + + simpleMap.demo(); + } +} diff --git a/MapExercises/src/TimeZoneDemo.java b/MapExercises/src/TimeZoneDemo.java new file mode 100644 index 0000000..48a1d05 --- /dev/null +++ b/MapExercises/src/TimeZoneDemo.java @@ -0,0 +1,39 @@ + +import java.util.HashMap; + +public class TimeZoneDemo +{ + private HashMap hashMap = new HashMap<>(); + + private void demo() + { + //HashMap hashMap = new HashMap<>();moved to the main scope to be visible to rest of methods + + hashMap.put("EST", -5); + hashMap.put("CST", -6); + hashMap.put("MST", -7); + hashMap.put("PST", -8); + hashMap.put("GMT", 0); + + System.out.println(getTimeDiff("EST","CST")); + System.out.println(getTimeDiff("PST", "EST")); + + } + + private int getTimeDiff(String timeZone1, String timeZone2) + { + int time1 = hashMap.get(timeZone1); + int time2 = hashMap.get(timeZone2); + + return time1 - time2; + } + + public static void main(String[] args) + { + TimeZoneDemo timeZoneDemo = new TimeZoneDemo(); + timeZoneDemo.demo(); + + // timeZoneDemo.getTimeDiff(); + + } +} diff --git a/MyExercises/MyExercises.iml b/MyExercises/MyExercises.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/MyExercises/MyExercises.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/MyExercises/src/com/company/Main.java b/MyExercises/src/com/company/Main.java new file mode 100644 index 0000000..d7552ff --- /dev/null +++ b/MyExercises/src/com/company/Main.java @@ -0,0 +1,87 @@ +package com.company; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) + { + + customerChoice(); + // secretNumber(99); + } + + private static void customerChoice() + { + final int SECRET_NUMBER = 99; + final int MAX_MEAL_OPTIONS = 5; + final int MAX_DAILY_SALES_ALLOWED = 10; + + Scanner in = new Scanner(System.in); + + + + int [] total = new int[MAX_DAILY_SALES_ALLOWED]; + + int currentTotal = 1; + int totalZero = 1; + int totalOne = 1; + int totalTwo = 1; + int totalThree = 1; + int totalFour = 1; + + + + + for(int i = 0; i < total.length; i++) + { + System.out.println("Choose from 0 - 4"); + int choice = in.nextInt(); + + if(choice == 0) + { + System.out.println("We've sold " + totalZero + " of " + choice); + totalZero++; + } + if(choice == 1) + { + System.out.println("We've sold " + totalOne + " of " + choice); + totalOne++; + } + if(choice == 2) + { + System.out.println("We've sold " + totalTwo + " of " + choice); + totalTwo++; + } + if(choice == 3) + { + System.out.println("We've sold " + totalThree + " of " + choice); + totalThree++; + } + if(choice == 4) + { + System.out.println("We've sold " + totalFour + " of " + choice); + totalFour++; + } + } + + } + + + + private static void secretNumber(int secret) + { + final int SECRET_NUMBER = 99; + + Scanner in = new Scanner(System.in); + + + secret = in.nextInt(); + if(secret == SECRET_NUMBER) + { + System.out.println("Goodbye"); + } + } + + +} diff --git a/ch01/Hello.java b/ch01/Hello.java index 593557b..56eee74 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -1,6 +1,8 @@ -public class Hello { +public class Hello +{ - public static void main(String[] args) { + public static void main(String[] args) + { // generate some simple output System.out.println("Hello, World!"); } diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..ddb7a67 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,37 @@ +public class Date +{ + public static void main(String args[]) + { + System.out.println("Date Exercise 2.2/4.3"); + + System.out.println("American format:"); + printAmerican("Friday", 19, "January", 2018); + + System.out.println("European format:"); + printEuropean("Thursday", 19, "January", 2018); + } + + private static void printAmerican(String day, int date, String month, int year) + { + System.out.print(day); + System.out.print(", "); + System.out.print(month); + System.out.print(" "); + System.out.print(date); + System.out.print(", "); + System.out.print(year); + System.out.println(); + } + + private static void printEuropean(String day, int date, String month, int year) + { + System.out.print(day); + System.out.print(" "); + System.out.print(month); + System.out.print(" "); + System.out.print(date); + System.out.print(" "); + System.out.print(year); + System.out.println(); + } +} diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..b62dd16 --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,16 @@ +public class DoubleByZero +{ + public static void main(String args[]) + { + + System.out.println("Exercise 2.6"); + + double a = 42; + double b = 0; + double result; + + result = a / b; + + System.out.println(result); + } +} diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..9f57604 --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,14 @@ +public class IntByZero +{ + public static void main(String args[]) + { + + int a = 42; + int b = 0; + int result; + + result = a / b; + + System.out.println(result); + } +} diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..1989622 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,20 @@ +public class IntExtremes +{ + public static void main(String arg[]) + { + + System.out.println("Exercise 2.4"); + int positiveInt = 2147483647; + + System.out.println(positiveInt); + + positiveInt = positiveInt + 1; + System.out.println(positiveInt); + + int negativeInt = -2147483648; + System.out.println(negativeInt); + + negativeInt = negativeInt - 1; + System.out.println(negativeInt); + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..8bbfa5e --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,38 @@ +public class Time +{ + public static void main(String args[]) + { + System.out.println("Time 2.3"); + int hour = 15; + int minute = 4; + //int second = (hour * 60 + minute) * 60; + int second = 16; + + int secondsPerMinute = 60; + int minutesPerHour = 60; + + int secondsSinceMidnight = (hour * minutesPerHour * secondsPerMinute) +(minute * secondsPerMinute) + second; + + + System.out.println("Number of seconds since midnight: " + secondsSinceMidnight); + + int hoursPerDay = 24; + int secondsInDay = hoursPerDay * minutesPerHour * secondsPerMinute; + int secondsRemainingInDay = secondsInDay - secondsSinceMidnight; + System.out.println("Number of seconds remaining in the day: " + secondsRemainingInDay); + + int percentageDayPassed = secondsSinceMidnight * 100 / secondsInDay; + System.out.println("Percentage of the day that has passed: " + percentageDayPassed); + + + int hour2 = 15; + int minute2 = 55; + int second2 = 25; + + int endingTime = (hour2 * minutesPerHour * secondsPerMinute) + (minute2 * secondsPerMinute) + second2; + + int timeElapsed = endingTime - secondsSinceMidnight; + System.out.print("Time elapsed in seconds since starting: " + timeElapsed); + + } +} diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..bdc6fbe --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,29 @@ +public class Withdrawal +{ + public static void main(String args[]) + { + int withdrawal = 137; + + int twenties = withdrawal / 20; + + System.out.println("$20: " + twenties); + + int remainder = withdrawal - (20 * twenties); + + //System.out.println(remainder + " left to go."); + + int tens = remainder / 10; + remainder = withdrawal - (twenties * 20) - (tens * 10); + + System.out.println("$10: " + tens); + + //System.out.println(remainder + " left to go."); + int fives = remainder / 5; + remainder = withdrawal - (twenties * 20) - (tens * 10) - (fives * 5); + + System.out.println("$5: " + fives); + //System.out.println(remainder + " left to go."); + + System.out.println("$1: " + remainder); + } +} diff --git a/ch02/myVariables.java b/ch02/myVariables.java new file mode 100644 index 0000000..c4efb2b --- /dev/null +++ b/ch02/myVariables.java @@ -0,0 +1,18 @@ +public class myVariables +{ + public static void main(String args[]) + { + System.out.println("Hi there!"); + + int a = 1; + int b = 2; + int c = a + b; + System.out.println(c); + + int d = 5; + int e = 2; + int f = d - e; + System.out.println(f); + + } +} diff --git a/ch03/Convert.java b/ch03/Convert.java index d31fe77..2edaa2f 100644 --- a/ch03/Convert.java +++ b/ch03/Convert.java @@ -7,9 +7,12 @@ public class Convert { public static void main(String[] args) { double cm; - int feet, inches, remainder; + int feet; + int inches; + int remainder; final double CM_PER_INCH = 2.54; final int IN_PER_FOOT = 12; + Scanner in = new Scanner(System.in); // prompt the user and get the value diff --git a/ch03/ConvertTemp.java b/ch03/ConvertTemp.java new file mode 100644 index 0000000..d161cd5 --- /dev/null +++ b/ch03/ConvertTemp.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class ConvertTemp +{ + public static void main(String[] args) + { + + double c; + + Scanner in = new Scanner(System.in); + + System.out.print("How many Celsius: "); + c = in.nextDouble(); + + double fahrenheit = c * 9 / 5 + 32; + + System.out.print(c); + System.out.print(" C = "); + System.out.print(fahrenheit); + System.out.println(" F"); + } +} diff --git a/ch03/ConvertTime.java b/ch03/ConvertTime.java new file mode 100644 index 0000000..98b7251 --- /dev/null +++ b/ch03/ConvertTime.java @@ -0,0 +1,33 @@ +import java.util.Scanner; + +public class ConvertTime +{ + public static void main(String args[]) + { + int hours; + int minutes; + int seconds; + + final int Hours = 60 * 60; + + int input; + + Scanner in = new Scanner(System.in); + + System.out.print("How many seconds?"); + input = in.nextInt(); + + hours = input / Hours; + minutes = input % Hours / 60; + seconds = (input - Hours) - minutes * 60; + + System.out.print(input + " seconds = "); + System.out.printf("%d hours, %d minutes, %d seconds", hours, minutes, seconds); + + //System.out.println(hours); + //System.out.println(minutes); + //System.out.println(seconds); + + } + +} diff --git a/ch03/GuessMyNumber.java b/ch03/GuessMyNumber.java new file mode 100644 index 0000000..a013646 --- /dev/null +++ b/ch03/GuessMyNumber.java @@ -0,0 +1,11 @@ +import java.util.Scanner; +import java.util.Random; + +public class GuessMyNumber +{ + + public static void main(String args[]) + { + + } +} diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 64984df..b25094f 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -1,15 +1,27 @@ import java.util.Random; +import java.util.Scanner; -/** - * Starter code for the "Guess My Number" exercise. - */ -public class GuessStarter { +public class GuessStarter +{ - public static void main(String[] args) { - // pick a random number + public static void main(String[] args) + { Random random = new Random(); int number = random.nextInt(100) + 1; - System.out.println(number); + int input; + + Scanner in = new Scanner(System.in); + + + System.out.println("I'm thinking of a number between 1 and 100. Can you guess what it is?"); + System.out.print("Type a number: "); + input = in.nextInt(); + + System.out.println("Your guess is: " + input); + + System.out.println("The number I was thinking of is: " + number); + System.out.print("You were off by: "); + System.out.println(input - number); } } diff --git a/ch04/.idea/vcs.xml b/ch04/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch04/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch04/Demo.java b/ch04/Demo.java new file mode 100644 index 0000000..9d0694e --- /dev/null +++ b/ch04/Demo.java @@ -0,0 +1,88 @@ +public class Demo +{ + public static void main(String[] args) + { + + + /* final int DAYS_IN_WEEK = 7; + int daysUntilSpringBreak = 30; + + System.out.println(daysUntilSpringBreak); + + daysUntilSpringBreak = daysUntilSpringBreak - 1; + + System.out.println(daysUntilSpringBreak); + + // Call the awesome method I wrote + printDaysLeft(daysUntilSpringBreak); + + daysUntilSpringBreak = 1; + printDaysLeft(daysUntilSpringBreak); + + printIsEvenOrOdd(4); + printIsEvenOrOdd(11); + + + getBeer(17); + getBeer(55); + } + + /*private static void printDaysLeft(int daysLeft) + { + if (daysLeft >= 10) + { + System.out.println("THERE ARE WAY TOO MANY DAYS TO GO!"); + } else if (daysLeft == 1) + { + System.out.println("There is only " + daysLeft + " day to go! Woot!"); + } + else + { + System.out.println("There are only " + daysLeft + " days to go! Woot!"); + System.out.println("I am so excited!"); + }*/ + printIsEqual(4,5, 6); + printIsEqual(7, 7, 7); + + +} + + + private static void printIsEqual(int x, int y, int z) + { + System.out.println("The value of x is: "+ x); + System.out.println("The value of y is: "+ y); + System.out.println("The value of z is: "+ z); + + + if (x == y && y == z) + { + + System.out.println("The numbers are equal"); + } + + } + /*private static void printIsEvenOrOdd(int someNumber) + { + int remainder = someNumber % 2; + + if (remainder == 1) { + System.out.println(someNumber + " is odd"); + } else { + System.out.println(someNumber + " is even"); + } + } + + private static void getBeer(int age) + { + final int DRINKING_AGE = 21; + if (age >= DRINKING_AGE) { + System.out.println("You are " + age + " so you get beer!"); + } else { + int yearsToWait = (DRINKING_AGE - age); + + System.out.println("You are only " + age + " so no beer for you!"); + System.out.println("Come back in " + yearsToWait + " years!"); + } + }*/ +} diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java new file mode 100644 index 0000000..3f58e8d --- /dev/null +++ b/ch04/SimpleMethods.java @@ -0,0 +1,25 @@ +public class SimpleMethods +{ + public static void main(String[] args) + { + printCount(5); + printSum(7, 13); + + printSum(4, 6); + printSum(7, 2); + } + + private static void printCount(int count) + { + System.out.println("This count is: " + count); + } + + private static void printSum(int a, int b) + { + System.out.print(a); + System.out.print(" + "); + System.out.print(b); + System.out.print(" = "); + System.out.println(a + b); + } +} diff --git a/ch05/.idea/vcs.xml b/ch05/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch05/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch05/EdsWholesaleStringCheese.java b/ch05/EdsWholesaleStringCheese.java new file mode 100644 index 0000000..3df78f9 --- /dev/null +++ b/ch05/EdsWholesaleStringCheese.java @@ -0,0 +1,67 @@ +import java.util.Scanner; + +public class EdsWholesaleStringCheese +{ + public static void main(String[] args) + { + int handlingCharge = 5; + + System.out.println("Welcome to Crazy Ed's Wholesale String Cheese!"); + System.out.println("What can we get for you today?"); + System.out.println("Select from 1 inch, 2 inch or 3 inch"); + + int order; + Scanner in = new Scanner(System.in); + order = in.nextInt(); + + System.out.println("How many yards would you like?"); + int yards; + yards = in.nextInt(); + + int oneInch = (2 * yards) + (2 * yards) + handlingCharge; + int twoInch = (4 * yards) + (2 * yards) + handlingCharge; + int threeInch = (6 * yards) + (4 * yards) + handlingCharge; + int freeShip1 = 2 * yards; + int freeShip2 = 2 * yards; + int freeShip3 = 4 * yards; + + if (order == 1 && yards <= 50) + { + System.out.println("$2/yd plus $2/yd shipping"); + System.out.println("Your total with shipping and handling fees is $" + oneInch); + } + else if (order == 1 && yards > 50) + { + System.out.println("$2/yd with FREE shipping!"); + System.out.println("Your total delivery charge with FREE shipping is $" + (oneInch - freeShip1)); + } + else if (order == 2 && yards <= 75) + { + System.out.println("$4/yard plus $2/yd shipping"); + System.out.println("Your total with shipping and handling fees is $" + twoInch); + } + else if (order == 2 && yards > 75) + { + System.out.println("$4/yd with FREE shipping!"); + System.out.println("Your total delivery charge with FREE shipping is $" + (twoInch - freeShip2)); + + } + else if (order == 3 && yards <=25) + { + System.out.println("$6/yd plus $4/yd shipping"); + System.out.println("Your total with shipping and handling fees is $" + threeInch); + + } + else if (order == 3 && yards > 25) + { + System.out.println("$6/yd with FREE shipping"); + System.out.println("Your total delivery charge with FREE shipping is $" + (threeInch - freeShip3)); + + } + else + { + System.out.println("Your order is too crazy!!!"); + } + + } +} diff --git a/ch05/FermatsLastTheorem.java b/ch05/FermatsLastTheorem.java new file mode 100644 index 0000000..c5023e3 --- /dev/null +++ b/ch05/FermatsLastTheorem.java @@ -0,0 +1,30 @@ +public class FermatsLastTheorem +{ + public static void main(String[] args) + { + + checkFermat(2, 4, 6, 8); + checkFermat(4, 4, 4, 2); + checkFermat(2, 2, 2, 2); + } + + private static void checkFermat(int a, int b, int c, int n) + { + System.out.println("checkFermat"); + int result = (int) Math.pow(a,n) + (int) Math.pow(b, n); + System.out.println(result); + int result2 = (int) Math.pow(c, n); + System.out.println(result2); + + if (n > 2 && result == result2) + { + System.out.println("Holy smokes, Fermat was wrong!"); + } + else + { + System.out.println("No, that doesn't work."); + } + System.out.println(); + } +} + diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..00734e2 --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,100 @@ +public class LogicMethods +{ + public static void main(String[] args) + { + System.out.println("Start of Program"); + printIsLarge(1); + printIsLarge(99); + printIsLarge(100); + printIsLarge(200); + + printIsLargeOrSmall(1); + printIsLargeOrSmall(50); + printIsLargeOrSmall(195); + + printLargest(10, 15); + printLargest(20, 2); + printLargest(5, 5); + + printLargestOdd(9,3); + printLargestOdd(9,57); + printLargestOdd(6,4); + printLargestOdd(7,7); + + + } + + private static void printIsLarge(int number) + { + System.out.println("printIsLarge"); + System.out.println("Number is: " + number); + + if (number > 99) + { + System.out.println("The number is large"); + + } + System.out.println(); + + } + + private static void printIsLargeOrSmall(int number) + { + System.out.println("printIsLargeOrSmall"); + System.out.println("Number is: " + number); + + if (number > 99) + { + System.out.println("The number is large"); + } else if (number < 10) + { + System.out.println("The number is small"); + } + System.out.println(); + } + + private static void printLargest(int number1, int number2) + { + System.out.println("printLargest"); + System.out.println("The numbers are: " + number1 + ", " + number2); + + if (number1 > number2) + System.out.println("The largest number is: " + number1); + + else if (number1 < number2) + { + System.out.println("The largest number is: " + number2); + } else { + System.out.println("The numbers are equal"); + } + System.out.println(); + } + + private static void printLargestOdd(int number1, int number2) + { + int remainder1 = number1 % 2; + int remainder2 = number2 % 2; + + System.out.println("printLargestOdd"); + System.out.println("The numbers are: " + number1 + ", " + number2); + + if (number1 > number2 && remainder1 == 1) + { + System.out.println("The largest odd number is: " + number1); + } + else if (number1 < number2 && remainder2 == 1) + { + System.out.println("The largest odd number is: " + number2); + } + else if (remainder1 == 0 && remainder2 == 0) + { + System.out.println("Neither number is odd"); + } + else + { + System.out.println("Two odds make an even"); + System.out.println("The sum is: " + (number1 + number2)); + } + System.out.println(); + } +} diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..b3e7c54 --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,33 @@ +import java.util.Scanner; + +public class TicketNumber +{ + public static void main(String[] args) + { + Scanner user = new Scanner(System.in); + + System.out.println("Enter ticket number: "); + int ticketNumber; + ticketNumber = user.nextInt(); + + int lastDigit; + lastDigit = ticketNumber % 10; + System.out.println("lastDigit: " + lastDigit); + + int ticketPrefix = (ticketNumber - lastDigit) / 10; + System.out.println("ticketPrefix: " + ticketPrefix); + + int remainder = ticketPrefix % 7; + System.out.println("Remainder is: " + remainder); + + boolean result = remainder == lastDigit; + if (result) + { + System.out.println("Good Number"); + } + else + { + System.out.println("Bad Number"); + } + } +} diff --git a/ch06/.idea/vcs.xml b/ch06/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch06/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch06/Exercise.java b/ch06/Exercise.java index b826b44..651688e 100644 --- a/ch06/Exercise.java +++ b/ch06/Exercise.java @@ -34,3 +34,8 @@ public static boolean isFrabjuous(int x) { } } + /* + System.out.println(); + + boolean isATriangle = isTriangle(12, 6, 6); + System.out.println(isATriangle); */ \ No newline at end of file diff --git a/ch06/ValueMethods.java b/ch06/ValueMethods.java new file mode 100644 index 0000000..0ca4943 --- /dev/null +++ b/ch06/ValueMethods.java @@ -0,0 +1,53 @@ +public class ValueMethods +{ + public static void main(String[] args) + { + + boolean isItDivisible = isDivisible(10, 5); + System.out.println(isItDivisible); + + System.out.println(); + + boolean isATriangle = isTriangle(12, 6, 6); + System.out.println(isATriangle); + + System.out.println(); + + double testMultadd = multadd(1.0, 2.0, 3.0); + System.out.println(testMultadd); + + + } + public static boolean isDivisible(int n, int m) + { + boolean divisible; + if (n % m == 0) + { + divisible = true; + } + else + { + divisible = false; + } + return divisible; + } + public static boolean isTriangle(int a, int b, int c) + { + boolean threeSides; + if (a + b < c || b + c < a || a + c < b) + { + threeSides = false; + } + else + { + threeSides = true; + } + return threeSides; + } + + public static double multadd(double a, double b, double c) + { + double add = a * b + c; + return add; + } +} diff --git a/ch07/Basketball.java b/ch07/Basketball.java new file mode 100644 index 0000000..047536e --- /dev/null +++ b/ch07/Basketball.java @@ -0,0 +1,84 @@ +import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; + +import java.util.Random; + +public class Basketball +{ + public static void main(String[] args) + { + System.out.println("Welcome to the Ultimate Basketball Game!"); + play(); + } + + public static void play() + { + final int WINNING_SCORE = 21; + + boolean gameOver = false; + int homeScore = 0; //starting score for each team + int visitorScore = 0; + + while(!gameOver) + { + //Play a round + homeScore += getPlayScore(); + visitorScore += getPlayScore(); + + + + System.out.println("Home: " + homeScore + " Visitor: " + visitorScore); + + if(homeScore >= WINNING_SCORE || visitorScore >= WINNING_SCORE) + { + gameOver = true; + } + } + if (homeScore >= visitorScore) + { + System.out.println("Home Team Wins!"); + } + else + { + System.out.println("Visitor Team Wins!"); + } + } + + private static int getPlayScore() + { + int diceRoll= twoDieRoll(); + + int score = 0; + + if (diceRoll == 2 || diceRoll == 10 || diceRoll == 12) + { + score = 3; + } + else if(diceRoll == 4 || diceRoll == 6 || diceRoll == 8) + { + score = 2; + } + else if(diceRoll == 5) + { + score = 1; + } + + return score; + } + private static int twoDieRoll() + { + + int die1 = dieRoll(); + int die2 = dieRoll(); + + int dieTotal = die1 + die2; + return dieTotal; + } + private static int dieRoll() + { + final int DIE_SIZE = 6; + + Random random = new Random(); + + return random.nextInt(DIE_SIZE) + 1; + } +} diff --git a/ch07/FinishLine.java b/ch07/FinishLine.java new file mode 100644 index 0000000..d9053c7 --- /dev/null +++ b/ch07/FinishLine.java @@ -0,0 +1,80 @@ +import java.util.Random; +import java.util.Scanner; + +public class FinishLine +{ + public static void main(String[] args) + { + System.out.println("Let's play Finish Line\n"); + + mainPlay(); + } + private static void mainPlay() + { + + //System.out.println("Random roll is: " + die()); //test string + + final int MAX_SCORE = 10; + final int STARTING_SCORE = 1; + + int player1 = STARTING_SCORE; + int player2 = STARTING_SCORE; + + while(player1 < MAX_SCORE && player2 < MAX_SCORE) { + int next = player1 + 1; + + int player1RollOne = die(); + int player1RollTwo = die(); + int player1RollTotal = player1RollOne + player1RollTwo; + + System.out.println("Player One rolled " + player1RollOne + " and " + player1RollTwo); + + if (player1RollTotal == next || (player1RollOne == next) || player1RollTwo == next) + { + System.out.println("Player 1 moves up!"); + + player1++; + System.out.println("New Score P1 " + player1); + System.out.println(); + } + + int next2 = player2 + 1; + + int player2RollOne = die(); + int player2RollTwo = die(); + int player2RollTotal = player2RollOne + player2RollTwo; + + System.out.println("Player Two rolled " + player2RollOne + " and " + player2RollTwo); + + if (player2RollTotal == next2 || (player2RollOne == next2) || player2RollTwo == next2) { + System.out.println("Player 2 moves up!"); + + player2++; + System.out.println("New Score P2 " + player2); + System.out.println(); + } + } + if(player1 == 10) + { + System.out.println("Player 1 Wins!"); + } + else + { + System.out.println("Player 2 Wins!"); + } + } + + + + private static int die() + { + final int DIE_SIZE = 6; + + Random random = new Random(); + + return random.nextInt(DIE_SIZE) + 1; + + + } +} + diff --git a/ch07/Loops7B.java b/ch07/Loops7B.java new file mode 100644 index 0000000..a5378fb --- /dev/null +++ b/ch07/Loops7B.java @@ -0,0 +1,42 @@ +public class Loops7B +{ + public static void main(String[] args) + { + System.out.println("sevenBForLoop"); + sevenBForLoop(); + System.out.println("sevenBWhileLoop"); + sevenBWhileLoops(); + System.out.println("sevenBDoWhileLoop"); + sevenBDoWhileLoop(); + + } + + public static void sevenBForLoop() + { + for(int x = 0; x <= 100; x += 10) + { + System.out.println(x); + } + } + + public static void sevenBWhileLoops() + { + int y = 0; + while(y <= 100) + { + System.out.println(y); + y += 10; + } + } + + public static void sevenBDoWhileLoop() + { + int z = 0; + do { + { + System.out.println(z); + z += 10; + } + }while (z <= 100); + } +} diff --git a/ch07/Loops7C.java b/ch07/Loops7C.java new file mode 100644 index 0000000..d3e614f --- /dev/null +++ b/ch07/Loops7C.java @@ -0,0 +1,42 @@ +public class Loops7C +{ + public static void main(String[] args) + { + System.out.println("sevenCForLoop"); + sevenCForLoop(); + System.out.println("sevenCWhileLoop"); + sevenCWhileLoop(); + System.out.println("sevenCDoWhileLoop"); + sevenCDoWhileLoop(); + } + + public static void sevenCForLoop() + { + for (int x = 100; x >= -100; x -= 8) { + System.out.println(x); + } + + } + + public static void sevenCWhileLoop() + { + int y = 100; + while (y >= -100) { + System.out.println(y); + y -= 8; + } + } + + public static void sevenCDoWhileLoop() + { + int z = 100; + do { + { + System.out.println(z); + z -= 8; + } + } while (z >= -100); + + } + +} diff --git a/ch07/Loops7D.java b/ch07/Loops7D.java new file mode 100644 index 0000000..43b91f8 --- /dev/null +++ b/ch07/Loops7D.java @@ -0,0 +1,16 @@ +public class Loops7D +{ + public static void main(String[] args) + { + sevenCForLoop(1); + } + + public static void sevenCForLoop(int x) + { + while(x <=31) + { + System.out.println(x); + x++; + } + } +} diff --git a/ch07/Loops7E.java b/ch07/Loops7E.java new file mode 100644 index 0000000..156ccc8 --- /dev/null +++ b/ch07/Loops7E.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class Loops7E +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + int x; + + System.out.print("Enter a number 0 to 10\t"); + x = in.nextInt(); + + while(x != 0) + { + System.out.print("Enter a number 0 to 10\t"); + x = in.nextInt(); + } + } + + +} + diff --git a/ch07/Loops7F.java b/ch07/Loops7F.java new file mode 100644 index 0000000..df71075 --- /dev/null +++ b/ch07/Loops7F.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +public class Loops7F +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + + int num = 0; + int runningTotal = 0; + + while (runningTotal <= 1000) + { + System.out.println("Enter a number: "); + num = in.nextInt(); + runningTotal += num; + System.out.println(runningTotal); + + } + System.out.println("You have exceeded 1000"); + } + + +} diff --git a/ch07/MoveBooks.java b/ch07/MoveBooks.java new file mode 100644 index 0000000..ca38421 --- /dev/null +++ b/ch07/MoveBooks.java @@ -0,0 +1,62 @@ +import java.util.Scanner; + +public class MoveBooks +{ + public static void main(String[] args) + { + //moveBooks(9); + // payTax(50); + printNumbers(1, 5); + } + + public static void payTax(int totalTax) + { + Scanner in = new Scanner(System.in); + + int taxPaid = 0; + + do + { + System.out.println("Enter amount to pay: "); + int payment = in.nextInt(); + + taxPaid = taxPaid + payment; + + } while (taxPaid < totalTax); + + System.out.println("No jail for you!"); + } + + public static void printNumbers(int min, int max) + { + /*int currentNumber = min; + while (currentNumber <= max) + { + System.out.println(currentNumber); + currentNumber++; + }*/ + //FIRST int currentNumber = min initializes ONCE + //SECOND currentNumber = currentNumber + 1 INCREMENTS BY ONE + //THIRD currentNumber <= max RUNS UNTIL IT NO LONGER READS TRUE + + for (int currentNumber = min; currentNumber <= max; currentNumber++) + { + System.out.println(currentNumber); + } + + } + + + public static void moveBooks(int numberOfBooks) + { + int remainingBooks = numberOfBooks; + + while (remainingBooks > 0) + { + System.out.println("Book moved!"); + remainingBooks--; + System.out.println("There are " + remainingBooks + " books remaining."); + } + System.out.println("All the books have been moved."); + } +} diff --git a/ch07/MyLoops.java b/ch07/MyLoops.java new file mode 100644 index 0000000..2f9a550 --- /dev/null +++ b/ch07/MyLoops.java @@ -0,0 +1,59 @@ +public class MyLoops +{ + public static void main(String [] args) + { + System.out.println("sevenAForLoop"); + sevenAForLoop(); + System.out.println("sevenAWhileLoop"); + sevenAWhileLoop(); + System.out.println("sevenADoWhileLoop"); + sevenADoWhileLoop(); + } + + public static void sevenAForLoop() + { + for(int x = 0; x <= 10; x++) + { + System.out.println(x); + } + for(int x = 10; x >= 1; x--) + { + System.out.println(x); + } + } + + public static void sevenAWhileLoop() + {int y = 0; + while (y <= 10) + { + System.out.println(y); + y++; + } + while (y >= 1) + { + System.out.println(y); + y--; + } + } + + public static void sevenADoWhileLoop() + { + int z = 0; + do + { + System.out.println(z); + z++; + } + while (z <= 10); + + int a = 10; + do { + { + System.out.println(a); + a--; + } + } + while (a >= 1); + + } +} diff --git a/ch07/NumberGuessingGame.java b/ch07/NumberGuessingGame.java new file mode 100644 index 0000000..9c33dd4 --- /dev/null +++ b/ch07/NumberGuessingGame.java @@ -0,0 +1,75 @@ + +import java.util.Random; +import java.util.Scanner; + +public class NumberGuessingGame +{ + public static void main(String[] args) + { + play(); + } + + private static void play() + { + System.out.println("Welcome to the ultimate number guessing game!"); + + + int randomNumber = getRandomNumber(); + //System.out.println("My secret number is: " + randomNumber); //USE AS A TEST FOR RANDOM + + Scanner in = new Scanner(System.in); + int guess; + + + do { + System.out.println("Enter your guess: "); + guess = in.nextInt(); + + if (isCold(guess, randomNumber)) + { + System.out.println("Cold!"); + } + else if (isHot(guess, randomNumber)) + { + System.out.println("Hot!"); + } + } + while (guess != randomNumber); + { + + + System.out.println("You got it!"); + } + } + + private static boolean isCold(int guess, int randomNumber) + { + final int COLD_DIFFERENCE = 10; + boolean cold = false; + if (guess > randomNumber + COLD_DIFFERENCE || guess < randomNumber - COLD_DIFFERENCE) + { + cold = true; + } + return cold; + } + private static boolean isHot(int guess, int randomNumber) + { + final int HOT_DIFFERENCE = 10; + boolean hot = false; + if (guess <= randomNumber + HOT_DIFFERENCE && guess != randomNumber || guess <= randomNumber - HOT_DIFFERENCE && guess != randomNumber) + { + hot = true; + } + return hot; + } + private static int getRandomNumber() + { + final int MAX_NUMBER = 100; + + Random random = new Random(); + //int randomNumber = random.nextInt(MAX_NUMBER) + 1; + //return randomNumber; + return random.nextInt(MAX_NUMBER) + 1; + + } +} \ No newline at end of file diff --git a/ch08/.idea/vcs.xml b/ch08/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch08/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch08/ArrayExercises.java b/ch08/ArrayExercises.java new file mode 100644 index 0000000..24a9189 --- /dev/null +++ b/ch08/ArrayExercises.java @@ -0,0 +1,75 @@ +public class ArrayExercises +{ + public static void main(String[] args) + { + int values[] = {1,2,3,4}; + printArray(values); + + int eightB[] = {10, 2, 3, 5}; + arrayTotal(eightB); + + int eightC[] = {10,40,15,2}; + arrayMax(eightC); + + int eightD[] = {1, 5, 9, 50, 10}; + arrayMaxIndex(eightD); + + double eightE[] = {1.0, 1.0, 5.0, 6.0, 3.0}; + arrayAverage(eightE); + } + + private static void printArray(int[] values) + { + for(int value : values) + { + System.out.print(value); + } + } + + private static void arrayTotal(int[] values) + { + int total = 0; + for(int i = 0; i < values.length; i++) + { + total += values[i]; + } + System.out.println("\nThe sum of elements: " + total); + } + + private static void arrayMax(int[] values) + { + int max = values[0]; + + for(int i = 0; i < values.length; i++) + { + if(values[i] > max) + { + max = values[i]; + } + } + System.out.println("Max number is: " + max); + } + private static void arrayMaxIndex(int[] values) + { + int maxValueIndex = 0; + + for(int i = 0; i < values.length; i++) + { + if(values[i] > values[maxValueIndex]) + { + maxValueIndex = i; + } + } + System.out.println("Index " + maxValueIndex + " holds the highest value element."); + } + + private static void arrayAverage(double[] values) + { + double average = 0.0; + for(double i = 0.0; i < values.length; i++) + { + average += values[(int) i] / values.length; + } + System.out.println("arrayAverage is: " + average); + } +} diff --git a/ch08/HowManyPets.java b/ch08/HowManyPets.java new file mode 100644 index 0000000..d8aacc9 --- /dev/null +++ b/ch08/HowManyPets.java @@ -0,0 +1,47 @@ +import java.util.Scanner; + +public class HowManyPets +{ + public static void main(String[] args) + { + numberOfPets(); + System.out.println(); + } + + + private static void numberOfPets() + { + Scanner in = new Scanner(System.in); + + System.out.println("How many pets?"); + int numberOfPets = in.nextInt(); + in.nextLine(); + + + int[] numPets = new int[numberOfPets]; + //System.out.println("Length is: " + numPets.length); + + int currentPet = 0; + + String[] petNames = new String[numPets.length]; + + while (currentPet < numPets.length) + { + System.out.println("Enter name of pet #" + (currentPet + 1)); + petNames[currentPet] = in.nextLine(); + currentPet++; + + } + getNames(petNames); + } + private static void getNames(String[] names) + { + int index = 0; + while(index < names.length) + { + System.out.println("Animal #" + (index + 1) + ": " + names[index]); + index++; + } + } + +} diff --git a/ch08/IntergalacticVending.java b/ch08/IntergalacticVending.java new file mode 100644 index 0000000..9adf97d --- /dev/null +++ b/ch08/IntergalacticVending.java @@ -0,0 +1,84 @@ + +import java.util.Scanner; + +public class IntergalacticVending +{ + + private static void customerChoice() + { + final int TOTAL_OPTIONS_AVAILABLE = 5; + final int SECRET_NUMBER = 99; + + Scanner in = new Scanner(System.in); + int choice = 0; + + int[] mealChoice = new int[TOTAL_OPTIONS_AVAILABLE]; + + + + + while (choice != SECRET_NUMBER && choice <= 4) + { + System.out.print("Enter Choice: "); + choice = in.nextInt(); + + if (choice == 0) + { + mealChoice[0]++; + printArray(mealChoice); + } + if (choice == 1) + { + mealChoice[1]++; + printArray(mealChoice); + } + else if (choice == 2) + { + mealChoice[2]++; + printArray(mealChoice); + } + else if (choice == 3) + { + mealChoice[3]++; + printArray(mealChoice); + + } + else if (choice == 4) + { + mealChoice[4]++; + printArray(mealChoice); + + } + else if (choice == SECRET_NUMBER) + { + System.out.println("Goodbye"); + printArray(mealChoice); + } + + } + + } + + private static void printArray(int[] meals) + { + System.out.print("{" + meals[0]); + for (int i = 1; i < meals.length; i++) + { + System.out.print(", " + meals[i]); + } + System.out.println("}"); + } + + public static void main(String[] args) + { + System.out.println("Intergalactic Vending Machine"); + System.out.println(); + + customerChoice(); + } + +} + + + + diff --git a/ch08/PetAge.java b/ch08/PetAge.java new file mode 100644 index 0000000..2033116 --- /dev/null +++ b/ch08/PetAge.java @@ -0,0 +1,45 @@ +import java.util.Scanner; + +public class PetAge +{ + public static void main(String[] args) + { + replayPetAges(); + } + + private static void replayPetAges() + { + Scanner in = new Scanner(System.in); + + System.out.println("How many pets do you have? "); + int numberOfPets = in.nextInt(); + + int[] petAge = new int[numberOfPets]; + System.out.println("Length is: " + petAge.length); + + int currentPet = 0; + + while (currentPet < petAge.length) + { + System.out.println("Enter age of pet " + (currentPet + 1)); + petAge[currentPet] = in.nextInt(); + currentPet++; + } + + // currentPet = 0; + + printArray(petAge, "Age "); + } + + private static void printArray(int[] values, String prefix) + { + System.out.println("Welcome to printArray"); + + int index = 0; + while (index < values.length) { + System.out.println(prefix + (index + 1) + " is " + values[index]); + index++; + } + } +} + diff --git a/ch09/.idea/vcs.xml b/ch09/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch09/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch09/MyStringExercises.java b/ch09/MyStringExercises.java new file mode 100644 index 0000000..7f27dda --- /dev/null +++ b/ch09/MyStringExercises.java @@ -0,0 +1,105 @@ +public class MyStringExercises +{ + + public static void main(String[] args){ + + printFirstCharacter("Hello"); + printFirstCharacter("Goodbye"); + + printLastCharacter("Hello"); + printLastCharacter("Goodbye"); + + printCharacters("Hello"); + printCharacters("Goodbye"); + + + printAllButFirstThree("Hello"); + printAllButFirstThree("Goodbye"); + + printFirstThree("Hello"); + printFirstThree("Goodbye"); + + printPhoneNumber("501-555-0100"); + printPhoneNumber("501-867-5309"); + + findFirstE("Hello"); + findFirstE("Goodbye"); + + isFinn("Finn"); + isFinn("Jake"); + + + } + + private static void printFirstCharacter(String first) + { + System.out.println("Print the first character in " + first); + System.out.println(first.charAt(0)); + } + + private static void printLastCharacter(String last) + { + int length = last.length(); + System.out.println("Print the last character in " + last); + System.out.println(last.charAt(length - 1)); + } + + private static void printCharacters(String allCharacters) + { + System.out.println("Print each character and position"); + + for (int i = 0; i < allCharacters.length(); i++) + { + char letter = allCharacters.charAt(i); + System.out.println("The letter " + letter + " is at the index " + i); + } + } + + private static void printAllButFirstThree(String after) + { + System.out.println(after.substring(3)); + } + + private static void printFirstThree(String before) + { + System.out.println(before.substring(0,3)); + } + + private static void printPhoneNumber(String phoneNumber) + { + + //Best to create variables instead of all in print line. + //String areaCode = phoneNumber.substring(0,3); + //System.out.println("Area Code: " + areaCode); + + System.out.println("Area Code: " + phoneNumber.substring(0, 3)); + System.out.println("Exchange: " + phoneNumber.substring(4, 7)); + System.out.println("Line Number: " + phoneNumber.substring(8, 12)); + } + + private static void findFirstE(String firstE) + { + int index = firstE.indexOf('e'); + + System.out.println(index); + //System.out.println(index); + } + + private static boolean isFinn(String hasFinn) + { + boolean finn = false; + + //if (name.equals("Finn")) + if (hasFinn.contentEquals("Finn")) + { + finn = true; + System.out.println(finn); + } + else + { + finn = false; + System.out.println(finn); + } + return finn; + } +} diff --git a/ch11/.idea/vcs.xml b/ch11/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch11/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch11/Date.java b/ch11/Date.java new file mode 100644 index 0000000..071a89a --- /dev/null +++ b/ch11/Date.java @@ -0,0 +1,13 @@ +public class Date +{ + private int day; + private int month; + private int year; + + public Date(int day, int month, int year) + { + this.day = 31; + this.month = 1; + this.year = 2018; + } +} diff --git a/ch11/Player.java b/ch11/Player.java new file mode 100644 index 0000000..eac7e88 --- /dev/null +++ b/ch11/Player.java @@ -0,0 +1,21 @@ +public class Player +{ + private String name; + private int score; + + public Player(String name) + { + name = "Player 1"; + score = 0; + } + + public void increaseScore() + { + score++; + } + + public void resetScore() + { + score = 0; + } +} diff --git a/ch11/Product.java b/ch11/Product.java new file mode 100644 index 0000000..d1cac1d --- /dev/null +++ b/ch11/Product.java @@ -0,0 +1,44 @@ +public class Product +{ + private String name; + private int quantityInStock; + + public Product(String name) + { + name = "Honeysuckle-Strawberry Jelly"; + } + + public String getName() + { + return name; + } + + public int getQuantityInStock() + { + return quantityInStock; + } + + public void stock() + { + int stock = 0; + stock = quantityInStock++; + } + + public void sell() + { + int sell = 0; + sell = quantityInStock --; + } + + public void recordLoss() + { + int loss = 0; + loss = quantityInStock --; + } + + public String toString() + { + return getName() + ", Quantity in Stock " + getQuantityInStock(); + } + +} diff --git a/ch11/Rectangle.java b/ch11/Rectangle.java new file mode 100644 index 0000000..e9351ca --- /dev/null +++ b/ch11/Rectangle.java @@ -0,0 +1,64 @@ +import java.util.Objects; + +public class Rectangle +{ + private int height; + private int width; + + public Rectangle() + { + height = 1; + width = 1; + } + + public Rectangle(int height, int width) + { + this.height = height; + this.width = width; + } + + public int getHeight() + { + return height; + } + + public int getWidth() + { + return width; + } + //had to get help with this step 11-d #6 + public Rectangle(Rectangle rectangle) + { + this.height = rectangle.getHeight(); + this.width = rectangle.getWidth(); + } + public int getArea() + { + int area = getHeight() * getHeight(); + + return area; + } + + public String toString() + { + return "Height: " + getHeight() + ", Width: " + getWidth() + ", Area: " + getArea(); + + } + + @Override + public boolean equals(Object o) + { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Rectangle rectangle = (Rectangle) o; + return height == rectangle.height && + width == rectangle.width; + } + + @Override + public int hashCode() + { + + return Objects.hash(height, width); + } +} diff --git a/ch11/TestVendingMachine.java b/ch11/TestVendingMachine.java new file mode 100644 index 0000000..2a9e25d --- /dev/null +++ b/ch11/TestVendingMachine.java @@ -0,0 +1,58 @@ +public class TestVendingMachine +{ + public static void main(String[] args ) + { + /*VendingMachine vendingMachine = new VendingMachine(); + System.out.println(vendingMachine); + + vendingMachine.addQuarter(); + System.out.println(vendingMachine); + + vendingMachine.addQuarter(); + System.out.println(vendingMachine); + + vendingMachine.addDime(); + System.out.println(vendingMachine); + + vendingMachine.addNickel(); + System.out.println(vendingMachine); + + System.out.println("Buying an item"); + vendingMachine.buyItem(); + System.out.println(vendingMachine); + + int changeReturned = vendingMachine.extractChange(); + System.out.println("I got " + changeReturned + " cents back!"); + System.out.println(vendingMachine);*/ + + /*Product product1 = new Product(); + System.out.println(product1); + + product1.stock(); + product1.stock(); + product1.stock(); + product1.stock(); + product1.stock(); + product1.stock(); + System.out.println(product1); + + product1.sell(); + System.out.println(product1); + + product1.recordLoss(); + System.out.println(product1);*/ + + Rectangle rectangle = new Rectangle(); + System.out.println(rectangle); + + rectangle.getArea(); + System.out.println(rectangle); + + rectangle.hashCode(); + System.out.println(rectangle); + + + Rectangle rectangle1 = new Rectangle(rectangle); + System.out.println(rectangle1); + } +} diff --git a/ch11/VendingMachine.java b/ch11/VendingMachine.java new file mode 100644 index 0000000..3839696 --- /dev/null +++ b/ch11/VendingMachine.java @@ -0,0 +1,110 @@ +public class VendingMachine +{ + private int quarters; + private int dimes; + private int nickels; + + //constructor--initializes VendingMachine + public VendingMachine() + { + quarters = 0; + dimes = 0; + nickels = 0; + } + + public void addQuarter() + { + quarters++; + } + public void addDime() + { + dimes++; + } + public void addNickel() + { + nickels++; + } + + public int getCentsEntered() + { + int cents = 0; + + cents += 25 * quarters; + cents += 10 * dimes; + cents += 5 * nickels; + + return cents; + } + public boolean buyItem() + { + final int ITEM_COST = 50; + boolean itemBought; + if(getCentsEntered() >= ITEM_COST) + { + spendAmount(ITEM_COST); + itemBought = true; + } + else + { + itemBought = false; + } + return itemBought; + } + private void spendAmount(int amount) + { + int remainingAmount = amount; + int maxQuarters = remainingAmount / 25; + + if (quarters >= maxQuarters) + { + remainingAmount-= maxQuarters * 25; + quarters -= maxQuarters; + } + else + { + remainingAmount -= quarters * 25; + quarters = 0; + } + + int maxDimes = remainingAmount / 10; + + if (dimes >= maxDimes) + { + remainingAmount-= maxDimes * 10; + + dimes -= maxDimes; + } + else + { + remainingAmount -= dimes * 10; + dimes = 0; + } + + int maxNickels = remainingAmount / 5; + + if (nickels >= maxNickels) + { + remainingAmount-= maxNickels * 5; + + nickels -= maxDimes; + } + else + { + nickels = 0; + } + } + public int extractChange() + { + int change = getCentsEntered(); + + quarters = 0; + dimes = 0; + nickels = 0; + + return change; + } + public String toString() + { + return "Vending Machine " + quarters + ":" + dimes + ":" + nickels + " = " + getCentsEntered(); + } +} \ No newline at end of file