diff --git a/ArrayListFun/src/com/company/ListDemo.java b/ArrayListFun/src/com/company/ListDemo.java new file mode 100644 index 0000000..1049c84 --- /dev/null +++ b/ArrayListFun/src/com/company/ListDemo.java @@ -0,0 +1,69 @@ +package com.company; + +import java.util.ArrayList; + +public class ListDemo +{ + public static void main(String[] args) + { + ListDemo listDemo = new ListDemo(); + + listDemo.execute(); + } + + private void execute() + { + ArrayList myList = new ArrayList<>(); + + myList.add("Table"); + myList.add("Chair"); + myList.add("Sofa"); + + printList(myList); + + myList.add(myList.size(), "Recliner"); + + printList(myList); + + myList.add(0, "Loveseat"); + + printList(myList); + + myList.add(2, "Ottoman"); + + printList(myList); + + myList.remove(myList.size() - 1); + + printList(myList); + + myList.remove(0); + + printList(myList); + + myList.remove(1); + + printList(myList); + + + } + + private void printList(ArrayList list) + { + String element; + + for (int i = 0; i < list.size(); i++) + { + element = list.get(i); + if (i != (list.size() - 1)) + { + System.out.print("" + element + " : "); + } + else if (i == (list.size() - 1)) + { + System.out.println("" + element); + } + } + + } +} diff --git a/ArrayListFun/src/com/company/ShoppingList.java b/ArrayListFun/src/com/company/ShoppingList.java new file mode 100644 index 0000000..d7c4085 --- /dev/null +++ b/ArrayListFun/src/com/company/ShoppingList.java @@ -0,0 +1,136 @@ +package com.company; + +import java.util.ArrayList; +import java.util.Scanner; + +public class ShoppingList +{ + private Scanner scanner = new Scanner(System.in); + + + public static void main(String[] args) + { + ShoppingList shoppingList = new ShoppingList(); + + shoppingList.run(); + } + + private void run() + { + ArrayList myList = new ArrayList<>(); + + String add = "Add"; + String remove = "Remove"; + String print = "Print"; + String clear = "Clear"; + String exit = "Exit"; + + String command; + + do{ + if (myList.size() == 0) + { + System.out.println("There aren't any items in this list. Please add items beginning at position 0."); + } + + + do{ + System.out.println("Enter one of the following commands: Add, Remove, Print, Clear, or Exit."); + + command = scanner.nextLine(); + + if (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && !command.equalsIgnoreCase(exit)) + { + System.out.println("Oops! Please enter a valid command."); + } + } while (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && !command.equalsIgnoreCase(exit)); + + + if (command.equalsIgnoreCase(add)) + { + add(myList); + } + else if (command.equalsIgnoreCase(remove)) + { + remove(myList); + } + else if (command.equalsIgnoreCase(print)) + { + print(myList); + } + else if (command.equalsIgnoreCase(clear)) + { + clear(myList); + } + + } while (!command.equalsIgnoreCase("Exit")); + + System.out.println("You have finished editing your list."); + print(myList); + } + + private void add(ArrayList list) + { + System.out.print("Enter the position where you would like to add to the list: "); + + int number = scanner.nextInt(); + scanner.nextLine(); + + System.out.println("What would you like to add to the list at position " + number + "?"); + + String addToList = scanner.nextLine(); + + list.add(number,addToList); + + print(list); + } + + private void remove(ArrayList list) + { + System.out.print("Enter the position of the item you would like to remove from the list: "); + + int number = scanner.nextInt(); + scanner.nextLine(); + + list.remove(number); + + print(list); + } + + private void print(ArrayList list) + { + String element; + + for (int i = 0; i < list.size(); i++) + { + element = list.get(i); + + if (i == 0 && list.size() == 1) + { + System.out.println("Item in position number " + i + ": " + element + "."); + } + else if (i == 0) + { + System.out.print("Item in position number " + i + ": " + element + ", "); + } + else if (i != (list.size() - 1)) + { + System.out.print("item in position number " + i + ": " + element + ", "); + } + else if (i == (list.size() - 1)) + { + System.out.println("item in position number " + i + ": " + element); + } + } + + } + + private void clear(ArrayList list) + { + list.clear(); + } + + +} diff --git a/ArrayListFun/src/com/company/ShoppingListSilver.java b/ArrayListFun/src/com/company/ShoppingListSilver.java new file mode 100644 index 0000000..07313d6 --- /dev/null +++ b/ArrayListFun/src/com/company/ShoppingListSilver.java @@ -0,0 +1,165 @@ +package com.company; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; + +public class ShoppingListSilver +{ + private Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) + { + ShoppingListSilver shoppingListSilver = new ShoppingListSilver(); + + shoppingListSilver.run(); + } + + private void run() + { + ArrayList myList = new ArrayList<>(); + + String add = "Add"; + String remove = "Remove"; + String print = "Print"; + String clear = "Clear"; + String exit = "Exit"; + String sort = "Sort"; + String find = "Find"; + + String command; + + do{ + if (myList.size() == 0) + { + System.out.println("There aren't any items in this list. Please add items beginning at position 0."); + } + + do{ + System.out.println("Enter one of the following commands: Add, Remove, Print, Clear, Sort, Find, or Exit."); + + command = scanner.nextLine(); + + if (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && + !command.equalsIgnoreCase(exit) && !command.equalsIgnoreCase(sort) && !command.equalsIgnoreCase(find)) + { + System.out.println("Oops! Please enter a valid command."); + } + } while (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && + !command.equalsIgnoreCase(exit)&& !command.equalsIgnoreCase(sort) && !command.equalsIgnoreCase(find)); + + if (command.equalsIgnoreCase(add)) + { + add(myList); + } + else if (command.equalsIgnoreCase(remove)) + { + remove(myList); + } + else if (command.equalsIgnoreCase(print)) + { + print(myList); + } + else if (command.equalsIgnoreCase(clear)) + { + clear(myList); + } + else if (command.equalsIgnoreCase(sort)) + { + sort(myList); + } + else if(command.equalsIgnoreCase(find)) + { + find(myList); + } + + } while (!command.equalsIgnoreCase("Exit")); + + System.out.println("You have finished editing your list."); + print(myList); + } + + private void add(ArrayList list) + { + System.out.println("What would you like to add to the list?"); + + String addToList = scanner.nextLine(); + + list.add(addToList); + + print(list); + } + + private void remove(ArrayList list) + { + System.out.print("Enter the position of the item you would like to remove from the list: "); + + int number = scanner.nextInt(); + scanner.nextLine(); + + list.remove(number); + + print(list); + } + + private void print(ArrayList list) + { + String element; + + for (int i = 0; i < list.size(); i++) + { + element = list.get(i); + + if (i == 0 && list.size() == 1) + { + System.out.println("Item in position number " + i + ": " + element + "."); + } + else if (i == 0) + { + System.out.print("Item in position number " + i + ": " + element + ", "); + } + else if (i != (list.size() - 1)) + { + System.out.print("item in position number " + i + ": " + element + ", "); + } + else if (i == (list.size() - 1)) + { + System.out.println("item in position number " + i + ": " + element + "."); + } + } + + } + + private void clear(ArrayList list) + { + list.clear(); + + print(list); + } + + private void sort(ArrayList list) + { + Collections.sort(list); + + print(list); + } + + private void find(ArrayList list) + { + System.out.println("Which item on the list are you looking for?"); + + String searchFor = scanner.nextLine(); + + if (list.contains(searchFor)) + { + System.out.println("Found it! It is in position " + list.indexOf(searchFor)); + } + else + { + System.out.println("No such item!"); + } + } +} diff --git a/ArrayListFun/src/com/company/SimpleList.java b/ArrayListFun/src/com/company/SimpleList.java new file mode 100644 index 0000000..8ad1192 --- /dev/null +++ b/ArrayListFun/src/com/company/SimpleList.java @@ -0,0 +1,38 @@ +package com.company; + +import java.util.ArrayList; + +public class SimpleList +{ + public static void main(String[] args) + { + SimpleList simpleList = new SimpleList(); + + simpleList.demo(); + } + + private void demo() + { + ArrayList myList = new ArrayList<>(); + + myList.add("Red"); + myList.add("Blue"); + myList.add("Green"); + myList.add("Purple"); + myList.add("Orange"); + + for (int i = 0; i < myList.size(); i++) + { + System.out.println(myList.get(i)); + } + + for (String name : myList) + { + System.out.println(name); + } + + + } + + +} diff --git a/Games/src/FinishLineGame.java b/Games/src/FinishLineGame.java new file mode 100644 index 0000000..dad5608 --- /dev/null +++ b/Games/src/FinishLineGame.java @@ -0,0 +1,113 @@ +import java.util.Random; + +public class FinishLineGame +{ + public static void main(String[] args) + { + final int GAME_START = 1; + int place1 = GAME_START; + int place2 = GAME_START; + + System.out.println("Let the game begin!"); + System.out.println(); + + //The game ends when either player reaches 10 + final int GAME_OVER = 10; + + //Turn for both players + while (place1 < GAME_OVER && place2 < GAME_OVER) + { + //Player 1 rolls + int next1 = place1 + 1; + + int player1Die1 = die1(); + int player1Die2 = die2(); + int rollTotal1 = player1Die1 + player1Die2; + + System.out.println("Player 1 rolled a " + player1Die1 + " and " + player1Die2 + " for a total of " + + rollTotal1 + " ."); + + if (player1Die1 == next1 || player1Die2 == next1 || rollTotal1 == next1) + { + System.out.println("Player 1 is moving up!"); + + place1++; + } + + //Player 2 rolls + int next2 = place2 + 1; + + int player2Die1 = die1(); + int player2Die2 = die2(); + int rollTotal2 = player2Die1 + player2Die2; + + System.out.println("Player 2 rolled a " + player2Die1 + " and " + player2Die2 + " for a total of " + + rollTotal2 + " ."); + + if (player2Die1 == next2 || player2Die2 == next2 || rollTotal2 == next2) + { + System.out.println("Player 2 is moving up!"); + + place2++; + } + + } + + int player1FinalScore = place1; + int player2FinalScore = place2; + + System.out.println(); + + if (winner1(player1FinalScore,player2FinalScore)) + { + System.out.println("Player 1 is the winner!"); + } + else if (winner2(player1FinalScore,player2FinalScore)) + { + System.out.println("Player 2 is the winner"); + } + else if (tieGame(player1FinalScore, player2FinalScore)) + { + System.out.println("Tie game!"); + } + else { + System.out.println("Oops, something went wrong in the game winning announcement if / else if ."); + } + + } + + private static int die1() + { + final int MAX_NUMBER = 6; + + Random random = new Random(); + int randomNumber = random.nextInt(MAX_NUMBER) + 1; + + return randomNumber; + } + + private static int die2() + { + final int MAX_NUMBER = 6; + + Random random = new Random(); + int randomNumber = random.nextInt(MAX_NUMBER) + 1; + + return randomNumber; + } + + private static boolean winner1(int player1FinalScore, int player2FinalScore) + { + return player1FinalScore > player2FinalScore; + } + + private static boolean winner2(int player1FinalScore, int player2FinalScore) + { + return player2FinalScore > player1FinalScore; + } + + private static boolean tieGame(int player1FinalScore, int player2FinalScore) + { + return player1FinalScore == player2FinalScore; + } +} diff --git a/Maps/src/com/company/PetHotel.java b/Maps/src/com/company/PetHotel.java new file mode 100644 index 0000000..b47e78c --- /dev/null +++ b/Maps/src/com/company/PetHotel.java @@ -0,0 +1,287 @@ +package com.company; + +import java.util.Map; +import java.util.Scanner; +import java.util.Set; +import java.util.TreeMap; + +public class PetHotel +{ + TreeMap hotelRooms; + private Scanner scanner; + + public PetHotel() + { + hotelRooms = new TreeMap<>(); + scanner = new Scanner(System.in); + } + + public static void main(String[] args) + { + PetHotel petHotel = new PetHotel(); + + petHotel.run(); + } + + private void run() + { + String command; + String[] words; + + String petName; + int roomNumber; + + do + { + int fromRoomNumber; + int toRoomNumber; + + if (hotelRooms.isEmpty()) + { + System.out.println("There aren't any pets here right now."); + } + + /* Valid commands: + Checkin ; + Checkout ; + Move ; + Swap ; + MoveOver (moves all pets to roomNumber + 1, 109 goes to 100) + Occupancy (list occupants); + CloseForSeason (clear map); + Exit (ends program) */ + + System.out.print("Enter command: "); + String inputLine = scanner.nextLine(); + words = inputLine.split(" "); + command = words[0]; + + if (command.equalsIgnoreCase("CheckIn")) + { + petName = words[1]; + roomNumber = Integer.parseInt(words[2]); + + checkIn(petName, roomNumber); + } + else if (command.equalsIgnoreCase("CheckOut")) + { + petName = words[1]; + roomNumber = Integer.parseInt(words[2]); + + checkOut(petName, roomNumber); + } + else if (command.equalsIgnoreCase("Move")) + { + petName = words[1]; + fromRoomNumber = Integer.parseInt(words[2]); + toRoomNumber = Integer.parseInt(words[3]); + + move(petName, fromRoomNumber, toRoomNumber); + } + else if (command.equalsIgnoreCase("MoveOver")) + { + moveOver(); + } + else if (command.equalsIgnoreCase("Occupancy")) + { + getOccupancy(); + } + else if (command.equalsIgnoreCase("CloseForSeason")) + { + closeForSeason(); + } + else if (command.equalsIgnoreCase("Swap")) + { + int firstRoomNumber = Integer.parseInt(words[1]); + int secondRoomNumber = Integer.parseInt(words[2]); + + swap(firstRoomNumber, secondRoomNumber); + } + else if (command.equalsIgnoreCase("Exit")) + { + System.out.println("You have ended the program!"); + } + else + { + System.out.println("Sorry, I didn't understand that. Please enter a valid command."); + } + } while (!command.equalsIgnoreCase("Exit")); + + } + + private boolean roomNotReal(int roomNumber) + { + return !(roomNumber >= 100 && roomNumber <= 109); + } + + private boolean roomTaken(int roomNumber) + { + return !(hotelRooms.get(roomNumber) == null); + } + + private boolean wrongPet(String petName, int roomNumber) + { + return !(hotelRooms.get(roomNumber).equalsIgnoreCase(petName)); + } + + private void checkIn(String petName, int roomNumber) + { + if (roomNotReal(roomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); + } + else if (roomTaken(roomNumber)) + { + System.out.println("Sorry, this room already has an occupant. Please select a different one."); + } + else + { + hotelRooms.put(roomNumber, petName); + System.out.println("" + petName + " checked into room " + roomNumber + "."); + } + } + + private void checkOut(String petName, int roomNumber) + { + if (roomNotReal(roomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); + } + else if (!roomTaken(roomNumber)) + { + System.out.println("Your pet is not in room number " + roomNumber + "."); + } + else + { + if (wrongPet(petName, roomNumber)) + { + System.out.println("This is not the pet you're looking for."); + } + else if (roomTaken(roomNumber)) + { + hotelRooms.remove(roomNumber, petName); + System.out.println("" + petName + " has been checked out of room number " + roomNumber + "."); + } + } + } + + private void move(String petName, int fromRoomNumber, int toRoomNumber) + { + if (roomNotReal(fromRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. " + petName + " is currently in a room that exists. Please try again."); + } + else if (roomNotReal(toRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. We cannot move " + petName + " to a room that does not exist."); + } + else if (!roomTaken(fromRoomNumber)) + { + System.out.println("Your pet is not in room number " + fromRoomNumber + "."); + } + else + { + if (roomTaken(toRoomNumber)) + { + System.out.println("Sorry, room number " + toRoomNumber + " already has an occupant. Please select a different one."); + } + else if (wrongPet(petName, fromRoomNumber)) + { + System.out.println("This is not the pet you're looking for."); + } + else + { + hotelRooms.remove(fromRoomNumber); + hotelRooms.put(toRoomNumber, petName); + System.out.println("" + petName + " has been moved from room number " + fromRoomNumber + " to room number " + toRoomNumber + "."); + } + } + } + + private void swap(int firstRoomNumber, int secondRoomNumber) + { + if (roomNotReal(firstRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. The pet you are looking for is currently in a room that exists. Please try again."); + } + else if (roomNotReal(secondRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. We cannot move the pet you are looking for to a room that does not exist."); + } + else if (!roomTaken(firstRoomNumber)) + { + System.out.println("The pet you are looking for is not in room number " + firstRoomNumber + "."); + } + else + { + if (!roomTaken(secondRoomNumber)) + { + System.out.println("Sorry, there aren't any pets in this room to swap with!"); + } + else + { + String petInFirstRoom = hotelRooms.get(firstRoomNumber); + String petInSecondRoom = hotelRooms.get(secondRoomNumber); + + hotelRooms.put(secondRoomNumber, petInFirstRoom); + hotelRooms.put(firstRoomNumber, petInSecondRoom); + System.out.println("" + petInFirstRoom + " has been moved to room number " + secondRoomNumber + + ", and " + petInSecondRoom + " has been moved to room number " + firstRoomNumber + "."); + } + } + } + + private void moveOver() + { + String firstPet = null; + + if (hotelRooms.get(109) != null) + { + firstPet = hotelRooms.get(109); + hotelRooms.remove(109); + } + + for (int i = 108; i >= 100; i--) + { + String currentPet = null; + + if (hotelRooms.get(i) != null) + { + currentPet = hotelRooms.get(i); + } + + if (currentPet != null) + { + hotelRooms.put(i + 1, currentPet); + hotelRooms.remove(i); + } + } + + if (firstPet != null) + { + hotelRooms.put(100, firstPet); + } + } + + private void getOccupancy() + { + Set> entries = hotelRooms.entrySet(); + + for (Map.Entry entry : entries) + { + int roomNumber = entry.getKey(); + String petName = entry.getValue(); + + System.out.println("" + petName + " is in room number " + roomNumber + "."); + } + + //Lambda version: + // hotelRooms.forEach((roomNumber,petName) -> System.out.println("" + petName + " is in room number " + roomNumber + ".")); + } + + private void closeForSeason() + { + hotelRooms.clear(); + System.out.println("We are closed for the rest of the season. Thank you for your patronage!"); + } +} diff --git a/Maps/src/com/company/SimpleMap.java b/Maps/src/com/company/SimpleMap.java new file mode 100644 index 0000000..4090562 --- /dev/null +++ b/Maps/src/com/company/SimpleMap.java @@ -0,0 +1,24 @@ +package com.company; + +import java.util.HashMap; + +public class SimpleMap +{ + public static void main(String[] args) + { + SimpleMap simpleMap = new SimpleMap(); + + simpleMap.demo(); + } + + private void demo() + { + HashMap hashMap = new HashMap<>(); + + hashMap.put("USA","United States"); + hashMap.put("MEX","Mexico"); + hashMap.put("CAN", "Canada"); + + System.out.println("The value stored with USA is: " + hashMap.get("USA")); + } +} diff --git a/Maps/src/com/company/TimeZoneDemo.java b/Maps/src/com/company/TimeZoneDemo.java new file mode 100644 index 0000000..68ac56f --- /dev/null +++ b/Maps/src/com/company/TimeZoneDemo.java @@ -0,0 +1,52 @@ +package com.company; + +import java.util.HashMap; + +public class TimeZoneDemo +{ + HashMap hashMap = new HashMap<>(); + + public static void main(String[] args) + { + TimeZoneDemo timeZoneDemo = new TimeZoneDemo(); + + timeZoneDemo.demo(); + } + + private void demo() + { + hashMap.put("EST",-5); + hashMap.put("CST",-6); + hashMap.put("MST",-7); + hashMap.put("PST",-8); + hashMap.put("GMT",0); + + System.out.println(getTimeDiff("PST","EST")); + System.out.println(getTimeDiff("EST","PST")); + + } + + private double getTimeDiff(String timeZone1, String timeZone2) + { + double timeDiff = 0; + + double tz1 = hashMap.get(timeZone1); + double tz2 = hashMap.get(timeZone2); + + //Check to make sure the result would be the same regardless of the order the keys are entered in + if (tz1 < tz2) + { + timeDiff = tz1 - tz2; + } + else if (tz2 < tz1) + { + timeDiff = tz2 - tz1; + } + else + { + System.out.println("Oops."); + } + + return timeDiff; + } +} 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..1ec0ce2 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,32 @@ +public class Date +{ + public static void main(String args[]) + { + String day = "Thursday"; + int date = 18; + String month = "January"; + int year = 2018; + + System.out.println("American format:"); + + System.out.print(day); + System.out.print(", "); + System.out.print(month); + System.out.print(" "); + System.out.print(date); + System.out.print(", "); + System.out.println(year); + + System.out.println("European format:"); + + System.out.print(day); + System.out.print(" "); + System.out.print(date); + System.out.print(" "); + System.out.print(month); + System.out.print(" "); + System.out.println(year); + + } + +} diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..57d8d7c --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,11 @@ +public class DoubleByZero +{ + public static void main(String[] args) + { + double a = 42.0; + double b = 0.0; + double result = a / b; + + System.out.println(result); + } +} diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..db4b750 --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,11 @@ +public class IntByZero +{ + public static void main(String[] args) + { + int a = 42; + int b = 0; + int result = a / b; + + System.out.println(result); + } +} diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..530f8f3 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,22 @@ +public class IntExtremes +{ + public static void main(String args[]) + { + int postiveInt = 2147483647; + + System.out.println(postiveInt); + + postiveInt = 2147483647 + 1; + + System.out.println(postiveInt); + + int negativeInt = -2147483648; + + System.out.println(negativeInt); + + negativeInt = -2147483648 -1; + + System.out.println(negativeInt); + + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..3094f14 --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,28 @@ +public class Time +{ + public static void main(String args[]) + { + int hour = 14; + int minute = 34; + int second = 30; + + System.out.print("Number of seconds since midnight: "); + System.out.println((hour * 60 * 60) + (minute * 60) + second); + + System.out.print("Number of seconds remaining in the day: "); + System.out.println((24 * 60 * 60) - ((hour * 60 * 60) + (minute * 60) + second)); + + System.out.print("Percent of the day that has passed: "); + System.out.println(((hour * 60 * 60) + (minute * 60) + second) * 100 / (24 * 60 * 60)); + + int hour2 = 14; + int minute2 = 48; + int second2 = 17; + + System.out.print("Time elapsed since beginning this exercise: "); + System.out.print(((hour2 * 60 * 60) + (minute2 * 60) + second2) - ((hour * 60 * 60) + (minute * 60) + second)); + System.out.println(" seconds."); + + } + +} diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..b86c7c5 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,28 @@ +public class Withdrawal +{ + public static void main(String[] args) + { + int withhdrawal = 137; + + int twenty = withhdrawal / 20; + int remainder1 = withhdrawal % 20; + + System.out.println(twenty + " $20 bills"); + + int ten = remainder1 / 10; + int remainder2 = remainder1 % 10; + + System.out.println(ten + " $10 bills"); + + int five = remainder2 / 5; + int remainder3 = remainder2 % 5; + + System.out.println(five + " $5 bills"); + + int one = remainder3; + + System.out.println(one + " $1 bills"); + + + } +} diff --git a/ch03/ConvertTemp.java b/ch03/ConvertTemp.java new file mode 100644 index 0000000..75122ae --- /dev/null +++ b/ch03/ConvertTemp.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class ConvertTemp +{ + public static void main(String[] args) { + double degC; + double degF; + + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Exactly how many degrees Celsius? "); + degC = in.nextDouble(); + + final double DEGC_TO_DEGF = (degC * 9 / 5) + 32; + + // convert and output the result + degF = DEGC_TO_DEGF; + System.out.printf("%.1f degrees Celsius = %.1f degrees Fahrenheit.\n", + degC, degF); + } +} diff --git a/ch03/ConvertTime.java b/ch03/ConvertTime.java new file mode 100644 index 0000000..cb8c7c9 --- /dev/null +++ b/ch03/ConvertTime.java @@ -0,0 +1,27 @@ +import java.util.Scanner; + +public class ConvertTime +{ + public static void main(String[] args) { + int secondsIn; + + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Exactly how many seconds? "); + secondsIn = in.nextInt(); + + final int SECONDS_PER_HOUR = 60 * 60; + + int hours = secondsIn / SECONDS_PER_HOUR; + + final int SECONDS_PER_MINUTE = 60; + + int minutes = (secondsIn - (hours * SECONDS_PER_HOUR)) / SECONDS_PER_MINUTE; + + int seconds = (secondsIn - (hours * SECONDS_PER_HOUR)) % SECONDS_PER_MINUTE; + + System.out.printf(secondsIn + " seconds = %d hours, %d minutes, and %d seconds.\n", + hours, minutes, seconds); + } +} diff --git a/ch03/GuessNumber.java b/ch03/GuessNumber.java new file mode 100644 index 0000000..14edc04 --- /dev/null +++ b/ch03/GuessNumber.java @@ -0,0 +1,27 @@ +import java.util.Scanner; +import java.util.Random; + +public class GuessNumber +{ + public static void main(String[] args) + { + int userGuess; + + Scanner in = new Scanner (System.in); + + //prompt user to guess a number + System.out.print("Guess a number between 1 and 100: "); + userGuess = in.nextInt(); + + // pick a random number + Random random = new Random(); + int number = random.nextInt(100) + 1; + + int difference = Math.abs(number - userGuess); + + System.out.println("Your guess was " + userGuess + "."); + System.out.println("The number I was thinking of was " + number + "."); + System.out.println("The difference between your guess and my number is " + difference + "."); + } + +} diff --git a/ch04/ExerciseFourPointThree.java b/ch04/ExerciseFourPointThree.java new file mode 100644 index 0000000..42c33e9 --- /dev/null +++ b/ch04/ExerciseFourPointThree.java @@ -0,0 +1,42 @@ +public class ExerciseFourPointThree +{ + public static void main(String args[]) + { + String day = "Thursday"; + int date = 18; + String month = "January"; + int year = 2018; + + printAmerican(day, date, month, year); + + printEuropean(day, date, month, year); + + } + + public static void printAmerican(String day, int date, String month, int year) + { + System.out.println("American format:"); + + System.out.print(day); + System.out.print(", "); + System.out.print(month); + System.out.print(" "); + System.out.print(date); + System.out.print(", "); + System.out.println(year); + + } + + public static void printEuropean(String day, int date, String month, int year) + { + System.out.println("European format:"); + + System.out.print(day); + System.out.print(" "); + System.out.print(date); + System.out.print(" "); + System.out.print(month); + System.out.print(" "); + System.out.println(year); + } +} diff --git a/ch04/SimpleMethhods.java b/ch04/SimpleMethhods.java new file mode 100644 index 0000000..3f39a14 --- /dev/null +++ b/ch04/SimpleMethhods.java @@ -0,0 +1,21 @@ +public class SimpleMethhods +{ + public static void main(String[] args) + { + printCount(5); + + printSum(4, 6); + printSum(7, 2); + } + + public static void printCount(int count) + { + System.out.println("This count is: " + count); + } + + public static void printSum(int x, int y) + { + int z = x + y; + System.out.println(x + " + " + y + " = " + z); + } +} diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..8b9fe43 --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,211 @@ +import java.util.Scanner; + +public class LogicMethods +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + + System.out.println("What size cheese would you like? "); + int diameter = in.nextInt(); + System.out.println(); + + if (diameter < 0 || diameter > 3) + { + System.err.println("Your order is too crazy! Please try again."); + return; + } + + System.out.println("How many yards of cheese would you like? "); + int yards = in.nextInt(); + System.out.println(); + + if (yards < 0 || yards > 1000000) + { + System.err.println("Your order is too crazy! Please try again."); + return; + } + + crazyCheesePrice(diameter,yards); + } + + private static void crazyCheesePrice(int diameter, int yards) + { + int costPerYard = 0; + + final int PRICE_ONE_INCH = 2; + final int PRICE_TWO_INCH = 4; + final int PRICE_THREE_INCH = 6; + + if (diameter == 1) + { + costPerYard = (PRICE_ONE_INCH * yards); + } else if (diameter == 2) + { + costPerYard = (PRICE_TWO_INCH * yards); + } else if (diameter == 3) + { + costPerYard = (PRICE_THREE_INCH * yards); + } + + + int shipping = 0; + + final int SHIPPING_ONE_OR_TWO = 2; + final int SHIPPING_THREE = 4; + + final int FREE_SHIPPING_ONE_INCH = 50; + final int FREE_SHIPPING_TWO_INCHES = 75; + final int FREE_SHIPPING_THREE_INCHES = 25; + + + if ((diameter == 1 && yards <= FREE_SHIPPING_ONE_INCH) || (diameter == 2 && yards <= FREE_SHIPPING_TWO_INCHES)) + + { + shipping = SHIPPING_ONE_OR_TWO * yards; + + } else if (diameter == 3 && yards <= FREE_SHIPPING_THREE_INCHES) + { + shipping = SHIPPING_THREE * yards; + } + + + final int HANDLING = 5; + int totalPrice = costPerYard + shipping + HANDLING; + + System.out.println("The total cost for your order is $" + totalPrice); + System.out.println(); + + } + + + + + + /* //5-4 + private static void checkFermat(int a, int b, int c, int n) + { + + if (n > 2) + { + if (Math.pow(a, n) + Math.pow(b, n) == Math.pow(c, n)) + { + System.out.println("Holy smokes, Fermat was wrong!"); + System.out.println(); + } else + { + + System.out.println("No, that doesn't work."); + System.out.println(); + } + } else + { + System.out.println("Holy smokes, Fermat was right!"); + System.out.println(); + } + } */ + +//5-A + /*private static void printIsLarge(int number) + { + if (number > 99) + { + System.out.println("The number is large"); + + System.out.println(); + } + }*/ + +//5-B + /*private static void printIsLargeOrSmall(int number) + { + if (number > 99) + { + System.out.println("The number is large"); + + System.out.println(); + } + + if (number < 10) + { + System.out.println("The number is small"); + + System.out.println(); + } + }*/ + +//5-C + /*private static void printLargest(int number1, int number2) + { + + if (number1 > number2) + { + System.out.println("The largest number is: " + number1); + + System.out.println(); + } + + else if (number2 > number1) { + + System.out.println("The largest number is: " + number2); + + System.out.println(); + } + + else if (number1 == number2) { + + System.out.println("The numbers are equal"); + System.out.println(); + + } + } */ + +//5-D + /* private static void printLargestOdd(int number1, int number2) + { + int remainder1 = (number1 % 2); + int remainder2 = (number2 % 2); + + if (remainder1 == 1 && remainder2 == 1) + { + if (number1 > number2) + { + System.out.println("The largest odd number is: " + number1); + + System.out.println(); + } + + else if (number2 > number1) { + + System.out.println("The largest odd number is: " + number2); + + System.out.println(); + } + + else if (number1 == number2) { + + System.out.println("Two odds make an even"); + System.out.println(number1 + " + " + number2 + " = " + (number1 + number2)); + + } + } + + else if (remainder1 == 1 && remainder2 == 0) + { + System.out.println("The largest odd number is: " + number1); + System.out.println(); + } + + else if (remainder1 == 0 && remainder2 == 1) + { + System.out.println("The largest odd number is: " + number2); + System.out.println(); + } + + else { + System.out.println("Neither number is odd"); + System.out.println(); + } + } */ + +} diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..508ccbf --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class TicketNumber +{ + public static void main(String[] args) + { + int ticketNumber; + + Scanner in = new Scanner(System.in); + + System.out.println("What is your ticket number? "); + ticketNumber = in.nextInt(); + System.out.println(); + + int lastDigit = ticketNumber % 10; + int ticketPrefix = ticketNumber / 10; + + final int TICKET_TEST = 7; + boolean result = ((ticketPrefix % TICKET_TEST) == lastDigit); + + + if (result) + { + System.out.println("Good Number"); + } else + { + System.out.println("Bad Number"); + } + + + } +} diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..72ce8de --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,15 @@ +public class Multadd +{ + public static void main(String[] args) + { + System.out.println(multadd(1.0, 2.0, 3.0)); + System.out.println(multadd(12.0, 20.0, 4.0)); + System.out.println(multadd(-11.0, 242.0, -30.0)); + } + + //Exercise 6.4: 1, 2, and 3 + private static double multadd(double a, double b, double c) + { + return (a * b + c); + } +} diff --git a/ch06/ValueMethods.java b/ch06/ValueMethods.java new file mode 100644 index 0000000..dc77f09 --- /dev/null +++ b/ch06/ValueMethods.java @@ -0,0 +1,25 @@ +public class ValueMethods +{ + public static void main(String[] args) + { + System.out.println(isTriangle(3,4,5)); + System.out.println(isTriangle(1,1,12)); + } + + //Exercise 6.3 + private static boolean isTriangle (int a, int b, int c) + { + return (a <= b + c && b <= a + c && c <= a + b); + } + + + + + + + //Exercise 6.2 + /* private static boolean isDivisible(int n, int m) + { + return (n % m == 0); + } */ +} diff --git a/ch07/MyLoops.java b/ch07/MyLoops.java new file mode 100644 index 0000000..c6af2a1 --- /dev/null +++ b/ch07/MyLoops.java @@ -0,0 +1,68 @@ +public class MyLoops +{ + public static void main(String[] args) + { + exercise7aForLoop(); + exercise7aForLoopBackwards(); + + exercise7aWhile(); + exercise7aWhileBackwards(); + + exercise7aDoWhile(); + exercise7aDoWhileBackwards(); + } + + private static void exercise7aDoWhileBackwards() + { + int x = 10; + do{ + System.out.println(x); + x--; + } while (x >= 1); + } + + private static void exercise7aDoWhile() + { + int x = 1; + do{ + System.out.println(x); + x++; + } while (x <= 10); + } + + private static void exercise7aWhileBackwards() + { + int x = 10; + while (x >= 1) + { + System.out.println(x); + x--; + } + } + + private static void exercise7aWhile() + { + int x = 1; + while (x <= 10) + { + System.out.println(x); + x++; + } + } + + private static void exercise7aForLoopBackwards() + { + for (int x = 10; x >= 1; x--) + { + System.out.println(x); + } + } + + private static void exercise7aForLoop() + { + for (int x = 1; x <= 10; x++) + { + System.out.println(x); + } + } +} diff --git a/ch07/MyLoopsB.java b/ch07/MyLoopsB.java new file mode 100644 index 0000000..2b81622 --- /dev/null +++ b/ch07/MyLoopsB.java @@ -0,0 +1,38 @@ +public class MyLoopsB +{ + public static void main(String[] args) + { + exercise7bForLoop(); + + exercise7bWhile(); + + exercise7bDoWhile(); + } + + private static void exercise7bForLoop() + { + for (int x = 0; x <= 100; x += 10) + { + System.out.println(x); + } + } + + private static void exercise7bWhile() + { + int x = 0; + while (x <= 100) + { + System.out.println(x); + x += 10; + } + } + + private static void exercise7bDoWhile() + { + int x = 0; + do{ + System.out.println(x); + x += 10; + } while (x <= 100); + } +} diff --git a/ch07/MyLoopsC.java b/ch07/MyLoopsC.java new file mode 100644 index 0000000..271cb16 --- /dev/null +++ b/ch07/MyLoopsC.java @@ -0,0 +1,39 @@ +public class MyLoopsC +{ + public static void main(String[] args) + { + exercise7cForLoop(); + + exercise7cWhile(); + + exercise7cDoWhile(); + } + + private static void exercise7cForLoop() + { + for (int x = 100; x >= -100; x -= 8) + { + System.out.println(x); + } + } + + + private static void exercise7cWhile() + { + int x = 100; + while (x >= -100) + { + System.out.println(x); + x -= 8; + } + } + + private static void exercise7cDoWhile() + { + int x = 100; + do{ + System.out.println(x); + x -= 8; + } while (x >= -100); + } +} diff --git a/ch07/MyLoopsD.java b/ch07/MyLoopsD.java new file mode 100644 index 0000000..2d6dd02 --- /dev/null +++ b/ch07/MyLoopsD.java @@ -0,0 +1,16 @@ +public class MyLoopsD +{ + public static void main(String[] args) + { + exercise7d(10); + } + + private static void exercise7d(int x) + { + int y = 1; + while (y <= x){ + System.out.println(y); + y++; + } + } +} diff --git a/ch07/MyLoopsE.java b/ch07/MyLoopsE.java new file mode 100644 index 0000000..845a8af --- /dev/null +++ b/ch07/MyLoopsE.java @@ -0,0 +1,28 @@ +import java.util.Scanner; + +public class MyLoopsE +{ + public static void main(String[] args) + { + exercise7E(); + } + + private static void exercise7E() + { + Scanner in = new Scanner(System.in); + + int x = 1; + + while (x != 0) + { + System.out.println("Please enter a number: "); + x= in.nextInt(); + + if (x != 0) + { + System.out.println("Oops! You did not enter \"0\"!"); + System.out.println(); + } + } + } +} diff --git a/ch07/MyLoopsF.java b/ch07/MyLoopsF.java new file mode 100644 index 0000000..e234cf0 --- /dev/null +++ b/ch07/MyLoopsF.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +public class MyLoopsF +{ + public static void main(String[] args) + { + exercise7F(); + } + + private static void exercise7F() + { + Scanner in = new Scanner(System.in); + + int x; + int y = 0; + + while (y <= 1000) + { + System.out.println("Please enter a number: "); + x = in.nextInt(); + y = y + x; + System.out.println(); + + } + + System.out.println(); + System.out.println(y); + } +} diff --git a/ch08/Ch8Exercises.java b/ch08/Ch8Exercises.java new file mode 100644 index 0000000..e35b87d --- /dev/null +++ b/ch08/Ch8Exercises.java @@ -0,0 +1,49 @@ +import java.util.Arrays; + +public class Ch8Exercises +{ + public static void main(String[] args) + { + int testArray[] = {5,4,3,55,2,1}; + arrayMax(testArray); + } + + //Exercise 8-C + private static void arrayMax(int[] a) + { + int max = 0; + int max2 = 0; + + for (int b : a) + { + max = Math.max(a[0], b); + max2 = Math.max(max2, max); + + } + + System.out.println(max2); + } + + + //Exercise 8-B + /* private static void arrayTotal(int[] a) + { + int total = 0; + + for (int b : a) + { + total += b; + } + + System.out.println("The sum of all of the values in the array is: " + total); + + } */ + + + //Exercise 8-A + /* private static void printArray(int[] y) + { + System.out.println(Arrays.toString(y)); + + } */ +} diff --git a/ch08/Exercise8A.java b/ch08/Exercise8A.java new file mode 100644 index 0000000..2564e4f --- /dev/null +++ b/ch08/Exercise8A.java @@ -0,0 +1,17 @@ +import java.util.Arrays; + +public class Exercise8A +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,55,4,5,6}; + + printArray(testArray); + } + + private static void printArray(int[] y) + { + System.out.println("The values in this array are: " + Arrays.toString(y)); + + } +} diff --git a/ch08/Exercise8B.java b/ch08/Exercise8B.java new file mode 100644 index 0000000..20fff47 --- /dev/null +++ b/ch08/Exercise8B.java @@ -0,0 +1,22 @@ +public class Exercise8B +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,4,5}; + + arrayTotal(testArray); + } + + private static void arrayTotal(int[] a) + { + int total = 0; + + for (int b : a) + { + total += b; + } + + System.out.println("The sum of all of the values in the array is: " + total); + + } +} diff --git a/ch08/Exercise8C.java b/ch08/Exercise8C.java new file mode 100644 index 0000000..1d43d34 --- /dev/null +++ b/ch08/Exercise8C.java @@ -0,0 +1,24 @@ +public class Exercise8C +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,55,600,4,5}; + + arrayMax(testArray); + } + + private static void arrayMax(int[] a) + { + int max; + int max2 = 0; + + for (int b : a) + { + max = Math.max(a[0], b); + max2 = Math.max(max2, max); + + } + + System.out.println("The largest value in the array is: " + max2); + } +} diff --git a/ch08/Exercise8D.java b/ch08/Exercise8D.java new file mode 100644 index 0000000..b1e9f2a --- /dev/null +++ b/ch08/Exercise8D.java @@ -0,0 +1,24 @@ +public class Exercise8D +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,60,4,5}; + + arrayMaxIndex(testArray); + } + + 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("The index in this array with the highest value is index: " + maxValueIndex); + } +} diff --git a/ch08/Exercise8E.java b/ch08/Exercise8E.java new file mode 100644 index 0000000..c05b0dc --- /dev/null +++ b/ch08/Exercise8E.java @@ -0,0 +1,23 @@ +public class Exercise8E +{ + public static void main(String[] args) + { + double testArray[] = {3.0, 5.0, 1.0, 9.0, 7.0}; + + arrayAverage(testArray); + } + + private static void arrayAverage(double[] values) + { + double total = 0.0; + + for (double value : values) + { + total += value; + } + + double avg = total / values.length; + + System.out.println("The average of all of the values in the array is: " + avg); + } +} diff --git a/ch08/Exercise8F.java b/ch08/Exercise8F.java new file mode 100644 index 0000000..29d77fc --- /dev/null +++ b/ch08/Exercise8F.java @@ -0,0 +1,35 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class Exercise8F +{ + public static void main(String[] args) + { + petNames(); + } + + private static void petNames() + { + Scanner in = new Scanner(System.in); + + System.out.println("How many pets do you have? "); + int numberOfPets = in.nextInt(); + in.nextLine(); + + String[] petNames = new String [numberOfPets]; + + int currentPet = 0; + + while (currentPet < numberOfPets) + { + System.out.println("What is the name of pet number " + (currentPet + 1) + "? "); + petNames [currentPet] = in.nextLine(); + currentPet++; + } + + System.out.println("The names of your pets are" + Arrays.toString(petNames) + "."); + + } + + +} diff --git a/ch08/Exercise8G.java b/ch08/Exercise8G.java new file mode 100644 index 0000000..73facef --- /dev/null +++ b/ch08/Exercise8G.java @@ -0,0 +1,61 @@ +import java.util.Scanner; + +import java.util.Arrays; + +public class Exercise8G +{ + public static void main(String[] args) + { + final int MENU_SIZE = 5; + + int[] menuChoices = new int[MENU_SIZE]; + + int customerChoice; + + Scanner in = new Scanner(System.in); + + while (menuChoices[0] < 99 && menuChoices[1] < 99 && menuChoices[2] < 99 && menuChoices[3] < 99 && menuChoices[4] < 99) + { + + do + { + System.out.println("Would you like menu item 0, 1, 2, 3, or 4? "); + customerChoice = in.nextInt(); + in.nextLine(); + + if (customerChoice < 0 || customerChoice > 4) + { + System.out.println("Sorry, that is not one of the items that we offer. Please make a different selection."); + } + } while (customerChoice < 0 || customerChoice > 4); + + menuChoices[customerChoice]++; + System.out.println(Arrays.toString(menuChoices)); + + + } + + + System.out.println("Gooodbye!"); + + boolean employeeScreen = false; + + + System.out.println("Are you an IVM employee? Please answer yes or no. "); + String employeeTest = in.nextLine(); + + if (employeeTest.equals("yes")); + { + employeeScreen = true; + } + + if (employeeScreen) + { + System.out.println(Arrays.toString(menuChoices)); + } + else + System.out.println("Sorry, this information is only accessible by IVM employees."); + } + + +} \ No newline at end of file diff --git a/ch09/Exercise9A.java b/ch09/Exercise9A.java new file mode 100644 index 0000000..0d7ecff --- /dev/null +++ b/ch09/Exercise9A.java @@ -0,0 +1,16 @@ +public class Exercise9A +{ + public static void main(String[] args) + { + printFirstCharacter("Hello"); + printFirstCharacter("Goodbye"); + + } + + private static void printFirstCharacter(String text) + { + System.out.println("Print the first character in " + text); + + System.out.println(text.charAt(0)); + } +} diff --git a/ch09/Exercise9B.java b/ch09/Exercise9B.java new file mode 100644 index 0000000..c21750a --- /dev/null +++ b/ch09/Exercise9B.java @@ -0,0 +1,16 @@ +public class Exercise9B +{ + public static void main(String[] args) + { + printLastCharacter("Hello"); + printLastCharacter("Goodbye"); + + } + + private static void printLastCharacter(String text) + { + System.out.println("Print the last character in " + text); + + System.out.println(text.charAt(text.length() - 1)); + } +} diff --git a/ch09/Exercise9C.java b/ch09/Exercise9C.java new file mode 100644 index 0000000..7ca0080 --- /dev/null +++ b/ch09/Exercise9C.java @@ -0,0 +1,17 @@ +public class Exercise9C +{ + public static void main(String[] args) + { + printCharacters("Hello"); + } + + private static void printCharacters(String text) + { + for (int i = 0; i < text.length(); i++) + { + char letter = text.charAt(i); + + System.out.println("" + letter + i); + } + } +} diff --git a/ch09/Exercise9D.java b/ch09/Exercise9D.java new file mode 100644 index 0000000..38325f8 --- /dev/null +++ b/ch09/Exercise9D.java @@ -0,0 +1,13 @@ +public class Exercise9D +{ + public static void main(String[] args) + { + printAllButFirstThree("Hello"); + printAllButFirstThree("Goodbye"); + } + + private static void printAllButFirstThree(String text) + { + System.out.println(text.substring(3)); + } +} diff --git a/ch09/Exercise9E.java b/ch09/Exercise9E.java new file mode 100644 index 0000000..1a19500 --- /dev/null +++ b/ch09/Exercise9E.java @@ -0,0 +1,14 @@ +public class Exercise9E +{ + public static void main(String[] args) + { + printFirstThree("Hello"); + printFirstThree("Goodbye"); + + } + + private static void printFirstThree(String text) + { + System.out.println(text.substring(0, 3)); + } +} diff --git a/ch09/Exercise9F.java b/ch09/Exercise9F.java new file mode 100644 index 0000000..2cb12d2 --- /dev/null +++ b/ch09/Exercise9F.java @@ -0,0 +1,20 @@ +public class Exercise9F +{ + public static void main(String[] args) + { + printPhoneNumber("501-555-0100"); + } + + private static void printPhoneNumber (String digits) + { + //Expected format: ###-###-#### + + String areaCode = digits.substring(0,3); + String exchange = digits.substring(4,7); + String lineNumber = digits.substring(8,12); + + System.out.println("Area Code: " + areaCode); + System.out.println("Exchange: " + exchange); + System.out.println("Line Number: " + lineNumber); + } +} diff --git a/ch09/Exercise9G.java b/ch09/Exercise9G.java new file mode 100644 index 0000000..827c240 --- /dev/null +++ b/ch09/Exercise9G.java @@ -0,0 +1,13 @@ +public class Exercise9G +{ + public static void main(String[] args) + { + System.out.println(findFirstE("Hello")); + System.out.println(findFirstE("Goodbye")); + } + + private static int findFirstE(String text) + { + return text.indexOf('e'); + } +} diff --git a/ch09/Exercise9H.java b/ch09/Exercise9H.java new file mode 100644 index 0000000..ad45695 --- /dev/null +++ b/ch09/Exercise9H.java @@ -0,0 +1,16 @@ +public class Exercise9H +{ + public static void main(String[] args) + { + System.out.println(isFinn("Finn")); + System.out.println(isFinn("Jake")); + + } + + private static boolean isFinn(String adventure) + { + String theHuman = "Finn"; + + return (adventure.equals(theHuman)); + } +} diff --git a/ch11/Date.java b/ch11/Date.java new file mode 100644 index 0000000..7a1587e --- /dev/null +++ b/ch11/Date.java @@ -0,0 +1,14 @@ +public class Date +{ + private int day; + private int month; + private int year; + + public Date(int a, int b, int c) + { + day = a; + month = b; + year = c; + } + +} diff --git a/ch11/Player.java b/ch11/Player.java new file mode 100644 index 0000000..948a33f --- /dev/null +++ b/ch11/Player.java @@ -0,0 +1,24 @@ +public class Player +{ + private String name; + private int score; + + public Player(String name) + { + score = 0; + + this.name = name; + } + + 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..6368f44 --- /dev/null +++ b/ch11/Product.java @@ -0,0 +1,41 @@ +public class Product +{ + + private String name; + private int quantityInStock; + + public Product(String name) + { + this.name = name; + } + + public String getName() + { + return this.name; + } + + public int getQuantityInStock() + { + return this.quantityInStock; + } + + public void stock(int newStock) + { + quantityInStock += newStock; + } + + public void sell(int soldStock) + { + quantityInStock -= soldStock; + } + + public void recordLoss(int stockLost) + { + quantityInStock -= stockLost; + } + + public String toString() + { + return "" + name + ", Quantity in Stock " + quantityInStock; + } +} diff --git a/ch11/Rectangle.java b/ch11/Rectangle.java new file mode 100644 index 0000000..9f2fcc8 --- /dev/null +++ b/ch11/Rectangle.java @@ -0,0 +1,62 @@ +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; + } + + public Rectangle(Rectangle rectangle) + { + this.height = rectangle.getHeight(); + this.width = rectangle.getWidth(); + } + + public int getArea() + { + return height * width; + } + + public String toString() + { + return "Height: " + height + ", Width: " + width + ", 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); + } +}