Created
February 7, 2014 05:28
-
-
Save martynchamberlin/8857756 to your computer and use it in GitHub Desktop.
Verbose Java. This is the first Java program I've written that actually took me a few hours to write and took some thought. It's probably a lot more verbose in length than it needs to be — in fact, I know this for a fact. I hope some day to review it and smile at how bad a programmer I was. The main method is stored in UX.java. That name only ma…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Martyn Chamberlin | |
*/ | |
import java.util.Scanner; | |
import java.util.ArrayList; | |
import java.util.Random; | |
/** | |
* In true purist fashion, every field in this class is set to private, and more | |
* difficulty, every single field is accessed and updated through methods, even | |
* when these fields are being accessed in its own class. The verboseness and layers | |
* of sophistication required to make this work with arrays is, admittedly, more | |
* than I realized when I started out. It nearly doubles the length of the class | |
* and makes parts of it quite confusing to read if you aren't familiar with the | |
* underlying principle. | |
*/ | |
public class RandomNumber { | |
private int start = 1; // the beginning allowable value for a random number | |
private int finish = 54; // the ending allowable value for a random number | |
private int minAllowed = 1000; // beginning allowable value for num of rand nums | |
private int maxAllowed = 10000; // ending allowable value for num of rand nums | |
private int n = 0; // user-generated number of random numbers to generate | |
private int numOccurrences[] = new int[getFinish()]; // store each random num in array | |
private int sum = 0; // stores sum of the random numbers generated | |
/** | |
* These next two fields are arrays with two indexes. The first index is the | |
* value of the number that contains the maximum and minimum number of occurrences, | |
* respectively. The second is the number of times that value occurred. You | |
* could think of the first index as the "key" and the second index as the "value." | |
* The assignment does not explicitly state that both pieces of information are | |
* important, but data is always a good thing to have. | |
*/ | |
private int maxOccurrences[] = {0, 0}; | |
/** | |
* It's impossible for a number to appear more than the max allowed. | |
* Even if every single number were the same (mathematically a chance | |
* in a zillion), it would equal getN(). | |
*/ | |
private int minOccurrences[] = {0, getN()}; | |
public int getRandom() | |
{ | |
return new Random().nextInt( finish ) + start; | |
} | |
public void printNums() | |
{ | |
System.out.println( "The number of occurrences for each number are listed as " | |
+ "shown below"); | |
int i = 0; | |
for ( int val : numOccurrences ) | |
{ | |
System.out.println(i + 1 + ": " + numOccurrences[i]); | |
i++; | |
} | |
findMax(); | |
findMin(); | |
System.out.println( "The random number that occured with the highest" | |
+ " frequency was " + getMaxOccurrences( 0 ) + ", with " | |
+ getMaxOccurrences( 1 ) + " occurrences."); | |
System.out.println("The random number that occured with the lowest" | |
+ " frequency was " + getMinOccurrences( 0 ) + ", with " | |
+ getMinOccurrences( 1 ) + " occurrences."); | |
/** | |
* It would be very strange if this next line of code does NOT print 27 to | |
* the screen. Why? Because 27 is 54/2, i.e., halfway between our two | |
* extremes, i.e., the average. Interestingly though, in some cases it will | |
* print 26 to the screen, when the user only selects 1000. The smaller | |
* the ocean of random numbers to generate, the more likely you are not going | |
* to be showing a pure average. Fascinating study in statistics. | |
*/ | |
System.out.println( "The average random number was " + | |
(int)( getSum() / getN() ) + "." ); | |
} | |
/** | |
* This finds the number that's been randomly generated the most number of times | |
* | |
* Only using Math.max() because the specifications required it. Not sure it | |
* makes a lot of sense to use it in real life because we HAVE to know which of | |
* the two values being compared are the winner. Why? Because that determines | |
* what the index is of the winning number. Math.max is too simple a function | |
* to handle this for us. | |
*/ | |
public void findMax() | |
{ | |
int i = 1; | |
for ( int val : numOccurrences ) | |
{ | |
setMaxOccurrences( 1, Math.max( getMaxOccurrences( 1 ), val) ); | |
if ( getMaxOccurrences( 1 ) == val ) // if current is being set | |
{ | |
setMaxOccurrences( 0, i ); | |
} | |
i++; | |
} | |
} | |
public void findMin() | |
{ | |
int i = 1; | |
for ( int val : numOccurrences ) | |
{ | |
setMinOccurrences( 1, Math.min( getMinOccurrences( 1 ), val ) ); | |
if ( getMinOccurrences( 1 ) == val ) // if current is being set | |
{ | |
setMinOccurrences( 0, i ); | |
} | |
i++; | |
} | |
} | |
public int calcAverage() | |
{ | |
int sum = 0; | |
for ( int val : numOccurrences ) | |
{ | |
sum += val; | |
} | |
return (int) sum / getFinish(); | |
} | |
public void getUserInput() | |
{ | |
Scanner in = new Scanner( System.in ); | |
this.setN( in.nextInt() ); | |
} | |
public int getStart() | |
{ | |
return this.start; | |
} | |
public int getMinAllowed() | |
{ | |
return this.minAllowed; | |
} | |
public int getMaxAllowed() | |
{ | |
return this.maxAllowed; | |
} | |
public int getFinish() | |
{ | |
return this.finish; | |
} | |
public int[] getNumOccurrences() | |
{ | |
return numOccurrences; | |
} | |
public int[] getMaxOccurrences() | |
{ | |
return maxOccurrences; | |
} | |
public int getMaxOccurrences( int i ) | |
{ | |
return maxOccurrences[ i ]; | |
} | |
public void setMaxOccurrences( int ind, int val ) | |
{ | |
maxOccurrences[ ind ] = val; | |
} | |
public int[] getMinOccurrences() | |
{ | |
return minOccurrences; | |
} | |
public int getMinOccurrences( int i) | |
{ | |
return minOccurrences[ i ]; | |
} | |
public void setMinOccurrences( int ind, int val ) | |
{ | |
minOccurrences[ ind ] = val; | |
} | |
public int getNumOccurrences( int i ) | |
{ | |
return numOccurrences[ i ]; | |
} | |
public void setNumOccurrences( int ind, int val ) | |
{ | |
numOccurrences[ ind ] = val; | |
} | |
public void setSum( int i, boolean add ) | |
{ | |
if ( ! add ) | |
{ | |
sum = i; | |
} | |
else | |
{ | |
sum += i; | |
} | |
} | |
public int getSum() | |
{ | |
return sum; | |
} | |
public int getN() | |
{ | |
return this.n == 0 ? getMaxAllowed() : this.n; | |
} | |
public void setN( int n ) | |
{ | |
if ( n >= getMinAllowed() && n <= getMaxAllowed() ) | |
{ | |
this.n = n; | |
} | |
else | |
{ | |
System.out.println("The number must be between " + getMinAllowed() | |
+ " and " + getMaxAllowed() + ". Please try again."); | |
this.getUserInput(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Martyn Chamberlin | |
*/ | |
public class UX { | |
public static void main(String[] args) { | |
System.out.println("Enter the number of random numbers you would like" | |
+ " to generate." ); | |
RandomNumber RN = new RandomNumber(); | |
RN.getUserInput(); | |
for ( int i = 0; i < RN.getN(); i++) | |
{ | |
int random = RN.getRandom(); | |
RN.setSum( random, true ); | |
RN.setNumOccurrences( random - 1, RN.getNumOccurrences( random - 1 ) + 1); | |
} | |
RN.printNums(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment