Fall 21 [Bono]

CS 455 Midterm 1 Solution

 

Problem 1

Part A.

Diagram

Description automatically generated

Part B.

[26, 32] [26, 32]

 

Problem 2

Part A  constructor   getLoc       getDir

Part B Solution shown in bold

// Ant that can move around in a two-dimensional world.

public class Ant2D {

   public static final int NORTH = 0;
   public static final int EAST = 1;
   public static final int SOUTH = 2;
   public static final int WEST = 3;
   public static final int NUM_DIRECTIONS = 4;

   private Point currLoc;

   private int currDirection;

   // Create a ant at location startPos, facing EAST
   public Ant2D(Point startPos) {

      currLoc = new Point(startPos);

      currDirection = EAST;

   }

   // Get ant’s current position
   public Point getLoc() {

      return currLoc;

   }

   // Get ant's current direction
   // Returns one of NORTH, EAST, SOUTH, or WEST
   public int getDir() {

      return currDirection;

   }

   // Turn ant right by 90 degrees
   public void turnRight() {

      currDirection++;

      if (currDirection == NUM_DIRECTIONS) {

         currDirection = NORTH;

      }

   }

   // Advance the ant forward by distance units in the direction
   // he is facing
   // PRE: distance >= 0 [ he can't go backwards ]
   public void advance(int distance) {

      if (currDirection == NORTH) {

         currDir.translate(0, -distance);

      } else if (currDirection == EAST) {

         currDir.translate(distance, 0);

      } else if (currDirection == SOUTH) {

         currDir.translate(0, distance);

      } else if (currDirection == WEST) {

         currDir.translate(-distance, 0);

      }

   }

}

 

Problem 3 Several correct solutions (two given here)

 

mkdir 09-16

cp $ASNLIB/public/09-16/* 09-16

 

The following 1-line solution uses   cp -r   (recursive copy):

cp -r $ASNLIB/public/09-16 .

 

Problem 4

Part A.

bottom     width     scale

Part B.

bottom    left     scale

Part C.

applicationHeight    label

 

Problem 5 Changes shown in bold
Part A

public class Square {

   private int length;
   private int area;

   // create a square with given length for a side
   public Square(int sideLength) {
      length = sideLength;
      area = sideLength*sideLength;

   }

   // get the area of the square
   public int getArea() { return area; }

// double the length of each side
   public void grow() {

      length = 2 * length;

      area = length * length;

   }
}

 

Problem 5
Part B

public class Square {

   private int length;

   private int area;

   // create a square with given length for a side
   public Square(int sideLength) {

      length = sideLength;

      area = sideLength*sideLength;

   }

   // get the area of the square
   public int getArea() {

      return area;

      return length * length;

   }

   // double the length of each side
   public void grow() { length = 2 * length; }

}

 

Problem 6

Part A  Several possible solutions.  Two of them shown below.

 

// Solution 1: this solution iterates over result

public static double[] neighborAverage(int[] nums) {

   assert nums.length > 0 && nums.length % 3 == 0; // optional

   double[] result = new double[nums.length / 3];

   for (int i = 0; i < result.length; i++) {

      result[i] = (nums[i*3] + nums[i*3+1] + nums[i*3 +2]) / 3.0;

   }

   return result;

}

 

// Solution 2: this solution iterates over nums
public static double[] neighborAverage2(int[] nums) {
   assert nums.length > 0 && nums.length % 3 == 0; // optional
 
   double[] result = new double[nums.length / 3];
   // (i < nums.length - 2) also works below
   for (int i = 0; i < nums.length; i+=3) {
      result[i/3] = (nums[i] + nums[i+1] + nums[i + 2]) / 3.0;
   }    
   return result;
}

 

Part B

assert   nums.length > 0  &&  nums.length % 3 == 0;