public static void switch (int[] a, int slot1, int slot2) { if ((slot1 >= 0) && (slot1 < a.length) && (slot2 >= 0) && (slot2 < a.length)) { // valid parameters so do the switch int temp = a[slot1]; // create a temporary variable to hold one of the values a[slot1] = a[slot2]; // switch one element a[slot2] = temp; // switch the other element } }Task 2
public static int [] copySwitch (int[] a, int slot1, int slot2) { int [] result = new int[a.length]; // create a new array of same length as a for (int i=0; i < a.length; i++) { // copy a to result result[i] = a[i]; } if ((slot1 >= 0) && (slot1 < a.length) && (slot2 >= 0) && (slot2 < a.length)) { // valid parameters so do the switch result[slot1] = a[slot2]; // we can do this because integers are not shared result[slot2] = a[slot1]; // for Object arrays we would need a temporary variable } return result; // return the answer }
public static int[][] palindromeArray (int[] a) // declare and initialize integer arrays for two answers int [] ans1 = new int[2*a.length-1]; int [] ans2 = new int[2*a.length-1]; // fill the arrays halfway starting from the beginning for (int i=0; i < a.length; i++) { ans1[i] = a[i]; // copy of initial array ans2[a.length-1-i] = a[i]; // reverse of initial array } // fill the rest of the answer arrays by "mirroring" // the existing elements in the answers for (int i=a.length; i < ans1.length; i++) { int mirrorIndex = ans1.length-1-i; ans1[i] = ans1[mirrorIndex]; ans2[i] = ans2[mirrorIndex]; } // create the final answer array int [][] finalAnswer = {ans1,ans2}; // an alternative way to create the finalAnswer is // int [][] finalAnswer = new int[][2]; // finalAnswer[0] = ans1; // finalAnswer[1] = ans2; return finalAnswer; }
seq1 | seq2 | ans1 | ans2 |
---|---|---|---|
[2,3,4] | [3,2,1] | [1,2,3,4] | [4,3,2,1] |
[3,4] | [2,1] | [2,1,2,3,4] | [3,4,3,2,1] |
[4] | [1] | [3,2,1,2,3,4] | [2,3,4,3,2,1] |
[] | [] | [4,3,2,1,2,3,4] | [1,2,3,4,3,2,1] |
Task 2
public static IntList [] palindromeListWhile (IntList L) { // initialize state variables IntList ans1 = L; IntList ans2 = ILO.reverse(L); IntList seq1 = tail(ans1); IntList seq2 = tail(ans2); while (!IL.isEmpty(seq1)) { // update state variables in iteration loop ans1 = IL.prepend(IL.head(seq1), ans1); ans2 = IL.prepend(IL.head(seq2), ans2); seq1 = IL.tail(seq1); seq2 = IL.tail(seq2); } // return answers in requested format return twoLists(ans1,ans2); }
Task 1 Object diagram is not available...sorry!
Task 2 Students can also be represented by an array of three integers which hold the intelligence rating, the diligence rating, and 10 times the gpa (assume gpas are calculated to 1 decimal place only).
public class Student { // Instance Variable -- name it anything we want private int [] attributes; // intelligence, diligence, 10*gpa // Constructor method public Student (int iRating, int dRating, double gpa) { // initialize instance variables attributes = new int[3]; attributes[0] = iRating; attributes[1] = dRating; attributes[2] = (int) 10*gpa; } // Instance methods public int getIntelligence () { return attributes[0]; } public int getDiligence () { return attributes[1]; } public double getGPA () { return attributes[2] * 0.1; // or ((double)attributes[2])/10.0 } public Point getIDRating () { return new Point(attributes[0],attributes[1]); } public void setIntelligence (int i) { attributes[0] = i; } public void setDiligence (int d) { attributes[1] = d; } public void setGPA (double gpa) { attributes[2] = (int) 10*gpa; } public void setIDRating (Point idRating) { attributes[0] = idRating.x; attributes[1] = idRating.y } public int getTotalRating () { return (int) 3*(attributes[0]+attributes[1]) + gpa; } public boolean isPassing () { return this.gpa >= 20; } public boolean isHardWorking () { return ((attributes[0]+attributes[1]) >= 10) && (this.gpa >= (attributes[0]*4)); } }
Task 3 Students can also be represented by three integer instance variables which hold the sum of the intelligence and diligence ratings, the difference between the intelligence and diligence ratings (i.e. intelligence-diligence), and the total rating of the student as specified by the contract.
public class Student { // Instance Variables private int sumId, diffId, totalRating; // Constructor method public Student (int iRating, int dRating, double gpa) { updateVariables(iRating, dRating, gpa); } // Instance methods public int getIntelligence () { return (sumId + diffId)/2; } public int getDiligence () { return sumId - getIntelligence(); } public double getGPA () { return (totalRating - 3*sumId)*0.1; } public Point getIDRating () { return new Point(getIntelligence(), getDiligence()); } public void setIntelligence (int i) { updateVariables(i, getDiligence(), getGPA()); } public void setDiligence (int d) { updateVariables(getIntelligence(), d, getGPA()); } public void setGPA (double gpa) { totalRating = 3*sumId + (int)(10*gpa); } public void setIDRating (Point idRating) { updateVariables(idRating.x, idRating.y, getGPA()); } public int getTotalRating () { return totalRating; } public boolean isPassing () { return getGPA() >= 2.0; } public boolean isHardWorking () { return (sum >= 10) && (getGPA() >= (getIntelligence()*0.4)); } // Auxiliary methods (still an instance method) // Since this method is not part of the contract, make it private. private void updateVariables (int iRating, int dRating, double gpa) { sumId = iRating + dRating; diffId = iRating - dRating; totalRating = 3*sumId + (int)(10*gpa); } }