1. Write a Java program FindStringPattern.java which a. Takes in a String inputS
ID: 3666368 • Letter: 1
Question
1. Write a Java program FindStringPattern.java which
a. Takes in a String inputString as a command-line argument; the objective is to find patterns of characters in this String;
b. Prompts the user for, and then scans using Scanner, a String input pattern, representing a pattern of characters to search string for;
c. Using a loop, search for the String pattern in the String inputString;
d. Whenever you find pattern in inputString, print it out in the following format (for example, searching for the pattern “ing” in the String thingamajing):
Matching pattern: ing;
e. Count up the number of times pattern is found in inputString, and print in the format at the end:
There are 2 matches to the pattern “ing”
f. Manually test this program (by running repeatedly) with the following test values:
Test1: inputString: “abbabba” pattern: “ab”
Test2: inputString: “abbabba” pattern: “ba”
Test3: inputString: “abbabba” pattern: “aa”
Test4: inputString: “abbabba” pattern: “bb”
Test5: inputString: “abbabba” pattern: “abb”
Test6: inputString: “abbabba” pattern: “bba”
Test7: inputString: “abbabba” pattern: “bab”
Test8: inputString: “abbabba” pattern: “abba”
Test9: inputString: “disestablishmentarianism” pattern: “is”
2. Write a program FindPrimes.java accepts an integer number as a command-line argument and parses it into an int. Then print out all the prime numbers up to that integer. For example, when the user enters 30, the program should print:
Primes from 1 to 30: 2 3 5 7 11 13 17 19 23 29
3. Write a Java class TestFindPrimes.java with a main method to test each of these programs. Follow the same approach to call the main method of TestFindPrimes, sending the command-line arguments as Strings. Use the following values to test your programs:
Test1: 10; Test2: 30; Test3: 50; Test4: 100; Test5: 1000; Test6: 100000; Test7: 1000000; Test8: 1000000000
Of course, your programs should compute correct results for any valid test inputs.
Explanation / Answer
1.
//FIndStringPattern.java
import java.util.Scanner;
public class FindStringPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputString = args[0];
System.out.println("Enter a pattern: ");
String inputPattern = sc.nextLine();
int n = inputString.length();
int m = inputPattern.length();
int count = 0;
for(int i=0;i<n-m; i++) {
int j=0;
for(;j<m;j++){
if(inputString.charAt(i+j) != inputPattern.charAt(j))
break;
}
if(j == m) {// found a pattern
System.out.println("Matching pattern: "+inputPattern);
count++;
}
}
System.out.println("There are "+count+" matches to the pattern "+inputPattern);
}
}
/*
For inputString: abbabba
Enter a pattern:
ab
Matching pattern: ab
Matching pattern: ab
There are 2 matches to the pattern ab
*/
2.
//FindPrimes.java
public class FindPrimes {
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
int counter = 0;
System.out.print("Primes from 1 to "+number+": ");
for (int i = 2; i <= number; i++) {
counter = 0;
for (int n = 2; n < i; n++) {
if (i % n == 0) {
counter++;
}
}
if (counter == 0) {
System.out.print(i+" ");
}
}
}
}
/*
OUTPUT:
For number = 30
Primes from 1 to 30: 2 3 5 7 11 13 17 19 23 29
*/
3.
//TestFindPrimes.java
public class TestFindPrimes {
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
FindPrimes.printPrimesUpToN(number);
}
}
/*
For number: 50
Output:
Primes from 1 to 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.