CMPS 200 Homework Assignment 6 Solution, Fall 2012

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

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

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

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

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

More CMPS 200 files

Show the text of this homework
/*Ghassan Fakher * section 6 * gkf01 * 201301739*/ //a program that tests different operations related to arrays public class ArrayOps { public static void main(String[] args) { int n = Integer.parseInt(args[0]); double[] a = {1., 2., 3., 6., 10., 5.}; int min = Integer.parseInt(args[1]); int max = Integer.parseInt(args[2]); for(int i = 0; i < n; i++) { System.out.print(generate1D(n)[i] + " "); } System.out.println(); for(int i = 0; i < n; i++) { System.out.print(generate1D(n, min, max)[i] + " "); } System.out.println(); System.out.println("The sum of the numbers in the array is " + sum(a)); System.out.println("The sum of numbers in the array less than the average is " + sumLessThanAverage(a)); } //generate and return an array of doubles with n elements filled with random numbers from 0 to 1 public static double[] generate1D(int n) { double[] randomArray = new double[n]; for(int i = 0; i<n; i++) { randomArray[i] = Math.random(); } return randomArray; } //generate and return a 1D array of doubles filled with random doubles between min and max public static double[] generate1D(int n, int min, int max) { double[] randomArray = new double[n]; for(int i = 0; i<n; i++) { double random = Math.random() * max; if(random >= 0 && random <= min) randomArray[i] = random + min; else randomArray[i] = random; } return randomArray; } //returns the sum of all the entries of a 1D array public static double sum(double[] a) { double sum = 0; for(int i = 0; i < a.length; i++) sum = sum + a[i]; return sum; } //returns the sum of the entries that are less than the average of the array elements public static double sumLessThanAverage(double[] a) { double sum = 0; double sum2 = 0; for(int i = 0; i < a.length; i++) sum = sum + a[i]; double average = sum / a.length; for(int i = 0; i < a.length; i++) { if(a[i] < average) { sum2 = sum2 + a[i]; } } return sum2; } } /*Ghassan Fakher * section 6 * gkf01 * 201301739*/ //program that prints out the alternating sum of its arguments public class ArraySum { public static void main(String[] args) { String[] x = new String[args.length]; for(int i = 0; i < x.length; i++) { x[i] = args[i]; } System.out.println("The alternating sum of the elements of the array is: " + aSum(getInput(x))); } //takes an array of strings and returns an array of inttegers public static int[] getInput(String[] x) { int[] y = new int[5]; for(int i = 0; i < x.length; i++) { y[i]= Integer.parseInt(x[i]); } return y; } //method that computes and returns the alternating sum of elements of the array public static int aSum(int[] x) { int altSum = 0; for(int i = 0; i < x.length; i++) { if(i % 2 == 0) { altSum = altSum + x[i]; } else { altSum = altSum - x[i]; } } return altSum; } } /*Ghassan Fakher * section 6 * gkf01 * 201301739*/ public class Caesar { public static void main(String[] args) { char[] alphabet={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R ','S','T','U','V','W','X','Y','Z'}; String[] words = new String[args.length-1]; int k = Integer.parseInt(args[0]); for(int i = 1; i <= args.length - 1; i++) { words[i-1] = args[i]; } String[] code = new String[args.length-1]; for(int i = 0; i < words.length; i++) { code[i]=codeWord(words[i], k, alphabet); } for (int i=0; i< code.length; i++) { System.out.print(code[i] + " "); } } //method that formulated a string made up of the ciphered characters public static String codeWord(String words, int k, char[] alphabet) { String CodedWord=""; for(int indexOfWord=0; indexOfWord<words.length(); indexOfWord+ +) //indexOfWord is the index of the given string { CodedWord+=letterCode(words.charAt(indexOfWord), k, alphabet); } return CodedWord; } // method that writes the accepted character in caesar code public static char letterCode(char letterInWord, int k, char[] alphabet) { char[] cipher=cipheredAlphabet(alphabet, k); char codeCharacter=letterInWord; boolean cipheredWord=false; for(int index=0; index <alphabet.length && cipheredWord == false; index++) //gets the index of the characterToCode in the alphabet { if(Character.isUpperCase(letterInWord) && Character.isLetter(letterInWord)) { if (letterInWord==alphabet[index]) { codeCharacter=cipher[index]; cipheredWord=true; } } } return codeCharacter; } //writes the alphabet as a caesar code public static char[] cipheredAlphabet(char[] alphabet, int k) { char[] coded=new char[26]; for(int i=0;i<26; i++) { if (i+k < alphabet.length) { coded[i]= alphabet[i+k]; }else { for(int j=0;j<k; j++) { coded[i]= alphabet[j]; i++; } } } return coded; } } /*Ghassan Fakher * section 6 * gkf01 * 201301739*/ public class LongestSortedSequence { public static void main(String[] args) { int[] array = new int[args.length]; for(int i = 0; i < array.length; i++) { array[i] = Integer.parseInt(args[i]); } System.out.println("The longest sorted (non decreasing) sequence is: " + longestSortedSequence(array)); } public static int longestSortedSequence(int[] array) { int count = 0; int i =0; while(i < array.length - 1) { int size = 1; while(i < array.length - 1 && array[i] <= array[i + 1]) { size++; i++; } i++; if(size > count) { count =size; } } return count; } } /*Ghassan Fakher * section 6 * gkf01 * 201301739*/ //program that gives the minimum and maximum integers of the array public class MinMax { public static void main(String[] args) { int []arrayInt = new int[args.length]; for(int i = 0; i < arrayInt.length; i++) { arrayInt[i] = Integer.parseInt(args[i]); } minMax(arrayInt); } public static void minMax(int[] arrayInt) { int min = (int) 1E10; int max = (int) 1E-90; for(int i = 0; i < arrayInt.length; i++) { if(arrayInt[i] < min) min = arrayInt[i]; if(arrayInt[i] > max) max = arrayInt[i]; } System.out.println("Minimum: " + min); System.out.println("Maximum: " + max); } } /*Ghassan Fakher * section 6 * gkf01 * 201301739*/ //program that tests two methods used to reverse the elements of a given array public class Reverse { public static void main(String[] args) { int[] a = new int[args.length]; System.out.print("The original array is: "); for(int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(args[i]); System.out.print(a[i] + " "); } System.out.println(); reverseInPlace(a); System.out.print("The reversed array of the method reverseCopy is: "); for(int i = 0; i < a.length; i++) { System.out.print(reverseCopy(a)[i] + " "); } System.out.println(); } //method that reverses the elements of the array passed in public static void reverseInPlace(int[] a) { for(int i = 0; i < a.length / 2; i++) { int reference = a[i]; a[i] = a[a.length -1 -i]; a[a.length -1 -i] = reference; } System.out.print("The reversed array of the method reverseInPlace is: "); for(int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } //method that creates a copy of the given array and modifies its contents without changing the original public static int[] reverseCopy(int[] a) { int[] b = new int[a.length]; for(int i = 0; i < a.length; i++) { b[i] = a[i]; } return b; } } /*Ghassan Fakher * section 6 * gkf01 * 201301739*/ The sample runs are taken from eclipse not Powershell 1) Program ArrayOps(i give it 3 arguments n and min and max respectively: 3 1 10) 0.7692275024515308 0.6469470060656305 0.7280351482530633 7.6522211407567315 1.7965509463096132 3.4746323947118176 The sum of the numbers in the array is 27.0 The sum of numbers in the array less than the average is 6.0 2) Program MinMax(given 3 7 -1 90 71 as arguments) Minimum: -1 Maximum: 90 3) Program ArraySum(given 1 2 3 4 5 as command line arguments) The alternating sum of the elements of the array is: 3 4) Program Reverse The original array is: 1 2 3 4 5 The reversed array of the method reverseInPlace is: 5 4 3 2 1 The reversed array of the method reverseCopy is: 5 4 3 2 1 5) Program LongSortedSequence(given the arguments 3 8 10 1 9 14 -3 0 14 207 56 98 12) The longest sorted (non decreasing) sequence is: 4 6) Program WordLengths(given the arguments: how are you feeling today) 1: 0 2: 0 3: 3*** 4: 0 5: 1* 6: 0 7: 1* 7)Program Caesar(given the args: 3 "VENI, VIDI, VICI") YHQL, YLGL, YLFL /*Ghassan Fakher * section 6 * gkf01 * 201301739*/ public class WordLengths { public static void main(String[] args) { String[] word = new String[args.length]; for(int i = 0; i < word.length; i++) { word[i] = args[i]; } wordLength(word); } public static void wordLength(String[] word) { int length1 = 0; int length2 = 0; int length3 = 0; int length4 = 0; int length5 = 0; int length6 = 0; int length7 = 0; for(int i = 0; i < word.length; i++) { String str = word[i]; int length = str.length(); if(length == 1) length1 += 1; else if(length == 2) length2 += 1; else if(length == 3) length3 += 1; else if(length == 4) length4 += 1; else if (length == 5) length5 += 1; else if(length == 6) length6 += 1; else if(length == 7) length7++; } System.out.print("1: " + length1); for(int i = 0; i < length1; i++) System.out.print("*"); System.out.println(); System.out.print("2: " + length2); for(int i = 0; i < length2; i++) System.out.print("*"); System.out.println(); System.out.print("3: " + length3); for(int i = 0; i < length3; i++) System.out.print("*"); System.out.println(); System.out.print("4: " + length4); for(int i = 0; i<length4; i++) System.out.print("*"); System.out.println(); System.out.print("5: " + length5); for(int i = 0; i< length5; i++) System.out.print("*"); System.out.println(); System.out.print("6: " + length6); for(int i = 0; i< length6; i++) System.out.print("*"); System.out.println(); System.out.print("7: " + length7); for(int i = 0; i < length7; i++) System.out.print("*"); System.out.println(); } }