The students should use a class to maintain a list of comparables (which you and I have already supplied). They should read in values for points, create the point objects, and insert them into the list. Then they should repeatedly remove the maximum from the list and print it (maximum using the distance from the origin).
We supply classes and interfaces:
They write:
// List: a simple list of comparable items
public interface List {
// isEmpty: is the list empty? (initially true)
public boolean isEmpty();
// isFull: is the list full? (initially false)
public boolean isFull();
// add: add x to the list. No check for duplicates
public boolean add(Comparable x);
// contains: is x in the list?
public boolean contains(Comparable x);
// size: return number of elements in the list
public int size();
// remove: remove the parameter
public boolean remove(Comparable x);
// clear: make the list empty
public void clear();
// removeMin: return the minimum and remove it
public Comparable removeMin();
// removeMax: return the minimum and remove it
public Comparable removeMax();
}
// CompList: a simple list of comparables
public class CompList implements List {
// fields
private Comparable[] listArray; // the actual list
private int currentSize; // current number of elements
private final int INITIAL_SIZE = 6; // initial maximum size
// CompList: constructor
public CompList () {
currentSize = 0; // not needed (already done)
listArray = new Comparable[INITIAL_SIZE]; // allocate array
}
// isEmpty: is the list empty? (initially true)
public boolean isEmpty() {
return size() == 0;
}
// isFull: is the list full? (initially false)
public boolean isFull() {
return size() == listArray.length;
}
// add: add x to the list. No check for duplicates
public boolean add(Comparable x) {
if (currentSize >= listArray.length)
doubleArray();
listArray[currentSize++] = x;
return true;
}
// contains: is x in the list?
public boolean contains(Comparable x) {
for (int i = 0; i < currentSize; i++)
if (listArray[i].compareTo(x) == 0) return true;
return false;
}
// size: return number of elements in the list
public int size() {
return currentSize;
}
// remove: remove the parameter
public boolean remove(Comparable x) {
for (int i = 0; i < currentSize; i++)
if (listArray[i].compareTo(x) == 0) {
for (int j = i; j < currentSize - 1; j++)
listArray[j] = listArray[j+1];
currentSize--;
return true;
}
return false;
}
// clear: make the list empty
public void clear() {
currentSize = 0;
}
// removeMin: return the minimum and remove it
public Comparable removeMin() {
if (size() == 0) return null;
Comparable min = listArray[0];
for (int i = 1; i < currentSize; i++)
if (listArray[i].compareTo(min) < 0) min = listArray[i];
remove(min);
return min;
}
// removeMax: return the maximum and remove it
public Comparable removeMax() {
if (size() == 0) return null;
Comparable max = listArray[0];
for (int i = 1; i < currentSize; i++)
if (listArray[i].compareTo(max) > 0) max = listArray[i];
remove(max);
return max;
}
// doubleArray: double the size of the array
private void doubleArray() {
Comparable[] tempArray = new Comparable[listArray.length * 2];
for(int k = 0; k < currentSize; k++)
tempArray[k] = listArray[k];
listArray = tempArray;
}
}
// GetData.java: fetch the next int or double from input.
import java.io.*;
public class GetData {
Reader in; // reader for reading input data
// GetData: constructor. Just open standard input
public GetData() {
in = new InputStreamReader(System.in); // create reader
}
// getNextChar: fetch the next character from standard input
private char getNextChar() {
char ch = ' '; // to make compiler happy
try {
ch = (char)in.read();
}
catch (IOException e)
{
System.out.println("Error reading character");
System.exit(1);
}
return ch;
}
// getNextInt: fetch next signed int
public int getNextInt() {
String s = "";
char ch;
ch = getNextChar();
// skip over initial blanks and newlines
while (ch == ' ' || ch == '\n')
ch = getNextChar();
// read optional + or - sign
if (ch == '+' || ch == '-') {
s += ch;
ch = getNextChar();
}
// read digits, store in s
while (Character.isDigit(ch)) {
s += ch;
ch = getNextChar();
}
return Integer.parseInt(s);
}
// getNextDouble: fetch next Double (no exponent part)
public double getNextDouble() {
String s = "";
char ch;
ch = getNextChar();
// skip over initial blanks and newlines
while (ch == ' ' || ch == '\n')
ch = getNextChar();
// read optional + or - sign
if (ch == '+' || ch == '-') {
s += ch;
ch = getNextChar();
}
// read digits, store in s
while (Character.isDigit(ch)) {
s += ch;
ch = getNextChar();
}
// optional decimal point
if (ch == '.') {
s += ch;
ch = getNextChar();
}
// more digits
while (Character.isDigit(ch)) {
s += ch;
ch = getNextChar();
}
return Double.parseDouble(s);
}
}
// PointDouble: a pair of doubles
// the same as class Point2D.Double, with import java.awt.geom.*
public class PointDouble {
private double x;
private double y;
public PointDouble (double xd, double yd) {
x = xd; y = yd;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
// NormedPoint: Point class with a norm
// (can also use the library class Point2D.Double,
// with import.awt.geom.*;
public class NormedPoint extends PointDouble implements Comparable {
private double norm;
public NormedPoint(double x, double y) {
super(x, y);
norm = Math.sqrt(x*x + y*y);
}
public double getNorm() {
return norm;
}
public int compareTo(Object obj) {
if (norm < ((NormedPoint)obj).norm) return -1;
if (norm > ((NormedPoint)obj).norm) return 1;
return 0;
}
public String toString() {
String ret = new String("(" + getX() + ", " + getY() +
"), norm = " + norm);
return ret;
}
}
// PointListMain: make use of the Lister class for Doubles
public class PointListMain {
public static void main(String[] args) {
CompList list1 = new CompList();
GetData getData = new GetData();
double x, y;
while(true) {
x = getData.getNextDouble();
y = getData.getNextDouble();
NormedPoint p = new NormedPoint(x, y);
if (p.getNorm() == 0) break;
list1.add(p);
}
// print list in decreasing order by norm
while (!list1.isEmpty())
System.out.println(list1.removeMax() + " ");
System.out.println();
}
}
Here is a sample run (user input in bold):
ten42% myjava PointListMain 4 3 12 5 8 15 24 7 0 0 (24.0, 7.0), norm = 25.0 (8.0, 15.0), norm = 17.0 (12.0, 5.0), norm = 13.0 (4.0, 3.0), norm = 5.0 ten42%