Question
LAB8 [1] Write a static method named wordCountAndFixSpaces(), which takes a Scanner object for a file as a parameter and reports various statistics about the
LAB8
[1] Write a static method named wordCountAndFixSpaces(), which takes a Scanner object for a file as a parameter and reports various statistics about the file. The method should generate the number of lines, the total number of words, the total number of characters, and the longest word (*Note: regard "can't" as one word and disregard any marks or signs such as ?, ", ., -, , \t, etc. when counting tokens). The text may contain multiple spaces and tabs. Those spaces should be reduced into a single space between each word in order to print them to the console. Your program can assume that the text file may contain at least one line of input (2pts).
The input file contains the following text:
"Writing is an exercise."
You get better and faster with practice.
If you were going to run a marathon a year from now,
would you wait for months and then run 26 miles cold?
No, you would build up slowly, running most days.
You might start on the flats and work up to more
demanding and difficult terrain. To become a writer, write.
Don't wait for that book manuscript or that monster external-review report
to work on your writing.
[2] Simulate the followng method by writing a completely executable program and review the execution of the method with each of the following arrays passed as its parameter, and write the value it would return. The element values of the array, list can be directly assigned to the parameter prior to calling the method (1pt).
public static int mystery1(int[] list) {
int x = 0;
for (int i = 1; i < list.length; i++) {
int y = list[i] - list[0];
if (y > x) {
x = y;
}
}
return x;
}
Input Array Value returned
{5} ______________
{3, 12} ______________
{4, 2, 10, 8} ______________
{1, 9, 3, 5, 7} ______________
{8, 2, 10, 4, 10, 9} ______________
[3] Simulate the followng method by writing a completely executable program and review the execution of the method with each of the following arrays passed as its parameter, and write the value it would return. The element values of the array, list can be directly assigned to the parameter prior to calling the method. (1pt).
public static int mystery2(int[] list) {
int x = 0;
for (int i = 0; i < list.length - 1; i++) {
if (list[i] > list[i + 1]) {
x++;
}
}
return x;
}
Array Value returned
{8} ______________
{14, 7} ______________
{7, 1, 3, 2, 0, 4} ______________
{10, 8, 9, 5, 6} ______________
{8, 10, 8, 6, 4, 2} ______________
[4] Write a complete program class which contains a static method named findMin that returns the minimum value in an array of integers. For example, if a variable named a1 refers to an array containing the values {16, 12, 25, 44}, the call of findMin(a1) should return 12 (the smallest value in the list). You may assume that the array has at least one element. (1.5pt).
Test your code with the following class:
public class TestFindMin {
public static void main(String[] args) {
int[] a1 = {16, 12, 25, 44};
int[] a2 = {587, 23, 8975, 19};
int[] a3 = {42};
System.out.println(findMin(a1)); // 12
System.out.println(findMin(a2)); // 19
System.out.println(findMin(a3)); // 42
}
// your code goes here
}
[5] Write a complete program class which contains a static method named rotateRight that accepts an array of integers as a parameter and rotates the values in the array to the right (i.e., forward in position) by one. Each element moves right by one, except the last element, which moves to the front. For example, if a variable named list refers to an array containing the values {3, 8, 19, 7}, the call of rotateRight(list) should modify it to store {7, 3, 8, 19}. A subsequent call of rotateRight(list) would leave the array as follows: {19, 7, 3, 8}. (1.5pt).
Test your code with the following class:
import java.util.*;
public class TestRotateRight {
public static void main(String[] args) {
int[] list = {3, 8, 19, 7};
rotateRight(list);
System.out.println(Arrays.toString(list)); // [7, 3, 8, 19]
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list)); // [8, 19, 7, 3]
rotateRight(list);
System.out.println(Arrays.toString(list)); // [3, 8, 19, 7]
rotateRight(list);
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list)); // [8, 19, 7, 3]
rotateRight(list);
rotateRight(list);
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list)); // [8, 19, 7, 3]
}
// your code goes here
}
[6] Write a complete program class which contains a static method named isSorted that takes an array of real numbers as a parameter and that returns true if the list is in sorted (nondecreasing) order and false otherwise. For example, if variables named list1 and list2 refer to arrays containing {16.1, 12.3, 22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls of isSorted(list1) and isSorted(list2) should return false and true respectively. Assume the array has at least one element. A one-element array is considered to be sorted. (1.5pt).
Test your code with the following class:
public class TestIsSorted {
public static void main(String[] args) {
double[] a1 = {16.1, 25.3, 12.2, 44.4};
double[] a2 = {1.5, 4.3, 7.0, 19.5, 25.1, 46.2};
double[] a3 = {42.0};
System.out.println(isSorted(a1)); // false
System.out.println(isSorted(a2)); // true
System.out.println(isSorted(a3)); // true
}
// your code goes here
}
[7] Write a complete program class which contains a static method named vowelCount that accepts a String as a parameter and produces and returns an array of integers representing the counts of each vowel in the String. The array returned by your method should hold 5 elements: the first is the count of As, the second is the count of Es, the third is the count of Is, the fourth is the count of Os, and the fifth is the count of Us. You may assume that the string contains no uppercase letters. (1.5pt).
For example, the call of vowelCount("black banana republic boots") should return an array containing {4, 1, 1, 2, 1}.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started