CMPS 200 Homework Assignment 4 Solution, Fall 2012

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

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

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

CMPS 200 Homework Assignment 4 Solution, Fall 2012, page 2 CMPS 200 Homework Assignment 4 Solution, Fall 2012, page 3 CMPS 200 Homework Assignment 4 Solution, Fall 2012, page 4 CMPS 200 Homework Assignment 4 Solution, Fall 2012, page 5

More CMPS 200 files

Show the text of this homework
/*Ghassan Fakher gkf01 201301739 section 6*/ public class Palindromes // checks whether the command line argument is plaindrome and prints it { public static void main(String[] args) { int numStrings = Integer.parseInt(args[0]); for(int i = 1; i <= numStrings; i++) { if(isPalindrome(args[i])) { System.out.println(args[i] + " is palindrome."); } } } public static boolean isPalindrome(String str) // checks each letter of the given argument and returns whether it is palindrome or not { boolean t = true; String low = str.toLowerCase(); for(int position = 0; position < str.length(); position++) { if((low.charAt(position) == low.charAt(low.length() - 1 - position))) { t = t && true; } else { t = false; } } return t; } } /*Ghassan Fakher gkf01 201301739 section 6*/ public class RandomString // takes a command line argument and prints random letters of this string { public static void main(String[] args) { int num = Integer.parseInt(args[0]); System.out.println(random(num, args[1])); } // takes the given words and returns a string formed of random characters public static String random(int num, String given) { String randomString = ""; for(int i = 1; i <= num ; i++) { int any = (int)(Math.random()*given.length()); char random = given.charAt(any); randomString += random; } return randomString; } } /*Ghassan Fakher section 6 gkf01 201301739*/ public class RemoveSpaces// accepts a string argument and prints it without spaces { public static void main(String[] args) { for(int n = 1; n <= Integer.parseInt(args[0]); n++) { System.out.println("The new string without spaces is : " + removeSpaces(args[n])); } } public static String removeSpaces(String str)// checks whether each character is a space an returns a string without spaces { String noSpaces = ""; for(int position = 0; position <= str.length() - 1; position++) { if(str.charAt(position) != ' ') { noSpaces = noSpaces + str.charAt(position); } } return noSpaces; } } /*Ghassan Fakher gkf01 201301739 section 6*/ 1) Program Triangle PS C:\Users\ghassan\cmps200\assignment4> javac .\Triangle.java PS C:\Users\ghassan\cmps200\assignment4> java Triangle 6 ****** .***** ..**** ...*** ....** .....* 2) Program RandomString C:\Users\ghassan\cmps200\assignment4> javac .\RandomString.java PS C:\Users\ghassan\cmps200\assignment4> java RandomString 5 ghassan asshh 3) Program RemoveSapces PS C:\Users\ghassan\cmps200\assignment4> javac .\RemoveSpaces.java PS C:\Users\ghassan\cmps200\assignment4> java RemoveSpaces 3 "Ghassan Fakher" "Computer Science" "cmps200" The new string without spaces is : GhassanFakher The new string without spaces is : ComputerScience The new string without spaces is : cmps200 4) Program Palindromes PS C:\Users\ghassan\cmps200\assignment4> javac .\Palindromes.java PS C:\Users\ghassan\cmps200\assignment4> java Palindromes 3 ghassan heeh madam heeh is palindrome. madam is palindrome. /*Ghassan Fakher gkf01 201301739 section 6*/ public class Triangle// builds two triangles made up of stars and dots { public static void main(String[] args) { int dimension = Integer.parseInt(args[0]); drawTriangles(dimension); } public static void drawTriangles(int dimension) { for(int x = dimension; x >= 1; x--) { for(int i = 1; x <= dimension - i; i++) { System.out.print("."); } for(int i = 1; i <= x; i++) { System.out.print("*"); } System.out.println(); } } }