Programming in Dr Java
java_questions.docx

Unformatted Attachment Preview

1/Complete the class code below and include a method named “search” that takes two String
parameters, a string to search and the other a target string to search for in the first string.
The search method will return an integer representing the number of times the search string
was found. The string being searched is a super-string formed by joining all lines of a text file,
so we might expect to get multiple hits with our method.
HINT: walk the String in a loop and use .substring(start, end) to examine a targetsized segment of the input string for equality to your target word.
public class WordSearch {
public static void main(String[] args) {
String fileName = “story.txt”;
String target = “help”;
Scanner fileIn = new Scanner(new File(fileName));
String story = “”;
while(fileIn.hasNextLine()) {
story += fileIn.nextLine(); // Build a super-String
}
System.out.println(“(” + target + “) found ” + search(story, target) + ” times”);
}
// method code here
} // end of WordSearch class
2/Complete the class code below and using only the main method, add code to make an
EXACT COPY of the input file to an output file named copyOf_.
For example, if we input the file named story.txt our class will make an exact copy of the file,
but save it under the name copyOf_story.txt
public class CopyFile {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(“Enter name of the file to copynFileName –> “);
// Your code here
}
} // end of CopyFile class
3/Write a static method named allDivide that takes an array of integers and an integer divisor
as parameters and returns a boolean value indicating whether or not ALL of the values in the
array are divisible by the divisor (true for yes, false for no).
For example, if a variable called list stores the following values:
int[] list = {18, 6, 27, 15, 24, 3, 48};
Then the call of allDivide(list, 3) should return “true” because each of these integers is evenly
divisible by 3.
If instead the list had stored these values:
int[] list = {18, 6, 27, 15, 24, 10, 3, 48};
Then the call should return “false” because, although most of these values are evenly divisible
by 3, the value 10 is not.
public class AllDivide {
public static void main(String[] args) {
int[] list1 = {18, 6, 27, 15, 24, 3, 48};
int[] list2 = {18, 6, 27, 15, 24, 10, 3, 48};
int divisor = 3;
System.out.println(“List1 ” + (allDivide(list1, 3)? “does”:”don’t”)
+ ” divide by ” + divisor);
System.out.println(“List2 ” + (allDivide(list2, 3)? “does”:”don’t”)
+ ” divide by ” + divisor);
}
// your code here
} // end of AllDivide class
4/Write a static method named “hasNoDuplicates” that takes an array of Strings as a
parameter and that returns a boolean value indicating whether or not any of the Strings in
the array are duplicated (true for yes, false for no).
For example, if a variable called list stores the following values:
String[] list = {“a”, “bb”, “acb”, “POP”, “dad”, “John”, “no”, “yes”};
Then the call of hasNoDuplicates(list) should return “true” because there are no duplicated
values in this list.
If instead the list stored these values:
String[] list = {“a”, “bb”, “acb”, “POP”, “dad”, “John”, “no”, “yes”, “no”};
Then the call should return “false” because the value “no” appears twice in this list. Notice
that given this definition, a list of 0 or 1 elements would be considered unique.
public class HasDuplicates {
public static void main(String[] args) {
String[] list1 = {“a”, “bb”, “acb”, “POP”, “dad”, “John”, “no”, “yes”};
String[] list2 = {“a”, “bb”, “acb”, “POP”, “dad”, “John”, “no”, “yes”, “no”};
System.out.println(“List1 ” + (hasDuplicates(list1)? “does”:”doesn’t”)
+ ” contain duplicates”);
System.out.println(“List2 ” + (hasDuplicates(list2)? “does”:”doesn’t”)
+ ” contain duplicates”);
}
// your code here
} // end of HasDuplicates class
5/Complete the class code below and include a method named partition which takes an
integer array and a partition integer as parameters and then partitions the original array into
two new arrays where elements of the first array are all less than or equal to the partition
integer and the second array are all greater than the partition integer.
The method should print the original array, followed by the lower partition and finally the
upper partition. Nothing should be returned by the method.
For example, given int[ ] arr = {1, 45, 16, 7, 39, 6, 5, 11, 72, 31, 15} a method call to partition(arr,
11) should output:
[1, 45, 16, 7, 39, 6, 5, 11, 72, 31, 15]
[1, 5, 6, 7, 11]
[15, 16, 31, 39, 45, 72]
public class Partition {
public static void main(String[] args) {
int[] arrayToPartition = {1, 45, 16, 7, 39, 6, 5, 11, 72, 31, 15};
int partitionNumber = 11;
partition(arrayToPartition, partitionNumber);
}
// your method code here
} // end of Partition class

Purchase answer to see full
attachment