CSE1020 Labtest 2E

Please read this part carefully

No collaboration is allowed during a labtest. Refrain from speaking with other students during the test. If you have a question, please ask the teaching assistants or the instructor.

In your home directory you will find a copy of your submitted work from Labs 03–06 in the directory unsubmit. You may use pens/pencils and blank sheets of paper. No other aids are allowed.

Your work will be evaluated based on its correctness and conformance to the style rules. Your code must compile to receive a passing grade.

Remember to save your work and submit your work at regular intervals during the test.

APIs

Test Problem 1 of 2

Cut-and-paste the following program into your editor:

import test2.TestUtility;
import java.io.PrintStream;
import java.util.ArrayList;

public class Test2E
{
   public static void main(String[] args)
   {
      PrintStream out = System.out;
      
      // get and print a random list of integers
      final ArrayList<Integer> LIST = TestUtility.randomList();
      out.println(LIST);
      
      // the number of elements in the list
      final int N = LIST.size();
      
      // YOUR CODE GOES AFTER HERE

   }
}


Complete the program so that it computes the sum of the elements in LIST. Notice that LIST is already created for you, and already contains N random integers.

For a maximum of 7 out of 10 marks: The output is simply the list on one line (already done for you), followed by the sum of the elements on the next line. Some examples (where you type what is in red to run the program, and your program outputs what is in black):

java Test2E
[8, 7, 2, 3, 5, 6, 9, 4, 1]
45

java Test2E
[3, 5, 7, 6, 9, 2, 4, 1, 8]
45

java Test2E
[5, 6, 7, 6, 6, 6, 6, 6, 3]
51


For a maximum of 10 out of 10 marks: The output shows the list on one line (already done for you), followed by the sum of the first two elements on the next line, followed by the sum of the first three elements on the next line, and so on:

java Test2E
[8, 7, 2, 3, 5, 6, 9, 4, 1]
8 + 7 = 15
8 + 7 + 2 = 17
8 + 7 + 2 + 3 = 20
8 + 7 + 2 + 3 + 5 = 25
8 + 7 + 2 + 3 + 5 + 6 = 31
8 + 7 + 2 + 3 + 5 + 6 + 9 = 40
8 + 7 + 2 + 3 + 5 + 6 + 9 + 4 = 44
8 + 7 + 2 + 3 + 5 + 6 + 9 + 4 + 1 = 45

java Test2E
[3, 5, 7, 6, 9, 2, 4, 1, 8]
3 + 5 = 8
3 + 5 + 7 = 15
3 + 5 + 7 + 6 = 21
3 + 5 + 7 + 6 + 9 = 30
3 + 5 + 7 + 6 + 9 + 2 = 32
3 + 5 + 7 + 6 + 9 + 2 + 4 = 36
3 + 5 + 7 + 6 + 9 + 2 + 4 + 1 = 37
3 + 5 + 7 + 6 + 9 + 2 + 4 + 1 + 8 = 45

java Test2E
[5, 6, 7, 6, 6, 6, 6, 6, 3]
5 + 6 = 11
5 + 6 + 7 = 18
5 + 6 + 7 + 6 = 24
5 + 6 + 7 + 6 + 6 = 30
5 + 6 + 7 + 6 + 6 + 6 = 36
5 + 6 + 7 + 6 + 6 + 6 + 6 = 42
5 + 6 + 7 + 6 + 6 + 6 + 6 + 6 = 48
5 + 6 + 7 + 6 + 6 + 6 + 6 + 6 + 3 = 51
 

Submit

Submit your program using the command:

submit 1020 test02 Test2E.java

 

Test Problem 2 of 2

Cut-and-paste the following program into your editor:

import princeton.introcs.Picture;
import java.awt.Color;
import java.io.PrintStream;

public class Test2F
{
   public static void main(String[] args)
   {
      PrintStream out = System.out;

      // the picture to use as the tile
      final String FILE = "/cse/course/1020/labtest/tile.png";
      Picture tile = new Picture(FILE);

      // the width and height in pixels of the tile
      final int TILE_WIDTH = tile.width();
      final int TILE_HEIGHT = tile.height();
      
      // the width and height of the picture we want to create
      final int PIC_WIDTH = Integer.parseInt(args[0]);
      final int PIC_HEIGHT = Integer.parseInt(args[1]);
      
      // the picture we want to create
      Picture pic = new Picture(PIC_WIDTH, PIC_HEIGHT);
      

      // YOUR CODE SHOULD BEGIN HERE
      

      // YOUR CODE SHOULD STOP BEFORE HERE
      pic.show();
   }
}


Suppose that you have a small digital picture that you want to use to fill in a large area. One way to do this is to repeat the small picture (which is called a tile) so that it fills the larger area. For example, here is a tile with size 100x100:

 

And here is a picture of the tile repeated four times in the horizontal direction and three times in the vertical direction so that it creates a tiling of size 400x300.

 

For a maximum of 15 out of 20 marks: Complete the program so that it creates a picture of size PIC_WIDTHxPIC_HEIGHT using a tile where the tiling starts in the upper-left corner of the picture. You may assume that that PIC_WIDTH is always larger than the width of the tile, and that PIC_HEIGHT is always larger than the height of the tile. Some examples using a tile of size 100x100:

java Test2E 400 300



java Test2E 250 150




For a maximum of 20 out of 20 marks: Complete the program so that the tiling is centered on the picture; this is probably easiest if you solve the 15 out of 20 question first. An example using a tile of size 100x100:

java Test2E 250 150


 

Submit

Submit your program using the command:

submit 1020 test02 Test2F.java