public class Problem1 { public static void main(String[] args) { String s1 = "fred"; String s2 = "sam"; String s3 = "abe"; String s4 = "freddy"; System.out.println("1: "+compareStrings(s1,s1)); System.out.println("2: "+compareStrings(s1,s2)); System.out.println("3: "+compareStrings(s1,s3)); System.out.println("4: "+compareStrings(s1,s4)); } // Return a Boolean value based on the alphabetical order of $s1$ and $s2$: private static boolean compareStrings(String s1, String s2) { int length = Math.min(s1.length(),s2.length() ); for (int i = 0; i < length; i++) { // Extract characters and convert to uppercase: char c1 = s1.charAt(i); if (c1 <= 'Z') c1 += ('a'-'A'); char c2 = s2.charAt(i); if (c2 <= 'Z') c2 += ('a'-'A'); // Compare current characters: if (c1 < c2) return true; else if (c2 > c1) return false; } // Take care of cases like s1 = "Bill", s2 = "Billy" if (s1.length() < s2.length()) return true; else return false; } } // Class Problem1 /*****************************************************************************/ public class Problem2 { private static final int BASE = 5; // base "length" of triangle (must be odd) private static int[][] nums; // upside-down triangle array // Create an triangle array and sort it: public static void main(String[] args) { createArray(); printArray("Unsorted:"); sortArray(); printArray("Sorted:"); } // Method main // Create an upside-down triangle array $nums$ in column-major format: private static void createArray() { nums = new int[BASE][]; for (int i=0; i<=BASE/2; i++) nums[i]=new int[i+1]; for (int i = BASE/2+1 ; i < BASE; i++) nums[i]=new int[nums[i-1].length-1]; } // Method createArray // Output the current values inside $nums$: private static void sortArray() { int minIndex, index, remIndex; int[] tmp; // Sort each element in ascending order: for(index = 0; index < BASE-1; index++) { minIndex = index; // Find smallest value in remaining elements: for(remIndex = index+1; remIndex < BASE; remIndex++) if (nums[remIndex].length > nums[minIndex].length) minIndex = remIndex; // Swap values: tmp = nums[minIndex]; nums[minIndex] = nums[index]; nums[index] = tmp; } } // Method sortArray // Sorts the columns of $nums$ in descending order and stores the // sorted array back in $nums$: private static void printArray(String s) { System.out.println(s); for (int i=0; i= MAX_X/2) top[x][z] += (z < MAX_Z/2) ? -2 : 2; } } // method setHeights // Create the 4D array $b$ such that each element stores Data for each room // in the building. Note that the *4th* dimension will store the Data reference: private static void printData(int time, int floor) { for (int z=0; z < MAX_Z; z++) { for (int x=0; x < MAX_X; x++) { if (b[time][z][x].length >= floor) { System.out.print("("+time+","+(z+1)+","+(x+1)+","+(floor+1)+")"+" "); System.out.print(b[time][z][x][floor]+"\t"); } } System.out.println(); } } // method printData // Output the Data for all rooms on a particular $floor$ for time $time$: private static void createBuilding() { b = new Data[MAX_T][][][]; for (int t=0; t < MAX_T; t++) { // time b[t] = new Data[MAX_Z][][]; for (int z=0; z < MAX_Z; z++) { // z b[t][z] = new Data[MAX_X][]; for (int x=0; x < MAX_X; x++) { // x b[t][z][x] = new Data[top[z][x]]; for (int y=0; y < b[t][z][x].length; y++) // inserting Data objects b[t][z][x][y] = new Data(); } // x } // z } // t } // method createBuilding } // class Problem3 // Class for storing motion data of each room: class Data { public double disp; // displacement of room for current time public double accel; // acceleration of room for current time public String toString() { return "[D: "+disp+", A: "+accel+"]"; } } // class Data /* Output: (0,3,3,8) [D: 0.0, A: 0.0] (0,3,4,8) [D: 0.0, A: 0.0] (0,4,3,8) [D: 0.0, A: 0.0] (0,4,4,8) [D: 0.0, A: 0.0] */ /*****************************************************************************/ // Problem 4 class Rower { private double eff; // efficiency private double rate; // dist per sec (m/s) private final double MIN = 0.75; // min distance can travel (m) private final double MAX = 1.50; // max distance can travel (m) public Rower() { rate = Math.random()*(MAX-MIN)+MIN; // give rate as 1.25 <= rate < 2.25 } private void changeEfficiency(int time) { eff = (-time*time/3240000.0) + (time/900.0); } // return distance rower manages to row boat public double rowDist(int time1, int time2) { int deltaTime = Math.abs(time2-time1); // not nec on prelim changeEfficiency(time2); return eff*rate*deltaTime; // (%)*(dist/time)*(time)->(% dist) } } // Class Rower class Boat { private double pos; // initial pos is zero Boat() { } public void moveBoat(double change) { pos += change; } public double getPosition() { return pos; } } public class Problem4 { public static void main(String[] args) { // create 50 rowers final int SIZE = 50; Rower r = new Rower[SIZE]; for (int i=0;i