 Arrays in Programming Language Java

Subject Computer Science Java Programming

Question

Question 1: Write a program to read integers N1 and N2 from the input file “input4_03.txt”. Create an array of N1 integers where each integer is a random number in the range [1, N2] inclusively. Write a method for each of the following items and then print the results to the output file “output4_03.txt”

a. All elements in the array (method name: printAllElements(), input parameter: input array)

b. All odd numbers in the array (method name: printAllOdds(), input parameter: input array)

c. All even numbers in the array (method name: printAllEvens(), input parameter: input array)

d. The Maximum number(s) of the array (method name: printAllMaxs(), input parameter: input array)

e. The Minimum number(s) of the array (method name: printAllMins(), input parameter: input array)

f. The average of the array (method name: printAverage(), input parameter: input array)

g. All prime numbers in the array (method name: printAllPrimes(), input parameter: input array)

Question 2: Write a program that has only one method call allLess(). This method accepts two arrays of integers and returns true if each elements in the first array is less than the element at the same index in the second array, and return false otherwise. Your method should also return false if the arrays are not the same length. Test your method and print out to the screen in main() function the following calls

a. allLess(arr1, arr2); (where arr1 = [1,2,3,4,5], arr2 = [4,5,6,7,8]);
b. allLess(arr1, arr2); (where arr1 = [1,2,3,4,5], arr2 = [5,6,7,8]);
c. allLess(arr1, arr2); (where arr1 = [5,2,3,4,1], arr2 = [4,5,6,7,8]);
d. allLess(arr1, arr2); (where arr1 = [5,5,3,4,5], arr2 = [9,9,9,9,1]);

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Question1 {

public static void main(String[] args) throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output4_03.txt", false)));
out.close();
Scanner input = new Scanner(new File("input4_03.txt"));
int n1, n2;
int[] array;
n1 = input.nextInt();
n2 = input.nextInt();
array = new int[n1];
for(int i = 0; i < n1; i++)
array[i] = (int)(Math.random() * n2 + 1);
printAllElements(array);
printAllOdds(array);
printAllEvens(array);
printAllMaxs(array);
printAllMins(array);
printAverage(array);
printAllPrimes(array);
input.close();
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Java Programming Assignment \$30.00
Java Programming
Variables
ToString Method
Equals Method
Computer Science
Java Program For Restaurants \$18.00
Java
Restaurants
Computer Science
Programming
Tips
Variables
Functions
Dollar Amounts
Functions
Input
Output
Statements
Multiplication
Bills
Percentage
Division
NumberFormat Class
Multiplication Table in Java \$40.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Rows
Columns
Input
Output
Integers
Strings
Multiplication
Tables
Operations
Conditions 