Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSC-111 Just some random array exercises
/*
* Name: A.C. Silvestri
* Date: 11/15/2019
* Course Number CSC-111
* Course Name: Intro to Java
* Problem Number: NA
* Email: silvestri@stcc.edu
* Description: Just some random array exercises
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class RandomArrayMethods {
private final static String TITLE = "Random Array Methods";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
//**********************************************
// Put as many methods you need here
private static void printData(double[] x) {
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + " ");
if ((i+1) % 10 == 0) {
System.out.println();
}
}
System.out.println();
}
private static double findMaximumValue(double[] data) {
double max = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] > max)
max = data[i];
}
return max;
}
private static double findMinimumValue(double[] data) {
double min = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] < min)
min = data[i];
}
return min;
}
private static double[] generateArray(int size) {
double x[] = new double[size];
for (int i = 0; i < x.length; i++) {
x[i] = 1000 * Math.random();
}
return x;
}
// First number in file indicates the number of numbers that follow
private static double[] readDataFileFromWebServer(String filename) throws IOException {
URL url = new URL("https://cs.stcc.edu/~silvestri/csc111/" + filename);
Scanner fileInput = new Scanner(url.openStream());
int size = fileInput.nextInt();
double x[] = new double[size];
for (int i = 0; i < x.length; i++)
x[i] = fileInput.nextDouble();
fileInput.close();
return x;
}
private static double[] readDataFileFromLocalFileSystem(String filename) throws IOException {
File file = new File(filename);
Scanner fileInput = new Scanner(file);
int size = fileInput.nextInt();
double x[] = new double[size];
for (int i = 0; i < x.length; i++)
x[i] = fileInput.nextDouble();
fileInput.close();
return x;
}
//**********************************************
// Start your logic coding in the process method
private static void process(Scanner sc, String args[]) throws IOException {
System.out.println("Enter filename: ");
String filename = sc.nextLine();
//double data[] = readDataFileFromWebServer(filename);
double data[] = readDataFileFromLocalFileSystem(filename);
printData(data);
System.out.print("Maximum Value: ");
double max = findMaximumValue(data);
System.out.println(max);
System.out.print("Minimum Value: ");
double min = findMinimumValue(data);
System.out.println(min);
//sc.nextLine();
}
//**********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
//**********************************************
// Do not change the main method
public static void main(String args[]) throws IOException {
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.