CMPS 200 Homework Assignment 10 with Solution, Fall 2012

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

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

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

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

More CMPS 200 files

Show the text of this homework
/*ghassan fakher * section 6 gkf01*/ public class Fraction { int numerator; int denomenator; // constructor n/p public Fraction(int n, int v) { numerator = n; denomenator = v; } //constructor, default denominator = 1 public Fraction(int v) { numerator = v; denomenator = 1; } // returns printed representation public String toString() { return (numerator + "/" + denomenator); } //multiplication public Fraction times(Fraction f) { int num = numerator * f.numerator; int deno = denomenator * f.denomenator; return new Fraction(num, deno); } // addition public Fraction plus(Fraction f) { int num = numerator * f.denomenator + f.numerator * denomenator; int deno = denomenator * f.denomenator; return(new Fraction(num, deno)); //not in irreducible form } //equality check public boolean equals(Fraction f) { boolean equals = false; if((numerator / f.numerator) * denomenator == f.denomenator) // two fractions are equal if their common ratio is the same equals = true; return equals; } // < operator public boolean lessThan(Fraction f) { boolean isLess = false; if(f.denomenator > denomenator) isLess = false; //as the denomenator increases, the value of the fraction decreases else if(denomenator == f.denomenator) { if(numerator > f.denomenator) isLess = false; else isLess = true; } return isLess; } } /*ghassan fakher * section 6 * gkf01 201301739*/ public class FractionsClient { public static void main(String[] args) { Fraction x = new Fraction(1, 2); Fraction y = new Fraction(1, 4); StdOut.println(x.toString()); // checking my methods written StdOut.println(y.toString()); // testing StdOut.println(x.times(y)); //check StdOut.println(x.plus(y)); //check //the required lines StdOut.println("Is x equals y? " + x.equals(y)); StdOut.println("Is x less than y? " + x.lessThan(y)); Fraction z = x.times(y); Fraction w = x.plus(y); StdOut.println(x + " * " + y + " = " + z); StdOut.println(x + " + " + y + " = " + w); } } /*ghassan fakher * section 6 gkf01*/ public class GridTraversal { public static void main(String[] args) { String str = args[0]; printGrid(str); } //just to see the contents of the folder to compare public static void printGrid(String str) { In input = new In(str); int n = input.readInt(); int[][] grid = new int[n][n]; for (int i = 0; i < grid.length; i++) { for (int j=0; j < grid[i].length; j++) { grid[i][j] = input.readInt(); StdOut.printf("%3s", grid[i][j] + " "); } System.out.println(); } System.out.println(); traverseGrid(grid, n); } public static void traverseGrid(int[][] grid, int n) { int row = 0; int column = 0; int i = 0; while (column < n) { System.out.print(grid[row][column] + " "); column++; i ++; } column--; int k = n-1; while (i < n*n) { for (int j = 0; j < 2 * k; j++) { if (j < k) row++; else column--; System.out.print(grid[row][column] + " "); i++; } k--; for (int j = 0; j < 2*k; j++) { if (j < k) row--; else column++; System.out.print(grid[row][column] + " "); i++; } k--; } } } /*ghassan fakher * section 6 gkf01*/ public class InsertionSortOfIntegers { public static void main(String[] args) { In input = new In("ints.txt"); int[] inputInts = new int[input.readInt()]; for(int i = 0; i < inputInts.length; i++) inputInts[i] = input.readInt(); printArray(insertionSort2(inputInts)); System.out.println(); printArray(inputInts); // to check that the original array hasn't changed System.out.println(); insertionSort(inputInts); } //method used to manipulate the array in order to sort it public static void insertionSort(int[] a){ for (int i = 1; i < a.length; i++){ int j = i; int numAti = a[i]; while ((j > 0) && (a[j-1] > numAti)){ // putting in a place where it is larger than all its left neighbors. a[j] = a[j-1]; j--; } a[j] = numAti; } } //method that makes a new array with the same elements as the original one public static int[] insertionSort2(int[] a){ int[] clone = new int[a.length]; for(int i = 0; i < clone.length; i++) // cloning the array. clone[i] = a[i]; insertionSort(clone); return clone; } public static void printArray(int[] a){ for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); } } /*ghassan fakher * gkf01 section 6*/ public class InsertionSortOfStrings { public static void main(String[] args) { int length = fileLength(args[0]); String [] words = new String [length]; buildArrayString(words, args[0]); stringSort(words); print(words, args[1]); } // calculates the length of the input file public static int fileLength(String fileName) { In input = new In(fileName); int numStrings = 0; while(!input.isEmpty()) { input.readString(); numStrings++; } return numStrings; } //constructs an array of the words of the input file public static String[] buildArrayString(String[] words, String fileName) { In input = new In(fileName); for(int i = 0; i < words.length; i++) { words[i] = input.readString(); } return words; } //method that sorts the strings according to dictionary order public static void stringSort(String[] words) { for (int j = 0; j < words.length; j++) { for(int i = 1; i < words.length; i++) { if(words[i].compareTo(words[i-1]) < 0) // if lexicographically smaller than the string before { String temp = words[i]; // swap places. words[i] = words[i-1]; words[i-1] = temp; } } } } //prints the output to a selected file public static void print(String[] words, String outputFile) { Out output = new Out (outputFile); for(int i = 0; i < words.length;i++) output.print(words[i]); System.out.println(); } } /*ghassan fakher * section 6 gkf01*/ public class PascalsTriangle { public static void main(String[] args) { int[][] triangle = new int[Integer.parseInt(args[0]) + 1][]; makeTriangle(triangle); } public static void makeTriangle(int[][] triangle) { for(int i = 0; i < triangle.length; i++) { triangle[i] = new int[i + 1]; triangle[i][0] = 1; triangle[i][i] = 1; for(int j = 1; j < i; j++) { triangle[i][j] = triangle[i - 1][j] + triangle[i- 1][j - 1]; } } printTriangle(triangle); } public static void printTriangle(int[][] triangle) { for(int i = 0; i < triangle.length; i++) { for(int j = 0; j < triangle[i].length; j++) StdOut.printf("%-2d ", triangle[i][j]); StdOut.println(); } } } (AUB)1863,AmericanAmericanBeirutCharteredEastGraduatesMiddleNewStateTheTheUniver sityYorkadvancementanandandandandandandandandandandandartsbasesbebelievesbeyond. civiccommittedcreativecriticaldeeplydialogue.diversityeducation,education.educat ionalencouragesexcellenceexpressionforfosterfoundedfreedomhigherhigherininininin individualsinstitutionintegrityisitsknowledgeleadership.learninglearning,liberal life- longmodelofofofofofofonparticipatepeoplespersonalphilosophy,practicesproviderese arch,respectresponsibility,seeksservestandards,thethethethethethinking,thoughtth roughtotototototoleranceuniversityuniversitywill