Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active March 30, 2022 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/51d416a90425c768628cf8229467a33b to your computer and use it in GitHub Desktop.
Save bytecodeman/51d416a90425c768628cf8229467a33b to your computer and use it in GitHub Desktop.
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.printf("%8.2f", 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[] findMinMaxValue(double[] data) {
double min = data[0];
double max = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] < min)
min = data[i];
else if (data[i] > max)
max = data[i];
}
return new double[] {min, max};
}
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 input, String args[]) throws IOException {
System.out.println("Enter filename: ");
String filename = input.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);
System.out.print("Min and Max Values: ");
double minmax[] = findMinMaxValue(data);
System.out.println(minmax[0] + " " + minmax[1]);
//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