CMPS 200 Homework Assignment 7 with Solution, Fall 2012

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

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

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

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

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

More CMPS 200 files

Show the text of this homework
The American University of Beirut (AUB) is an institution of higher learning founded to provide excellence in education, to participate in the advancement of knowledge through research, and to serve the peoples of the Middle East and beyond. Chartered in New York State in 1863, the university bases its educational philosophy, standards, and practices on the American liberal arts model of higher education. The university believes deeply in and encourages freedom of thought and expression and seeks to foster tolerance and respect for diversity and dialogue. Graduates will be individuals committed to creative and critical thinking, life-long learning, personal integrity and civic responsibility, and leadership. /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ public class BarcodeA { public static void main(String[] args) { int given = Integer.parseInt(args[0]); int[] numReversed = new int[args[0].length()];//the array will put the numbers reversed for(int i = 1; i <= numReversed.length; i++) { numReversed[i - 1] = (int)((given % Math.pow(10 , i)) /(Math.pow(10, i - 1))); } String[] barCode = {"||:::", ":::||", "::|:|", "::||:", ":|::|", ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"}; System.out.print("|"); int sum = 0; for(int i = numReversed.length - 1; i >= 0 ; i--) { System.out.print(barCode[numReversed[i]]); sum += numReversed[i]; } if(sum % 10 == 0) System.out.print(barCode[0]); else { int checksum = (10 - (sum % 10)); System.out.print(barCode[checksum]); } System.out.print("|"); System.out.println(); } } /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ public class BarcodeB { public static void main(String[] args) { StdDraw.setCanvasSize(800, 400); int given = Integer.parseInt(args[0]); double x0=.03125; double dis=.03125; int[] numReversed = new int[args[0].length()];//the array will put the numbers reversed for(int i = 1; i <= numReversed.length; i++) { numReversed[i - 1] = (int)((given % Math.pow(10 , i)) /(Math.pow(10, i - 1))); } int sum = 0; StdDraw.line(0, 0, 0, .5); for(int i = numReversed.length - 1; i >= 0 ; i--) { if (numReversed[i]=='0'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (numReversed[i]=='1'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (numReversed[i]=='2'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (numReversed[i]=='3'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (numReversed[i]=='4'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (numReversed[i]=='5'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (numReversed[i]=='6'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (numReversed[i]=='7'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (numReversed[i]=='8'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (numReversed[i]=='9'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } } StdDraw.line(0.96875, 0, .96875, .5); if(sum % 10 == 0) { StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); } else { int checksum = (10 - (sum % 10)); if (checksum=='0'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (checksum=='1'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (checksum=='2'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (checksum=='3'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (checksum=='4'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (checksum=='5'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (checksum=='6'){ StdDraw.line(x0, 0, x0, .25); StdDraw.line(x0+dis, 0, x0+dis, .5); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (checksum=='7'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .5); x0+=5*dis; } if (checksum=='8'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .25); StdDraw.line(x0+3*dis, 0, x0+3*dis, .5); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } if (checksum=='9'){ StdDraw.line(x0, 0, x0, .5); StdDraw.line(x0+dis, 0, x0+dis, .25); StdDraw.line(x0+2*dis, 0, x0+2*dis, .5); StdDraw.line(x0+3*dis, 0, x0+3*dis, .25); StdDraw.line(x0+4*dis, 0, x0+4*dis, .25); x0+=5*dis; } } StdDraw.line(0.96875, 0, .96875, .5); System.out.println(); } } import java.awt.Color; /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ public class BouncingBall { public static void main(String[] args) { StdDraw.setXscale(-1.,1.); StdDraw.setYscale(-1., 1.); double rx = -0.0480; double ry = -0.60; double vx = 0.05015; double vy = 0.0123; double radius = .05; StdDraw.clear(Color.GRAY); StdDraw.setPenRadius(radius); StdDraw.setPenColor(Color.BLACK); while(ry < 0.9 || rx <0) { if(Math.abs(rx + vx) + radius > 1.0) vx = -vx; if(Math.abs(ry + vy) + radius > 1.0) vy = -vy; rx = rx + vx; ry = ry + vy; StdDraw.point(rx, ry); StdDraw.show(50); } StdDraw.clear(Color.GRAY); StdDraw.show(); } } /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ import java.awt.Color; //to access color library public class ChangingColor { public static void main(String[] args) { while(true) { double r = Math.random()*256; int r1 = (int) r; double g = Math.random()*256; //generating colors according to RGB int g1 = (int) g; //representation double b = Math.random()*256; int b1 = (int) b; Color c = new Color(r1 ,g1 ,b1); StdDraw.setPenColor(c); StdDraw.filledSquare(0.5, 0.5, 0.5); StdDraw.show(500); //change color every half a minute } } } /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ import java.awt.Color; public class CheckerBoard { public static void main(String[] args) { int x0 = 0; int x1 = 10; int y0 = 0; int y1 = 10; int length = args.length; //number of squares of circles double border = x1 / 3; //represents the border each square of circles should occupy double x = 0; double y = 0; StdDraw.setXscale(x0, x1); StdDraw.setYscale(y0, y1); for(int i = 0; i < length; i++) { int N = Integer.parseInt(args[i]); double r = (x1 / 3. - 2.25) / N ; //radius depends on the scale of x and y. -2 because it represents the distance between two boxes double positionX = border * i; //keeps track of the initial position of the ball to place in on the canvas x = positionX; drawBoard(x1, N, length,x ,y,r,positionX); } } //method that draws the three desired boxes on the same canvas public static void drawBoard(int x1,int N, int length, double x, double y,double r, double positionX) { for(int row = 1; row <= N; row++) { for(int column = 1 ; column <= N; column++) { if((column + row )% 2 == 0) { StdDraw.setPenColor(Color.RED); StdDraw.filledCircle(x,y,r); } else { StdDraw.setPenColor(Color.BLACK); StdDraw.filledCircle(x, y, r); } x = x + 2*r; //the position of x gets updates according to the radius } x = positionX; y = y +2*r; //the ordinate of the box gets updated by twice the radius } y = 0.; //resets y to 0 after completing the drawing } } /*Ghassan Fakher * gkf01 * section 6 * 201301739*/ public class Discount { public static void main(String[] args) { printTable(); } //method used to print the aligned table of the prices, discount and its equivalence to LL public static void printTable() { StdOut.printf("Price%14s %21s\n","Discounted","LL Equivalent Price");//prints the first row of the table while(!StdIn.isEmpty()) { double price = StdIn.readDouble();//reads the prices from the prices.txt file stored earlier double discounted = price - (1./3)*price;//computes the discount int equivalentPrice = (int)(1508 * discounted);//converts to Lebanese pounds if(equivalentPrice % 100 > 0 && equivalentPrice % 100 < 50) equivalentPrice += (50 - (equivalentPrice % 100)); else if(equivalentPrice % 100 > 50 && equivalentPrice % 100 < 100) equivalentPrice += (100 - (equivalentPrice % 100)); StdOut.printf("$%6.2f",price); StdOut.printf("%3s%-9.2f","$",discounted); StdOut.printf("%5s %6d\n","LL",equivalentPrice); } } } /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ public class DocumentStats { public static void main(String[] args) { String document = ""; int numWords = 0; int numCharacters = 0; while(!StdIn.isEmpty()) { document += StdIn.readString(); numWords++; } for(int i = 0; i < document.length(); i++) { if(document.charAt(i) != ' ') { numCharacters++; } } System.out.println("Number of words: " + numWords); System.out.println("Number of characters without spaces: " + (numCharacters)); } } /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ public class GuessTheNumber { public static void main(String[] args) { int x = 1000000; int max = 10; int min = 0; StdOut.println("Think of a number between 1 and a million, then answer truthfully the questions asked by true or false"); System.out.print("Type a number between 1 and a million here: "); int guess = StdIn.readInt(); while(guess != x) { StdOut.print("My number is less than or equal to " + x + " ? "); boolean answer = StdIn.readBoolean(); if(answer == true) { max = x; x = x/2; min = x; x = (int)(max/2.); } else { min = x; x =((min + max)/ 2); } } } } /*Ghassan Fakher gkf01 201301739 section 6*/ //program that returns the minimum and maximum numbers that are interactively given by the user public class MinMax { public static void main(String[] args) { int numberOfInput = Integer.parseInt(args[0]); int max = -99999; int min = 99999999; for(int i = 0; i < numberOfInput; i++) { StdOut.printf("Enter integer %1d%-3s", i+1,":"); int given = StdIn.readInt(); System.out.println(); if(given > max) max = given; if(given <= min)//we don't use if / else because the two conditions might be valid... min = given; } System.out.println("Minimum: " + min); System.out.println("Maximum: " + max); } } /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ Note: I didn't know how to get sample runs for the programs that have drawings as output... 1) Program MinMax PS C:\Users\ghassan\cmps200\assignment7\src> javac .\MinMax.java C:\Users\ghassan\cmps200\assignment7\src>java MinMax 3 Enter integer 1: 321 Enter integer 2: 123 Enter integer 3: 19 Minimum: 19 Maximum: 321 2) Program Discount C:\Users\ghassan\cmps200\assignment7\src>javac Discount.java C:\Users\ghassan\cmps200\assignment7\src>java Discount<prices.txt Price Discounted LL Equivalent Price $ 80.00 $53.33 LL 80450 $140.00 $93.33 LL 140750 $321.00 $214.00 LL 322750 $159.00 $106.00 LL 159850 3)Progam CheckerBoard 4)Program Barcode BarcodeA: PS C:\Users\ghassan\cmps200\assignment7\src> javac .\BarcodeA.java PS C:\Users\ghassan\cmps200\assignment7\src> java BarcodeA 95014 ||:|:::|:|:||::::::||:|::|:::||| BarcodeB: 5)Program ChangingColor 6)Program BouncingBall 7)Program Squares 8)Program GuessTheNumber 9)Program DocumentStats C:\Users\ghassan\cmps200\assignment7\src>javac DocumentStats.java C:\Users\ghassan\cmps200\assignment7\src>java DocumentStats < AUBMissionStatement.txt Number of words: 105 Number of characters without spaces: 613 import java.awt.Color; /*Ghassan Fakher * section 6 * 201301739 * gkf01*/ public class Squares { public static void main(String[] args) { int numSquares = Integer.parseInt(args[0]); double probability = Double.parseDouble(args[1]); double minLength = Double.parseDouble(args[2]); double maxLength = Double.parseDouble(args[3]); double radius = Math.sqrt((Math.random() * (maxLength - minLength) + minLength)) / 2; StdDraw.setXscale(0,1); StdDraw.setYscale(0,1); for(int i = 1; i <= numSquares; i++) { double x = Math.random(); double y = Math.random(); double random = Math.random(); if(minLength != maxLength) { if(random < probability) { StdDraw.setPenColor(Color.BLACK); StdDraw.filledSquare(x, y, radius); } else { StdDraw.setPenColor(Color.WHITE); StdDraw.filledSquare(x, y, radius); } } else { if(random < probability) { StdDraw.setPenColor(Color.BLACK); StdDraw.filledSquare(x, y, Math.sqrt(minLength* minLength + maxLength*maxLength) / 2); } else { StdDraw.setPenColor(Color.WHITE); StdDraw.filledSquare(x, y, Math.sqrt(minLength* minLength + maxLength*maxLength) / 2); } } } } } 80.00 140.00 321.00 159.00