Hi this program must be completed using a Eclipes Compiler for Java only . This
ID: 3875617 • Letter: H
Question
Hi this program must be completed using a Eclipes Compiler for Java only . This is and intro to java class , so only intro methods should be used for this assignment. Please include a copy of the input data and a sample of the out put data . Thanks a bunchNote : the maximum size is known in advance and we can only use array not not arraylist
Hi this program must be completed using a Eclipes Compiler for Java only . This is and intro to java class , so only intro methods should be used for this assignment. Please include a copy of the input data and a sample of the out put data . Thanks a bunch
Note : the maximum size is known in advance and we can only use array not not arraylist Expert Q&A; Done Hi, this program is to be completed using Eclipse compiler for Java only. This is an intro to Java class and should only use intro teqniques Please include a copy of the input data (should have at least 15 inputs and a sample copy of the output data Thanks a million! Homework-Topic 7 cont.-Count Zeros: Write a complete program to do the Sollowing cap Wrte a m thod called !amat which rocerves ong parameter, an array of nepm called yals, which is initially uninitialined. The method neads integers from an input ile until it reaches EOF, storing the data values in the array val The method prints the data values read in and the count to an output file. The method retums the total namber of data values read in (b, Write a method called 9uurueet, which necmes two parameters, an integer and an array yals. The method counts and neturns how many of the first n elements of the vals array are For esample, ifthe array holds 66 0-4 0 4 31 with 6, has twoO values. c) Write a method called append0 which reads in several new values into an array appending them at the end of the anay. As a result, it must change both the amay and the alac of The method receives -two parainders as coutzend The method eeturns the new total number of data values stoned within the array For example, Assume the army inmally holds 660-4 0 4 31 with-6;afier the method call, the array might hold 66 0-404 31 220 49 wih n-9(Make sure that several new 0 values are added to the aay d) Write a main peogram which calls these methods Fist, the main peogram calls cadDra to read a set of data into an array called numhers, which contains no mone han 100 integers The number of elements actually read is returned by the method and slorod in a variable called sie Then the main program calls the method countZero) to find how many of the sine array elements are O. The main program prints this value to the output file Nnt the main program call-pend) to modify the-nhen array and Thepend method reads new values from a file uml imput failure, adding the new values to the array and incrementing The new values in the array and mew sotal count are printed (in either the main program or the method) to the output file. Finally, the main peogram calls the function counco again to determine how many elements in the new armray are 0 and prints the result to the output file
Explanation / Answer
Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Numbers {
public static void main(String[] args) throws IOException {
int numbers[] = new int[100];
int numOfElements = readData(numbers);
System.out.print("Number of elements read: "+ numOfElements+" Elements are ");
for(int i =0 ; i< numOfElements; i++) {
System.out.print(numbers[i]+" ");
}
int zeroCount = countZeroes(numOfElements, numbers);
System.out.println("First "+ numOfElements + " elements in the array contains "+ zeroCount + " zero elements");
numOfElements = append(numOfElements, numbers);
System.out.print("After appending random values, Number of elements read: "+ numOfElements+" Elements are ");
for(int i =0 ; i< numOfElements; i++) {
System.out.print(numbers[i]+" ");
}
zeroCount = countZeroes(numOfElements, numbers);
System.out.println(" First "+ numOfElements + " elements in the array contains "+ zeroCount + " zero elements");
}
private static int append(int numOfElements, int[] numbers) {
int totalNums = numOfElements;
int tempNums[] = new int[] {0,1,0,-3};
for(int i = 0; i< tempNums.length; i++) {
numbers[totalNums++] = tempNums[i];
}
return totalNums;
}
private static int countZeroes(int i, int[] numbers) {
int count = 0;
while(i > 0) {
if(numbers[i-1] == 0) {
count++;
}
i--;
}
return count;
}
private static int readData(int[] vals) throws IOException {
File file = new File("/nums.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int i = 0;
String line;
while((line = br.readLine()) != null) {
vals[i] = Integer.parseInt(line);
i++;
}
return i;
}
}
Sample output;
Number of elements read: 9
Elements are 1 0 2 3 5 0 0 3 5 First 9 elements in the array contains 3 zero elements
After appending random values, Number of elements read: 13
Elements are 1 0 2 3 5 0 0 3 5 0 1 0 -3
First 13 elements in the array contains 5 zero elements
Sample Input in the file:
1
0
2
3
5
0
0
3
5
Numbers in the file must be in the above format. One number per line.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.