Assignment 2


Objectives and example

 

This assignment will focus on modules 5-7.
 

A2.0 Audio:
 

We'll first demonstrate some problem-solving. Consider this problem. Suppose we want to print out lines like this:

zyxwvutsrq
zyxwvutsr
zyxwvuts
zyxwvut
zyxwvu
zyxwv
zyxw
zyx
zy
z
    
And suppose we want to use for-loops to achieve this. How do we go about solving this problem?
 

A2.1 Exercise: At this point do not write any code. Try to sketch out a solution. Go as far as you can with coding before reading the solution.
 

Now read the solution
 

A2.2 Audio:
 

And now, on to your assignment. Good luck!


Assignment problems

 

  1. The program below intends to have the following output:
    The product of numbers 1 through 1 is: 1
    The product of numbers 1 through 2 is: 2
    The product of numbers 1 through 3 is: 6
    The product of numbers 1 through 4 is: 24
    The product of numbers 1 through 5 is: 120
        
    This is the program:
    public class ForLoopInCorrect {
    
        public static void main (String[ argv)
        {
    	System.out.print ("The product of numbers 1 through " + i + " is: ");
    	for (int i=1; i<=5; i--) {
    	    int prod = 1;
    	    for (int j=1; j<=i; j++) {
    		prod = prod + j;
    		System.out.println (prod);
    	    }
    	}
        }
    
    }
        
    Thus, in an inner loop, the program computes the product of integers from 1 up to i. And since i varies from 1 to 5, it should print out the desired output. There are both syntactic and logical mistakes in the program. Fix the program and rename it to ForLoopCorrect.java.

  2. Write a program called BiggerList.java that, given a number like N (for example N=9) prints the following:
    Numbers bigger than 1:  2 3 4 5 6 7 8 9
    Numbers bigger than 2:  3 4 5 6 7 8 9
    Numbers bigger than 3:  4 5 6 7 8 9
    Numbers bigger than 4:  5 6 7 8 9
    Numbers bigger than 5:  6 7 8 9
    Numbers bigger than 6:  7 8 9
    Numbers bigger than 7:  8 9
    Numbers bigger than 8:  9
        
    Thus, the first line has 1 followed by the the list of numbers 2, 3, ... etc ... until N. The last line has N-1, followed by a list containing only N. To get the number N, use the IOTool.readIntFromTerminal() method as seen in Module 6. For this purpose, you will need IOTool.java

  3. For this part of the assignment, you will need WordTool.java and wordsWithPOSAndPron.txt Your goal is to get N random words (nouns, perhaps), extract their first letters and put all those letters into a string. Here is sample output:
    Word #1 is pioneer
    Word #2 is sunken
    Word #3 is bedim
    Word #4 is spill
    Word #5 is step
    Word #6 is shutdown
    Word #7 is menace
    Word #8 is grit
    Word #9 is jelly
    Word #10 is bifurcate
    All the first letters: psbsssmgjb
        
    Write your code in FirstLetters.java. To get the number N, use the IOTool.readIntFromTerminal() method as seen in Module 6.

  4. Art project. The screenshot below is the result of a simple program written to use DrawTool.java:

    It uses three rather simple for-loops to draw the above picture. Write a program called GeometricArtwork.java to draw the above pattern. Then, write a program called BetterGeometricArtwork.java that uses for-loops to draw your own artwork. We'll impose the following rule: all your drawing (of lines, ovals, whatever) must occur inside for-loops. Some of the DrawTool commands you'll find useful:

        // Draw a rectangle with top left at (1.5,2), width 5, height 6.5.
        DrawTool.drawRectangle (1.5, 2, 5, 6.5);
    
        // Set the color of a particular geometric object (circles are ovals)
        DrawTool.setOvalColor ("red")
    
        // Draw a circle with center at (1,2) and radius 5.8
        DrawTool.drawCircle (1, 2, 5.8);
        
    For others, skim through the code in DrawTool.java (open it up in pico). Available colors are "black", "blue", "cyan", "gray", "green", "magenta", "orange", "pink", "white", "yellow". Don't forget, the first lines in your code need to set up the display and the range of x,y values, as in:
    	DrawTool.display();
    	DrawTool.setXYRange (0,10,0,10);
        

  5. For this question, you will merely write plain text in assignment2.pdf. Find some articles on computers that play chess and, especially, the history of computers playing chess. Write up a few paragraphs on chess-playing computers in a PDF file, addressing the following:
    • When were the first chess-playing programs written?
    • What were the key elements that led to computers being competitive with humans?
    • Look up similar (and recent) results for the game Go. What are the implications of computers being successfully programmed to defeat humans?

How to submit:
 

And that's the end of Unit 0!
 

Back to Unit 0 page


© 2017, Rahul Simha