HazeUsendaya

halp meh ethan

Oct 12th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. /**
  2.  * @author Matthew Nyce
  3.  * CISC122
  4.  * October 12th, 2017
  5.  * Programming Project #1
  6.  */
  7.  
  8. import javax.swing.*;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.util.Scanner;
  12.  
  13. public class TwoDimArray {
  14.  
  15.     private int arr[][];
  16.  
  17.     public TwoDimArray() {
  18.         loadArray();
  19.     }
  20.  
  21.     public void loadArray(){
  22.         /**
  23.          * loadArray() method loads user defined filename
  24.          * @return text file's contents
  25.          */
  26.         String fileName = "";
  27.         try {
  28.             fileName = JOptionPane.showInputDialog("Enter file name: ");
  29.         }
  30.         catch (Exception e){
  31.             JOptionPane.showMessageDialog(null, "can not open " + fileName + " to read");
  32.             return;
  33.         }
  34.         try {
  35.             Scanner in = new Scanner(new File(fileName));
  36.             int rows, cols;
  37.             rows = in.nextInt();
  38.             cols = in.nextInt();
  39.             arr = new int[rows][cols];
  40.             for(int i = 0; i < rows; ++i){
  41.                 for(int j = 0; j < cols; ++j){
  42.                     arr[i][j] = in.nextInt();
  43.                 }
  44.             }
  45.             in.close();
  46.         } catch (FileNotFoundException e) {
  47.             JOptionPane.showMessageDialog(null, "can not open " + fileName + " to read");
  48.         }
  49.     }
  50.  
  51.     public int getArrayMaxValue(){
  52.         /**
  53.          * @return the max vale of the array
  54.          */
  55.         int maxVal = Integer.MIN_VALUE;
  56.         for(int i = 0; i < arr.length; ++i){
  57.             for(int j = 0; j < arr.length; ++j){
  58.                 if(arr[i][j] > maxVal){
  59.                     maxVal = arr[i][j];
  60.                 }
  61.             }
  62.         }
  63.         return maxVal;
  64.     }
  65.  
  66.     public int getArrayMinValue(){
  67.         /**
  68.          * @return the minimum value of the array
  69.          */
  70.         int minVal = Integer.MAX_VALUE;
  71.         for(int i = 0; i < arr.length; ++i){
  72.             for(int j = 0; j < arr.length; ++j){
  73.                 if(arr[i][j] < minVal){
  74.                     minVal = arr[i][j];
  75.                 }
  76.             }
  77.         }
  78.         return minVal;
  79.     }
  80.  
  81.     public int getTotal(int[][] array) {
  82.         int total = 0;
  83.  
  84.         for (int row = 0; row < array.length; row++) {
  85.             for (int col = 0; col < array[row].length; col++) {
  86.                 total += array[row][col];
  87.             }
  88.         }
  89.  
  90.         return total;
  91.     }
  92.  
  93.     public int getAverage(int[][] array) {
  94.         return getTotal(array) / getElementCount(array);
  95.     }
  96.  
  97.     public int getRowTotal(int[][] array, int row) {
  98.         int total = 0;
  99.  
  100.         for (int col = 0; col < array[row].length; col++) {
  101.             total += array[row][col];
  102.         }
  103.  
  104.         return total;
  105.     }
  106.  
  107.     public int getColumnTotal(int[][] array, int col) {
  108.         int total = 0;
  109.  
  110.         for (int row = 0; row < array.length; row++) {
  111.             total += array[row][col];
  112.         }
  113.  
  114.         return total;
  115.     }
  116.  
  117.     public int getHighestInRow(int[][] array, int row) {
  118.         int highest = array[row][0];
  119.  
  120.         for (int col = 1; col < array[row].length; col++) {
  121.             if (array[row][col] > highest) {
  122.                 highest = array[row][col];
  123.             }
  124.         }
  125.         return highest;
  126.     }
  127.  
  128.     public int getLowestInRow(int[][] array, int row) {
  129.         int lowest = array[row][0];
  130.  
  131.         for (int col = 1; col < array[row].length; col++) {
  132.             if (array[row][col] < lowest) {
  133.                 lowest = array[row][col];
  134.             }
  135.         }
  136.         return lowest;
  137.     }
  138.  
  139.     public static int getElementCount(int[][] array) {
  140.         int count = 0;
  141.  
  142.         for (int row = 0; row < array.length; row++) {
  143.             count += array[row].length;
  144.         }
  145.         return count;
  146.     }
  147.    
  148.     public static void main(String[] args){
  149.         TwoDimArray twoDimArray = new TwoDimArray();
  150.         System.out.println("Max value: " + twoDimArray.getArrayMaxValue());
  151.         System.out.println("Min value: " + twoDimArray.getArrayMinValue());
  152.         System.out.println("Average: " + twoDimArray.getAverage(twoDimArray.arr));
  153.         System.out.println("Total: " + twoDimArray.getTotal(twoDimArray.arr));
  154.         System.out.println("Row Total: " + twoDimArray.getRowTotal(twoDimArray.arr, 1));
  155.         System.out.println("Column Total: " + twoDimArray.getColumnTotal(twoDimArray.arr, 1));
  156.         System.out.println("Highest in Row: " + twoDimArray.getHighestInRow(twoDimArray.arr, 1));
  157.         System.out.println("Lowest in Row: " + twoDimArray.getLowestInRow(twoDimArray.arr, 1));
  158.         }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment