CMPS 200 Homework Assignment 9 with Solution, Fall 2012

American University of Beirut · Introduction to Programming · 12 pages · with solution

All CMPS 200 files
CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 2 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 3 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 4 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 5 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 6 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 7 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 8 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 9 CMPS 200 Homework Assignment 9 with Solution, Fall 2012, page 10

Showing the first 10 of 12 pages. Download the PDF to see all of it.

More CMPS 200 files

Show the text of this homework
/*ghassan fakher * section 6 *gkf01*/ public class Answers { public static void main(String[] args) { In studentAnswers = new In(args[0]); In correct = new In(args[1]); char[][] answers = new char[Integer.parseInt(studentAnswers.readLine())][5];// 5 because there are only 5 questions===answers char[] solution = new char[5]; //5 questions asked for(int i = 0; i < 5; i++) solution[i] = correct.readChar(); for(int i = 0; i < answers.length; i++) { String ans = studentAnswers.readLine(); for(int j = 0; j < 5; j++) { answers[i][j] = ans.charAt(j); } } makeGrades(answers, solution); //method that computes the grades of each student and puts them in an array } public static void makeGrades(char[][] answers, char[] solution) { int[] grades = new int[answers.length]; //array having the grades of the students int grade = 0; for(int i = 0; i < answers.length; i++) { for(int j = 0; j < 5; j++) { if(answers[i][j] == solution[j]) grade += 4; } grades[i] = grade; grade = 0; // the grade must become zero because each student has his own grade } printGrades(grades); } //method used to print the grades of the students using the array of grades public static void printGrades(int[] grades) { for(int i = 0; i < grades.length; i++) StdOut.println("Student " + (i + 1) + " : " + grades[i]); } } /*ghassan fakher section 6 gkf01*/ public class ArrayOperations { public static void main(String[] args) { int n = 5; int m = 5; double [][] randomNumbersArray = generate2D(n,m); int min = 2; int max = 9; double[][] randomNumbersArray1 = generate2D(n, m, min, max); StdOut.println("The sum of the elements of the array in the first method is: " + sum(randomNumbersArray)); StdOut.println("The sum of the elements of the array in the second method is: " + sum(randomNumbersArray1)); StdOut.println("The sum of the lower block of the double[][] array of the first method is: " + sumLowerBlock(randomNumbersArray)); StdOut.println("The sum of the lower block of the double[][] array of the second method is: " + sumLowerBlock(randomNumbersArray1)); } //generate and return an array of doubles with m columns and n rows filled with random numbers from 0 to 1 public static double[][] generate2D(int n, int m) { double[][] randomNumbersArray = new double[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) randomNumbersArray[i][j] = Math.random(); } return randomNumbersArray; } //generate and return an n by m array of doubles filled with random numbers between min and max public static double[][] generate2D(int n, int m, int min, int max) { double[][] randomNumbersArray = new double[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) randomNumbersArray[i][j] = Math.random()*max + min; } return randomNumbersArray; } //returns the sum of all the entries of a 2D array public static double sum(double[][] a) { double sum = 0; for(int i = 0; i < a.length; i++) { for(int j = 0; j < a[i].length; j++) { sum += a[i][j]; } } return sum; } public static double sumLowerBlock(double[][] a) { double sumLowBlock = 0; for(int i = 0; i < a.length; i++) { for(int j = 0; j < a[i].length; j++) { if(i > j) // the row is greater than the column sumLowBlock += a[i][j]; } } return sumLowBlock; } } /*ghassan fakher section 6 gkf01*/ public class DayOfTheYear { public static void main(String[] args) { String month = args[0]; int day = Integer.parseInt(args[1]); StdOut.println(dayOfYear(month, day)); } public static int dayOfYear(String month, int day) { String [] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; int [] days = {31, 28, 31, 30, 31, 30, 31, 31, 30 ,31, 30, 31}; int monthNumber = 0; for(int i = 0; i < months.length; i++) { if(month.equals(months[i])) monthNumber = i; } int totalDays= 0; for(int i = 0; i < monthNumber; i++) { totalDays += days[i]; } totalDays += day; return totalDays; } } import java.awt.Color; /*ghassan fakher * section 6 gkf01*/ public class Mirror { public static void main(String[] args) { Picture original = new Picture(args[0]); Color[][] originalColors = makeArrayOriginalColors(original); //the colors of the smaller picture drawPictureModified(originalColors); } public static Color[][] makeArrayOriginalColors(Picture original) { Color[][] originalColors = new Color[original.height()] [original.width()]; for(int i = 0; i < original.height(); i++) { for(int j = 0; j < original.width(); j++) originalColors[i][j] = original.get(j, i); } return originalColors; } public static void drawPictureModified(Color[][] originalColors) { Picture modified = new Picture(2 * originalColors[0].length, originalColors.length); //the original picture has the same width all over so i put 0 for(int i = 0; i < modified.height(); i++) { for(int j = 0; j < modified.width() / 2; j++) modified.set(j, i, originalColors[i][j]); } for(int i = 0; i < modified.height(); i++) { for(int j = modified.width()/2 ; j < modified.width(); j++) modified.set(j, i, originalColors[i][modified.width() - 1 - j]); } modified.show(); } } /*ghassan fakher * section 6 * gkf01 * 201301739*/ public class PasswordChecker { public static void main(String[]args) { boolean isAccepted = accepted(); while(!isAccepted) { StdOut.println("Try again, weak password"); isAccepted = accepted(); } System.out.println("Good password"); } //method that checks whether the password is acceptable by checking all the required criteria public static boolean accepted() { StdOut.print("Type password here: "); String input = StdIn.readString(); int[][] password = new int[5][input.length()]; //5 because the criteria used to judge the password are 5 boolean isGood = false; if(input.length() >= 8) isGood = true; else { isGood = false; return isGood; } for(int i = 0; i < input.length(); i++) { char passwordCharacter = input.charAt(i); if(checkNumbers(passwordCharacter) == true) password[1][i]++; if(checkUpperCase(passwordCharacter) == true) password[2][i]++; if(checkLowerCase(passwordCharacter) == true) password[3][i] = 1; if(checkNonAlphaNumericCharacters(passwordCharacter) == true) password[4][i]++; password[0][i]++; } return(checkArray(password)); } //checks if the password satisfies the condition related to the numbers public static boolean checkNumbers(char passwordCharacter) { String numbers = "0123456789"; if(numbers.contains("" + passwordCharacter)) return true; else return false; } //checks if the password satisfies the condition related to uppercase characters public static boolean checkUpperCase(char passwordCharacter) { String upperCaseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if(upperCaseAlphabet.contains("" + passwordCharacter)) return true; else return false; } //checks the validity of the password in relation to the lowerCase characters public static boolean checkLowerCase(char passwordCharacter) { String lowerCaseAlphabet = "abccdefghijklmnopqrstuvwxyz"; if(lowerCaseAlphabet.contains("" + passwordCharacter)) return true; else return false; } //similarly for the non alpha numeric characters public static boolean checkNonAlphaNumericCharacters(char passwordCharacter) { String nonAlphaNumericCharacters = "!@#$%^&*()_+}{.|<>?-:|'\""; //the string of almost all the required characters, hope i didn't forget any if(nonAlphaNumericCharacters.contains("" + passwordCharacter)) return true; else return false; } public static boolean checkArray(int[][] password) { int count = 0; boolean isGood = false; for(int i = 0; i < password.length; i++) { for(int j = 0; j < password[i].length; j++) { count += password[i][j]; } if(count != 0) isGood = true; else { isGood = false; return isGood; } if(i != password.length - 1) count = 0; } if(count != 0) isGood = true; else isGood = false; return isGood; } } import java.awt.Color; public class TicTacToe { public static void main(String[] args) { int[] cells = new int[9]; int turn = 0; boolean winner = false; int start = (int)(Math.random() * 2); while(turn < 9 && !winner) { if((start + turn) % 2 == 0) winner = userTurn(cells); else winner = computerTurn(cells); drawTable(cells); turn++; } if(!winner && turn == 9) StdOut.println("Tie"); else if(checkwin(cells, 1)) StdOut.println("Congratulations, you win!"); else StdOut.println("You lose, Game over..Try again.."); } public static boolean userTurn(int[] cells) { StdOut.print("Give an integer between 0 and 8 inclusive: "); int num = StdIn.readInt(); while(cells[num] != 0 && num >= 0 && num <9) { StdOut.println("Number already chosen, pick again."); StdOut.print("Give an integer between 0 and 8 inclusive: "); num = StdIn.readInt(); } cells[num] = 1; return checkwin(cells, 1); } public static boolean computerTurn(int[] cells) { int num = (int)(Math.random() * 9); while(cells[num] != 0) num = (int)(Math.random() * 9); StdOut.println("The computer chooses " + num); cells[num] = 2; return checkwin(cells, 2); } public static boolean checkwin(int[] cells, int id) { boolean win = false; for(int x = 0; x < 3; x++) { win = true; for(int y = 0; y < 3; y++) { if(cells[3 * y + x] != id) win = false; } if(win) return win; } for(int y = 0; y < 3; y++) { win = true; for(int x = 0; x < 3; x++) { if(cells[3 * y + x] != id) win = false; } if(win) return win; } int x = 0; int y = 2; for(int i = 0; i < 3; i++) { if(cells[3 * y + x] != id) win = false; x++; y--; } if(win) return win; x = 2; y = 0; for(int i = 0; i < 3; i++) { if(cells[3 * x + y] != id) win = false; y++; x--; } if(cells[0]== id && cells[4] == id && cells[8]== id) win = true; return win; } public static void drawTable(int[] cells) { StdDraw.setXscale(0, 300); StdDraw.setYscale(0, 300); StdDraw.setPenColor(Color.black); StdDraw.square(150., 150., Math.sqrt(180000) / 2); for(int row = 50 ; row <= 250 ; row += 100) { for(int column = 50; column <= 250; column+= 100) { StdDraw.square(row * 1.0, column * 1.0 , Math.sqrt(10000) / 2.0); } } for(int y = 50 ; y <= 250 ; y += 100) { for(int x = 50; x <= 250; x+= 100) { for(int i = 0; i < cells.length; i++) { if(i == 0) {x = 50; y = 50;} else if(i == 1) {x = 150; y = 50;} else if(i == 2) {x = 250; y = 50;} else if(i == 3) {x = 50; y = 150;} else if(i == 4) {x = 150; y = 150;} else if(i == 5) {x = 250; y = 150;} else if(i == 6) {x = 50 ; y = 250;} else if(i == 7) {x = 150 ; y = 250;} else if(i == 8) {x = 250 ; y = 250;} if(cells[i] == 1) { StdDraw.line(x - 50, y - 50, x + 50, y + 50); StdDraw.line(x - 50, y + 50, x + 50, y - 50); } else if(cells[i] == 2) { StdDraw.filledCircle(x, y, 40.); } } } } } } /*ghassan fakher * gkf01 section 6*/ public class Transpose { public static void main(String[] args) { String filename = args[0]; int[][] a = readArray(filename); transpose(a); } // Create and return a 2D array read from standard input public static int[][] readArray(String filename) { In matrix = new In(filename); int[][] a = new int[matrix.readInt()][matrix.readInt()]; for(int i = 0; i < a.length; i++) { for(int j = 0; j <a[i].length ; j++) a[i][j] = matrix.readInt(); } return a; } // Transposes a 2D array, argument unchanged, new array returned public static void transpose(int[][] a) { int newWidth = a.length; int newLength = a[newWidth-1].length; int[][] transposed = new int[newLength][newWidth]; for (int k=0; k<newWidth; k++) { for (int r = 0; r < newLength; r++) transposed[r][k] = a[k][r]; } printArray(transposed); } // Print a 2D array to standard output public static void printArray(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[i].length; j++) StdOut.printf("%-3d" + " ", a[i][j]); StdOut.println(); } } } import java.awt.Color; public class WordLengths { public static void main(String[] args) { In file = new In(args[0]); int[] lengths = new int[10]; // assuming the largest word is of 10 characters while(!file.isEmpty()) { String word = file.readString(); lengths[word.length()]++; } wordLength(lengths); printHistogram(lengths); } public static void wordLength(int[] lengths) { for(int i = 1; i <lengths.length; i++) { System.out.print(i + ": " + lengths[i] + " "); for(int j = 0; j < lengths[i]; j++) System.out.print("*"); System.out.println(); } } public static void printHistogram(int[] lengths) { StdDraw.setCanvasSize(300, 300); StdDraw.setXscale(0.0, 10); StdDraw.setYscale(0.0, largestNumber(lengths)); StdDraw.setPenRadius(0.01900); int x = 0; for(int i = 0; i < lengths.length;i++) { StdDraw.setPenColor(randomColor()); StdDraw.line(x, 0.0, x, lengths[i]); x += 1; } StdDraw.show(); } public static Color randomColor() { Color random = new Color((int)(Math.random() * 255) , (int) (Math.random() * 255), (int)(Math.random() * 255)); return random; } public static int largestNumber(int[] lengths) { int max = lengths[0]; for(int i = 0; i < lengths.length; i++) { if(lengths[i] > max) max = lengths[i]; } return max; } }