Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Matthew Nyce
- * CISC122
- * October 12th, 2017
- * Programming Project #1
- */
- import javax.swing.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class TwoDimArray {
- private int arr[][];
- public TwoDimArray() {
- loadArray();
- }
- public void loadArray(){
- /**
- * loadArray() method loads user defined filename
- * @return text file's contents
- */
- String fileName = "";
- try {
- fileName = JOptionPane.showInputDialog("Enter file name: ");
- }
- catch (Exception e){
- JOptionPane.showMessageDialog(null, "can not open " + fileName + " to read");
- return;
- }
- try {
- Scanner in = new Scanner(new File(fileName));
- int rows, cols;
- rows = in.nextInt();
- cols = in.nextInt();
- arr = new int[rows][cols];
- for(int i = 0; i < rows; ++i){
- for(int j = 0; j < cols; ++j){
- arr[i][j] = in.nextInt();
- }
- }
- in.close();
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null, "can not open " + fileName + " to read");
- }
- }
- public int getArrayMaxValue(){
- /**
- * @return the max vale of the array
- */
- int maxVal = Integer.MIN_VALUE;
- for(int i = 0; i < arr.length; ++i){
- for(int j = 0; j < arr.length; ++j){
- if(arr[i][j] > maxVal){
- maxVal = arr[i][j];
- }
- }
- }
- return maxVal;
- }
- public int getArrayMinValue(){
- /**
- * @return the minimum value of the array
- */
- int minVal = Integer.MAX_VALUE;
- for(int i = 0; i < arr.length; ++i){
- for(int j = 0; j < arr.length; ++j){
- if(arr[i][j] < minVal){
- minVal = arr[i][j];
- }
- }
- }
- return minVal;
- }
- public int getTotal(int[][] array) {
- int total = 0;
- for (int row = 0; row < array.length; row++) {
- for (int col = 0; col < array[row].length; col++) {
- total += array[row][col];
- }
- }
- return total;
- }
- public int getAverage(int[][] array) {
- return getTotal(array) / getElementCount(array);
- }
- public int getRowTotal(int[][] array, int row) {
- int total = 0;
- for (int col = 0; col < array[row].length; col++) {
- total += array[row][col];
- }
- return total;
- }
- public int getColumnTotal(int[][] array, int col) {
- int total = 0;
- for (int row = 0; row < array.length; row++) {
- total += array[row][col];
- }
- return total;
- }
- public int getHighestInRow(int[][] array, int row) {
- int highest = array[row][0];
- for (int col = 1; col < array[row].length; col++) {
- if (array[row][col] > highest) {
- highest = array[row][col];
- }
- }
- return highest;
- }
- public int getLowestInRow(int[][] array, int row) {
- int lowest = array[row][0];
- for (int col = 1; col < array[row].length; col++) {
- if (array[row][col] < lowest) {
- lowest = array[row][col];
- }
- }
- return lowest;
- }
- public static int getElementCount(int[][] array) {
- int count = 0;
- for (int row = 0; row < array.length; row++) {
- count += array[row].length;
- }
- return count;
- }
- public static void main(String[] args){
- TwoDimArray twoDimArray = new TwoDimArray();
- System.out.println("Max value: " + twoDimArray.getArrayMaxValue());
- System.out.println("Min value: " + twoDimArray.getArrayMinValue());
- System.out.println("Average: " + twoDimArray.getAverage(twoDimArray.arr));
- System.out.println("Total: " + twoDimArray.getTotal(twoDimArray.arr));
- System.out.println("Row Total: " + twoDimArray.getRowTotal(twoDimArray.arr, 1));
- System.out.println("Column Total: " + twoDimArray.getColumnTotal(twoDimArray.arr, 1));
- System.out.println("Highest in Row: " + twoDimArray.getHighestInRow(twoDimArray.arr, 1));
- System.out.println("Lowest in Row: " + twoDimArray.getLowestInRow(twoDimArray.arr, 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment