Change this code into multiple methods so that the main isnt so big(JAVA): publi
ID: 3806371 • Letter: C
Question
Change this code into multiple methods so that the main isnt so big(JAVA):
public class SequenceDemo {
public static final int SQUARE_ROWS = 100;
public static final int SQUARE_COLUMNS = 10;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int totalPrimes = Integer.parseInt(args[1]);
int firstArg = Integer.parseInt(args[0]);
int firstPrime;
boolean validPrime;
int numCounter = 0;
PrimeSequence OurPrimeSequence = new PrimeSequence();
ArrayList<List<Integer>> primeArray = new ArrayList<List<Integer>>();
OurPrimeSequence.start(firstArg);
do {
firstPrime = OurPrimeSequence.Next();
validPrime = OurPrimeSequence.primeAlgorithm(firstPrime);
if (validPrime == true) {
for (int i = 0; i < SQUARE_ROWS; i++) {
ArrayList<Integer> row = new ArrayList<Integer>();
for (int j = 0; j < SQUARE_COLUMNS; j++) {
row.add(firstPrime);
}
primeArray.add(row);
}
}
numCounter++;
firstPrime++;
}
while (numCounter < totalPrimes);
for (int i = 0; i < SQUARE_ROWS; i++) {
for (int j = 0; j < SQUARE_COLUMNS; j++) {
System.out.print(primeArray.get(i).get(j));
System.out.print(", ");
}
System.out.println("");
}
}
}
Explanation / Answer
package com;
import java.util.ArrayList;
import java.util.List;
public class SequenceDemo {
public static final int SQUARE_ROWS = 100;
public static final int SQUARE_COLUMNS = 10;
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
int totalPrimes = Integer.parseInt(args[1]);
int firstArg = Integer.parseInt(args[0]);
int numCounter = 0;
PrimeSequence OurPrimeSequence = new PrimeSequence();
ArrayList<List<Integer>> primeArray = new ArrayList<List<Integer>>();
OurPrimeSequence.start(firstArg);
createPrimeArray(totalPrimes, numCounter, OurPrimeSequence, primeArray);
printPrimeArray(primeArray);
}
private static int createPrimeArray(int totalPrimes, int numCounter, PrimeSequence OurPrimeSequence,
ArrayList<List<Integer>> primeArray) {
int firstPrime;
boolean validPrime;
do {
firstPrime = OurPrimeSequence.Next();
validPrime = OurPrimeSequence.primeAlgorithm(firstPrime);
if (validPrime == true) {
for (int i = 0; i < SQUARE_ROWS; i++) {
addRow(firstPrime, primeArray);
}
}
numCounter++;
firstPrime++;
} while (numCounter < totalPrimes);
return numCounter;
}
private static void printPrimeArray(ArrayList<List<Integer>> primeArray) {
for (int i = 0; i < SQUARE_ROWS; i++) {
for (int j = 0; j < SQUARE_COLUMNS; j++) {
System.out.print(primeArray.get(i).get(j));
System.out.print(", ");
}
System.out.println("");
}
}
private static void addRow(int firstPrime, ArrayList<List<Integer>> primeArray) {
ArrayList<Integer> row = new ArrayList<Integer>();
for (int j = 0; j < SQUARE_COLUMNS; j++) {
row.add(firstPrime);
}
primeArray.add(row);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.