/********************************** * Exercise 12 * Date: 04/26/2004 * * Mike Jittivanich: mmj7 **********************************/ public class Task1{ public static void main(String[] args){ //extract int values from command line input int[] input = new int[4]; for(int i=0; i<4; i++){ input[i] = Integer.parseInt(args[i]); } printArray(createArray(input[0],input[1],input[2],input[3])); } //Create a 2-D array with with the given specifications public static int[][] createArray(int row, int col, int low, int high){ int[][] array = new int[row][col]; for(int i=0; i=0) return real+"+"+imag+"i"; else return real+""+imag+"i"; } } /********************************** * Exercise 12 * Date: 04/26/2004 * * Dan Williams * Task 3 **********************************/ public class Task3{ public static void main(String args[]){ System.out.print("How many complex numbers do you want to sort: "); Complex[] numbers = new Complex[SavitchIn.readInt()]; //I think digits between 1 and 9 look nice for (int i = 0; i < numbers.length; i++) numbers[i] = new Complex(MyMath.myRandom(1, 9), MyMath.myRandom(1, 9)); //copy the array for our tests Complex[] numbersSSU = new Complex[numbers.length]; Complex[] numbersSSD = new Complex[numbers.length]; Complex[] numbersISU = new Complex[numbers.length]; for (int i = 0; i < numbers.length; i++){ //new constructor introduced to copy the number properly numbersSSU[i] = new Complex(numbers[i]); numbersSSD[i] = new Complex(numbers[i]); numbersISU[i] = new Complex(numbers[i]); } printNumbers("no sorting", numbers); printNumbers("SelectionSortUp", selectSortUp(numbersSSU)); printNumbers("SelectionSortDown", selectSortDown(numbersSSD)); printNumbers("InsertionSortUp", insertSortUp(numbersISU)); } /* * print the Complex numbers with a header (tag) */ public static void printNumbers(String tag, Complex[] x){ System.out.println("Sorted using "+tag+":"); for (int i = 0; i < x.length; i++) System.out.println(x[i]); } /* * NOTE: * borrowed the sorting code from http://www.cs.cornell.edu/courses/cs100j/2001fa/Notes/lecture_notes.html */ // select sort public static Complex[] selectSortUp(Complex[] x) { int min, index, scan; Complex temp; // Sort each element of $x$ in ascending order: for(index = 0; index < x.length-1; index++) { min = index; // Find the smallest value to the "left" of current element: for(scan = index+1; scan < x.length; scan++) if (x[scan].compareTo(x[min])< 0) min = scan; // Swap values if necessary: if (min != index){ temp = x[min]; x[min] = x[index]; x[index] = temp; } } return x; } public static Complex[] selectSortDown(Complex[] x) { int max, index, scan; Complex temp; // Sort each element of $x$ in ascending order: for(index = 0; index < x.length-1; index++) { max = index; // Find the largest value for(scan = index+1; scan < x.length; scan++) if (x[scan].compareTo(x[max]) > 0) max = scan; // Swap values if necessary: if (max != index){ temp = x[max]; x[max] = x[index]; x[index] = temp; } } return x; } public static Complex[] insertSortUp(Complex[] x) { // First element x[0] is sorted // Scan through remaining elements for (int i=1; i0 && (x[pos-1].compareTo(key) > 0)) { x[pos]=x[pos-1]; pos--; } // Insert $key$: x[pos]=key; } return x; } } class Complex { // fields private int real; // real component private int imag; // imaginary component // constructor public Complex(int r, int i) { real = r; imag = i; } //this constructor is helpful in copying the array at the beginning public Complex(Complex other) { real = other.real; imag = other.imag; } // methods public Complex add(Complex other) { return new Complex(real+other.real, imag+other.imag); } public Complex sub(Complex other){ return new Complex(real-other.real, imag-other.imag); } /* * NOTE: * comparison code borrowed from http://www.cs.cornell.edu/courses/cs211/2003sp/LectureNotes/L9/Compare.txt */ public boolean equals(Complex c) { return imag==c.imag && real==c.real && mag(this)==mag(c); } public int compareTo(Complex c) { int result=9999; if (mag(this) < mag(c)) result=-1; else if (this.equals(c)) result=0; else if (mag(this) > mag(c)) result=1; return result; } private double mag(Complex c) { double asqu = c.real*c.real; double bsqu = c.imag*c.imag; return Math.sqrt(asqu+bsqu); } public Complex mul(Complex other){ int newReal = real*other.real - imag*other.imag; int newImag = real*other.imag + imag*other.real; return new Complex(newReal, newImag); } public String toString() { if(imag>=0) return real+"+"+imag+"i"; else return real+""+imag+"i"; } } class MyMath { public static int myRandom(int low, int high) { return (int) (Math.random()*(high-low+1)) + (int) low; } } /********************************** * Exercise 12 * * Tibor * Task 4 **********************************/ //Circle.java: class Circle extends Ellipse { public Circle (Point center, double radius) { super(center, radius, radius); } } //Ellipse.java: class Ellipse extends Shape { private Point center; private double semiaxis1; private double semiaxis2; public Ellipse(Point c, double s1, double s2) { center = (Point)c.clone(); semiaxis1 = s1; semiaxis2 = s2; } public double getArea () { return java.lang.Math.PI * semiaxis1 * semiaxis2; } } //Parallelogram.java class Parallelogram extends Trapezoid { public Parallelogram(Point[] vertex) { // Only three points are needed; the fourth one can be computed. // Parantheses used to emphasize structure of expressions. super(new Point[] { vertex[0], vertex[1], vertex[2], new Point(vertex[0].getX() + (vertex[2].getX() - vertex[1].getX()), vertex[0].getY() + (vertex[2].getY() - vertex[1].getY()))}); } } //Point.java class Point implements Cloneable { private double x; private double y; public Point (double x, double y) { this.x = x; this.y = y; } public double getX () { return x; } public double getY () { return y; } public Object clone() { return new Point(x, y); } public boolean equals(Point p) { return (x == p.x) && (y == p.y); } public double distance(Point p) { double deltaX = x - p.x; double deltaY = y - p.y; return Math.sqrt(deltaX * deltaX + deltaY * deltaY); } } //Quadrilateral.java class Quadrilateral extends Shape { protected Point[] vertex; // All Quadrilaterals are defined by 4 points, listed in clockwise or // counter-clockwise order. We assume that all quadrilaterals are convex. public Quadrilateral(Point[] vertex){ this.vertex = (Point[])vertex.clone(); } // Decompose into two disjoint triangles, and compute their respective areas. public double getArea() { return (new Triangle(new Point[] {vertex[0], vertex[1], vertex[2]})).getArea() + (new Triangle(new Point[] {vertex[2], vertex[3], vertex[0]})).getArea(); } } //Rectangle.java class Rectangle extends Parallelogram { public Rectangle(Point[] vertex) { super(vertex); } public double getArea() { return vertex[0].distance(vertex[1]) * vertex[1].distance(vertex[2]); } } //Rhomboid.java class Rhomboid extends Parallelogram implements RhomboidInterface { public Rhomboid(Point[] vertices) { super(vertices); } } //RhomboidInterface.java interface RhomboidInterface { } //Shape.java abstract class Shape { abstract public double getArea(); } //Square.java class Square extends Rectangle implements RhomboidInterface { public Square(Point[] vertex) { super(vertex); } public double getArea() { double edgeLength = vertex[0].distance(vertex[1]); return edgeLength * edgeLength; } } //Test.java class Test { public static void main(String[] args) { System.out.println((new Ellipse(new Point(5, 7), 5, 10)).getArea()); System.out.println((new Circle(new Point(5, 7), 10)).getArea()); System.out.println((new Triangle(new Point[] { new Point(-5, -3), new Point(5, -3), new Point(0, 7) })).getArea()); System.out.println((new Trapezoid(new Point[] { new Point(0, 0), new Point(50, 0), new Point(40, 10), new Point(30, 10)})).getArea()); System.out.println((new Rhomboid(new Point[] { new Point(-10, -10), new Point(10, -10), new Point(20, 10)})).getArea()); System.out.println((new Rectangle(new Point[] { new Point(-5, -7), new Point(5, -7), new Point(5, 23)})).getArea()); System.out.println((new Square(new Point[] { new Point(-17, 17), new Point(17, 17), new Point(17, 0)})).getArea()); } } //Trapezoid.java class Trapezoid extends Quadrilateral { // We assume that line v0v1 is parallel to v2v3. public Trapezoid(Point[] vertex) { super(vertex); } // Area = 0.5 * height * (base1 + base2) // = (0.5 * height * base1) * (1 + base2/base1) // = (area of triangle consisting of base1 and one of the other points) // * (1 + base2/base1) public double getArea() { return (new Triangle(new Point[] { vertex[0], vertex[1], vertex[2]}).getArea()) * (1 + vertex[2].distance(vertex[3])/vertex[1].distance(vertex[0])); } } //Triangle.java class Triangle extends Shape { private Point[] vertex; public Triangle(Point [] vertex) { this.vertex = (Point[])vertex.clone(); } // Formula used for area: // | x0 y0 | // 0.5 * det( | x1 y1 | ) // | x2 y2 | public double getArea() { return .5 * ((vertex[1].getX() - vertex[0].getX()) * (vertex[2].getY() - vertex[0].getY()) - (vertex[2].getY() - vertex[0].getY()) * (vertex[1].getY() - vertex[0].getY())); } }