|
/* |
|
* This program lists in ascending order the consecute intervals between any set of integer generated by a user. |
|
* |
|
* N.B. No mechanisms have been implemented to safeguard the user from missusing the program. |
|
* This can be done with some conditional statements in the prompt method and reprompting the user to correct the input, as well as try/catch clauses. |
|
* |
|
* N.B. It is not clear what to do when the user inputs an odd number of integers. |
|
* While a number of approaches resolve this issue, I decided to treat the largest number to appear twice. |
|
* In other word, input: |
|
* 1 |
|
* 2 |
|
* 3 |
|
* Would generate output: |
|
* [1, 2] with consecutive count of 2 |
|
* [3, 3] with consecutive count of 1 |
|
* |
|
* N.B. What's more, nothing in the condition seems to indicate what "with consecutive count of 1" is supposed to compute. |
|
* I took this to be the lenght of the interval + 1 as this is a reasonable generalisation of the example outputs. |
|
* I implemented this computation as is, since it is consistent through out the examples. |
|
* |
|
* To reiterate, one reason the problem may be confusing to some is |
|
* the inconsistency between the general condition and the example inputs, |
|
* and then yet again - between the examples themselves. |
|
* The first example illustrating how the program should work |
|
* takes each input integer i and outputs the interval [i, i]. |
|
* Only the second example satisfies the condition, |
|
* even though it computes the additinal " j - i + 1 " for each interval [i, j], |
|
* instead of the interval lenght " j - i ". |
|
*/ |
|
|
|
package intervals; |
|
|
|
import java.util.ArrayList; |
|
import java.util.Collections; |
|
import java.util.Comparator; |
|
import java.util.Hashtable; |
|
import java.util.Map; |
|
import java.util.Random; |
|
import java.util.Scanner; |
|
|
|
/** |
|
* |
|
* @author Georgi Karapetrov |
|
*/ |
|
|
|
public class Intervals |
|
{ |
|
//store all integers in a list |
|
private static ArrayList<Integer> points; |
|
|
|
//constructor |
|
public Intervals() |
|
{ |
|
points = new ArrayList<Integer>(); |
|
} |
|
|
|
//returns the list |
|
private ArrayList<Integer> getPonts() |
|
{ |
|
return points; |
|
} |
|
|
|
//promt, and storing the keys and summing over the values |
|
public static void prompt() |
|
{ |
|
Scanner input = new Scanner( System.in ); // scanner for standart input |
|
String line = ""; // initialize a string to store input line by line |
|
|
|
//prompt |
|
System.out.print( "This program outputs in ascending order the consecutive intervals between integers.\n" |
|
+ "Enter the integers one by one each on it's onw line like this:\n" |
|
+ "1\n" |
|
+ "2\n" |
|
+ "3\n" |
|
+ "Enter EXIT if you are finished.\n" ); |
|
|
|
//store the integers |
|
while (input.hasNext()) |
|
{ |
|
line = input.nextLine(); //store line |
|
|
|
//break if terminated by user |
|
if (line.toLowerCase().contains("exit")) |
|
{ |
|
break; |
|
} |
|
|
|
//add each integer to the list |
|
points.add( Integer.parseInt(line) ); |
|
} |
|
} |
|
|
|
public static void printResults() |
|
{ |
|
int lengthPlusOne; //the condition seems to require us to print one longer than the lenght of the interval, for each interval |
|
|
|
//handle an even number of points |
|
if (points.size() % 2 == 0) |
|
{ |
|
//word wrapping |
|
for( int i=0; i < points.size(); i = i + 2 ) |
|
{ |
|
lengthPlusOne = points.get(i+1) - points.get(i); |
|
System.out.println( "[" + points.get(i) + ", " + points.get(i+1) + "] with consecutive count of " + lengthPlusOne ); |
|
} |
|
} |
|
|
|
//handle odd number of points |
|
else |
|
{ |
|
//word wrapping |
|
for( int i=0; i < points.size(); i = i + 2 ) |
|
{ |
|
if( i < points.size() - 1 ) |
|
{ |
|
lengthPlusOne = points.get(i+1) - points.get(i); |
|
System.out.println( "[" + points.get(i) + ", " + points.get(i+1) + "] with consecutive count of " + lengthPlusOne ); |
|
} |
|
//consider the odd point out to be its own interval |
|
else |
|
{ |
|
System.out.println( "[" + points.get(i) + ", " + points.get(i) + "] with consecutive count of 1" ); |
|
} |
|
} |
|
} |
|
System.out.println( ); |
|
} |
|
|
|
//test the computetional methods |
|
/* |
|
* This test is no proof that the program always halts or always works as intended. |
|
* Neither is this test able to test the prompt() method from the main file as we cannot simulate stdin. |
|
* However as many times as I have run this test, the tested methods run as intended. |
|
* Also as many times as I ran the main program, it works as long as you keep the input format (int + "\n") |
|
*/ |
|
public static void generatePoints() |
|
{ |
|
Random integerGenerator = new Random(); //generate random integers |
|
int integer; //store the random number so we can both print it and use it in testing |
|
|
|
//add nineteen numbers to the list and print them (nineteen for certainty, though any other number would work.) |
|
for(int i=1; i <= 19; ++i) |
|
{ |
|
integer = integerGenerator.nextInt(); //store the random number so we can both print it and use it in testing |
|
points.add( integer ); |
|
System.out.println( integer ); |
|
} |
|
|
|
System.out.println("Output:" ); |
|
} |
|
|
|
public static void main(String[] args) |
|
{ |
|
Intervals example = new Intervals(); |
|
|
|
//call prompt (comment out the following line if you want to test automatically |
|
example.prompt(); |
|
|
|
//or generate points automatically (uncomment the following line if you want to test automatically) |
|
//example.generatePoints(); |
|
|
|
//compute the three highest values |
|
Collections.sort( example.getPonts() ); |
|
|
|
//print result |
|
example.printResults(); |
|
} // end method main |
|
} // end class Keys |