Task 1 Let's say we have an array of integers. We can write
a method switch
which will switch the contents of two
slots in the array. So, if we have an integer array a1=[1,2,3,4] and
we do switch(a1,0,2)
then a1=[3,2,1,4]. Flesh out the
skeleton for the switch
method below.
public static void switch (int[] a, int slot1, int slot2);
Does not modify a if either slot1 or slot2 is invalid. Otherwise, modifies a so that the contents of slot1 are in slot2 and vice versa. Note that no new arrays are created by this method.
Task 2 We can write a version of the switch
method called copySwitch
which returns a new array which
is the result of switching the two elements of the original array,
but does not modify the original array. For example, if we have a1
from above and we do int [] a2 = copySwitch(a1,0,2)
then
a1=[1,2,3,4] (unchanged) and a2=[3,2,1,4]. Flesh out the skeleton for
the copySwitch
method below.
public static int [] copySwitch (int[] a, int slot1, int slot2);
Returns a copy of a if either slot1 or slot2 is invalid. Otherwise, returns a copy of a with the contents of slot1 in slot2 and vice versa.
Palindromes are sequences of characters that read the same
forwards and backwards. For example, "abcba". Given a sequence of
digits, we can create two palindromes from it depending on if we want
that sequence of digits to be at the beginning or the end of the
palindrome. For example, if we have "123", we could make the
following two palindromes: "12321" and "32123". Let's store our
digits in an integer array. Flesh out the skeleton for our
palindromeArray
method which returns to us an array with
two elements, each element is one of the answers above.
public static int[][] palindromeArray (int[] a);
Returns an array with the two possible palindromes. Each palindrome created from the integer array is represented by an integer array. Auxiliary methods are not needed and your solution should create the fewest number of arrays necessary to get the job done. The original array a should not be modified.
We can also create palindromes from sequences of numbers stored in
an IntList structure. We'll assume that the ILO.reverse method is
available to us as well as all the methods in the IntList contract
which we can use by prefixing the methods with IL. Here is a
tail-recursive version of palindromeList
which specifies
the iteration in four state variables named seq1, seq2,
ans1, ans2:
public static IntList [] palindromeList (IntList L) { IntList reverseL = ILO.reverse(L); return palindromeListTail(tail(L),tail(reverseL),L,reverseL); } public static IntList [] palindromeListTail (IntList seq1, IntList seq2, IntList ans1, IntList ans2) { if (IL.isEmpty(seq1)) { return twoLists(ans1,ans2); } else { palindromeListTail(IL.tail(seq1),IL.tail(seq2), IL.prepend(head(seq1), ans1), IL.prepend(head(seq2), ans2)); } } // Auxiliary method used by palindromeList public static IntList [] twoLists (IntList L1, IntList L2) { IntList [] result = new IntList[2]; result[0] = L1; result[1] = L2; return result; }
Task 1 Suppose that IntList A has the printed
representation [1,2,3,4]. Fill in the following table to show the
parameters passed to successive calls to
palindromeListTail
in the computation that begins with
the invocation palindromeList(A)
. You have been provided
with more rows than you need so some rows should remain empty when
you are done.
seq1 |
seq2 |
ans1 |
ans2 |
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 2 It is possible to express any iteration as a
while
loop. Flesh out the following code skeleton of an
palindromeListWhile()
method that behaves just like the
above palindromeList()
method except that it uses a
while
loop rather than tail recursion to express the
iteration of palindromeListTail()
. Your
palindromeListWhile
should not call any auxiliary
methods other than twoLists()
and the
IntList
contract and ILO.reverse.
public static IntList [] palindromeListWhile (IntList L);
The recent issue of Counterpoint has an article about
Wellesley stereotypes. Inspired by that, we will model students based
on their intelligence, diligence, and gpa. Intelligence and diligence
are integer ratings from 1-10. GPA is a double from 0.0 to 4.0. It is
often interesting to graph student data in a three dimensional graph
where intelligence and diligence correspond to a typical x-y axis and
gpa is the z axis. The contract for the Student
class is
as follows:
Student
contract
public Student (int iRating, int dRating, double gpa);
Creates a new Student with the given intelligence rating, diligence rating, and gpa.
public int getIntelligence ();
Returns the intelligence rating (1-10).
public int getDiligence ();
Returns the diligence rating (1-10).
public double getGPA ();
Returns the gpa (0.0-4.0).
public Point getIDRating ();
Returns a Point with intelligence rating as x-coordinate and diligence rating as y-coordinate.
public void setIntelligence (int i);
Sets the intelligence rating.
public void setDiligence (int d);
Sets the diligence rating.
public void setGPA (double gpa);
Sets the gpa.
public void setIDRating (Point idRating);
Sets the intelligence and diligence ratings.
public int getTotalRating ();
Returns a total rating which is three times the sum of the intelligence and diligence ratings plus ten times the gpa.
public boolean isPassing ();
Returns true if gpa is greater or equal to 2.0.
public boolean isHardWorking ();
Returns true if the sum of the intelligence and diligence ratings is greater than or equal to ten and the gpa is greater than or equal to the intelligence rating times 0.4.
public class Student { private Point idRating; // intelligence, diligence rating private double gpa; public Student (int iRating, int dRating, double gpa) { this.idRating = new Point(iRating, dRating); this.gpa = gpa; } public int getIntelligence () { return idRating.x; } public int getDiligence () { return idRating.y; } public double getGPA () { return this.gpa; } public Point getIDRating () { return idRating; } public void setIntelligence (int i) { this.idRating.x = i; } public void setDiligence (int d) { this.idRating.y = d; } public void setGPA (double gpa) { this.gpa = gpa; } public void setIDRating (Point idRating) { this.idRating = idRating; } public int getTotalRating () { return (int) 3*(this.idRating.x+this.idRating.y) + gpa*10; } public boolean isPassing () { return this.gpa >= 2.0; } public boolean isHardWorking () { return ((this.idRating.x+this.idRating.y) >= 10) && (this.gpa >= (this.idRating.x*0.4)); } }
Task 1 Consider the following TestStudents
class:
public class TestStudents { public static void test() { Students [] class1 = new Students[4]; Student barb = new Student(7,3,3.5); class1[0] = barb; class1[1] = new Student(2,8,1.8); class1[3] = new Student(6,6,2.6); barb.setDiligence(class[3].getDiligence()); class1[3].setIDRating(class[0].getIDRating()); class1[1].setGPA(3.0); double barbRating = barb.getTotalRating(); boolean allPass = class1[0].isPassing() && class[1].isPassing() && class[3].isPassing(); boolean hasHardWorker = class1[0].isHardWorking() || class1[1].isHardWorking() || class1[2].isHardWorking(); } }
Draw one execution frame for the execution of
TestStudents.test()
. Do not draw any other execution
frames. Draw object diagrams for all objects created by the execution
of TestStudents.test()
.
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). Write a version of the Student
class using
this representation which fulfills the entire Student
contract.
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. Write a version of the
Student
class using this representation which fulfills
the entire Student
contract.