Assignment
University
Stony Brook UniversityCourse
CSE 114 | Introduction to Object-Oriented ProgrammingPages
5
Academic year
2023
Riley
Views
20
CSE114: Introduction to Object Oriented Programming Spring 2022 Homework2: Due Date: Homework2 is due by 11:59 PM on Sunday, March 21, 2022 . Submit your work (the .java source code files ONLY, not the compiled .class files!) through the “Homework2” link on Blackboard. You may submit an unlimited number of times; we will only grade the last/latest submission attempt, but be sure to attach all of your files to each submission attempt. Be sure to include your name and Stony Brook ID number in a comment at the beginning of each file that you submit. Submission Guidelines: 1. All the files should be inside a folder named as ‘LastName_FirstName_SBUID’ and upload it as zip file. [Example: A student named John Smith with SBUID 100200 will name the folder as Smith_John_100200 and should upload the zipped folder named Smith_John_100200.zip] 2. Each file should be named as HW*problem* (Example: ‘HW2problem1’) 3. If there are multiple classes for a problem, put each problem in a separate folder. Ensure that you compile the program before uploading (as structure mentioned above). If the program does not compile, you will lose points. Instructions: This assignment is worth 40 points (4*10). 1. Write a program that takes a user ID as an input from the keyboard and steps through every character, counting how many digits are in the ID; if there are exactly two digits, output that the user ID is valid, otherwise that it is invalid. [10 points] Here is a sample run: Enter the user ID: abc123de The input user ID is invalid Enter the user ID: xyz9a2bc The input user ID is valid 2. Write a program that inputs a word from the keyboard representing a binary number (0’s and 1’s). first your program should check that it is indeed a binary number, that is, the number contains only 0’s and 1’s. If that is not the case, your program should output that “The input number is not a valid binary number”. Otherwise, if that input word is a binary number and contains at least three consecutive 1’s, your program should out put that “The binary word is accepted”, otherwise that “The binary word is rejected”. [10 points] Here is a sample run:
Enter the word: 12345 The input word is not a valid binary number Enter the word: 10101101 The binary word is rejected. Enter the word: 101011101 The binary word is accepted. 3. Write a method that checks whether the input string or a sentence (a string with spaces) is a palindrome or not. The method should be case insensitive and should ignore spaces. Write a test program that prompts the user to input a string and invokes this method. Some example runs are: [10 points] Enter the input string: madam Input string madam is a palindrome Enter the input string: banana Input string banana is NOT a palindrome Enter the input string: Race Car Input string Race Car is a palindrome Enter the input string: Too HOT to hoot Input string Too HOT to hoot is a palindrome 4. Two strings are anagrams if they are written using the same exact letters. Write a method to check if given two strings are anagrams or not. You have to ignore the case and space characters. Write a test program for that prompts the user to input two strings and invokes this method. Some example runs are: [10 points] Enter the first string: abbacba Enter the second string: abcabba abbacba and abcabba are anagrams Enter the first string: banana Enter the second string: cabana banana and cabana are NOT anagrams Enter the first string: Eleven plus two Enter the second string: Twelve plus one Eleven plus two and Twelve plus one are anagrams
Part 1 import java.util.* ; public class HW2problem1 { public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print( "Enter the user ID: " ) ; String userID = input.nextLine() ; int numOfDigits = 0 ; for ( int i = 0 ; i < userID.length() ; i++) { char character = userID.charAt(i) ; if (character >= '0' && character <= '9' ) numOfDigits++ ; } if (numOfDigits == 2 ) System.out.println( "The input user ID is valid" ) ; else System.out.println( "The input user ID is invalid" ) ; } } Part 2 import java.util.* ; public class HW2problem2 { public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print( "Enter the word: " ) ; String binary = input.nextLine() ; String substring ; int length = binary.length() ; for ( int i = 0 ; i < length ; i++) { char character = binary.charAt(i) ; if (character != '0' && character != '1' ) { System.out.println( "The input word is not a valid binary number" ) ; System.exit( 0 ) ; } if (character == '1' && i < length - 2 ) { int numOfOne = 1 ; substring = binary.substring(i + 1 ) ; for ( int j = 0 ; j < 2 ; j++) { if (substring.charAt(j) == '1' ) numOfOne++ ; } if (numOfOne == 3 ) { System.out.println( "The binary word is accepted." ) ; System.exit( 0 ) ; } } } System.out.println( "The binary word is rejected." ) ; } }
Part 3 import java.util.* ; public class HW2problem3 { public static void main(String[] args) { Scanner input = new Scanner(System.in) ; System.out.print( "Enter the input string: " ) ; String userInput = input.nextLine() ; String lowerCase = userInput.toLowerCase() ; int length = userInput.length() ; int i = 0 ; int j = length - 1 ; while (i < length) { char c1 = lowerCase.charAt(i) ; char c2 = lowerCase.charAt(j) ; if (c1 == ' ' ) { i++ ; continue; } if (c2 == ' ' ) { j-- ; continue; } if (c1 != c2) { System.out.printf( "Input string %s is NOT a palindrome" , userInput) ; System.exit( 0 ) ; } i++ ; j-- ; } System.out.printf( "Input string %s is a palindrome" , userInput) ; } }
Part 4 import java.util.* ; public class HW2problem4 { public static void main(String[] args) { Scanner input = new Scanner(System.in) ; System.out.print( "Enter the first string: " ) ; String string1 = input.nextLine() ; System.out.print( "Enter the second string: " ) ; String string2 = input.nextLine() ; String lowerCase1 = string1.toLowerCase() ; String lowerCase2 = string2.toLowerCase() ; for ( int i = 0 ; i < lowerCase1.length() ; i++) { char c = lowerCase1.charAt(i) ; if (c != ' ' && lowerCase2.indexOf(c) == - 1 ) { System.out.printf( "%s and %s are NOT anagrams" , string1 , string2) ; System.exit( 0 ) ; } } for ( int i = 0 ; i < lowerCase2.length() ; i++) { char c = lowerCase2.charAt(i) ; if (c != ' ' && lowerCase1.indexOf(c) == - 1 ) { System.out.printf( "%s and %s are NOT anagrams" , string1 , string2) ; System.exit( 0 ) ; } } System.out.printf( "%s and %s are anagrams" , string1 , string2) ; } }
CSE114: Homework 2 Spring 2022
Please or to post comments