Question 1: -int 5 -int 11 -int 0 -boolean true -boolean true -boolean false -boolean false -String 32c -boolean false (you can deduce that 'x' can't be both less than 'A' and greater than 'Z' since we know from the few things we do need to know about the chart that 'A' < 'Z' --since we know that A-Z are 26 in a row) Question 2: - x.length() > 5 - (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') || (x >= '0' && x <= '9') - (x >= '0' && x <= '9') && (x - '0') == y - x > 5 || x < -5 - Math.abs(x) > 5 - (x || y) && !(x && y) (alternate answer: (x && !y) || (!x && y) Question 3: public class ComplexNumber { private double real; private double complex; //"this" is optional in all of the following since //there is no variable called real/complex besides this.real/complex public ComplexNumber(double initialReal, double initialComplex) { this.real = initialReal; this.complex = initialComplex; } public void setReal(double newReal) { this.real = newReal; } public double getReal() { return this.real; } public void setComplex(double newComplex) { this.complex = newComplex; } public double getComplex() { return this.complex; } public static ComplexNumber add(ComplexNumber c1, ComplexNumber c2) { return new ComplexNumber( c1.getReal() + c2.getReal, c1.getComplex() + c2.getComplex()) /* could also do it in more steps: double c1Real = c1.getReal(); double c2Real = c2.getReal(); double sumReal = c1Real + c2Real; double c1Complex = c1.getComplex(); double c2Complex = c2.getComplex(); double sumComplex = c1Complex + c2Complex; ComplexNumber sum = new ComplexNumber(sumReal, sumComplex); return sum; */ /*Note finally that since we are inside the ComplexNumber class, we actually can access private properties So return new ComplexNumber(c1.real + c2.real, c1.complex + c2.complex) This seems perhaps a bit odd at first, but the point is that since we are still inside the class ComplexNumber.java, we really aren't exposting these internal details to OTHER classes */ } public static ComplexNumber multiply(ComplexNumber c1, ComplexNumber c2) { //note there is a slight error in the formula in the questions double newReal = c1.getReal() * c2.getReal() - c1.getComplex() * c2.getComplex(); double newComplex = -(c1.getComplex() * c2.getReal() + c2.getComplex() * c1.getReal()); return new ComplexNumber(newReal, newComplex); } } public class ComplexNumberTest { public static void main(String[] args) { ComplexNumber i = new ComplexNumber(0,1); ComplexNumber iSquared = ComplexNumber.multiply(i,i); if (iSquared.getReal() == -1 && iSquared.getComplex() == 0) { System.out.println("The universe is normal"); } else { System.out.println("The universe is not normal as i * i is not actually -1! Either that oar there is a bug!"); } } } Question 4: public class StudentGrades { public static int countPasses(SmartIntArray array) { int count = 0; //note that array HAS an array in it. It is NOT an array //this means that it does NOT have a lenght property directly defined on it. //similarly, we can't do array[i] since only arrays can be accessed via [ ] for (int i=0; i < array.size(); i++) { if (array.getAt(i) > 50) { count++; } } return count; } public static double calculateMean(SmartIntArray array) { int sum = 0; for (int i=0; i < array.size(); i++ ) { sum += array.getAt(i); } return (double)sum / array.size(); } public static double calculateMedian(SmartIntArray array) { SmartIntArray sorted = array.sort(); int size = sorted.size(); if (size % 2 == 0 ) { return (sorted.getAt(size / 2) + sorted.getAt(size / 2 - 1)) / 2.0; } else { return sorted.getAt(size / 2); } } public static void main(String[] args) { SmartIntArray grades = new SmartIntArray(); java.util.Scanner reader = new java.util.Scanner(System.in); //have to be careful to make sure we skip the loop entirely if the first number //is less than 0. Also have to be careful to make sure we always read the last number //but don't add any negative numbers int nextNumber = reader.nextInt(); while (nextNumber >= 0) { grades.add(nextNumber); nextNumber = reader.nextInt(); } double mean = calculateMean(grades); double median = calculateMedian(grades); double numPasses = countPasses(grades); System.out.println("The average grade was " + mean + " and the median was " + median + ". The total number of passes is " + numPasses + " which comes to a percent of " + 100.0 * numPasses / grades.size() + "%"); } } Question 5: public static int countPositive(int[] array) { int count = 0; for (int i=0; i < array.length; i++) { if (array[i] > 0 ){ count++; } } return count; } public static int countCloseTo(double[] array, double target, double epsilon) { //notice everything about this method is identical except that the condition is differnet int count = 0; for (int i=0; i < array.length; i++ ) { if ( Math.abs(target - array[i]) < epsilon) { count++; } } return count; } public static double[] doubleValues(double[] original) { //we could change the original arrya if we wanted rather than making a new variable and array //We'll see when we talk about references that this could cause an unforeseen issue but might //be good as long as you realize what's happening (not needed for the midterm) double[] doubled = new double[original.length]; for (int i=0; i < original.length; i++) { doubled[i] = original[i] * 2; } return doubled; } public static double[] removeFirst(double[] original) { double[] shorter = new double[original.length - 1]; for (int i = 1; i < original.length; i++ ) { shorter[i - 1] = original[i]; } return shorter; } public static boolean reverse(boolean[] original) { //the question asks us to return a different array. This means we don't swap "in place" boolean[] reversed = new boolean[original.length]; for (int i=0 ; i < reversed.length; i++ ) { reversed[i] = original[original.length - i - 1]; } return reversed; } public static int computeSum(int[] array) { int sum = 0; for (int i=0; i < array.length; i++) { sum += array[i]; } return sum; } public static int[] removeAt(int[] original, int index) { int[] shorter = new int[original.length - 1]; for (int i = 0; i < index; i++ ) { shorter[i] = original[i]; } for (int i = index + 1; i < original.length; i++) { shorter[i] = original[i + 1]; } } //this is a very useful helper method for the next part public static int findTarget(int[] array, int target) { for (int i=0; i < array.length; i++ ) { if (array[i] == target) { return i; } } return -1; } public static int[] removeTarget(int[] original, int target) { int location = findTarget(original, target); while (location >= 0 ) { original = removeAt(original, location); location = findTarget(original, target); } return original; } public static double[] sort(double[] original) { //outer for loop controls which part of the array we are looking at for (int i=0; i < original.length; i++) { //now in here, we will find the smallest value from i until the end of the array, and put it in the front int smallestIndex = i; for (int j = i + 1; j < original.length; j++) { if (original[j] < original[smallestIndex]) { smallestIndex = j; } } //now swap the ith position with the smallestIndex position int temp = original[i]; original[i] = original[smallestIndex]; original[smallestIndex] = temp; /* we also could call the helper method as suggested original = placeNthNumber(original, i); see below for definition of the method */ } public static int[] placeNthNumber(int[] array, int startPoint) { //find the minimum number from startPoint ---> end of array, and put it at startPoint int smallestIndex = startPoint; for (int i = smallestIndex + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) { smallestIndex = i; } } int temp = array[startPoint]; array[startPoint] = array[smallestIndex]; array[smallestIndex] = temp; return array; }