diff --git a/FinishLine/src/Basketball.java b/FinishLine/src/Basketball.java new file mode 100644 index 0000000..0cc29e9 --- /dev/null +++ b/FinishLine/src/Basketball.java @@ -0,0 +1,113 @@ +public class Basketball +{ + private static int roundNumber = 0; + private static int gameNumber = 0; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + public void play() + { + while(bluePeg.getPosition()<22&&redPeg.getPosition()<22) + { + takeTurn(bluePeg); + System.out.println("Blue has: " + bluePeg.getPosition() + " points"); + System.out.println(); + + takeTurn(redPeg); + System.out.println("Red has: " + redPeg.getPosition() + " points"); + System.out.println(); + } + + if(redPeg.getPosition()>bluePeg.getPosition()) + System.out.println("Red wins with " + redPeg.getPosition() + " points!"); + if(bluePeg.getPosition()>redPeg.getPosition()) + System.out.println("Blue wins with " + bluePeg.getPosition() + " points!"); + if(bluePeg.getPosition()==redPeg.getPosition()) + System.out.println("Wow what a game! It ended in a tie!"); + } + + private void takeTurn(Peg peg) + { + die1.roll(); + die2.roll(); + + System.out.println("The roll is: " + (die1.getValue() + die2.getValue())); + + if(die1.getValue()+die2.getValue()==2) + { + peg.positionUp(); + peg.positionUp(); + peg.positionUp(); + System.out.println("There's a three pointer!"); + } + + if(die1.getValue()+die2.getValue()==3) + { + System.out.println("Walking violation, unlucky!"); + } + + if(die1.getValue()+die2.getValue()==4) + { + peg.positionUp(); + peg.positionUp(); + System.out.println("The two point shot is good!"); + } + + if(die1.getValue()+die2.getValue()==5) + { + peg.positionUp(); + System.out.println("He converts one foul shot!"); + } + + if(die1.getValue()+die2.getValue()==6) + { + peg.positionUp(); + peg.positionUp(); + System.out.println("Two point shot!"); + } + + if(die1.getValue()+die2.getValue()==7) + { + System.out.println("Double dribble violation, unlucky!"); + } + + if(die1.getValue()+die2.getValue()==8) + { + peg.positionUp(); + peg.positionUp(); + System.out.println("He converts both foul shots!"); + } + + if(die1.getValue()+die2.getValue()==9) + { + System.out.println("Ohh, close but no cigar!"); + } + + if(die1.getValue()+die2.getValue()==10) + { + peg.positionUp(); + peg.positionUp(); + peg.positionUp(); + System.out.println("Wow, he made it from downtown!"); + } + + if(die1.getValue()+die2.getValue()==11) + { + System.out.println("Offensive foul, unlucky!"); + } + + if(die1.getValue()+die2.getValue()==12) + { + peg.positionUp(); + peg.positionUp(); + peg.positionUp(); + System.out.println("A wonderful three point shot!"); + } + + + } +} diff --git a/FinishLine/src/Die.java b/FinishLine/src/Die.java new file mode 100644 index 0000000..008ffa9 --- /dev/null +++ b/FinishLine/src/Die.java @@ -0,0 +1,22 @@ +import java.util.Random; + +public class Die +{ + private int value; + + public Die() + { + roll(); + } + + public int getValue() + { + return value; + } + + public void roll() + { + Random random = new Random(); + value = random.nextInt(6) + 1; + } +} diff --git a/FinishLine/src/FinishLine.java b/FinishLine/src/FinishLine.java new file mode 100644 index 0000000..7168db0 --- /dev/null +++ b/FinishLine/src/FinishLine.java @@ -0,0 +1,99 @@ +public class FinishLine +{ + private static int roundNumber = 0; + private static int gameNumber = 0; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + private static int redWin = 0; + private static int blueWin = 0; + private static int tie = 0; + + public void play() + { + gameNumber++; + System.out.println("Game number: " + gameNumber); + + redPeg.positionUp(); + bluePeg.positionUp(); + + while (redPeg.getPosition() < 10 && bluePeg.getPosition() < 10) + { + roundNumber++; + System.out.println("Round number: " + roundNumber); + takeTurn(redPeg); + System.out.print("Red position: " + redPeg.getPosition() + "\t"); + + takeTurn(bluePeg); + System.out.println("Blue position: " + bluePeg.getPosition()); + System.out.println(); + } + + resetRound(); + + if (redPeg.getPosition() == bluePeg.getPosition()) + { + System.out.println("It was a tie! Both pegs ended up at 10"); + tie++; + System.out.println(); + } + + else if (bluePeg.getPosition() == 10) + { + System.out.println("Blue won!"); + blueWin++; + } + + else if (redPeg.getPosition() == 10) + { + System.out.println("Red won!"); + redWin++; + } + + bluePeg.resetPosition(); + redPeg.resetPosition(); + } + + public static int getRedWin() + { + return redWin; + } + + public static int getBlueWin() + { + return blueWin; + } + + public static int getTie() + { + return tie; + } + + private static void takeTurn(Peg peg) + { + System.out.println(); + + die1.roll(); + die2.roll(); + + if (peg.getNextPosition() == die1.getValue() || + peg.getNextPosition() == die2.getValue() || + peg.getNextPosition() == die1.getValue() + die2.getValue()) + + peg.positionUp(); + } + + public void resetRound() + { + roundNumber=0; + } + + public static int getGameNumber() + { + return gameNumber; + } +} diff --git a/FinishLine/src/Football.java b/FinishLine/src/Football.java new file mode 100644 index 0000000..51382fb --- /dev/null +++ b/FinishLine/src/Football.java @@ -0,0 +1,196 @@ +import java.util.SortedMap; +import java.util.Scanner; + +public class Football +{ + private int yardsToFirst = 10; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + private Scanner in = new Scanner(System.in); + + public void play() + { + redPeg.setPosition(20); + while(redPeg.getScore()<28&&bluePeg.getScore()<28) + { + takeTurn(redPeg, bluePeg); + System.out.println("Red's score is now: " + redPeg.getScore()); + + takeTurn(bluePeg, redPeg); + System.out.println("Blue's score is now: " + bluePeg.getScore()); + } + + + if(redPeg.getScore()>bluePeg.getScore()) + { + System.out.println("Red wins with " + redPeg.getScore() + " points and blue had " + bluePeg.getScore()); + } + + if(bluePeg.getScore()>redPeg.getScore()) + { + System.out.println("Blue wins with " + bluePeg.getScore() + " points and red had " + redPeg.getScore()); + } + + if(bluePeg.getScore()==redPeg.getScore()) + { + System.out.println("The game was a tie at " + redPeg.getPosition() + " points! What a game!"); + } + } + + private void takeTurn(Peg peg, Peg otherPeg) + { + int down = 1; + int yardsToFirst = 10; + String punt = ""; + while (down<5) + { + System.out.println(); + die1.roll(); + die2.roll(); + + System.out.println("Down: " + down); + System.out.println("Yards to TD: " + (100 - peg.getPosition())); + System.out.println("Yards to first: " + yardsToFirst); + System.out.println("The roll is: " + (die1.getValue() + die2.getValue())); + + + if (die1.getValue() + die2.getValue() == 2) + { + System.out.println("Touchdown!!!!!!!!"); + peg.scoreUp(6); + + die1.roll(); + if(die1.getValue()>=5) + { + System.out.println("The extra point is good!"); + peg.scoreUp(1); + } + + otherPeg.setPosition(20); + down=5; + } + + if (die1.getValue() + die2.getValue() == 3) + { + System.out.println("Interception!"); + System.out.println("What a job on defense! It's a turnover!"); + + otherPeg.setPosition(100-(peg.getPosition()-10)); + break; + } + + if (die1.getValue() + die2.getValue() == 4) + { + peg.setPosition(peg.getPosition()+20); + System.out.println("It's a reverse for a gain of 20 yards! Amazing!"); + yardsToFirst-=20; + down++; + } + + if (die1.getValue() + die2.getValue() == 5) + { + System.out.println("The quaterback is sacked for a huge loss!"); + if(peg.getPosition()>10) + peg.setPosition(peg.getPosition()-10); + else + peg.setPosition((0)); + yardsToFirst+=10; + down++; + } + + if (die1.getValue() + die2.getValue() == 6) + { + peg.setPosition(peg.getPosition()+5); + System.out.println("Its a draw for a gain of 5"); + yardsToFirst-=5; + down++; + } + + if (die1.getValue() + die2.getValue() == 7) + { + System.out.println("Incomplete pass over the middle"); + down++; + } + + if (die1.getValue() + die2.getValue() == 8) + { + peg.setPosition(peg.getPosition()+5); + System.out.println("The quarterback completes a simple screen pass for 5 yards"); + yardsToFirst-=5; + down++; + } + + if (die1.getValue() + die2.getValue() == 9) + { + System.out.println("What a pass down the middle! It's a 25 yard gain!"); + peg.setPosition(peg.getPosition()+25); + yardsToFirst-=25; + down++; + } + + if (die1.getValue() + die2.getValue() == 10) + { + System.out.println("They stuffed that run!"); + down++; + } + + if (die1.getValue() + die2.getValue() == 11) + { + System.out.println("It's a fumble!"); + down = 5; + } + + if (die1.getValue() + die2.getValue() == 12) + { + peg.setPosition(peg.getPosition()+30); + System.out.println("What a throw, a 30 yard gain!"); + yardsToFirst-=30; + down++; + } + + if(peg.getPosition()>=100) + { + System.out.println("Touchdown!!!!!!!!!"); + peg.scoreUp(6); + die1.roll(); + if(die1.getValue()>=5) + { + System.out.println("The extra point is good!"); + peg.scoreUp(1); + } + otherPeg.setPosition(20); + break; + } + + if(yardsToFirst<=0) + down=1; + if(yardsToFirst<=0) + yardsToFirst=10; + + if(down==4) + { + System.out.println("It's fourth down. Would you like to punt? (Y/N)"); + punt = in.nextLine(); + + if(punt.equals("y")||punt.equals("Y")) + { + die1.roll(); + otherPeg.setPosition(peg.getPosition()-(10*die1.getValue())); + System.out.println("The ball was punted " + (10*die1.getValue()) + " yards"); + break; + } + } + + if(down>4) + { + System.out.println("What a job on defense, its a turnover!"); + otherPeg.setPosition(100-peg.getPosition()); + } + } + } +} diff --git a/FinishLine/src/Peg.java b/FinishLine/src/Peg.java new file mode 100644 index 0000000..4c61f65 --- /dev/null +++ b/FinishLine/src/Peg.java @@ -0,0 +1,51 @@ +public class Peg +{ + private int position; + private int score; + + public Peg() + { + position = 0; + } + + public int getPosition() + { + return position; + } + + public void setPosition(int position) + { + this.position = position; + } + + public int getNextPosition() + { + return position + 1; + } + + public void positionUp() + { + position++; + } + + public void resetPosition() + { + position = 0; + } + + public void positionDown() + { + position--; + } + + public void scoreUp(int score) + { + this.score+= score; + } + + public int getScore() + { + return score; + } + +} diff --git a/FinishLine/src/RoundAbout.java b/FinishLine/src/RoundAbout.java new file mode 100644 index 0000000..f9baaed --- /dev/null +++ b/FinishLine/src/RoundAbout.java @@ -0,0 +1,76 @@ +public class RoundAbout +{ + private static int roundNumber = 0; + private static int gameNumber = 0; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + public void play() + { + redPeg.resetPosition(); + bluePeg.resetPosition(); + + gameNumber++; + + while(redPeg.getPosition()<11&&bluePeg.getPosition()<11) + { + takeTurn(redPeg); + System.out.print("Red position: " + redPeg.getPosition() + "\t"); + + if(redPeg.getPosition()==bluePeg.getPosition()) + { + if(bluePeg.getPosition()>0) + System.out.println("Red knocked blue back to start!"); + bluePeg.resetPosition(); + } + + takeTurn(bluePeg); + System.out.println("Blue position: " + bluePeg.getPosition()); + + if(redPeg.getPosition()==bluePeg.getPosition()) + { + if(redPeg.getPosition()>0) + System.out.println("Blue knocked red back to start!"); + + redPeg.resetPosition(); + + } + } + + if(bluePeg.getPosition()==11) + System.out.println("Blue won!"); + if(redPeg.getPosition()==11) + System.out.println("Red won!"); + } + + private static void takeTurn(Peg peg) + { + System.out.println(); + + die1.roll(); + die2.roll(); + + System.out.println(die1.getValue()); + System.out.println(die2.getValue()); + + if(peg.getPosition()==0) + { + if (die1.getValue()==5|| + die2.getValue()==5|| + die1.getValue()+die2.getValue()==5) + peg.positionUp(); + } + + if(peg.getPosition()>0) + { + if (peg.getNextPosition() == die1.getValue() || + peg.getNextPosition() == die2.getValue() || + peg.getNextPosition() == die1.getValue() + die2.getValue()) + peg.positionUp(); + } + } +} diff --git a/MapExercises/src/PetHotel.java b/MapExercises/src/PetHotel.java new file mode 100644 index 0000000..f28e2f5 --- /dev/null +++ b/MapExercises/src/PetHotel.java @@ -0,0 +1,254 @@ + +import java.util.*; + +public class PetHotel +{ + public static void main(String[] args) + { + run(); + } + static void run() + { + Map petOccupants = new TreeMap<>(); + + Scanner scanner = new Scanner(System.in); + + System.out.println("Welcome to the pet hotel!"); + System.out.println("Enter one of the following commands: "); + System.out.println(); + printCommands(); + + boolean go = true; + final int roomNumberMin = 100; + final int roomNumberMax = 109; + + while(go) + { + String commandLine = scanner.nextLine(); + String[] commands = commandLine.split(" "); + String command = commands[0].toUpperCase(); + boolean isOccupied = false; + + + if(command.equals("CHECKIN")) + { + String petName = commands[1]; + + if(commands.length!=3) + { + System.out.println("That is an invalid entry."); + } + else + { + int room = Integer.valueOf(commands[2]); + + for (int roomNumber : petOccupants.keySet()) + { + if (roomNumber == room) + { + isOccupied = true; + } + } + + if (room < roomNumberMin || room > roomNumberMax) + { + System.out.println("That is not a valid room number. Try again."); + System.out.println(); + + } + + else if (isOccupied) + { + if(petOccupants.get(room).size() > 3) + { + System.out.println("Sorry, there are too many pets in this room already. Pick a different room."); + } + else + { + System.out.println("Checked " + petName + " in to room " + room); + petOccupants.get(room).add(petName); + } + } + + else + { + ArrayList petGroup = new ArrayList<>(); + petOccupants.put(room, petGroup); + petOccupants.get(room).add(petName); + System.out.println("Checked " + petName + " in to room " + room); + System.out.println(); + } + } + + } + + else if(command.equals("CHECKOUT")) + { + boolean isInRoom = false; + int room = Integer.valueOf(commands[1]); + + if(commands.length==2) + { + System.out.println("Checked all pets out of room " + room); + petOccupants.remove(Integer.valueOf(commands[1])); + } + + else if(commands.length==3) + { + String petName = commands[2]; + int petIndex = 0; + for(int i = 0;i < petOccupants.get(room).size();i++) + { + if(petOccupants.get(room).get(i).equals(petName)) + { + isInRoom = true; + petIndex = i; + } + } + + if(isInRoom) + { + petOccupants.get(room).remove(petName); + System.out.println(petName + " was checked out from room " + room); + } + else + { + System.out.println("Sorry, that pet is not in that room."); + } + } + } + + else if(command.equals("MOVE")) + { + isOccupied = false; + int currentRoom = Integer.valueOf(commands[1]); + int nextRoom = Integer.valueOf(commands[2]); + + for(int roomNumber : petOccupants.keySet()) + { + if(roomNumber == nextRoom) + { + isOccupied = true; + } + } + + if(currentRoom roomNumberMax || + nextRoom roomNumberMax + ) + { + System.out.println("One of those room numbers is invalid. Try again."); + System.out.println(); + } + + else if(isOccupied) + { + System.out.println("The room you want to move to is occupied. Please pick another room."); + } + + else + { + System.out.println("Moved " + petOccupants.get(currentRoom) + " to room " + nextRoom); + petOccupants.put(nextRoom,petOccupants.get(currentRoom)); + } + } + + else if(command.equals("SWAP")) + { + int firstRoom = Integer.valueOf(commands[1]); + int secondRoom = Integer.valueOf(commands [2]); + int holder = Integer.valueOf(commands[1]); + + System.out.println(petOccupants.get(secondRoom) + " is now in room " + firstRoom + " and " + petOccupants.get(firstRoom) + " is now in room " + secondRoom); + + petOccupants.put(firstRoom, petOccupants.get(secondRoom)); + petOccupants.put(secondRoom, petOccupants.get(holder)); + + } + + else if(command.equals("MOVEUP")) + { + System.out.println("All pets were moved into the next room up."); + Map holder = new TreeMap<>(); + + for(Map.Entry entry : petOccupants.entrySet()) + { + int room = entry.getKey() + 1; + if(room > roomNumberMax) + { + room = roomNumberMin; + } + holder.put(room,entry.getValue()); + } + petOccupants = holder; + } + + else if(command.equals("MOVEDOWN")) + { + System.out.println("All pets were moved into the next room down."); + Map holder = new TreeMap<>(); + + for(Map.Entry entry : petOccupants.entrySet()) + { + int room = entry.getKey() - 1; + if(room < roomNumberMin) + { + room = roomNumberMax; + } + holder.put(room,entry.getValue()); + } + petOccupants = holder; + } + + else if(command.equals("OCCUPANCY")) + { + if(petOccupants.size()==0) + { + System.out.println("There are no pets currently checked in"); + } + else + { + Set printPets = petOccupants.entrySet(); + System.out.println("Pet occupants: " + printPets); + } + } + + else if(command.equals("CLOSEFORSEASON")) + { + petOccupants.clear(); + System.out.println("All pets have been checked out."); + } + + else if(command.equals("SHOWCOMMANDS")) + { + printCommands(); + } + + else if(command.equals("EXIT")) + { + go = false; + System.out.println("Thank you for using this system. Goodbye!"); + } + + else + { + System.out.println("That is an invalid command."); + } + + if(go) + System.out.println("Enter another command: (To view commands, enter ShowCommands) "); + } + } + + public static void printCommands() + { + System.out.println("CheckIn "); + System.out.println("CheckOut "); + System.out.println("MoveUp"); + System.out.println("MoveDown"); + System.out.println("Occupancy"); + System.out.println("CloseForSeason"); + System.out.println("Exit"); + } +} diff --git a/ShoppingList/.idea/misc.xml b/ShoppingList/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/ShoppingList/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ShoppingList/.idea/modules.xml b/ShoppingList/.idea/modules.xml new file mode 100644 index 0000000..bd26b87 --- /dev/null +++ b/ShoppingList/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ShoppingList/.idea/vcs.xml b/ShoppingList/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ShoppingList/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ShoppingList/ShoppingList.iml b/ShoppingList/ShoppingList.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/ShoppingList/ShoppingList.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/ShoppingList/src/company/ListItem.java b/ShoppingList/src/company/ListItem.java new file mode 100644 index 0000000..23d97c2 --- /dev/null +++ b/ShoppingList/src/company/ListItem.java @@ -0,0 +1,41 @@ +package company; + +public class ListItem +{ + private String name; + private int quantity = 1; + + ListItem(String name, int quantity) + { + this.name = name; + this.quantity = quantity; + } + + public String getName() + { + return name; + } + + public int getQuantity() + { + return quantity; + } + + public void quantityUp() + { + quantity++; + } + + public void quantityDown() + { + quantity--; + } + + public void addQuantity( int numToAdd) + { + quantity += numToAdd; + } + + public void subQuanitity(int numToSub){ quantity -= numToSub; } + +} diff --git a/ShoppingList/src/company/ShoppingList.java b/ShoppingList/src/company/ShoppingList.java new file mode 100644 index 0000000..fa8baff --- /dev/null +++ b/ShoppingList/src/company/ShoppingList.java @@ -0,0 +1,204 @@ +package company; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class ShoppingList +{ + public static void main(String[] args) + { + ShoppingList shoppingList = new ShoppingList(); + + run(); + } + + private static void run() + { + Scanner scanner = new Scanner(System.in); + boolean go = true; + + System.out.println("Welcome to my store. Go buy something."); + + List shoppingList = new ArrayList<>(); + + while(go) + { + System.out.println("Enter one of the following commands:"); + System.out.println(); + System.out.println("Add "); + System.out.println("Print"); + System.out.println("Remove "); + System.out.println("Find"); + System.out.println("Clear"); + System.out.println("Exit"); + + String commandLine = scanner.nextLine(); + String[] commands = commandLine.split(" "); + String command = commands[0].toUpperCase(); + + int quantity = 0; + + if(commands.length == 3) + { + try + { + quantity = Integer.parseInt(commands[2]); + } + catch(Exception e) + { + System.out.println(commands[2] + " is not a valid number. Try again."); + } + } + + boolean isInList = false; + int itemIndex = 0; + if(command.equals("ADD")) + { + if(commands.length > 3) + { + System.out.println("Please enter only the item name and quantity."); + } + String itemName = commands[1]; + for(int i = 0;i shoppingList.size()) + { + System.out.println("That's not a valid item number. Try again."); + System.out.println(); + } + + else + { + shoppingList.get(index - 1).quantityDown(); + if (shoppingList.get(index - 1).getQuantity() <= 0) + { + shoppingList.remove(index - 1); + } + } + } + + else + { + System.out.println("That is not a valid item number."); + System.out.println(); + } + + } + + else if(command.equals("CLEAR")) + { + shoppingList.clear(); + } + + else if(command.equals("FIND")) + { + if(shoppingList.size()==0) + { + System.out.println("Your shopping list is empty"); + System.out.println(); + } + + for(int i = 0;i emergencyTasks = new Stack<>(); + Scanner scanner = new Scanner(System.in); + String command = ""; + String emergency = ""; + + public static void main(String[] args) + { + EmergencyTracker emergencyTracker = new EmergencyTracker(); + + emergencyTracker.run(); + } + void run() + { + System.out.println("Welcome to the emergency input system. Enter one of the following commands: "); + printCommands(); + + do + { + try + { + String commandLine = scanner.nextLine(); + String[] commands = commandLine.split(" "); + command = commands[0].toUpperCase(); + + if(commands.length == 2) + emergency = commands[1]; + if(commands.length > 2) + { + System.out.println("Invalid input. Don't enter more than 2 values."); + } + else if(command.equals("ADD")) + { + add(emergency); + } + else if(command.equals("PEEK")) + { + peek(); + } + else if(command.equals("REMOVE")) + { + remove(); + } + else if(command.equals("HOWMANY")) + { + remove(); + } + else if(command.equals("PANIC")) + { + panic(); + } + else if(command.equals("EXIT")) + { + System.out.println("Thank you for using this emergency tracking system."); + } + else + { + System.out.println("Invalid command."); + } + } + catch (Exception e) + { + System.out.println("Invalid input."); + } + + if(!command.equals("EXIT")) + System.out.print("Enter another command: \n"); + + }while(!command.equals("EXIT")); + + System.out.println("Goodbye."); + } + + void printCommands() + { + System.out.println("Add "); + System.out.println("Peek"); + System.out.println("Remove"); + System.out.println("HowMany"); + System.out.println("Panic"); + } + + void add(String emergency) + { + emergencyTasks.add(emergency); + System.out.println(emergency + " add to the list of emergencies"); + } + void peek() + { + if (emergencyTasks.size() > 0) + System.out.println("The next emergency is " + emergencyTasks.peek()); + else + System.out.println("Thankfully, there are no emergencies to view."); + } + void remove() + { + if (emergencyTasks.size() > 0) + { + System.out.println(emergencyTasks.pop() + " has been completed and removed."); + if(emergencyTasks.size() > 0) + System.out.println("The next emergency is " + emergencyTasks.peek()); + else + System.out.println("That was the last emergency."); + } + else + { + System.out.println("Thankfully, there are no emergencies to remove."); + } + } + void howMany() + { + if(emergencyTasks.size() > 0) + System.out.println("There are currently " + emergencyTasks.size()); + else + System.out.println("Thankfully, there are not emergencies right now."); + } + void panic() + { + System.out.println("All emergencies have been cleared. That was a close one."); + emergencyTasks.clear(); + } +} diff --git a/SimpleQueue/src/com/company/SimpleQueue.java b/SimpleQueue/src/com/company/SimpleQueue.java new file mode 100644 index 0000000..895e12c --- /dev/null +++ b/SimpleQueue/src/com/company/SimpleQueue.java @@ -0,0 +1,39 @@ +package com.company; + +import java.util.LinkedList; +import java.util.Queue; + +public class SimpleQueue +{ + public static void main(String[]args) + { + SimpleQueue simpleQueue = new SimpleQueue(); + + simpleQueue.run(); + } + static void run() + { + Queue myQueue = new LinkedList<>(); + + myQueue.add("One"); + myQueue.add("Two"); + myQueue.add("Three"); + myQueue.add("Four"); + myQueue.add("Five"); + + System.out.println(myQueue); + + System.out.println(myQueue.remove()); + + System.out.println(myQueue); + + System.out.println(myQueue.peek()); + System.out.println(myQueue); + System.out.println(myQueue.peek()); + System.out.println(myQueue); + System.out.println(myQueue.peek()); + System.out.println(myQueue); + System.out.println(myQueue.peek()); + System.out.println(myQueue); + } +} diff --git a/SimpleQueue/src/com/company/SimpleStack.java b/SimpleQueue/src/com/company/SimpleStack.java new file mode 100644 index 0000000..578cd7e --- /dev/null +++ b/SimpleQueue/src/com/company/SimpleStack.java @@ -0,0 +1,33 @@ +package com.company; +import java.util.Stack; + +public class SimpleStack +{ + public static void main(String[] args) + { + SimpleStack simpleStack = new SimpleStack(); + simpleStack.run(); + } + void run() + { + Stack myStack = new Stack<>(); + + myStack.add("One"); + myStack.add("Two"); + myStack.add("Three"); + myStack.add("Four"); + myStack.add("Five"); + + System.out.println(myStack); + + System.out.println(myStack.pop()); + + System.out.println(myStack); + + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + + } +} diff --git a/SimpleQueue/src/com/company/TaskHelper.java b/SimpleQueue/src/com/company/TaskHelper.java new file mode 100644 index 0000000..4dbb1e2 --- /dev/null +++ b/SimpleQueue/src/com/company/TaskHelper.java @@ -0,0 +1,216 @@ +package com.company; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class TaskHelper +{ + Queue tasks = new LinkedList<>(); + + public static void main(String[] args) + { + TaskHelper taskHelper = new TaskHelper(); + + taskHelper.run(); + } + public void run() + { + String command = ""; + + System.out.println("Welcome to the task manager. Here are a list of commands. At any time, enter PrintCommmands to view the commands."); + printCommands(); + try + { + do + { + Scanner scanner = new Scanner(System.in); + + String commandLine = scanner.nextLine(); + + String[] commands = commandLine.split(" "); + command = commands[0].toUpperCase(); + + if (command.equals("ADD")) + { + if(commands.length >= 2) + { + addTask(commands); + } + + else + { + System.out.println("I have to know what task to add. Enter some tasks this time."); + } + } + + else if (command.equals("PEEK")) + { + int numTasks = 0; + + if (commands.length == 2) + { + numTasks = Integer.valueOf(commands[1]); + peek(numTasks, tasks); + } + else if(commands.length == 1) + { + peek(numTasks, tasks); + } + + else + { + System.out.println("Invalid input. Enter Peek, then one number."); + } + + } + + else if (command.equals("REMOVE")) + { + int numTasks = 0; + + if (commands.length == 2) + { + numTasks = Integer.valueOf(commands[1]); + remove(numTasks); + } + else if(commands.length == 1) + { + remove(numTasks); + } + + else + { + System.out.println("Invalid input. Enter Remove, then one number."); + } + } + + else if (command.equals("HOWMANY")) + { + howMany(); + } + + else if(command.equals("PRINTCOMMANDS")) + { + printCommands(); + } + + else if (command.equals("FLEE")) + { + flee(); + } + + else if (command.equals("EXIT")) + { + exit(); + } + + else + { + System.out.println("Invalid command."); + } + + if(!command.equals("EXIT")) + System.out.print("Enter another command:"); + + } while (!command.equals("EXIT")); + } + catch(Exception e) + { + System.out.println("Invalid input. Try another command."); + } + } + + public void printCommands() + { + System.out.println("Add ..."); + System.out.println("Peek"); + System.out.println("Remove"); + System.out.println("HowMany"); + System.out.println("Flee"); + } + + public void addTask(String[] taskNames) + { + for(int i = 1;i < taskNames.length;i++) + { + tasks.add(taskNames[i]); + System.out.println("Added " + taskNames[i] + " to the list of tasks."); + } + } + + public void peek(int numTasks, Queue holder) + { + if(tasks.size() == 1 && numTasks == 0) + System.out.println("The next task is " + tasks.peek()); + + else if(tasks.size() == 0) + { + System.out.println("There are no tasks in the list."); + } + + else if(tasks.size() > 1 && numTasks <= tasks.size()) + { + holder = new LinkedList<>(); + + for(String task : tasks) + { + holder.add(task); + } + System.out.println("The next " + numTasks + " tasks are: "); + + for(int i = 0;i < numTasks;i++) + { + System.out.println(holder.poll()); + } + } + + else if(numTasks > tasks.size()) + { + System.out.println("There aren't that many tasks in the list."); + } + else + System.out.println("There are no tasks left in the list."); + } + + public void remove(int numTasks) + { + if(tasks.size() == 1) + { + System.out.println("Removed " + tasks.peek() + " from the list."); + tasks.remove(); + } + else if(tasks.size() > 1 && numTasks <= tasks.size()) + { + System.out.println("Removed the following tasks: "); + + for(int i = 0;i < numTasks;i++) + { + System.out.println(tasks.remove()); + } + } + + else if(numTasks > tasks.size()) + { + System.out.println("There aren't that many tasks in the list."); + } + else + System.out.println("There are no tasks left in the list."); + } + + public void howMany() + { + System.out.println("Tasks left to be completed: " + tasks.size()); + } + + public void flee() + { + tasks.clear(); + System.out.println("All tasks have been cleared from the list."); + } + + public void exit() + { + System.out.println("Thank you for using this task manager. Goodbye."); + } +} diff --git a/SimpleSet/src/com/company/BingoNumbers.java b/SimpleSet/src/com/company/BingoNumbers.java new file mode 100644 index 0000000..c2f4b7a --- /dev/null +++ b/SimpleSet/src/com/company/BingoNumbers.java @@ -0,0 +1,249 @@ +package com.company; + +import java.util.HashSet; +import java.util.Random; +import java.util.Scanner; +import java.util.Set; + +public class BingoNumbers +{ + static Set bingoNumbers = new HashSet<>(); + static Set bingoNumbersString = new HashSet<>(); + static boolean isValidNumber = false; + public static void main(String[] args) + { + BingoNumbers bingoNumbers = new BingoNumbers(); + + bingoNumbers.run(); + } + private static void run() + { + Scanner scanner = new Scanner(System.in); + System.out.println("Let's play some bingo. Enter one of the following commands: "); + printCommands(); + String command = ""; + + do + { + try + { + String entered = scanner.nextLine(); + String[] commandLine; + commandLine = entered.split(" "); + + command = commandLine[0].toUpperCase(); + + isValidNumber = false; + + if (commandLine.length > 1 && Integer.parseInt(commandLine[1]) > 0 && Integer.parseInt(commandLine[1]) <= 75) + { + isValidNumber = true; + } + + if (command.equals("CALL")) + { + if(commandLine.length > 1) + { + int numberEntered = Integer.parseInt(commandLine[1]); + call(numberEntered); + } + else + { + Random random = new Random(); + isValidNumber = true; + int bingoNumber = random.nextInt(75) + 1; + call(bingoNumber); + } + } + + + + else if (command.equals("CALLED")) + { + called(); + } + + else if (command.equals("VERIFY")) + { + int numberEntered = Integer.parseInt(commandLine[1]); + verify(numberEntered); + } + + else if (command.equals("TOGO")) + { + toGo(bingoNumbers); + } + + else if (command.equals("CHALLENGE")) + { + int numberEntered = Integer.parseInt(commandLine[1]); + challenge(numberEntered); + } + + else if (command.equals("BINGO")) + { + bingo(); + } + + else + { + System.out.println("Invalid input. Enter another command."); + } + } + catch(Exception e) + { + System.out.println("Invalid input. Enter another command."); + } + + }while(command != "EXIT"); + + + System.out.println("Thanks for playing Bingo. Goodbye!"); + } + + private static void printCommands() + { + System.out.println("Call <1 - 75>"); + System.out.println("Called"); + System.out.println("Verify <1 - 75>"); + System.out.println("Challenge <1 - 75>"); + System.out.println("ToGo"); + System.out.println("Bingo!"); + System.out.println("Exit"); + } + + private static void call(int bingoNumber) + { + if(bingoNumbers.contains(bingoNumber)) + { + printNumber(bingoNumber); + System.out.println(" has already been called. Try another one."); + } + else if (isValidNumber) + { + bingoNumbers.add(bingoNumber); + bingoNumbersString.add(getBingoLetter(bingoNumber) + String.valueOf(bingoNumber)); + System.out.print("Added "); + printNumber(bingoNumber); + System.out.println(" to the list of Bingo numbers"); + } + + else + { + System.out.println("I can't add that number. Enter a number between 1 and 75."); + } + } + + private static void called() + { + if(bingoNumbers.size() > 0) + { + System.out.println("List of numbers called so far: "); + System.out.println(bingoNumbersString); + } + + else + { + System.out.println("No numbers have been called."); + } + } + + private static void verify(int bingoNumber) + { + if(isValidNumber && bingoNumbers.contains(bingoNumber)) + { + System.out.println(bingoNumber + "has already bee called."); + } + + else if(isValidNumber && !bingoNumbers.contains(bingoNumber)) + { + System.out.println("That number has not been called yet."); + } + else if(!isValidNumber) + { + System.out.println("I can't verify that number. Enter a number between 1 and 75."); + } + } + + private static void challenge(int bingoNumber) + { + if(isValidNumber && bingoNumbers.contains(bingoNumber)) + { + bingoNumbers.remove(bingoNumber); + System.out.println(bingoNumber + " removed from the list of called numbers."); + } + else + { + System.out.println("I can't remove that number. Either it hasn't been called yet, or it is an invalid number."); + } + } + + private static void toGo(Set calledNumbers) + { + if(bingoNumbers.size() >= 75) + { + System.out.println("All numbers have been called. Surely you got a bingo."); + } + else + { + Set allBingoNumbers = new HashSet<>(); + Set allBingoNumbersString = new HashSet<>(); + + for (int i = 1; i <= 75; i++) + { + allBingoNumbers.add(i); + } + + allBingoNumbers.removeAll(calledNumbers); + for (int x : allBingoNumbers) + { + allBingoNumbersString.add(getBingoLetter(x) + x); + } + + System.out.println("Numbers that have not been called yet: " + allBingoNumbersString); + } + } + + private static void bingo() + { + if(bingoNumbers.size() >= 4) + { + System.out.println("Congratulations! You win a fruit cake."); + bingoNumbers.clear(); + } + else + { + System.out.println("Cheater! You haven't even called four numbers. You couldn't possibly have won."); + } + } + + private static void printNumber(int number) + { + if (number > 0 && number <= 15) + System.out.print("B" + number); + if (number >= 16 && number <= 30) + System.out.print("I" + number); + if (number >= 31 && number <= 45) + System.out.print("N" + number); + if (number >= 46 && number <= 60) + System.out.print("G" + number); + if (number >= 61 && number <= 75) + System.out.print("O" + number); + } + + private static String getBingoLetter(int number) + { + if (number > 0 && number <= 15) + return "B"; + if (number >= 16 && number <= 30) + return "I"; + if (number >= 31 && number <= 45) + return "N"; + if (number >= 46 && number <= 60) + return "G"; + if (number >= 61 && number <= 75) + return "O"; + else + return ""; + } +} diff --git a/SimpleSet/src/com/company/SimpleSet.java b/SimpleSet/src/com/company/SimpleSet.java new file mode 100644 index 0000000..2bf1888 --- /dev/null +++ b/SimpleSet/src/com/company/SimpleSet.java @@ -0,0 +1,30 @@ +package com.company; + +import java.util.HashSet; +import java.util.Set; + +public class SimpleSet +{ + public static void main(String[] args) + { + SimpleSet simpleSet = new SimpleSet(); + + simpleSet.run(); + } + + static void run() + { + Set mySet = new HashSet<>(); + + for(int i=1;i<6;i++) + { + mySet.add(i); + } + + System.out.println(mySet); + + mySet.add(3); + + System.out.println(mySet); + } +} diff --git a/ch02/Arithmetic.java b/ch02/Arithmetic.java new file mode 100644 index 0000000..294aebe --- /dev/null +++ b/ch02/Arithmetic.java @@ -0,0 +1,19 @@ +public class Arithmetic +{ + public static void main(String[] args) + { + int num = 100; + int factor = 20; + int sum = 0; + + sum = num + factor; //100 + 20 + System.out.println("Addition sum: " + sum); + sum=num - factor; //100 - 20 + System.out.println("Subtraction sum: " + sum); + + sum = num * factor; //100 * 20 + System.out.println("Multiplication sum: " + sum); + sum = num / factor; //100 / 20 + System.out.println("Division sum: " + sum); + } +} diff --git a/ch02/Assignment.java b/ch02/Assignment.java new file mode 100644 index 0000000..5cdd12c --- /dev/null +++ b/ch02/Assignment.java @@ -0,0 +1,22 @@ +public class Assignment +{ + public static void main(String[] args) + { + String txt = "Fantastic "; + String lang = "Java"; + txt += lang; //Assign concatenated String + System.out.println("Add & Assign Strings: " + txt); + + int sum = 10; + int num = 20; + sum += num; //Assign result(10+20 = 30) + System.out.println("Add & Assign Integers " + sum); + + int factor = 5; + sum *= factor; //Assign result(30 *= 5) + System.out.println("Multiplication sum: " + sum); + + sum /= factor; //Assign result (150 / 5 = 30) + System.out.println("Division sum: " + sum); + } +} diff --git a/ch02/Constants.java b/ch02/Constants.java new file mode 100644 index 0000000..45d1259 --- /dev/null +++ b/ch02/Constants.java @@ -0,0 +1,22 @@ +public class Constants +{ + public static void main(String[] args) + { + //Constant score values + final int TOUCHDOWN = 6; + final int CONVERSION = 1; + final int FIELDGOAL = 3; + + int td, pat, fg, total; + + //Calculate points scored + td=4*TOUCHDOWN; + pat=3*CONVERSION; + fg=2*FIELDGOAL; + total=(td+pat+fg); + + //Output calculated total + System.out.println("Score: " + total); + + } +} diff --git a/ch02/DataTypes.java b/ch02/DataTypes.java new file mode 100644 index 0000000..96dfafe --- /dev/null +++ b/ch02/DataTypes.java @@ -0,0 +1,17 @@ +public class DataTypes +{ + public static void main(String[] args) + { + char letter = 'm'; + String title = "Java in easy steps"; + int number = 365; + float decimal = 98.6f; + boolean result = true; + + System.out.println("Initial is "+letter); + System.out.println("Book is "+title); + System.out.println("Days are "+number); + System.out.println("Temperature is "+decimal); + System.out.println("ANswer is "+result); + } +} diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..6b23235 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,15 @@ +public class Date +{ + public static void main(String[] args) + { + String day = "Tuesday"; + int date = 17; + String month = "April"; + int year = 2018; + + System.out.println("American format:"); + System.out.println(day + ", " + month + " " + date + ", " + year); + System.out.println("European format:"); + System.out.println(day + " " + date + " " + month + " " + year); + } +} diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..e2da165 --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,12 @@ +public class DoubleByZero +{ + public static void main(String[] args) + { + double first = 42.0; + double second = 0.0; + double result = first/second; + + System.out.println(result); + + } +} diff --git a/ch02/FirstVariable.java b/ch02/FirstVariable.java new file mode 100644 index 0000000..2c5d785 --- /dev/null +++ b/ch02/FirstVariable.java @@ -0,0 +1,11 @@ +public class FirstVariable +{ + public static void main(String[] args) + { + String message = "Initial value"; + System.out.println(message); + + message="Modified value"; + System.out.println(message); + } +} diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..4c85b36 --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,11 @@ +public class IntByZero +{ + public static void main(String[] args) + { + int first = 42; + int second = 0; + int result = first/second; + + System.out.println(result); + } +} diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..80463a5 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,19 @@ +public class IntExtremes +{ + public static void main(String[] args) + { + int positiveInt = 2147483647; + System.out.println(positiveInt); + + positiveInt++; + + System.out.println(positiveInt); + + int negativeInt = -2147483648; + System.out.println(negativeInt); + + negativeInt--; + + System.out.println(negativeInt); + } +} diff --git a/ch02/LoveJava.java b/ch02/LoveJava.java new file mode 100644 index 0000000..cbb6753 --- /dev/null +++ b/ch02/LoveJava.java @@ -0,0 +1,12 @@ +public class LoveJava +{ + public static void main(String[] args) + { + System.out.println("I love Java!"); + String one = "I "; + String two = "love "; + String three = "Java!"; + + System.out.println(one + two + three ); + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..c3fcc90 --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,25 @@ +public class Time +{ + public static void main(String[] args) + { + int hour = 14; + int minute = 50; + int second = 45; + + int secSinceMidnight; + int secRemaining; + + secSinceMidnight = (60*60*14) + (50*60) + 45; + System.out.println("Seconds since midnight: " + secSinceMidnight); + + secRemaining = (24*60*60) - secSinceMidnight; + System.out.println("Seconds until the day ends: " + secRemaining); + + hour = 14; + minute = 55; + second = 30; + + int timeTaken = ((55-50) * 60) + (15+30); + System.out.println("Seconds it took me to complete this exercise: " + timeTaken); + } +} diff --git a/ch02/Variables.java b/ch02/Variables.java index a295abf..c48ea0e 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -1,10 +1,12 @@ /** * Examples from Chapter 2. */ -public class Variables { - - public static void main(String[] args) { +public class Variables +{ + public static void main(String[] args) + { + //Josiah String message; int x; @@ -59,7 +61,7 @@ public static void main(String[] args) { System.out.println(0.1 * 10); System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 - + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); + + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); double balance = 123.45; // potential rounding error int balance2 = 12345; // total number of cents @@ -78,6 +80,7 @@ public static void main(String[] args) { hour = minute + 1; // correct // minute + 1 = hour; // compiler error + } } diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..1d616a7 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,23 @@ +public class Withdrawal +{ + public static void main(String[] args) + { + int withdrawal = 137; + int twenty; + int ten; + int five; + int one; + + twenty = withdrawal / 20; + withdrawal -= twenty * 20; + + ten = withdrawal / 10; + withdrawal -= ten * 10; + + five = withdrawal / 5; + withdrawal -= five * 5; + + System.out.printf(("$20 (%d), $10 (%d), $5 (%d), $1 (%d)"), twenty, ten, five, withdrawal); + } + +} diff --git a/ch03/CelsToFar.java b/ch03/CelsToFar.java new file mode 100644 index 0000000..b16c125 --- /dev/null +++ b/ch03/CelsToFar.java @@ -0,0 +1,19 @@ +import java.util.Scanner; +public class CelsToFar + +{ + public static void main(String[] args) + { + double cels; + double far; + + Scanner input = new Scanner(System.in); + + System.out.print("Enter a value in degrees Celcius: "); + cels = input.nextDouble(); + + far = cels * (9/5) + 32; + + System.out.printf("%.1f C = %.1f F", cels, far); + } +} diff --git a/ch03/Escape.java b/ch03/Escape.java new file mode 100644 index 0000000..e0fb5cd --- /dev/null +++ b/ch03/Escape.java @@ -0,0 +1,15 @@ +public class Escape +{ + public static void main(String[] args) + { + String header = "\n\t NEW YORK 3-DAY FORECAST:\n"; + header+="\n\tDay\t\tHigh\tLow\tConditions\n"; + header+="\t---\t\t---\t---\t----------\n"; + + String forecast = "\tSunday\t\t68F\t48F\tSunny\n"; + forecast+= "\tMonday\t\t69F\t57F\tSunny\n"; + forecast+="\tTuesday\t\t71F\t50F\tCloudy"; + + System.out.println((header+forecast)); + } +} diff --git a/ch03/TimeConversion.java b/ch03/TimeConversion.java new file mode 100644 index 0000000..764be40 --- /dev/null +++ b/ch03/TimeConversion.java @@ -0,0 +1,29 @@ +import java.util.Scanner; +public class TimeConversion +{ + public static void main(String[] args) + { + int secInput; + int hr; + int min; + int secConverted; + final int SEC_PER_MIN = 60; + final int SEC_PER_HR = SEC_PER_MIN * 60; + + Scanner input = new Scanner(System.in); + + System.out.print("Enter a number of seconds:"); + secInput = input.nextInt(); + + secConverted = secInput; + + hr = secInput/SEC_PER_HR; + secConverted = secInput%SEC_PER_HR; + + min = secConverted/SEC_PER_MIN; + secConverted = secConverted%SEC_PER_MIN; + + System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds", secInput, hr, min, secConverted); + + } +} diff --git a/ch04/BigMethodSignature.java b/ch04/BigMethodSignature.java new file mode 100644 index 0000000..82a630b --- /dev/null +++ b/ch04/BigMethodSignature.java @@ -0,0 +1,16 @@ +public class BigMethodSignature +{ + public static void main(String[] args) + { + printSum(1,2,3,4,5,6,7,8,9,10); + printSum(67,65,45,4,34,3,2,34,67,8); + } + + public static void printSum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) + { + int sum = a + b + c + d + e + f + g + h + i + j; + + System.out.print("The sum of " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + ", "); + System.out.println(g + ", " + h + ", " + i + ", and " + j + ", " + " is " + sum); + } +} diff --git a/ch04/DemoMath.java b/ch04/DemoMath.java new file mode 100644 index 0000000..59b7db5 --- /dev/null +++ b/ch04/DemoMath.java @@ -0,0 +1,21 @@ +public class DemoMath +{ + public static void main(String[] args) + { + int a = -4; + int b = 8; + double c = 3.5; + + System.out.println("Value of int a: " + a); + System.out.println("Absolute value of int a: " + Math.abs(a)); + + System.out.println("Value of int a: " + a); + System.out.println("Value of int b: " + b); + System.out.println("Max value of int a and int b: " + Math.max(a,b)); + + System.out.println("Value of double c: " + c); + System.out.println("Double c to the power of c: " + Math.pow(c,c)); + + System.out.println("Value of pi: " + Math.PI); + } +} diff --git a/ch04/Employee.java b/ch04/Employee.java new file mode 100644 index 0000000..712e7fa --- /dev/null +++ b/ch04/Employee.java @@ -0,0 +1,68 @@ +import java.util.Scanner; +import java.util.Random; +public class Employee +{ + public static void main(String[] args) + { + int birthYear = 1999; + boolean isUnionMember = true; + String fName = "Josiah"; + String mName = "David"; + String lName = "Douglas"; + int employeeNumber; + Scanner scanner = new Scanner(System.in); + + printHeader(); + System.out.println("Please enter your 5 digit employee number:"); + employeeNumber = scanner.nextInt(); + + printFullName(fName,mName,lName); + + printUnionStatus(isUnionMember); + + printAge(birthYear); + + printEvenOrOdd(employeeNumber); + + printGenerateSecretPassword(employeeNumber); + } + + public static void printHeader() + { + System.out.println("Welcome to the WallabyTech Employee Application"); + System.out.println("==============================================="); + } + + public static void printFullName(String first, String middle, String last) + { + System.out.println(last + ", " + first + " " + middle); + } + + public static void printUnionStatus(boolean unionMember) + { + System.out.println("Your union status is: " + unionMember ); + } + + public static void printAge(int age) + { + age = 2018 - age; + System.out.println("Your age is: " + age); + } + + public static void printEvenOrOdd(int evenOdd) + { + evenOdd %=2; + System.out.println("Employee number is : " + evenOdd); + } + + public static void printGenerateSecretPassword(int eNum) + { + Random random = new Random(); + + int ranNum = random.nextInt(10) + 1; + int password = (eNum + ranNum) * 5; + + System.out.println("Employee's random secret pw is: " + password); + } + +} diff --git a/ch04/MathUtil.java b/ch04/MathUtil.java new file mode 100644 index 0000000..b5676fd --- /dev/null +++ b/ch04/MathUtil.java @@ -0,0 +1,21 @@ +public class MathUtil +{ + public static void main(String[] args) + { + printDifference(1000,4000000); + + } + + public static void printDifference(int x, int y) + { + int difference = x - y; + System.out.println("The difference of " + x + " and " + y + " is " + difference); + printAbsValue(difference); + } + + public static void printAbsValue(int x) + { + System.out.println("Value is: " + x + " and abs value is: " + Math.abs(x)); + } + +} diff --git a/ch04/PrintAmerican.java b/ch04/PrintAmerican.java new file mode 100644 index 0000000..d82f7dd --- /dev/null +++ b/ch04/PrintAmerican.java @@ -0,0 +1,24 @@ +public class PrintAmerican +{ + public static void main(String[] args) + { + String day = "Wednesday"; + int date = 18; + String month = "April"; + int year = 2018; + + printAmerican(day, date, month, year); + System.out.println(); + printEuropean(day, date, month, year); + } + + public static void printAmerican(String day, int date, String month, int year) + { + System.out.println("American date: " + day + ", " + month + " " + date + ", " + year); + } + + public static void printEuropean(String day, int date, String month, int year) + { + System.out.println("European date: " + day + " " + date + " " + month + " " + year); + } +} diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java new file mode 100644 index 0000000..054c611 --- /dev/null +++ b/ch04/SimpleMethods.java @@ -0,0 +1,31 @@ +public class SimpleMethods +{ + public static void main(String[] args) + { + printCount(5); + + printSum(4,6); + printSum(7,2); + + printBoolean(true); + printBoolean(false); + + } + + public static void printCount(int count) + { + System.out.println("The count is: " + count); + } + + public static void printSum(int x, int y) + { + System.out.println(x + " + " + y + " = " + (x+y)); + } + + public static void printBoolean(boolean isStudent) + { + System.out.println("I am a student: " + isStudent); + } + +} + diff --git a/ch05/Comparison.java b/ch05/Comparison.java new file mode 100644 index 0000000..e2891a0 --- /dev/null +++ b/ch05/Comparison.java @@ -0,0 +1,24 @@ +public class Comparison +{ + public static void main(String[] args) + { + String txt = "Fantastic "; + String lang = "Java"; + boolean state = (txt==lang); + + System.out.println("String Equality Test: " + state); + + state = (txt!=lang); + System.out.println("String Inequality Test: " + state); + + int dozen = 12; + int score = 20; + state = (dozen>score); + + System.out.println("Greater Than Test: " + state); + + state = (dozen < score); + + System.out.println("Less Than Test: " + state); + } +} diff --git a/ch05/Condition.java b/ch05/Condition.java new file mode 100644 index 0000000..b9cbeaf --- /dev/null +++ b/ch05/Condition.java @@ -0,0 +1,14 @@ +public class Condition +{ + public static void main(String[] args) + { + int num1 = 1357; + int num2 = 2468; + + String result = (num1 % 2 != 0)? "Odd" : "Even"; + System.out.println(num1 + " is " + result); + + result = (num2 % 2 != 0)? "Odd" : "Even"; + System.out.println(num2 + " is " + result); + } +} diff --git a/ch05/CrazyEd.java b/ch05/CrazyEd.java new file mode 100644 index 0000000..0f0e6e2 --- /dev/null +++ b/ch05/CrazyEd.java @@ -0,0 +1,89 @@ +import java.util.Scanner; + +public class CrazyEd +{ + public static void main(String[] args) + { + calculateCost(); + } + private static void calculateCost() + + { + final int oneInchCost = 2; + final int twoInchCost = 4; + final int threeInchCost = 6; + final int oneInchShip = 2; + final int twoInchShip = 2; + final int threeInchShip = 4; + final int handlingCharge = 5; + int initialCost = 0; + int totalShip = 0; + int totalCost = 0; + + Scanner input = new Scanner(System.in); + + System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); + System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); + + int cheeseSize = input.nextInt(); + input.nextLine(); + + if (cheeseSize > 3 || cheeseSize < 1) + { + System.out.println("This order is too crazy. Try a value between 1 and 3."); + calculateCost(); + } + + else + + { + System.out.print("OK, so you want the diameter of your string cheese to be " + cheeseSize + "."); + System.out.println(" How many yards would you like to order?"); + + int cheeseLength = input.nextInt(); + input.nextLine(); + + System.out.println("So, you want " + cheeseLength + " yards of " + cheeseSize + " inch diameter string cheese."); + + if (cheeseSize == 1) + { + + initialCost = (oneInchCost * cheeseLength); + totalShip = (oneInchShip * cheeseLength); + } else if (cheeseSize == 2) + { + + initialCost = (twoInchCost * cheeseLength); + totalShip = (twoInchShip * cheeseLength); + } else if (cheeseSize == 3) + { + + initialCost = (threeInchCost * cheeseLength); + totalShip = (threeInchShip * cheeseLength); + } + + if (cheeseSize == 1 && cheeseLength > 50) + { + totalShip = 0; + } + + if (cheeseSize == 2 && cheeseLength > 75) + { + totalShip = 0; + } + + if (cheeseSize == 3 && cheeseLength > 25) + { + totalShip = 0; + } + + totalCost = initialCost + totalShip + handlingCharge; + + System.out.println("Cost of cheese: $" + initialCost); + System.out.println("Cost of shipping: $" + totalShip); + System.out.println("Handling charge: $" + handlingCharge); + System.out.println("Total cost: $" + totalCost); + + } + } +} diff --git a/ch05/Else.java b/ch05/Else.java new file mode 100644 index 0000000..648a130 --- /dev/null +++ b/ch05/Else.java @@ -0,0 +1,18 @@ +public class Else +{ + public static void main(String[] args) + { + int hrs = 15; + + if(hrs<13) + { + System.out.println("Good morning: " + hrs); + } + + else + { + System.out.println("Good evening: " + hrs); + } + + } +} diff --git a/ch05/FreeCoffee.java b/ch05/FreeCoffee.java new file mode 100644 index 0000000..ed23084 --- /dev/null +++ b/ch05/FreeCoffee.java @@ -0,0 +1,58 @@ + +public class FreeCoffee +{ + public static void main(String[] args) + { + System.out.print("Bicycle rider commuting 20 miles: "); + calculateDiscount(true, false, 20); + System.out.println(); + + System.out.print("Bicycle rider commuting 30 miles: "); + calculateDiscount(true, false, 30); + System.out.println(); + + System.out.print("Bicycle rider commuting 52 miles: "); + calculateDiscount(true, false, 52); + System.out.println(); + + System.out.print("Bus rider commuting 35 miles: "); + calculateDiscount(false, true, 35); + System.out.println(); + + System.out.print("Bus rider commuting 50 miles: "); + calculateDiscount(false, true, 50); + } + + private static void calculateDiscount(boolean isBicycle, boolean isBus, int distance) + { + float discount = 0; + String coffee = ""; + + if(distance < 21) + { + coffee = " and you get free coffee!"; + } + + if(isBicycle && distance < 30) + { + discount += .1; + } + + if(isBus && distance >= 35) + { + discount += .2; + } + + if(isBicycle && distance < 50) + { + discount += .2; + } + + if(isBus && distance < 50) + { + discount += .3; + } + + System.out.println("Your discount is " + discount + "0%" + coffee); + } +} diff --git a/ch05/If.java b/ch05/If.java new file mode 100644 index 0000000..1d8309e --- /dev/null +++ b/ch05/If.java @@ -0,0 +1,25 @@ +public class If +{ + public static void main(String[] args) + { + if(5<1) + { + System.out.println("Five is greater than one"); + } + + if(2<4) + { + System.out.println("Two is less than four"); + System.out.println("Test succeeded"); + } + + int num = 10; + + if(((num>5)&&(num<10))||(num==12)) + { + System.out.println("Number is 6-9 inclusive, or 12"); + } + + + } +} diff --git a/ch05/Logic.java b/ch05/Logic.java new file mode 100644 index 0000000..423bfa6 --- /dev/null +++ b/ch05/Logic.java @@ -0,0 +1,16 @@ +public class Logic +{ + public static void main(String[] args) + { + boolean yes = true; + boolean no = false; + + System.out.println("Both YesYes True: " + (yes && yes)); + System.out.println("Both YesNo True: " + (yes && no)); + System.out.println("Either YesYes True: " + (yes || yes)); + System.out.println("Either YesNo True: " + (yes || no)); + System.out.println("Eitehr NoNo True: " + (no || no)); + System.out.println("Original Yes Value: " + yes); + System.out.println("Original No Value" + no); + } +} diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..0ff7c09 --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,79 @@ +public class LogicMethods +{ + public static void main(String[] args) + { + int x = 9; + int y = 9; + + printIsLarge(x); + printIsLargeOrSmall(x); + printLargest(x,y); + printLargestOdd(x,y); + } + + private static void printIsLarge(int number) + { + if(number>99) + { + System.out.println("The number is large"); + } + } + + private static void printIsLargeOrSmall(int number) + { + if(number>99) + { + System.out.println("The number is large"); + } + + else if(number<10) + { + System.out.println("The number is small"); + } + } + + private static void printLargest(int number1, int number2) + { + if(number1>number2) + { + System.out.println("The largest number is: " + number1); + } + + else if(number2>number1) + { + System.out.println("The largest number is: " + number2); + } + + else if(number1 == number2) + { + System.out.println("The numbers are equal"); + } + } + + private static void printLargestOdd(int number1, int number2) + { + if(number1 % 2 == 1 || number2 % 2 == 1) + { + if(number1>number2) + { + System.out.println("The largest number odd number is: " + number1); + } + + else if(number2>number1) + { + System.out.println("The largest odd number is: " + number2); + } + } + + else if(number1 % 2 == 0 && number2 % 2 == 0) + { + System.out.println("Neither number is odd"); + } + + if(number1 % 2 == 1 && number2 % 2 == 1 && number1 == number2) + { + int sum = number1 + number2; + System.out.println("Two odds make an even: " + sum); + } + } +} diff --git a/ch05/Precedence.java b/ch05/Precedence.java new file mode 100644 index 0000000..a7d3068 --- /dev/null +++ b/ch05/Precedence.java @@ -0,0 +1,17 @@ +public class Precedence +{ + public static void main(String[] args) + { + int sum = 32 - 8 + 16 * 2; + + System.out.println("Default order: " + sum); + + sum = (32 - 8 + 16) * 2; + + System.out.println("Specified order: " + sum); + + sum = (32 - (8 + 16)) * 2; + + System.out.println("Nested specified order: " + sum); + } +} diff --git a/ch05/Switch.java b/ch05/Switch.java new file mode 100644 index 0000000..0a55553 --- /dev/null +++ b/ch05/Switch.java @@ -0,0 +1,17 @@ +public class Switch +{ + public static void main(String[] args) + { + int month = 2; + int year = 2018; + int num = 31; + + switch(month) + { + case 4: case 6: case 9: case 11: num = 30; break; + case 2: num = (year%4 == 0) ? 29 : 28; break; + } + + System.out.println(month + "/" + year + ": " + num + " days"); + } +} diff --git a/ch05/SwitchExample.java b/ch05/SwitchExample.java new file mode 100644 index 0000000..e54e7f8 --- /dev/null +++ b/ch05/SwitchExample.java @@ -0,0 +1,41 @@ +public class SwitchExample +{ + public static void main(String[] args) + { + lastNameWinner("lazenby"); + dayOfWeek(-2); + } + + private static void lastNameWinner(String lName) + { + switch(lName) + { + case "smith": case "Jones": System.out.println("Congratulations grand winner"); break; + case "lazenby": System.out.println("Hey, he owes me dinner"); break; + default: System.out.println("Sorry, not a winner"); break; + } + } + + private static void dayOfWeek(int day) + { + switch (day) + { + case 1: + System.out.println("Sunday"); break; + case 2: + System.out.println("Monday"); break; + case 3: + System.out.println("Tuesday"); break; + case 4: + System.out.println("Wednesday"); break; + case 5: + System.out.println("Thursday"); break; + case 6: + System.out.println("Friday"); break; + case 7: + System.out.println("Saturday"); break; + default: + System.out.println("Invalid value: " + day); + } + } +} diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..3dfd9f8 --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,27 @@ +import java.util.Scanner; +public class TicketNumber +{ + public static void main(String[] args) + { + Scanner input = new Scanner(System.in); + + System.out.print("Enter your ticket number: "); + int ticketNumber = input.nextInt(); + + int lastDigit = ticketNumber % 10; + //System.out.println(lastDigit); + + int ticketPrefix = ticketNumber / 10; + //System.out.println(ticketPrefix); + + int ticketPrefixRemainder = ticketPrefix % 7; + //System.out.println(ticketPrefixRemainder); + + boolean result = lastDigit == ticketPrefixRemainder; + + if(result) + System.out.println("Good number"); + else + System.out.println("Bad number"); + } +} diff --git a/ch06/IsDivisible.java b/ch06/IsDivisible.java new file mode 100644 index 0000000..3f83f8a --- /dev/null +++ b/ch06/IsDivisible.java @@ -0,0 +1,38 @@ +public class IsDivisible +{ + public static void main(String[] args) + { + int x = 34; + int y = 5; + + int a = 11; + int b = 1; + int c = 12; + + System.out.println(isDivisible(x,y)); + System.out.println(isTriangle(a, b, c)); + } + + private static boolean isDivisible(int n, int m) + { + boolean result = false; + + if(n % m == 0) + { + result = true; + } + return result; + } + + private static boolean isTriangle(int a, int b, int c) + { + boolean result = true; + + if(a > b + c || b > a + c || c > a + b) + { + result = false; + } + + return result; + } +} diff --git a/ch06/MathUtil.java b/ch06/MathUtil.java new file mode 100644 index 0000000..707227d --- /dev/null +++ b/ch06/MathUtil.java @@ -0,0 +1,34 @@ +import java.util.Scanner; +public class MathUtil +{ + public static void main(String[] args) + { + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter two integers:"); + int x = scanner.nextInt(); + int y = scanner.nextInt(); + int result = absoluteSum(x,y); + + System.out.println("The sum of the absolute values of those integers is: " + result); + + System.out.println("Now enter three integers: "); + x = scanner.nextInt(); + y = scanner.nextInt(); + int z = scanner.nextInt(); + + result = absoluteSum(x,y,z); + + System.out.println("The sum of the absolute vales of those three integers is: " + result); + } + + private static int absoluteSum(int x, int y) + { + return Math.abs(x) + Math.abs(y); + } + + private static int absoluteSum(int x, int y, int z) + { + return Math.abs(x) + Math.abs(y) + Math.abs(z); + } +} diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..bd517cc --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,17 @@ +public class Multadd +{ + public static void main(String[] args) + { + double a = 1.0; + double b = 2.0; + double c = 3.0; + + System.out.println(multadd(a,b,c)); + + } + + private static double multadd(double a, double b, double c) + { + return a * b + c; + } +} diff --git a/ch07/DoWhile.java b/ch07/DoWhile.java new file mode 100644 index 0000000..f06b502 --- /dev/null +++ b/ch07/DoWhile.java @@ -0,0 +1,13 @@ +public class DoWhile +{ + public static void main(String[] args) + { + int num = 100; + + do + { + System.out.println("DoWhile Countup: " + num); + num += 10; + }while(num < 10); + } +} diff --git a/ch07/ForLoop.java b/ch07/ForLoop.java new file mode 100644 index 0000000..ae0082e --- /dev/null +++ b/ch07/ForLoop.java @@ -0,0 +1,16 @@ +public class ForLoop +{ + public static void main(String[] args) + { + int num = 0; + for(int i = 0;i < 4;i++) + { + System.out.println("Outer loop i = " + i); + for(int j = 1; j < 4;j++) + { + System.out.print("\tInner Loop j = " + j); + System.out.println("\t\tTotal num = " + (++num)); + } + } + } +} diff --git a/ch07/Loop7C.java b/ch07/Loop7C.java new file mode 100644 index 0000000..f4b3b68 --- /dev/null +++ b/ch07/Loop7C.java @@ -0,0 +1,39 @@ +public class Loop7C +{ + public static void main(String[] args) + { + System.out.println("For loop:"); + forLoop(); + System.out.println("While loop:"); + whileLoop(); + System.out.println("Do while loop:"); + doWhileLoop(); + } + + private static void forLoop() + { + for(int i = 100;i > -101; i -= 8) + System.out.println(i); + } + + private static void whileLoop() + { + int i = 100; + + while(i > -101) + { + System.out.println(i); + i -= 8; + } + } + + private static void doWhileLoop() + { + int i = 100; + do + { + System.out.println(i); + i -= 8; + }while(i > -101); + } +} diff --git a/ch07/Loops7A.java b/ch07/Loops7A.java new file mode 100644 index 0000000..16ccdb9 --- /dev/null +++ b/ch07/Loops7A.java @@ -0,0 +1,73 @@ +public class Loops7A +{ + public static void main(String[] args) + { + System.out.println("For Loop Up:"); + forLoopUp(); + System.out.println("For Loop Down:"); + forLoopDown(); + System.out.println("While Loop Up:"); + whileLoopUp(); + System.out.println("While Loop Down:"); + whileLoopDown(); + System.out.println("Do While UP:"); + doWhileLoopUp(); + System.out.println("Do While Down:"); + doWhileLoopDown(); + } + + private static void forLoopUp() + { + for(int i = 1;i < 11; i++) + System.out.println(i); + } + + private static void forLoopDown() + { + for(int i = 10; i > 0; i--) + { + System.out.println(i); + } + } + + private static void whileLoopUp() + { + int i = 1; + + while(i < 11) + { + System.out.println(i); + i++; + } + } + + private static void whileLoopDown() + { + int i = 10; + while(i > 0) + { + System.out.println(i); + i--; + } + } + + private static void doWhileLoopUp() + { + int i = 1; + do + { + System.out.println(i); + i++; + }while(i < 11); + } + + private static void doWhileLoopDown() + { + int i = 10; + do + { + System.out.println(i); + i--; + }while(i > 0); + } +} diff --git a/ch07/Loops7B.java b/ch07/Loops7B.java new file mode 100644 index 0000000..071dac2 --- /dev/null +++ b/ch07/Loops7B.java @@ -0,0 +1,39 @@ +public class Loops7B +{ + public static void main(String[] args) + { + System.out.println("For loop:"); + forLoopUp(); + System.out.println("While loop:"); + whileLoopUp(); + System.out.println("Do while loop:"); + doWhileLoopUp(); + } + + private static void forLoopUp() + { + for(int i = 0;i < 101; i += 10) + System.out.println(i); + } + + private static void whileLoopUp() + { + int i = 0; + + while(i < 101) + { + System.out.println(i); + i +=10; + } + } + + private static void doWhileLoopUp() + { + int i = 0; + do + { + System.out.println(i); + i += 10; + }while(i < 101); + } +} diff --git a/ch07/Loops7D.java b/ch07/Loops7D.java new file mode 100644 index 0000000..54819ff --- /dev/null +++ b/ch07/Loops7D.java @@ -0,0 +1,15 @@ +public class Loops7D +{ + public static void main(String[] args) + { + loop(178); + } + + private static void loop(int x) + { + for(int i = 1; i <= x; i++) + { + System.out.println(i); + } + } +} diff --git a/ch07/Loops7E.java b/ch07/Loops7E.java new file mode 100644 index 0000000..fc01b78 --- /dev/null +++ b/ch07/Loops7E.java @@ -0,0 +1,14 @@ +import java.util.Scanner; +public class Loops7E +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + int input; + System.out.println("Enter a number: "); + do + { + input = in.nextInt(); + }while(input != 0); + } +} diff --git a/ch07/Loops7F.java b/ch07/Loops7F.java new file mode 100644 index 0000000..e27d54e --- /dev/null +++ b/ch07/Loops7F.java @@ -0,0 +1,21 @@ +import java.util.Scanner; + +public class Loops7F +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + int total = 0; + int input; + while(total < 1000) + { + System.out.print("Enter a number:"); + input = in.nextInt(); + total += input; + if(total < 1000) + System.out.println("The total so far is: " + total); + } + + System.out.println("The total is: " + total); + } +} diff --git a/ch07/Loops7G.java b/ch07/Loops7G.java new file mode 100644 index 0000000..7816df5 --- /dev/null +++ b/ch07/Loops7G.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +public class Loops7G +{ + public static void main(String[] args) + { + Scanner input = new Scanner(System.in); + + System.out.println("Enter a number:"); + int maxInput = input.nextInt(); + System.out.print(0 + " | "); + + for(int i = 1; i <= maxInput; i++) + { + System.out.printf("%5d", i); + } + System.out.println(); + System.out.print("- \t"); + + for(int i = 1; i <= maxInput; i++) + { + System.out.print("-----"); + } + + System.out.println(); + for(int i = 1; i <= maxInput; i++) + { + if(i<10) + System.out.print(i + " | "); + else if(i>=10&&i<100) + System.out.print(i + " | "); + else + System.out.print(i + "| "); + for(int j = 1; j <= maxInput ; j++) + { + System.out.printf("%5d", i * j); + } + System.out.println(); + } + + } + + +} diff --git a/ch07/While.java b/ch07/While.java new file mode 100644 index 0000000..7a2ac6f --- /dev/null +++ b/ch07/While.java @@ -0,0 +1,13 @@ +public class While +{ + public static void main(String[] args) + { + int num = 100; + + while(num > 0) + { + System.out.println("While countdown: " + num); + num -= 10; + } + } +} diff --git a/ch08/Array.java b/ch08/Array.java new file mode 100644 index 0000000..6da2d6f --- /dev/null +++ b/ch08/Array.java @@ -0,0 +1,17 @@ +public class Array +{ + public static void main(String[] args) + { + String[] str = {"Much", "More", "Java"}; + int[] num = new int[3]; + num[0] = 100; + num[1] = 200; + + str[1] = "Better"; + + System.out.println("String array length is: " + str.length); + System.out.println("Integer array length is: " + num.length); + System.out.println(num[0] + "," + num[1] + "," + num[2]); + System.out.println(str[0] + str[1] + str[2]); + } +} diff --git a/ch08/ArrayDemo.java b/ch08/ArrayDemo.java new file mode 100644 index 0000000..2555245 --- /dev/null +++ b/ch08/ArrayDemo.java @@ -0,0 +1,82 @@ +public class ArrayDemo +{ + public static void main(String[] args) + { + int[] array = {1,5,9}; + double[] doubleArray = {10,15.0,20}; + String[] stringArray = new String[10]; + stringArray[0] = "Hi"; + stringArray[3] = "Hello"; + stringArray[9] = "Bye"; + printArray(array); + printTotal(array); + System.out.println("Array max is: " + arrayMax(array)); + System.out.println("Max index is: " + arrayMaxIndex(array)); + System.out.println("The average is: " + arrayAverage(doubleArray)); + printArray(stringArray); + } + + private static void printArray(int[] array) + { + for(int i=0;imax) + max=array[i]; + } + return max; + } + + private static int arrayMaxIndex(int[]array) + { + int index=0; + int max=0; + for(int i=0;imax) + index=i; + } + return index; + } + + private static double arrayAverage(double[] array) + { + int total=0; + double arrayLength = array.length; + for(int i=0;i 0) + return false; + prevCount = count; + } + } + return true; + } + + private static String[] sort(String[] str) + { + + String holder; + for(int i=0;i0) + { + holder=str[i]; + str[i]=str[j]; + str[j]=holder; + } + + } + } + + return str; + } + +} diff --git a/ch09/StringUtilSilver.java b/ch09/StringUtilSilver.java new file mode 100644 index 0000000..cd0746e --- /dev/null +++ b/ch09/StringUtilSilver.java @@ -0,0 +1,198 @@ +import java.util.Arrays; + +public class StringUtilSilver +{ + public static void main(String[] args) + { + printPhoneNumber("565-786-5677"); + System.out.println("Reverse Hello: " + reverse("Hello")); + System.out.println("Is Palindrome: " + isPalindrome("")); + System.out.println("Starts with learn: " + allLetters("dog")); + String[] names = {"t"}; + System.out.println("All letters no dupes: " + allLettersNoDupes(names)); + } + + private static boolean allLettersNoDupes(String[] x) + { + if(x.length==0) + return false; + if(x.length==1&&x[0].substring(0, 1).equals("l")||x[0].substring(0, 1).equals("e")||x[0].substring(0, 1).equals("a")||x[0].substring(0, 1).equals("r")||x[0].substring(0, 1).equals("n")) + return true; + + int count=0; + for(int i=0;i