Lab 5: 2D Arrays and Imagesdue Thursday, October 10 at 11:59pm on Canvas Pair Programming Extra Credit (+5pts E.C.)
Image Processing
Starter Code:
import java.io.*; import java.util.Scanner; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class ImageManip { public static void main(String[] args) throws IOException { int[][] img_array; File file; BufferedImage img; String fileName; Scanner input = new Scanner(System.in); System.out.println("Welcome!\n"); System.out.print("Enter the name of an image file (jpg extension): "); } } How to Read in an Image File and Extract Its Values:
Converting to Grayscale (Black-and-White):
int average = (R + B + G) / 3
int a = (pixel >> 24) & 255; int r = (pixel >> 16) & 255; int g = (pixel >> 8) & 255; int b = pixel & 255;
1111 1101 0001 1001 >> 4 yields 0000 1111 1101 0001
pixel = (a << 24) | (avg << 16) | (avg << 8) | avg;
Converting to Sepia:
newRed = 0.393*R + 0.769*G + 0.189*B
newGreen = 0.349*R + 0.686*G + 0.168*B
newBlue = 0.272*R + 0.534*G + 0.131*B
If any of these output values is greater than 255,
simply set it to 255. Update the RGB to contain the newRed, newGreen, and newBlue values (use bitwise shifts and & and | as above)
Sample Output:
Welcome! Enter the name of an image file (jpg extension): helicopter2.jpg Thank you! Check bw.jpg for a black and white image of helicopter2.jpg, and sepia.jpg for a sepia image of helicopter2.jpg What to Submit: When you are satisfied your program is working properly, submit the following to Canvas:
How You Will Be Graded:
Lab 12: Abstract Classes and Polymorphismdue Monday, February 24 at 9:20am on Canvas Tic-Tac-Toe
* Tic-Tac-Toe Game * @author * @author * CIS 36B */ import java.util.Random; import java.util.Scanner; public class TicTacToe { /** * Initializes the board by assigning all array elements * the value of '_' (underscore) * @param board the array representing the tic-tac-toe board */ public static void setUpBoard(char board[]) { } /** * Capitalizes the x or o entered by a player * Ignores already capitalized X and O * @param character the x or o * @return the capitalized X or O */ public static char capitalizeXO(String character) { return '\0'; } /** * Prints the board to the console in the form of a grid, * including column and row numbers and the message Tic-Tac-Toe: * @param board the array representing the tic-tac-toe board */ public static void displayBoard(char board[]) { System.out.print("\nTic-Tac-Toe:\n "); } /** * Converts the player choice of row and column * in the form of RC into the correct index of * the board array. * Hint: Use integer division by 10 to extract the row * Hint: Use modulus by 10 to extract the column * @param rowCol the row and column, e.g. 12 or 33 * @return the correct index of the array that * corresponds to the row and column */ public static int convertToIndex(int rowCol) { return -1; } /** * Determines whether a particular position * on the board has already been taken. * @param board the array representing the game board * @param position the position to check * @return whether that position has already been taken */ public static boolean spotTaken(char board[], int position) { return false; } /** * Selects a location for the computer AI's next * move, using the following algorithm: * 1. Selects the center position (4) if it is open * 2. Otherwise, blocks the player provided the player played * in the center position and now has two in a row * including the center * 3. Otherwise, generates a random position from * which to play * @param board the game board * @param player the character (X or O) of the player * @return the position (0-8) on the board * at which the computer should play next */ public static int findAIPlacement(char board[], char player) { return -1; } /** * A helper method to findAIPlacement * Gives a random position on the board * Used for generating moves for the computer * @param size the length of the board array * @return a random index in the board array */ public static int randomPosition(int size) { Random r = new Random(System.currentTimeMillis()); return r.nextInt(size); } /** * Determines whether three characters are all Xs or all Os * Used as a helper method to the gameOver method * @param a the first character to compare, either X, O, or - * @param b the second character to compare, either X, O, or - * @param c the third character to compare, either X, O or - * @return whether the characters are all Xs or all Os */ public static boolean threeInRow(char a, char b, char c) { return false; } /** * Determines whether the game is over * due to one player winning, using * a series of if statements. * Calls the threeInRow method for each * possible row on the board. * @param board the tic-tac-toe game board * @return whether the game is over */ public static boolean gameOverWinner(char board[]) { return false; } /** * Determines whether the game is over * due to a tie. * Compares numMoves to the length. * @param board the tic-tac-toe game board * @param numMoves the number of moves that have been made so far * @return whether the game is over */ public static boolean gameOverTie(char board[], int numMoves) { return false; } public static void main(String[] args) { System.out.println("Welcome to Tic-Tac-Toe!"); char board[] = new char[9]; int numMoves = 0; //increments when player or game A.I. makes a move } } Welcome to Tic-Tac-Toe! Welcome to Tic-Tac-Toe!Would you like to play as X or O: X Tic-Tac-Toe: 1 2 3 1 _ _ _ 2 _ _ _ 3 _ _ _ Please enter your move: 22 Tic-Tac-Toe: 1 2 3 1 _ _ _ 2 _ X _ 3 _ _ _ Counter move! Tic-Tac-Toe: 1 2 3 1 O _ _ 2 _ X _ 3 _ _ _ Please enter your move: 21 Tic-Tac-Toe: 1 2 3 1 O _ _ 2 X X _ 3 _ _ _ Counter move! Tic-Tac-Toe: 1 2 3 1 O _ _ 2 X X O 3 _ _ _ Please enter your move: 12 Tic-Tac-Toe: 1 2 3 1 O X _ 2 X X O 3 _ _ _ Counter move! Tic-Tac-Toe: 1 2 3 1 O X _ 2 X X O 3 _ O _ Please enter your move: 13 Tic-Tac-Toe: 1 2 3 1 O X X 2 X X O 3 _ O _ Counter move! Tic-Tac-Toe: 1 2 3 1 O X X 2 X X O 3 O O _ Please enter your move: 33 Tic-Tac-Toe: 1 2 3 1 O X X 2 X X O 3 O O X It's a tie! ***Game Over*** Would you like to play as X or O: o Tic-Tac-Toe: 1 2 3 1 _ _ _ 2 _ _ _ 3 _ _ _ Please enter your move: 11 Tic-Tac-Toe: 1 2 3 1 O _ _ 2 _ _ _ 3 _ _ _ Counter move! Tic-Tac-Toe: 1 2 3 1 O _ _ 2 _ X _ 3 _ _ _ Please enter your move: 12 Tic-Tac-Toe: 1 2 3 1 O O _ 2 _ X _ 3 _ _ _ Counter move! Tic-Tac-Toe: 1 2 3 1 O O _ 2 X X _ 3 _ _ _ Please enter your move: 13 Tic-Tac-Toe: 1 2 3 1 O O O 2 X X _ 3 _ _ _ O wins! ***Game Over*** Welcome to Tic-Tac-Toe! Would you like to play as X or O: x Tic-Tac-Toe: 1 2 3 1 _ _ _ 2 _ _ _ 3 _ _ _ Please enter your move: 22 Tic-Tac-Toe: 1 2 3 1 _ _ _ 2 _ X _ 3 _ _ _ Counter move! Tic-Tac-Toe: 1 2 3 1 _ O _ 2 _ X _ 3 _ _ _ Please enter your move: 22 That spot is already taken! Please enter your move: 12 That spot is already taken! Please enter your move: 33 Tic-Tac-Toe: 1 2 3 1 _ O _ 2 _ X _ 3 _ _ X Counter move! Tic-Tac-Toe: 1 2 3 1 O O _ 2 _ X _ 3 _ _ X Please enter your move: 31 Tic-Tac-Toe: 1 2 3 1 O O _ 2 _ X _ 3 X _ X Counter move! Tic-Tac-Toe: 1 2 3 1 O O O 2 _ X _ 3 X _ X O wins! ***Game Over*** What to Submit: When you are satisfied your program is working properly, submit the following to Canvas:
Lab 1.2: Halley's Comet (80 pts)
240BC,
164BC, 87BC, 12BC, 66, 141, 218, 295, 374, 451, 530, 607, 684, 760,
837, 912, 989, 1066, 1145, 1222, 1301, 1378, 1456, 1531, 1607, 1682,
1758, 1835, 1910, 1986, 2061, 2134,
public static int binarySearch(int[] dates, int year) {
Welcome! Enter a year between 240BC and 2200 or 'x' to exit: 67 Halley's Comet did not appear in the year 67. Enter a year between 240BC and 2200 or 'x' to exit: 66 Halley's Comet did appear in the year 66. Enter a year between 240BC and 2200 or 'x' to exit: 2020 Halley's Comet will not appear in the year 2020. Enter a year between 240BC and 2200 or 'x' to exit: 2061 Halley's Comet will appear in the year 2061. Enter a year between 240BC and 2200 or 'x' to exit: 87BC Halley's Comet did appear in the year 87BC. Enter a year between 240BC and 2200 or 'x' to exit: 88BC Halley's Comet did not appear in the year 88BC. Enter a year between 240BC and 2200 or 'x' to exit: 300BC Invalid date! Enter a year between 240BC and 2200 or 'x' to exit: 2300 Invalid date! Enter a year between 240BC and 2200 or 'x' to exit: x Goodbye!
What to Submit:
How You Will Be Graded:
Pair Programming Required (or No Credit)
Pac-Cookie!
Game Specifications:
Welcome to Pac-Cookie! Your goal is to eat all remaining cookies (at least 20) - before you escape! But, beware the not-so-friendly ghosts... They will eat the cookies... and YOU! Ready to play? Let's go! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): u Ouch! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r):
How do you want to move? (u/d/l/r): r Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Ouch! Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Ouch! Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r):
Score: 31 Remaining Cookies: 9 H H H H H H H H H H H H H H H H * * H H H H * P H H H H * H H H G * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u
How do you want to move? (u/d/l/r): dd Invalid move! Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r):
Character.java Code:
/** * Character.java * @author * @author * CIS 36B, Lab 12 */ public abstract class Character { private int xPos; private int yPos; /** * Sets the x (column) location of the character * @param x the x position of the character in the x-y plane */ public void setXPos(int x) { xPos = x; } /** * Sets the y (row) location of the character * @param y the y position of the character in the x-y plane */ public void setYPos(int y) { yPos = y; } /** * Returns the x position of the character * @return the x position of the character in the x-y plane */ public int getXPos() { return xPos; } /** * Returns the y position of the character * @return the y position of the character in the x-y plane */ public int getYPos() { return yPos; } /** * Moves the character one space to the left * by subtracting one from its x position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveLeft() { xPos-=1; } /** * Moves the character one space to the right * by adding one to its x position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveRight() { xPos+=1; } /** * Moves the character one space down * by adding one to its y position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveDown() { yPos+=1; } /** * Moves the character one space up * by subtracting one from its y position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveUp() { yPos-=1; } } Player.java
/** * Player.java * @author FILL IN HERE * @author FILL IN HERE * CIS 36B, Lab 12 */ public final class Player extends Character{ private int score; /** * Player default constructor * Gives the player a starting * position of [1,1] on the board * and sets score to 0 */ public Player() { } /** * Returns the player's current score * @return the score */ public int getScore() { return -1; } /** * Updates the player's score by 1 */ public void updateScore() { return; } } Ghost.java:
/** * Ghost.java * @author FILL IN HERE * @author FILL IN HERE * CIS 36B, Lab 12 */ import java.util.Random; public class Ghost extends Character{ private int numMoves; int max; /** * One argument constructor * Calls setXPos() and setYPos() * to give the ghost its initial * placement at the bottom right * corner of the board [max-2, max-2] * Also initializes max (the max * x and y dimension on the board) * and numMove to 0 * @param max the */ public Ghost(int max) { } /** * Moves the Ghost according to an * algorithim: * If the number of moves it has made * is divisible by 10, it generates a * random move. Otherwise, it moves up * until it cannot go farther. Then, * it moves left until it can go no * farther. * Once it reaches the [1,1] * corner it jumps back to its starting * position. Updates numMoves. */ public void move() { } /** * Places the ghost at a random * board position */ public void generateRandomMove() { } } Game.java:
/** * Game.java * @author FILL IN HERE * @author FILL IN HERE * CIS 36B, Lab 12 */ import java.util.Scanner; public class Game { private int upperX; private int upperY; private int totalCookies; private Ghost g; private Player p; private String board[][]; /** * Constructor for the Game class * Initializes private variables * Calls initialize board to intialize * the board. Places characters at their * starting positions. * Note that there are 53 cookies on * the board at the start of the game */ public Game() { } /** * Initializes the board to *s for cookies * and Hs for walls * Called by the constructor */ private void initializeBoard() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if( i == 0 || j == 0) { board[i][j] = "H"; } else if (j == board.length - 1 || i == board.length - 1) { board[i][j] = "H"; } else if (j == board.length / 3 && i > 2 && i < 8 ) { board[i][j] = "H"; } else if (j == (2 * board.length) / 3 && i > 2 && i < 8 ) { board[i][j] = "H"; } else { board[i][j] = "*"; } } } } /** * Places the player and ghost at a * new spot on the board */ public void updateBoard() { return; } /** * Replaces the current location of the * player and ghost with two blank spaces * unless the ghost is walking through a wall * in which case, only the player's space * is cleared * Hint: See initializeBoard method for * placement of interior walls */ public void clearSpace() { return; } /** * Prints out board as shown in sample output * including displaying the current score and * the number of remaining cookies */ public void printBoard() { return; } /** * Determines whether the player has lost by either * being eaten by the ghost or because there * are not enough cookies left on the board to win * @return whether the player has lost */ public boolean gameOverLose() { return false; } /** * Determines whether the player has won * the game by scoring at least 20 points, * eating all possible cookies, and * escaping from the hole in the leftside * wall * @return whether the player has won */ public boolean gameOverWin() { return false; } public static void main(String[] args) { Scanner input = new Scanner(System.in); String choice = ""; System.out.println("Welcome to Pac-Cookie!\n"); System.out.println("Your goal is to eat all remaining cookies (at least 20) - before you escape!"); System.out.println("But, beware the not-so-friendly ghosts..." + "\nThey will eat the cookies... and YOU!"); System.out.println("\nReady to play? Let's go!\n"); Game game = new Game(); //Add your code here! input.close(); } } Base Game (Before Spooky Added) Sample Output: Example 1: Winning the Game: Your goal is to eat all remaining cookies (at least 20) - before you escape! But, beware the not-so-friendly ghosts... They will eat the cookies... and YOU! Ready to play? Let's go! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): u Ouch! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 1 Remaining Cookies: 51 H H H H H H H H H H H * * * * * * * H H P * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 2 Remaining Cookies: 49 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H * H * * H * * H H P * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 4 Remaining Cookies: 45 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H * H * * H * * H H * H * * H * G H H P * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 5 Remaining Cookies: 43 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H * H * * H * G H H * H * * H * H H * H * * H * H H P * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 6 Remaining Cookies: 41 H H H H H H H H H H H * * * * * * * H H * * * * * * G H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 7 Remaining Cookies: 39 H H H H H H H H H H H * * * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 8 Remaining Cookies: 37 H H H H H H H H H H H * * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 9 Remaining Cookies: 35 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 10 Remaining Cookies: 34 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H P H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 11 Remaining Cookies: 33 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H P H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 12 Remaining Cookies: 32 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H P H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 30 H H H H H H H H H H H * * * G H H * * * * * * H H P H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 14 Remaining Cookies: 28 H H H H H H H H H H H * * G H H P * * * * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 15 Remaining Cookies: 26 H H H H H H H H H H H * G H H P * * * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 16 Remaining Cookies: 24 H H H H H H H H H H H G H H P * * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 17 Remaining Cookies: 23 H H H H H H H H H H H G H H P * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 18 Remaining Cookies: 22 H H H H H H H H H H H H H P * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 19 Remaining Cookies: 21 H H H H H H H H H H H H H P H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * G H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 20 Remaining Cookies: 20 H H H H H H H H H H H H H H H H * * H P H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 21 Remaining Cookies: 19 H H H H H H H H H H H H H H H H * * H H H H * * H P H H H * * H * H H H * * H * H H H * * H * G H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 22 Remaining Cookies: 18 H H H H H H H H H H H H H H H H * * H H H H * * H H H H * * H P H H H * * H * G H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 23 Remaining Cookies: 17 H H H H H H H H H H H H H H H H * * H H H H * * H H H H * * H G H H H * * H P H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 24 Remaining Cookies: 16 H H H H H H H H H H H H H H H H * * H H H H * * H G H H H * * H H H H * * H H H H * * H P H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 25 Remaining Cookies: 15 H H H H H H H H H H H H H H H H * * H G H H H * * H H H H * * H H H H * * H H H H * * H H H * * * * P * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 26 Remaining Cookies: 14 H H H H H H H H H H H H H G H H H * * H H H H * * H H H H * * H H H H * * H H H H * * H H H * * * P * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 27 Remaining Cookies: 13 H H H H H H H H H H H G H H H H H * * H H H H * * H H H H * * H H H H * * H H H H * * H H H * * P * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 28 Remaining Cookies: 12 H H H H H H H H H H H G H H H H H * * H H H H * * H H H H * * H H H H * * H H H H * P H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 29 Remaining Cookies: 11 H H H H H H H H H H H G H H H H H * * H H H H * * H H H H * * H H H H * P H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 30 Remaining Cookies: 10 H H H H H H H H H H H H H H H H * * H H H H * * H H H H * P H H H H * H H H G * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 31 Remaining Cookies: 9 H H H H H H H H H H H H H H H H * * H H H H * P H H H H * H H H G * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 32 Remaining Cookies: 8 H H H H H H H H H H H H H H H H * P H H H H * H H H G * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Ouch! Score: 32 Remaining Cookies: 8 H H H H H H H H H H H H H H H H * P H H H H * H H H G * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 33 Remaining Cookies: 7 H H H H H H H H H H H H H H H H P H H H G * H H H H * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 34 Remaining Cookies: 6 H H H H H H H H H H H H H H H G H H H H P H H H H * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 35 Remaining Cookies: 5 H H H H H H H H H H H H H G H H H H H H H H H H H P H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 36 Remaining Cookies: 4 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H P H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 37 Remaining Cookies: 3 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H P H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 38 Remaining Cookies: 2 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H H H * P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 38 Remaining Cookies: 2 H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H * P G * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 38 Remaining Cookies: 2 H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H G H H H * P * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H H H H H H H H H H H H H G H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H H H H H H H H H G H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H H H H H G H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 40 Remaining Cookies: 0 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H H H P H H H H H H H H H H Congratulations! You win! Final score: 40 Example 2: Losing the Game: Your goal is to eat all remaining cookies (at least 20) - before you escape! But, beware the not-so-friendly ghosts... They will eat the cookies... and YOU! Ready to play? Let's go! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 1 Remaining Cookies: 51 H H H H H H H H H H H P * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 2 Remaining Cookies: 49 H H H H H H H H H H H P * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 3 Remaining Cookies: 47 H H H H H H H H H H H P * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 4 Remaining Cookies: 45 H H H H H H H H H H H P * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 5 Remaining Cookies: 43 H H H H H H H H H H H P * * H H * * * * * * * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 6 Remaining Cookies: 41 H H H H H H H H H H H P * H H * * * * * * * G H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 7 Remaining Cookies: 39 H H H H H H H H H H H G H H * * * * * * * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H You lose! Example 3: Losing the Game: How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 8 H H H H H H H H H H H H H * H H * H H H H G H H H H H * H H H P * H * H H H H * H H H * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 7 H H H H H H H H H H H H H * H H G H H H H H H H H P H * H H H * H * H H H H * H H H * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 6 H H H H H H H H H H H H H G H H H H H H P H H H H H * H H H * H * H H H H * H H H * * H H H H H H H H H H You lose! What to Submit:
How You Will Be Graded:
Pair Programming Required (Or No Credit)
Lab 6.1: The Strung Class (100 pts)
Strung.java Starter Code: /** * @author * @author * CIS 36B, Lab 6.1 */ public class Strung { private char[] array; private int numChars; /** * Constructor for the Strung class * @param s a String to convert * to a Strung */ public Strung(String s) { } /** * Constructor of the Strung class * @param a an array of chars */ public Strung(char[] a) { } /** * Copy constructor for the Strung class * @param s another Strung * Makes a DEEP copy */ public Strung(Strung s) { } /** * Returns the char at the given position * @param i the position of the char * @return the char located at i or a * null character in the case of error */ public char charAt(int i) { return '\0'; //the null character } /** * Returns the location of a char * @param c the char to locate * @return the location of c */ public int indexOf(char c) { return -1; } /** * Returns the number of characters * @return the number of characters */ public int length() { return -1; } /** * Determines if a specified char is * contained within the Strung * @param c the char to locate * @return whether c is in Strung */ public boolean contains(char c) { return false; } /** * Returns a smaller Strung from start to end * @param start starting index in the Strung * @param end ending index in the Strung * @return another Strung or null in the case * of error */ public Strung substrung(int start, int end) { return null; } /** * Returns a smaller Strung from start * to the end of the Strung * @param start starting index in the Strung * @return another Strung or null in * the case of error */ public Strung substrung(int start) { return null; } /** * Converts this Strung to * all lower case characters */ public void toLowerCase() { } /** * Converts this Strung to * all upper case characters */ public void toUpperCase() { } /** * ToString method for the Strung class * @return a String of all characters * with no spaces in between any character */ @Override public String toString(){ return null; } /** * Determines whether this Strung * is equal to another o * Two Strungs are equal if they contain * the same chars in the same order */ @Override public boolean equals(Object o){ return false; } } StrungTest.java: /**
* @author parrishj * CIS 36B, Lab 6.1 */ public class StrungTest { public static void main(String[] args) { /*Constructors*/ System.out.println("\n***Testing Constructors***"); Strung s = new Strung("ABC"); System.out.println("Testing String Constructor: Should print ABC: " + s); Strung s1 = new Strung(""); System.out.println("Testing String Constructor: Should print an empty String: " + s1); Strung s2 = new Strung(s); System.out.println("Testing Copy Constructor: Should print ABC: " + s2); s2.toLowerCase(); System.out.println("Testing For Deep Copy: Copy should print abc: " + s2); System.out.println("Testing For Deep Copy: Original should print ABC: " + s); char[] array = {'a', 'b', 'c', 'd', 'e'}; Strung s3 = new Strung(array); System.out.println("Testing Array Constructor: Should print abcde: " + s3); /*CharAt*/ System.out.println("\n***Testing CharAt()***"); System.out.println("Should print b: " + s3.charAt(1)); System.out.println("Should print b: " + s2.charAt(1)); System.out.print("Should print error: "); System.out.println(s.charAt(10)); /*Substrung(start, end)*/ System.out.println("\n***Testing Substrung(start, end)***"); System.out.println("Should print b: " + s2.substrung(1,2)); System.out.println("Should print abc: " + s3.substrung(0,3)); System.out.print("Should print error message: "); s3.substrung(4, 3); System.out.print("Should print error message: "); s3.substrung(-1, 10); /*Substrung(start)*/ System.out.println("\n***Testing Substrung(start)***"); System.out.println("Should print bc: " + s2.substrung(1)); System.out.println("Should print cde: " + s3.substrung(2)); System.out.print("Should print error message: "); s3.substrung(-1); System.out.print("Should print error message: "); s3.substrung(10); /*Contains*/ System.out.println("\n***Testing Contains***"); System.out.println("Should print true: " + s2.contains('a')); System.out.println("Should print false: " + s2.contains('f')); /*IndexOf*/ System.out.println("\n***Testing IndexOf***"); System.out.println("Should print 0: " + s3.indexOf('a')); System.out.println("Should print 3: " + s3.indexOf('d')); System.out.println("Should print -1: " + s3.indexOf('f')); /*Length*/ System.out.println("\n***Testing Length***"); System.out.println("Should print 3: " + s.length()); System.out.println("Should print 0: " + s1.length()); System.out.println("Should print 3: " + s2.length()); System.out.println("Should print 5: " + s3.length()); /*ToLowerCase*/ System.out.println("\n***Testing ToLowerCase***"); s.toLowerCase(); System.out.println("Should print abc: " + s); /*ToUpperCase*/ System.out.println("\n***Testing ToUpperCase***"); s.toUpperCase(); System.out.println("Should print ABC: " + s); s2.toUpperCase(); System.out.println("Should print ABC: " + s2); /*Equals*/ System.out.println("\n***Testing Equals***"); System.out.println("Should print true: " + s.equals(s2)); s2.toLowerCase(); System.out.println("Should print false: " + s.equals(s2)); System.out.println("Should print false: " + s2.equals(s3)); System.out.println("Should print false: " + s.equals("ABC")); } } StrungTest Output: Your Output must be IDENTICAL to the output below for full credit: ***Testing Constructors*** Testing String Constructor: Should print ABC: ABC Testing String Constructor: Should print an empty String: Testing Copy Constructor: Should print ABC: ABC Testing For Deep Copy: Copy should print abc: abc Testing For Deep Copy: Original should print ABC: ABC Testing Array Constructor: Should print abcde: abcde ***Testing CharAt()*** Should print b: b Should print b: b Should print error: CharAt Error: Index must be in the range 0 to 2 ***Testing Substrung(start, end)*** Should print b: b Should print abc: abc Should print error message: Substrung Error: starting index must be less than ending index Should print error message: Substrung Error: Index must be in the range 0 to 4 ***Testing Substrung(start)*** Should print BC: bc Should print cde: cde Should print error message: Substrung Error: Index must be in the range 0 to 4 Should print error message: Substrung Error: Index must be in the range 0 to 4 ***Testing Contains*** Should print true: true Should print false: false ***Testing IndexOf*** Should print 0: 0 Should print 3: 3 Should print -1: -1 ***Testing Length*** Should print 3: 3 Should print 0: 0 Should print 3: 3 Should print 5: 5 ***Testing ToLowerCase*** Should print abc: abc ***Testing ToUpperCase*** Should print ABC: ABC Should print ABC: ABC ***Testing Equals*** Should print true: true Should print false: false Should print false: false Should print false: false What to Submit:
How You Will Be Graded:
Lab 6.2: Valued Rewards Members (100 pts)
Jiming Wu F 4082123458 jwu@gmail.com abc123 RM 987684 James Brown M 8315678432 jaime.boy.brown@hotmail.com jjjbbb123 NRM 0 Leanna Perez F 4087654433 leaperez@yahoo.com letmein NRM 0 Xing Li M 8313214555 xing.li@sbcglobal.net 555!!!hi NRM 0 Stacey Cahill O 8312123333 scahill@gmail.com r0tr@tr^n RM 945544 Mohammed Abbas M 4083134444 downtown_abbas@yahoo.com 1@m@bb@s RM 945575 Kumari Chakrabarti F 4086667777 kukuchakrabarti@hotmail.com passw0rd NRM 0 Shakil Smith M 4082123333 shakattaq@gmail.com qwerty RM 945682 Jung Ahrin F 8319257788 jung_ahrin@yahoo.com password1 NRM 0 Pedro Martinez M 4086162323 pedro2018@sbcglobal.net 123456789 RM 945873 Ally Gu O 4089256776 i_am_ally@comcast.net trustno1 NRM 0 Tamara White F 8317778978 tamtastic@gmail.com 1qaz2wsx RM 956689 Alvin Ngo M 4089256677 drngo@hotmail.com qwertyuiop NRM 0 Abir Fadel M 8316645325 greatabs@comcast.com qazwsx RM 942211 Brad Feinman M 8312023443 bradisrad@gmail.com 11111 NRM 0 Xiaohang Yue M 8318990033 yue95@yahoo.com adobe123 NRM 0
public int linearSearch(String email, String password, ArrayList<RewardMember> rm)
public static void bubblesort(ArrayList<User> users) public static int binarySearch(String email, String password, ArrayList<User> users) {
Sample Output - Example Showing User Who is Not a Reward Member: Welcome! We are proudly serving 16 users and counting! Enter your email address: kukuchakrabarti@hotmail.com Enter your password: passw0rd Hi, Kumari Chakrabarti! We have the following information on file for you: Name: Kumari Chakrabarti Gender: F Phone: (408)666-7777 Email: kukuchakrabarti@hotmail.com Sample Output - Example Showing User Who is a Reward Member: Welcome! We are proudly serving 16 users and counting! Enter your email address: pedro2018@sbcglobal.net Enter your password: 123456789 Welcome valued reward member, Pedro Martinez! We have the following information on file for you: Name: Pedro Martinez Gender: M Phone: (408)616-2323 Email: pedro2018@sbcglobal.net Member Number: 945873 Thank you for being one of our 7 valued reward members. To show our appreciation, enjoy 10% off all shopping today! Have a great day! Sample Output - Example Showing a New User Who Becomes a Reward Member: Welcome! We are proudly serving 16 users and counting! Enter your email address: myEmail@gmail.com Enter your password: iforgetit Sorry! We don't have your account on file. Let's create an account for you! Enter your name: Joe Schmoe Enter your gender: M Enter your 10 digit phone number (no spaces or punctuation): 1112223344 Thank you, Joe Schmoe! Your account has now been created. Name: Joe Schmoe Gender: M Phone: (111)222-3344 Email: myEmail@gmail.com Would you like to join our Reward Member program and save 10% off all purchases today? Enter yes or no: Yes Thank you! In exchange for becoming a valued member, we reserve the right to collect detailed information about you and your shopping habits and sell this information to other companies. Here is your new member number: 99557 Have a great day! Sample Output - Example Showing a New User Who Does Not Become a Reward Member: We are proudly serving 16 users and counting! Enter your email address: myEmail@gmail.com Enter your password: iforgetit Sorry! We don't have your account on file. Let's create an account for you! Enter your name: Joe Schmoe Enter your gender: M Enter your 10 digit phone number (no spaces or punctuation): 1112223344 Thank you, Joe Schmoe! Your account has now been created. Name: Joe Schmoe Gender: M Phone: (111)222-3344 Email: myEmail@gmail.com Would you like to join our Reward Member program and save 10% off all purchases today? Enter yes or no: no Goodbye! What to Submit:
How You Will Be Graded:
Sign in|Report Abuse|Print Page|Powered By Google Sites Lab 8.1: A Generic Container Class (100 pts)
/** * Default constructor * Creates a new Container of length 10 */ @SuppressWarnings("unchecked") public Container() { numItems = 0; array = (E[]) new Object[10]; }
/** * Container.java * @author * @author * CIS 36B, Lab 8.1 */ public class Container<E> { private int numItems; private E[] array; /**Constructors*/ /** * Default constructor * Creates a new Container of length 10 */ @SuppressWarnings("unchecked") public Container() { numItems = 0; array = (E[]) new Object[10]; } /** * Constructor for Container class * @param initialCapacity the initial * length of the Container * Creates a Container of length * initialCapacity */ @SuppressWarnings("unchecked") public Container(int initialCapacity) { } /** * Copy constructor for Container class * @param c another Container * Creates a Container that is a * copy of c */ @SuppressWarnings("unchecked") public Container(Container<E> c) { } /**Mutators*/ /** * Shifts all items down to * Insert a new item at the start * of the Container * Calls increaseCapacity when the * adding a new item when * numItems == array.length * @param item a new item */ public void addStart(E item) { } /** * Inserts a new item at the * end of the Container * Calls increaseCapacity when the * adding a new item when * numItems == array.length * @param item a new item */ public void addEnd(E item) { } /** * Inserts a new item at the * specified index of the Container * @param index the index at which * to insert * @param item a new item * Calls increaseCapacity when the * adding a new item when * numItems == array.length * Prints an error message if index < 0 or * index > numItems and returns null */ public void add(int index, E item) { } /** * Removes the first element in the * container * Prints an error message if the * container is empty */ public void removeStart() { } /** * Removes the item at the specified index * @param index of the item to remove * Prints an error message if index is * < 0 or index > numItems */ public void remove(int index) { } /** * Removes the last element in the * container * Prints an error message if the * container is empty */ public void removeEnd() { } /**Accessors*/ /** * Counts the number of times * item appears in the container * @param item the item to search for * @return the number of times * item appears in the container */ public int countOccurences(E item) { int count = 0; return count; } /** * Returns the item stored at * the given index * @param index the index of the * item to return * @return a random item * Prints an error message if index < 0 or * index > numItems and returns null */ public E get(int index) { return null; } /** * Returns the number of items * in the Container * @return the number of items */ public int length() { return 0; } /**Additional Operations*/ /** * Resizes the container by making a new * array that has a length of * newCapacity * @param newCapacity the new length * of the array */ @SuppressWarnings("unchecked") private void increaseCapacity(int newCapacity) { } /** * Searches for the specified item * in the Container * @param item the item to search for * @return the location (index) of the item */ public int search(E item) { return -1; } /** * Determines if two Containers * store the same items in the * same order */ @SuppressWarnings("unchecked") public boolean equals(Object o) { return false; }/** * Creates a String of all items * in the Container, separated by * a blank space, with a new line character * added to the end of the String */ public String toString() { return "\n"; } }
ContainerTest.java File * ContainerTest.java * @author parrishj * CIS 36B, Lab 8.1 */ public class ContainerTest { public static void main(String[] args) { System.out.println("***Testing addStart***\n"); Container<String> c1 = new Container<>(); c1.addStart("A"); c1.addStart("B"); c1.addStart("C"); System.out.println("Should print C B A: " + c1); System.out.println("\n***Testing addEnd***\n"); c1.addEnd("D"); c1.addEnd("E"); System.out.println("Should print C B A D E: " + c1); System.out.println("\n***Testing add***\n"); c1.add(1, "F"); System.out.println("Should print C F B A D E: " + c1); c1.add(3, "Z"); System.out.println("Should print C F B Z A D E: " + c1); System.out.println("Should print an error message: "); c1.add(-1, "A"); System.out.println("\n***Testing increaseCapacity***\n"); c1.addEnd("X"); c1.addEnd("X"); c1.addEnd("X"); c1.addEnd("X"); System.out.println("Should print C F B Z A D E X X X X: " + c1); System.out.println("\n***Testing length***\n"); Container<Integer> c2 = new Container<>(5); System.out.println("Length of 0: " + c2.length()); System.out.println("Length of 11: " + c1.length()); c2.addEnd(1); c2.addEnd(2); c2.addEnd(3); System.out.println("\n***Testing copy constructor***\n"); Container<Integer> c3 = new Container<>(c2); System.out.println("Original should be 1 2 3: " + c2); System.out.println("Copy should be 1 2 3: " + c3); Container<Double> c4 = new Container<>(null); System.out.println("Copy should be empty (nothing displayed): " + c4); System.out.println("\n***Testing for deep copy***\n"); c3.addEnd(4); System.out.println("Original should be 1 2 3: " + c2); System.out.println("Copy should be 1 2 3 4: " + c3); System.out.println("Original length should be 3: " + c2.length()); System.out.println("Copy length should be 4: " + c3.length()); System.out.println("\n***Testing remove start***\n"); c1.removeStart(); System.out.println("Should print F B Z A D E X X X X: " + c1); System.out.println("Should print an error message: "); c4.removeStart(); System.out.println("\n***Testing remove index***\n"); c1.remove(1); System.out.println("Should print F Z A D E X X X X: " + c1); System.out.println("Should print an error message: "); c1.remove(-1); System.out.println("\n***Testing remove end***\n"); c1.removeEnd(); System.out.println("Should print F Z A D E X X X: " + c1); System.out.println("Should print an error message: "); c4.removeEnd(); System.out.println("\n***Testing countOccurences***\n"); System.out.println("Should print 3: " + c1.countOccurences("X")); System.out.println("Should print 0: " + c3.countOccurences(0)); System.out.println("\n***Testing get***\n"); System.out.println("Should print X: " + c1.get(5)); System.out.println("Should print 1: " + c3.get(0)); System.out.println("Should print an error: "); c3.get(-1); System.out.println("\n***Testing search***\n"); System.out.println("Should print 5: " + c1.search("X")); System.out.println("Should print 1: " + c3.search(2)); System.out.println("\n***Testing equals***\n"); System.out.println("Should print false: " + c2.equals(c3)); c3.remove(3); System.out.println("Should print true: " + c2.equals(c3)); System.out.println("Should print false: " + c1.equals(c3)); System.out.println("\n\n***End of Tests!***"); } } Should print C B A: C B A ***Testing addEnd*** Should print C B A D E: C B A D E ***Testing add*** Should print C F B A D E: C F B A D E Should print C F B Z A D E: C F B Z A D E Should print an error message: Add error: index -1 is out of bounds. ***Testing increaseCapacity*** Should print C F B Z A D E X X X X: C F B Z A D E X X X X ***Testing length*** Length of 0: 0 Length of 11: 11 ***Testing copy constructor*** Original should be 1 2 3: 1 2 3 Copy should be 1 2 3: 1 2 3 Copy should be empty (nothing displayed): ***Testing for deep copy*** Original should be 1 2 3: 1 2 3 Copy should be 1 2 3 4: 1 2 3 4 Original length should be 3: 3 Copy length should be 4: 4 ***Testing remove start*** Should print F B Z A D E X X X X: F B Z A D E X X X X Should print an error message: RemoveStart error. Container is empty. Cannot remove ***Testing remove index*** Should print F Z A D E X X X X: F Z A D E X X X X Should print an error message: Remove error: index -1 is out of bounds. ***Testing remove end*** Should print F Z A D E X X X: F Z A D E X X X Should print an error message: RemoveEnd error. Container is empty. Cannot remove ***Testing countOccurences*** Should print 3: 3 Should print 0: 0 ***Testing get*** Should print X: X Should print 1: 1 Should print an error: Get error: Index -1 is out of bounds ***Testing search*** Should print 5: 5 Should print 1: 1 ***Testing equals*** Should print false: false Should print true: true Should print false: false ***End of Tests!*** What to Submit:
How You Will Be Graded:
Pair Programming Required (Or No Credit)
Lab 9.1: A Container Class with Exception Handling (100 pts)due Thursday, June 6 at 11:59pm on Canvas
/** * Returns the item stored at * the given index * @param index the index of the * item to return * @return a random item * @throws IndexOutOfBoundsException * if index < 0 or index > numItems */ public E get(int index) throws IndexOutOfBoundsException { if (index < 0 || index > numItems) { throw new IndexOutOfBoundsException("Get error: Index " + index + " is out of bounds."); } return array[index]; }
/** * Container.java * @author * @author * CIS 36B, Lab 9.1 */ public class Container<E> { private int numItems; private E[] array; /**Constructors*/ /** * Default constructor * Creates a new Container of length 10 */ @SuppressWarnings("unchecked") public Container() { numItems = 0; array = (E[]) new Object[10]; } /** * Constructor for Container class * @param initialCapacity the initial * length of the Container * Creates a Container of length * initialCapacity */ @SuppressWarnings("unchecked") public Container(int initialCapacity) { } /** * Copy constructor for Container class * @param c another Container * Creates a Container that is a * copy of c */ @SuppressWarnings("unchecked") public Container(Container<E> c) { } /**Mutators*/ /** * Shifts all items down to * Insert a new item at the start * of the Container * Calls increaseCapacity when the * adding a new item when * numItems == array.length * @param item a new item */ public void addStart(E item) { } /** * Inserts a new item at the * end of the Container * @param item a new item * Calls increaseCapacity when the * adding a new item when * numItems == array.length */ public void addEnd(E item) { } /** * Inserts a new item at the * specified index of the Container * @param index the index at which * to insert * @param item a new item * @throws IndexOutOfBoundsException * if index < 0 or index > numItems * Calls increaseCapacity when the * adding a new item when * numItems == array.length */ public void add(int index, E item) throws IndexOutOfBoundsException { } /** * Removes the first element in the * container * @throws IllegalStateException * if the container is empty */ public void removeStart() throws IllegalStateException { } /** * Removes the item at the specified index * @param index of the item to remove * @throws IndexOutOfBoundsException * if index < 0 or index > numItems */ public void remove(int index) throws IndexOutOfBoundsException { } /** * Removes the last element in the * container * @throws IllegalStateException * if the container is empty */ public void removeEnd() throws IllegalStateException { } /**Accessors*/ /** * Counts the number of times * item appears in the container * @param item the item to search for * @return the number of times * item appears in the container */ public int countOccurences(E item) { return -1; } /** * Returns the item stored at * the given index * @param index the index of the * item to return * @return a random item * @throws IndexOutOfBoundsException * if index < 0 or index > numItems */ public E get(int index) throws IndexOutOfBoundsException { return null; } /** * Returns the number of items * in the Container * @return the number of items */ public int length() { return -1; } /**Additional Operations*/ /** * Resizes the container by making a new * array that has a length of * newCapacity * @param newCapacity the new length * of the array */ @SuppressWarnings("unchecked") private void increaseCapacity(int newCapacity) { } /** * Searches for the specified item * in the Container * @param item the item to search for * @return the location (index) of the item */ public int search(E item) { return -1; } /** * Determines if two Containers * store the same items in the * same order */ @SuppressWarnings("unchecked") public boolean equals(Object o) { return false; } /** * Creates a String of all items * in the Container, separated by * a blank space, with a new line character * added to the end of the String */ public String toString() { String result = "\n"; return result; } }
try { System.out.println("\n***Testing get***\n"); System.out.println("Should print X: " + c1.get(5)); System.out.println("Should print 1: " + c3.get(0)); System.out.println("Should print an error: "); c3.get(-1); } catch (IndexOutOfBoundsException e) { System.out.println(e); //calling toString() on e }
***Testing get*** Should print X: X Should print 1: 1 Should print an error: java.lang.IndexOutOfBoundsException: Get error: Index -1 is out of bounds.
/** * ContainerTest.java * @author parrishj * CIS 36B, Lab 9.1 */ public class ContainerTest { public static void main(String[] args) { System.out.println("***Testing addStart***\n"); Container<String> c1 = new Container<>(); c1.addStart("A"); c1.addStart("B"); c1.addStart("C"); System.out.println("Should print C B A: " + c1); System.out.println("\n***Testing addEnd***\n"); c1.addEnd("D"); c1.addEnd("E"); System.out.println("Should print C B A D E: " + c1); System.out.println("\n***Testing add***\n"); c1.add(1, "F"); System.out.println("Should print C F B A D E: " + c1); c1.add(3, "Z"); System.out.println("Should print C F B Z A D E: " + c1); System.out.println("Should print an error message: "); c1.add(-1, "A"); System.out.println("\n***Testing increaseCapacity***\n"); c1.addEnd("X"); c1.addEnd("X"); c1.addEnd("X"); c1.addEnd("X"); System.out.println("Should print C F B Z A D E X X X X: " + c1); System.out.println("\n***Testing length***\n"); Container<Integer> c2 = new Container<>(5); System.out.println("Length of 0: " + c2.length()); System.out.println("Length of 11: " + c1.length()); c2.addEnd(1); c2.addEnd(2); c2.addEnd(3); System.out.println("\n***Testing copy constructor***\n"); Container<Integer> c3 = new Container<>(c2); System.out.println("Original should be 1 2 3: " + c2); System.out.println("Copy should be 1 2 3: " + c3); Container<Double> c4 = new Container<>(null); System.out.println("Copy should be empty (nothing displayed): " + c4); System.out.println("\n***Testing for deep copy***\n"); c3.addEnd(4); System.out.println("Original should be 1 2 3: " + c2); System.out.println("Copy should be 1 2 3 4: " + c3); System.out.println("Original length should be 3: " + c2.length()); System.out.println("Copy length should be 4: " + c3.length()); System.out.println("\n***Testing remove start***\n"); c1.removeStart(); System.out.println("Should print F B Z A D E X X X X: " + c1); System.out.println("Should print an error message: "); c4.removeStart(); System.out.println("\n***Testing remove index***\n"); c1.remove(1); System.out.println("Should print F Z A D E X X X X: " + c1); System.out.println("Should print an error message: "); c1.remove(-1); System.out.println("\n***Testing remove end***\n"); c1.removeEnd(); System.out.println("Should print F Z A D E X X X: " + c1); System.out.println("Should print an error message: "); c4.removeEnd(); System.out.println("\n***Testing countOccurences***\n"); System.out.println("Should print 3: " + c1.countOccurences("X")); System.out.println("Should print 0: " + c3.countOccurences(0)); System.out.println("\n***Testing get***\n"); System.out.println("Should print X: " + c1.get(5)); System.out.println("Should print 1: " + c3.get(0)); System.out.println("Should print an error: "); c3.get(-1); System.out.println("\n***Testing search***\n"); System.out.println("Should print 5: " + c1.search("X")); System.out.println("Should print 1: " + c3.search(2)); System.out.println("\n***Testing equals***\n"); System.out.println("Should print false: " + c2.equals(c3)); c3.remove(3); System.out.println("Should print true: " + c2.equals(c3)); System.out.println("Should print false: " + c1.equals(c3)); System.out.println("\n\n***End of Tests!***"); } }
***Testing addStart*** Should print C B A: C B A ***Testing addEnd*** Should print C B A D E: C B A D E ***Testing add*** Should print C F B A D E: C F B A D E Should print C F B Z A D E: C F B Z A D E Should print an error message: java.lang.IndexOutOfBoundsException: Add error: index -1 is out of bounds. ***Testing increaseCapacity*** Should print C F B Z A D E X X X X: C F B Z A D E X X X X ***Testing length*** Length of 0: 0 Length of 11: 11 ***Testing copy constructor*** Original should be 1 2 3: 1 2 3 Copy should be 1 2 3: 1 2 3 Copy should be empty (nothing displayed): ***Testing for deep copy*** Original should be 1 2 3: 1 2 3 Copy should be 1 2 3 4: 1 2 3 4 Original length should be 3: 3 Copy length should be 4: 4 ***Testing remove start*** Should print F B Z A D E X X X X: F B Z A D E X X X X Should print an error message: java.lang.IllegalStateException: RemoveStart error. Container is empty. Cannot remove. ***Testing remove index*** Should print F Z A D E X X X X: F Z A D E X X X X Should print an error message: java.lang.IndexOutOfBoundsException: Remove error: index -1 is out of bounds. ***Testing remove end*** Should print F Z A D E X X X: F Z A D E X X X Should print an error message: java.lang.IllegalStateException: RemoveEnd error. Container is empty. Cannot remove. ***Testing countOccurences*** Should print 3: 3 Should print 0: 0 ***Testing get*** Should print X: X Should print 1: 1 Should print an error: java.lang.IndexOutOfBoundsException: Get error: Index -1 is out of bounds. ***Testing search*** Should print 5: 5 Should print 1: 1 ***Testing equals*** Should print false: false Should print true: true Should print false: false ***End of Tests!***
How You Will Be Graded:
Lab 9.2: Project Proposal (100 pts)due Monday, June 10 at 9:20am on Canvas
Part 2: List Array Class (100 pts) due Monday, May 6 at 9:20am on Canvas
/** * ListArray.java * @author * @author * CIS 36B */ public class ListArray { private String[] array; private int numElements; /**Constructors*/ /** * Default constructor for ListArray * Creates an empty array of length 10 * Sets numElements to 0 */ public ListArray() { } /** * Constructor for ListArray * Creates an empty array of length size * Sets numElements to 0 * @param size the initial size of the * ListArray */ public ListArray(int size) { } /** * Copy constructor for ListArray * Creates a new list array of the * same size as the ListArray passed * in as a parameter, and copies the * parameter's data into the new * list array using a for loop * Also sets the numElements to be * the same as the parameter's * numElements * @param la the ListArray to copy */ public ListArray(ListArray la) { } /**Accessor Methods*/ /** * Returns whether the list array is * currently empty * @return whether the list array is * empty */ public boolean isEmpty() { return false; } /** * Returns the current number of * elements stored in the list * array * @return the number of elements */ public int size() { return -1; } /** * Returns the element at the specified * index. Prints an error message and * returns an empty String if index is * larger than or equal to numElements * @param index the index of the element * to access * @return the element at index */ public String get(int index) { return null; } /** * Uses the linearSearch algorithm to * locate an element in the list array * @param str the element to locate * @return whether or not the element * is in the list array */ public boolean contains(String str) { return false; } /** * Uses the linearSearch algorithm to * locate an element in the list array * @param str the element to locate * @return the location of the element * or -1 if the element is not in the * list array */ public int indexOf(String str) { return -1; } /** * Determines whether the list array * is currently full * @return whether the list array * is at maximum capacity */ private boolean atCapacity() { return false; } /**Mutator Methods*/ /** * Resizes the list array by * and assigning a new length * to your array. Hint: call new * 10 larger than the current array's * length (capacity) */ private void reSize() { } /** * Inserts a new element to the end * of the list array. * Resizes the list array if it is * at capacity before inserting * @param str the element to insert */ public void add(String str) { } /** * Inserts a new element at the specified * index in the list array. * Resizes the list array if it is * at capacity before inserting * @param index the index at which to insert * @param str the element to insert */ public void add(int index, String str) { } /** * Assigns a new value to the list array * at a specified index * @param index the index at which to update * @param str the new element */ public void set(int index, String str) { } /** * Removes an element at a specified index in * the list array * @param index the index at which to remove * @return the element that was removed */ public String remove(int index) { return null; } /** * Removes the first instance of the specified * element in the list array * @param str the element to remove * @return whether the element was successfully * removed */ public boolean remove(String str) { return false; } /**Additional Methods*/ /** * Creates a String of all elements, * with [] around the elements, * each element separated from the next * with a comma */ @Override public String toString() { return null; } }
* ListArrayTest.java * @author * @author * CIS 36B */ public class ListArrayTest { public static void main(String[] args) { System.out.println("***Testing Default Constructor and One Parameter Constructor***\n"); ListArray test1 = new ListArray(); /* System.out.println("After default constructor called:"); System.out.println("Should print size of 0: " + test1.size()); System.out.println("Should print the list array is empty - true: " + test1.isEmpty()); System.out.println("Should print []: " + test1); ListArray test2 = new ListArray(15); System.out.println("\nAfter one parameter constructor called:"); System.out.println("\nShould print size of 0: " + test2.size()); System.out.println("Should print the list array is empty - true: " + test2.isEmpty()); System.out.println("Should print []: " + test2); System.out.println("\n***Testing Add Methods***\n"); test1.add("B"); System.out.println("Adding B: "); System.out.println("Should print size of 1: " + test1.size()); System.out.println("Should print the list array is empty - false: " + test1.isEmpty()); System.out.println("Should print B at index 0: " + test1.get(0)); System.out.println("Should print [B]: " + test1); test1.add("D"); System.out.println("\nAdding D: "); System.out.println("Should print size of 2: " + test1.size()); System.out.println("Should print the list array is empty - false: " + test1.isEmpty()); System.out.println("Should print B at index 0: " + test1.get(0)); System.out.println("Should print D at index 1: " + test1.get(1)); System.out.println("Should print [B, D]: " + test1); System.out.println("\nShould print error message. Cannot add at index 10:"); test1.add(10, "A"); test1.add(0, "A"); System.out.println("\nAdding A at index 0: "); System.out.println("Should print size of 3: " + test1.size()); System.out.println("Should print the list array is empty - false: " + test1.isEmpty()); System.out.println("Should print A at index 0: " + test1.get(0)); System.out.println("Should print B at index 1: " + test1.get(1)); System.out.println("Should print [A, B, D]: " + test1); test1.add(2, "C"); System.out.println("\nAdding C at index 2: "); System.out.println("Should print size of 4: " + test1.size()); System.out.println("Should print the list array is empty - false: " + test1.isEmpty()); System.out.println("Should print A at index 0: " + test1.get(0)); System.out.println("Should print C at index 2: " + test1.get(2)); System.out.println("Should print [A, B, C, D]: " + test1); System.out.println("\nAdding 1-20 in ListArray of size 15. Should resize to 20:"); for(int i = 1; i <= 20; i++) { test2.add("" + i); } System.out.println("Should print size of 20: " + test2.size()); System.out.println("Should print the list array is empty - false: " + test2.isEmpty()); System.out.println("Should print 1 at index 0: " + test2.get(0)); System.out.println("Should print 20 at index 19: " + test2.get(19)); System.out.println("\n***Testing Copy Constructor***\n"); ListArray test3 = new ListArray(test1); System.out.println("Should print size of 4: " + test1.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print A at index 0: " + test3.get(0)); System.out.println("Should print C at index 2: " + test3.get(2)); System.out.println("Should print [A, B, C, D]: " + test3); System.out.println("\nTesting for deep copy: "); test3.add("A"); test3.add("B"); test3.add("C"); System.out.println("Original list array size should be 4: " + test1.size()); System.out.println("New list array size should be 7: " + test3.size()); System.out.println("Printing original [A, B, C, D]: " + test1); System.out.println("Printing new [A, B, C, D, A, B, C]: " + test3); System.out.println("\n***Testing Set Method***\n"); System.out.println("Should print error message when setting index 6: "); test1.set(6, "Z"); test1.set(2, "Z"); System.out.println("\nShould print size of 4: " + test1.size()); System.out.println("Should print the list array is empty - false: " + test1.isEmpty()); System.out.println("Should print A at index 0: " + test1.get(0)); System.out.println("Should print Z at index 2: " + test1.get(2)); System.out.println("Should print [A, B, Z, D]: " + test1); test1.set(0, "Z"); System.out.println("\nShould print size of 4: " + test1.size()); System.out.println("Should print the list array is empty - false: " + test1.isEmpty()); System.out.println("Should print Z at index 0: " + test1.get(0)); System.out.println("Should print Z at index 2: " + test1.get(2)); System.out.println("Should print [Z, B, Z, D]: " + test1); System.out.println("\n***Testing Remove Methods***\n"); System.out.println("Remove index 3, element D: " + test3.remove(3)); System.out.println("Should print size of 6: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print A at index 0: " + test3.get(0)); System.out.println("Should print A at index 3: " + test3.get(3)); System.out.println("Should print [A, B, C, A, B, C]: " + test3); System.out.println("\nRemove index 0, element A: " + test3.remove(0)); System.out.println("Should print size of 5: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print B at index 0: " + test3.get(0)); System.out.println("Should print B at index 3: " + test3.get(3)); System.out.println("Should print [B, C, A, B, C]: " + test3); System.out.println("\nRemove index 10, should print error message: "); test3.remove(10); System.out.println("Should print size of 5: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print B at index 0: " + test3.get(0)); System.out.println("Should print B at index 3: " + test3.get(3)); System.out.println("Should print [B, C, A, B, C]: " + test3); System.out.println("\nRemove index of size - 1 (last element) C: " + test3.remove(test3.size() - 1)); System.out.println("Should print size of 4: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print B at index 0: " + test3.get(0)); System.out.println("Should print B at index size - 1: " + test3.get(test3.size() - 1)); System.out.println("Should print [B, C, A, B]: " + test3); System.out.println("\nCan remove B (first instance): " + test3.remove("B")); System.out.println("Should print size of 3: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print C at index 0: " + test3.get(0)); System.out.println("Should print B at index size - 1: " + test3.get(test3.size() - 1)); System.out.println("Should print [C, A, B]: " + test3); System.out.println("\nCan remove B (only instance): " + test3.remove("B")); System.out.println("Should print size of 2: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print C at index 0: " + test3.get(0)); System.out.println("Should print A at index size - 1: " + test3.get(test3.size() - 1)); System.out.println("Should print [C, A]: " + test3); System.out.println("\nShould print error message: "); System.out.println("Cannot remove Z (not stored): " + test3.remove("Z")); System.out.println("Should print size of 2: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print C at index 0: " + test3.get(0)); System.out.println("Should print A at index size - 1: " + test3.get(test3.size() - 1)); System.out.println("Should print [C, A]: " + test3); test3.remove("C"); System.out.println("\nRemove C:"); System.out.println("Should print size of 1: " + test3.size()); System.out.println("Should print the list array is empty - false: " + test3.isEmpty()); System.out.println("Should print A at index 1: " + test3.get(0)); System.out.println("Should print A at index size - 1: " + test3.get(test3.size() - 1)); System.out.println("Should print [A]: " + test3); test3.remove("A"); System.out.println("\nRemove A:"); System.out.println("Should print size of 0: " + test3.size()); System.out.println("Should print the list array is empty - true: " + test3.isEmpty()); System.out.println("Should print []: " + test3); System.out.println("\n***Testing Size Method***\n"); System.out.println("Should print size of 0: " + test3.size()); System.out.println("Should print size of 20: " + test2.size()); System.out.println("Should print size of 4: " + test1.size()); System.out.println("\n***Testing IsEmpty Method***\n"); System.out.println("Should print true: " + test3.isEmpty()); System.out.println("Should print false: " + test2.isEmpty()); System.out.println("Should print false: " + test1.isEmpty()); System.out.println("\n***Testing Contains Method***\n"); System.out.println("Contains Z, should print true: " + test1.contains("Z")); System.out.println("Contains D, should print true: " + test1.contains("D")); System.out.println("Contains A, should print false: " + test1.contains("A")); System.out.println("\n***Testing IndexOf Method***\n"); System.out.println("Index of Z, should print 0: " + test1.indexOf("Z")); System.out.println("Index of D, should print 3: " + test1.indexOf("D")); System.out.println("Index of A, should print -1: " + test1.indexOf("A")); System.out.println("\n***Testing Get Method***\n"); System.out.println("Element at 0, should print Z: " + test1.get(0)); System.out.println("Element at 3, should print D: " + test1.get(3)); System.out.println("Element at 6, should print error message: "); test1.get(6); */ } }
Output of ListArrayTest.java After default constructor called: Should print size of 0: 0 Should print the list array is empty - true: true Should print []: [] After one parameter constructor called: Should print size of 0: 0 Should print the list array is empty - true: true Should print []: [] ***Testing Add Methods*** Adding B: Should print size of 1: 1 Should print the list array is empty - false: false Should print B at index 0: B Should print [B]: [B] Adding D: Should print size of 2: 2 Should print the list array is empty - false: false Should print B at index 0: B Should print D at index 1: D Should print [B, D]: [B, D] Should print error message. Cannot add at index 10: Error: Cannot add element A at index 10. Index is outside the bounds of the list array. Index: 10, Size: 2 Adding A at index 0: Should print size of 3: 3 Should print the list array is empty - false: false Should print A at index 0: A Should print B at index 1: B Should print [A, B, D]: [A, B, D] Adding C at index 2: Should print size of 4: 4 Should print the list array is empty - false: false Should print A at index 0: A Should print C at index 2: C Should print [A, B, C, D]: [A, B, C, D] Adding 1-20 in ListArray of size 15. Should resize to 20: Should print size of 20: 20 Should print the list array is empty - false: false Should print 1 at index 0: 1 Should print 20 at index 19: 20 ***Testing Copy Constructor*** Should print size of 4: 4 Should print the list array is empty - false: false Should print A at index 0: A Should print C at index 2: C Should print [A, B, C, D]: [A, B, C, D] Testing for deep copy: Original list array size should be 4: 4 New list array size should be 7: 7 Printing original [A, B, C, D]: [A, B, C, D] Printing new [A, B, C, D, A, B, C]: [A, B, C, D, A, B, C] ***Testing Set Method*** Should print error message when setting index 6: Error: Cannot set at index outside bounds of list array. Index: 6 , Size: 4 Should print size of 4: 4 Should print the list array is empty - false: false Should print A at index 0: A Should print Z at index 2: Z Should print [A, B, Z, D]: [A, B, Z, D] Should print size of 4: 4 Should print the list array is empty - false: false Should print Z at index 0: Z Should print Z at index 2: Z Should print [Z, B, Z, D]: [Z, B, Z, D] ***Testing Remove Methods*** Remove index 3, element D: D Should print size of 6: 6 Should print the list array is empty - false: false Should print A at index 0: A Should print A at index 3: A Should print [A, B, C, A, B, C]: [A, B, C, A, B, C] Remove index 0, element A: A Should print size of 5: 5 Should print the list array is empty - false: false Should print B at index 0: B Should print B at index 3: B Should print [B, C, A, B, C]: [B, C, A, B, C] Remove index 10, should print error message: Error: Cannot remove element at index 10. Index is outside bounds of list array. Should print size of 5: 5 Should print the list array is empty - false: false Should print B at index 0: B Should print B at index 3: B Should print [B, C, A, B, C]: [B, C, A, B, C] Remove index of size - 1 (last element) C: C Should print size of 4: 4 Should print the list array is empty - false: false Should print B at index 0: B Should print B at index size - 1: B Should print [B, C, A, B]: [B, C, A, B] Can remove B (first instance): true Should print size of 3: 3 Should print the list array is empty - false: false Should print C at index 0: C Should print B at index size - 1: B Should print [C, A, B]: [C, A, B] Can remove B (only instance): true Should print size of 2: 2 Should print the list array is empty - false: false Should print C at index 0: C Should print A at index size - 1: A Should print [C, A]: [C, A] Should print error message: Error: Cannot remove element Z. No such element in the list array. Cannot remove Z (not stored): false Should print size of 2: 2 Should print the list array is empty - false: false Should print C at index 0: C Should print A at index size - 1: A Should print [C, A]: [C, A] Remove C: Should print size of 1: 1 Should print the list array is empty - false: false Should print A at index 1: A Should print A at index size - 1: A Should print [A]: [A] Remove A: Should print size of 0: 0 Should print the list array is empty - true: true Should print []: [] ***Testing Size Method*** Should print size of 0: 0 Should print size of 20: 20 Should print size of 4: 4 ***Testing IsEmpty Method*** Should print true: true Should print false: false Should print false: false ***Testing Contains Method*** Contains Z, should print true: true Contains D, should print true: true Contains A, should print false: false ***Testing IndexOf Method*** Index of Z, should print 0: 0 Index of D, should print 3: 3 Index of A, should print -1: -1 ***Testing Get Method*** Element at 0, should print Z: Z Element at 3, should print D: D Element at 6, should print error message: Error: Cannot get element at index 6. Outside bounds of list array. Index: 6, Size: 4 What to Submit:
How You Will Be Graded:
TEXTING TRANSLATOR
4 cu ikr k l8 l8r lmk m8 nvm r smh u ur wu
for see you I know right? okay late later let me know mate never mind are shaking my head you you are what's up?
Starter Code /** * @author * @author * CIS 36B */ import java.util.Scanner; import java.io.File; import java.io.IOException; import java.io.PrintWriter; public class TextTrans {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in); System.out.println("Welcome to the Texting Translator!"); System.out.println("\nGoodbye!"); }
/** * Inserts a String element into an array at a specified index * @param array the list of String values * @param numElements the current number of elements stored * @indexToInsert the location in the array to insert the new element * @param newValue the new String value to insert in the array */
public static void insert(String array[], int numElements, int indexToInsert, String newValue) {
}
/** * Removes a String element from an array at a specified index * @param array the list of String values * @param numElements the current number of elements stored * @param indexToRemove where in the array to remove the element */
public static void remove(String array[], int numElements, int indexToRemove) { return; } /** * Print an array of Strings either to the console * or a file * @param names the list of abbreviations * @param translations the list of corresponding translations * @param numElements the current number of elements stored * @param file the file name */ public static void printFile(String[] abbreviations, String[] translations, int numElements, String file) throws IOException { return; }
/** * Searches for a specified String in a list * @param array the array of Strings * @param value the String to serach for * @param numElements the number of elements in the list * @return the index where value is located in the array */ public static int linearSearch(String array[], String value, int numElements) { return -1; } } Sample Output Welcome to the Texting Translator! Below are the texting abbreviations: 0. 4 1. cu 2. ikr 3. k 4. l8 5. l8r 6. lmk 7. m8 8. nvm 9. r 10. smh 11. u 12. ur 13. wu Please enter a menu option (A, R, S, X) below: A. Add an Abbreviation R. Remove an Abbreviation S. Search for a Translation X. Exit Please enter your choice: A Enter the abbreviation: rofl Enter the position at which to add rofl: 10 Enter the translation of rofl: rolling on the floor laughing Below are the texting abbreviations: 0. 4 1. cu 2. ikr 3. k 4. l8 5. l8r 6. lmk 7. m8 8. nvm 9. r 10. rofl 11. smh 12. u 13. ur 14. wu Please enter a menu option (A, R, S, X) below: A. Add an Abbreviation R. Remove an Abbreviation S. Search for a Translation X. Exit Please enter your choice: R Please enter the abbreviation to remove: l8 Removing l8... Is this correct (y/n): y l8 has been removed. Below are the texting abbreviations: 0. 4 1. cu 2. ikr 3. k 4. l8r 5. lmk 6. m8 7. nvm 8. r 9. rofl 10. smh 11. u 12. ur 13. wu Please enter a menu option (A, R, S, X) below: A. Add an Abbreviation R. Remove an Abbreviation S. Search for a Translation X. Exit Please enter your choice: S Enter the abbreviation: u The abbreviation "u" means: you Below are the texting abbreviations: 0. 4 1. cu 2. ikr 3. k 4. l8r 5. lmk 6. m8 7. nvm 8. r 9. rofl 10. smh 11. u 12. ur 13. wu Please enter a menu option (A, R, S, X) below: A. Add an Abbreviation R. Remove an Abbreviation S. Search for a Translation X. Exit Please enter your choice: X Enter the name of a file to write your abbreviations: output.txt Goodbye! Contents of corresponding output file (output.txt in my example - but user should specify the name): 0. 4: for 1. cu: see you 2. ikr: I know right? 3. k: okay 4. l8r: later 5. lmk: let me know 6. m8: mate 7. nvm: never mind 8. r: are 9. rofl: rolling on the floor laughing 10. smh: shaking my head 11. u: you 12. ur: you are 13. wu: what's up?
What to Submit:
How You Will Be Graded:
|