Part1
Token-Based File Processing
1. Write a static method named evenNumbers that accepts a string of text as a parameter. Assume that the text
is a series of integers, and process this text and report various statistics about the integers. Report the total
number of numbers, the sum of the numbers, the total count of even numbers and the percent of even
numbers. For example, if the string is the following:
"5 7 2 8 9 10 12 98 7 14 20 22"
Your method should produce the following output.
Total numbers
Sum of numbers
Total evens
Percent evens
=
=
=
=
12
214
8
66.66666666666667
If a non-integer element is encountered, it should be ignored & not cause the program
to crash. For example: "5 7 2 8 9 10 oops 12 98 7 14 20 22" will product output
identical to the original example above.
import java.io.*;
import java.util.*;
public class Numbers {
public static void main(String[] args)
throws FileNotFoundException {
Scanner input = new Scanner(new File("evennumbers.txt"));
evenNumbers(input);
}
public void evenNumbers(Scanner sc) {
int count = 0;
int evenCount = 0;
int sum = 0;
while(sc.hasNext()) {
int num = sc.nextInt();
count++;
sum += num;
if(num % 2 == 0)
evenCount++;
}
double percent = (double)(evenCount * 100) / count;
System.out.println(count + " numbers, sum = " + sum);
System.out.println(evenCount + " evens (" +
String.format("%.2f", percent) + "%)");
}
}
1 of 20
Method Only
public void evenNumbers(Scanner sc) {
int count = 0;
int evenCount = 0;
int sum = 0;
while(sc.hasNext()) {
int num = sc.nextInt();
count++;
sum += num;
if(num % 2 == 0)
evenCount++;
}
double percent = (double)(evenCount * 100) / count;
System.out.println(count + " numbers, sum = " + sum);
System.out.println(evenCount + " evens (" +
String.format("%.2f", percent) + "%)");
}
2. Write a static method named negativeSum that accepts a string of text as a parameter. Assume that the text
is a series of integers, and determine whether or not the cumulative sum starting from the first number is
ever negative. The method should produce a message indicating whether or not a negative sum is possible
and it should return true if a negative sum can be reached and false if it can't be reached. For example, if
the string contains the following text,
"38 4 19 -27 -15 -3 4 19 38"
your method will consider the sum of just one number (38), the sum of the first two numbers (38 + 4), the
sum of the first three numbers (38 + 4 + 19), and so on up to the sum of all of the numbers. None of these
sums is negative, so the method would produce the following message:
no negative sum
and would return false. If the text is the following,
"14 7 -10 9 -18 -10 17 42 98"
the method finds that a negative sum is reached after adding 6 numbers together
(14 + 7 + -10 + 9 + -18 + -10) and that the sum is -8. It should report the following:
-8 after 6 steps
and should return true, indicating that a negative sum can be reached. The method should return the first
negative sum it encounters. Also: As in question #1, if non-integer element is encountered, it should be
ignored. It should not cause the program to crash.
import java.io.*;
import java.util.*;
public class Sum {
public static void main(String[] args)
throws FileNotFoundException {
Scanner input = new Scanner(new File("negativesum.txt"));
negativeSum(input);
2 of 20
}
public static boolean negativeSum(Scanner console) {
int sum = 0;
int step = 0;
while (console.hasNextInt()) {
sum += console.nextInt();
step++;
if (sum < 0) {
System.out.printf("%d after %d steps\n", sum, step);
return true;
}
}
System.out.println("no negative sum");
return false;
}
}
Methods Only
public static boolean negativeSum(Scanner console) {
int sum = 0;
int step = 0;
while (console.hasNextInt()) {
sum += console.nextInt();
step++;
if (sum < 0) {
System.out.printf("%d after %d steps\n", sum, step);
return true;
}
}
System.out.println("no negative sum");
return false;
}
3.
Write a static method named boyGirl that takes a string of text as a parameter. Assume that the text
represents a series of names followed by integers, and that the names alternate between boys' names and girls
names. Your method should compute the sum of the boys' integers and the sum of the girls' integers separately
and print them. The line could end with either a boy or girl; you may not assume that it contains an even
number of names. For example, if the String contains the following text,
"JP 3 Helene 7 Jordan 14 Iva 13 Sergey 4 Marianne 9 Kenneth 6"
then your method should produce the following output:
4 boys, 3 girls
3 of 20
Boys sum = 26
Girls sum = 29
import java.io.*;
import java.util.*;
public class Gender {
public static void main(String[] args)
throws FileNotFoundException {
Scanner input = new Scanner (new File("boyGirl.txt"));
boyGirl(input);
}
public static void boyGirl(Scanner console) {
int boy = 0;
int bSum = 0;
int girl = 0;
int gSum = 0;
int i = 2;
while (console.hasNext()) {
console.next();
if ((i / 2) % 2 != 0) {
bSum += console.nextInt();
boy++;
} else {
gSum += console.nextInt();
girl++;
}
i += 2;
}
System.out.println(boy + " boys, " + girl + " girls");
System.out.println("Difference between boys' and girls' sums: " + Math.abs(bSum
- gSum));
}.out.println("Girls sum = " + girlSum);
}
}
Methods Only
public static void boyGirl(Scanner console) {
int boy = 0;
int bSum = 0;
int girl = 0;
int gSum = 0;
int i = 2;
while (console.hasNext()) {
console.next();
if ((i / 2) % 2 != 0) {
bSum += console.nextInt();
boy++;
} else {
gSum += console.nextInt();
girl++;
}
i += 2;
}
4 of 20
System.out.println(boy + " boys, " + girl + " girls");
System.out.println("Difference between boys' and girls' sums: " + Math.abs(bSum gSum));
}
Line-based Processing
4. Write a static method named textCount that takes a Scanner representing a file as a parameter and that
reports various statistics about the file. In particular, your method should report the number of lines in the
file, the total number of characters (not counting any new-line \n characters) in the file, and the length and
text of the longest line. You may assume that the input file has at least one line of input.
For example, if the file contains the following text:
Twas brillig and the slithy toves
did gyre and gimble in the wabe.
All mimsey were the borogroves,
and the mome raths outgrabe.
"Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch."
The program would find that there are 9 lines (blank lines count), a total of 254 characters in the file (33 on
the first line, 32 on the next line, 31 on the next line, 28 on the next, 0 on the next, etc) and that the longest
line is the one that begins with "the jaws that bite". The method would produce the following output:
Total lines
=
Total chars
=
Length of longest line =
Longest line
=
9
254
41
the jaws that bite, the claws that catch,
import java.io.*;
import java.util.*;
public class Text {
public static void main(String[] args)
throws FileNotFoundException {
Scanner input = new Scanner (new File("text.txt"));
textCount(input);
}
public static void textCount(Scanner input){
int totalLines = 0;
int totalChars = 0;
int maxChar = 0;
String longestLine= "";
while(input.hasNextLine()){
String line = input.nextLine();
totalLines++;
totalChars+=line.length();
if(maxChar < line.length()){
maxChar = line.length();
5 of 20
longestLine = line;
}
}
System.out.println("Total lines = " + totalLines);
System.out.println("Total characters = " + totalChars);
System.out.println("Length of longest line = " + maxChar);
System.out.println("Longest line = " + longestLine);
}
}
Methods Only
public static void textCount(Scanner input){
int totalLines = 0;
int totalChars = 0;
int maxChar = 0;
String longestLine= "";
while(input.hasNextLine()){
String line = input.nextLine();
totalLines++;
totalChars+=line.length();
if(maxChar < line.length()){
maxChar = line.length();
longestLine = line;
}
}
System.out.println("Total lines = " + totalLines);
System.out.println("Total characters = " + totalChars);
System.out.println("Length of longest line = " + maxChar);
System.out.println("Longest line = " + longestLine);
}
Line-and-Token Processing
5. Write a static method named fixSpacing that accepts a Scanner representing a file as a parameter and
writes that file's text to the console, with multiple spaces or tabs reduced to single spaces between words
that appear on the same line.
For example, if the input file contains the following text,
four
score
seven
fathers brought
on this
a
new
and
years ago
our
forth
continent
nation
then your method should produce the following output:
four score and
6 of 20
seven years ago our
fathers brought forth
on this continent
a new
nation
Notice that some lines might be blank. Each word is to appear on the same line in the output on which it
appears in the input file.
import java.io.*;
import java.util.*;
public class Spacing {
public static void main(String[] args)
throws FileNotFoundException {
Scanner input = new Scanner (new File("fixspacing.txt"));
fixSpacing(input);
}
public static void fixSpacing(Scanner input){
while(input.hasNextLine()){
String words = input.nextLine();
Scanner spaces = new Scanner (words);
if (spaces.hasNext()) {
System.out.print(spaces.next());
while (spaces.hasNext()) {
System.out.print(" " + spaces.next());
}
}
System.out.println();
}
}
}
Methods Only
public static void fixSpacing(Scanner input){
while(input.hasNextLine()){
String words = input.nextLine();
Scanner spaces = new Scanner (words);
if (spaces.hasNext()) {
System.out.print(spaces.next());
while (spaces.hasNext()) {
System.out.print(" " + spaces.next());
}
}
7 of 20
System.out.println();
}
}
Part2
Array Programming
1. Write a static method named findMin that returns the minimum value in an array of integers. For example,
if a variable named list refers to an array containing the values {16, 12, 25, 44}, the call of
findMin(list) should return 12 (the smallest value in the list). You may assume that the array has at least
one element.
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
}
Answer
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
public static int findMin(int[] a){
int min = a[0];
for(int i=0; i a[j]) {
//making isSorted flag to false
isSorted = false;
break;
}
}
}
//return flag
return isSorted;
}
10 of 20
3. Write a static method named count that accepts an array of integers and a target integer value as parameters
and returns the number of occurrences of the target value in the array. For example, if a variable named
list refers to an array containing values {3, 5, 2, 1, 92, 38, 3, 14, 5, 73, 5} then the call of
count(list, 3) should return 2 because there are 2 occurrences of the value 3 in the array.
Test your code with the following class:
public class TestCount {
public static void main(String[] args) {
int[] list = {3, 5, 2, 1, 92, 38, 3, 14, 5, 73, 5};
System.out.println(count(list, 3));
// 2
System.out.println(count(list, 5));
// 3
System.out.println(count(list, 42)); // 0
}
// your code goes here
}
Answer
public class TestCount {
public static void main(String[] args) {
int[] list = {3, 5, 2, 1, 92, 38, 3, 14, 5, 73, 5};
System.out.println(count(list, 3));
// 2
System.out.println(count(list, 5));
// 3
System.out.println(count(list, 42)); // 0
}
// your code goes here
public static int count(int[] list, int target) {
int count = 0;
for (int i = 0; i < list.length; i++) {
if (list[i] == target) {
count++;
}
}
return count;
}
}
11 of 20
Method Only
public static int count(int[] list, int target) {
int count = 0;
for (int i = 0; i < list.length; i++) {
if (list[i] == target) {
count++;
}
}
return count;
}
4. Write 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}.
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));
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list));
rotateRight(list);
System.out.println(Arrays.toString(list));
rotateRight(list);
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list));
rotateRight(list);
rotateRight(list);
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list));
}
// [7, 3, 8, 19]
// [8, 19, 7, 3]
// [3, 8, 19, 7]
// [8, 19, 7, 3]
// [8, 19, 7, 3]
// your code goes here
}
12 of 20
Answer
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
public static void rotateRight(int[] list){
//last element of list placed to temp variable
int temp=list[list.length-1];
//Declaring new array
int[] array_new=new int[list.length];
//placing the value stored into temp as first element of new array
13 of 20
array_new[0]=temp;
//copying the remaining elemnets to new array
//position get shifted to one position right
for(int k=0; k
Purchase answer to see full
attachment