package main; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; class PyramidCalculator { private File file; private ArrayList numList; private ArrayList intList; /** * Constructor for the orthogonal pyramid calculator * @param file a file parameter for further use * @throws FileNotFoundException */ public PyramidCalculator(File file) throws FileNotFoundException{ this.file = file; this.numList = new ArrayList<>(); this.intList = new ArrayList<>(); this.extractFromFile(); this.convertToInt(); } /** * This method extracts number from a txt file to an array list line by line * @throws FileNotFoundException */ public void extractFromFile() throws FileNotFoundException { Scanner reader = new Scanner(this.file); while (reader.hasNextLine()) { String line = reader.nextLine(); numList.add(line.split(" ")); } } /** * This method converts all string numbers in the arraylist to integer */ public void convertToInt() { for (String[] strings : this.numList) { int[] tempArray = new int[strings.length]; int j = 0; for (String number : strings) { tempArray[j] = Integer.parseInt(number); j++; } this.intList.add(tempArray); } } /** * This method checks whether given number is prime * @param number any number to check * @return returns boolean value */ public static boolean checkPrime(int number) { for (int i = 2; i < number; i++) { if (number % i == 0) { return false; } } return true; } /** * Check whether which number is greater and not prime by using previous * number's index to detect possible next two numbers. If both of the possible * two numbers are prime, return -1 to stop the addition * @param line a line that contains possible next two numbers * @param idx index of the last chosen number * @return returns the greatest and prime number, if doesn't exist, returns -1 */ public int checkGreater(int[] line, int idx) { if (line[idx] > line[idx + 1] && !checkPrime(line[idx])) { return line[idx]; } else if (line[idx + 1] > line[idx] && !checkPrime(line[idx + 1])) { return line[idx + 1]; } else if (checkPrime(line[idx]) && checkPrime(line[idx + 1])) { return -1; } else { if (!checkPrime(line[idx])) { return line[idx]; } else { return line[idx + 1]; } } } /** * This method prints the possible maximum sum where all the numbers are the greatest * and not prime as well as in diagonally or downwards order */ public void maxSumPossible() { int maxSum = this.intList.get(0)[0]; int lineNum = 0; int idx = 0; while (lineNum < this.intList.size() - 1) { int tempNum = this.checkGreater(this.intList.get(lineNum + 1), idx); if (tempNum == -1) { break; } else { maxSum += tempNum; int[] temp = this.intList.get(lineNum + 1); for (int i = 0; i < temp.length; i++) { if (temp[i] == tempNum) { idx = i; } } lineNum++; } } System.out.println(maxSum); } } /** * This is the main method which firstly takes a file to process and * creates an object of the PyramidCalculator class to calculate the maximum * possible sum from the file */ public class Main { public static void main(String[] args) throws FileNotFoundException{ File file = new File("assignment.txt"); PyramidCalculator pc = new PyramidCalculator(file); pc.maxSumPossible(); } }