Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Project #2
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by the start of class on Thursday, 09/25/2014

# Problems

The following five programming problems can all be completed using nested loops. You job is to write five different programs, one for each problem.

1. Write a program, triangle1.java, which takes a single integer argument, n. The program should print a String of characters, representing a right triangle whose legs (the sides that meet at a 90 degree angle) consist of n * characters. The widest edges should be on the left and bottom. For example:

```John-Lasseter:~ jlasseter\$ java  triangle1 5
*
* *
* * *
* * * *
* * * * *
```
2. Write a program, triangle2.java, which takes a single integer argument, as in Problem 1. The program should print a String of characters, representing a right triangle whose legs (the sides that meet at a 90 degree angle) consist of n * characters. The widest edges should be on the left and top. For example:

```John-Lasseter:~ jlasseter\$ java  triangle2 5
* * * * *
* * * *
* * *
* *
*
```
3. Write a third program, triangle3.java, which prints a a (2n-1) by n isosceles triangle, with the widest edge on the left. For example:

```John-Lasseter:~ jlasseter\$ java  triangle3 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
```
4. Your fourth program, triangle4.java, should print out a (2n-1) by n isosceles triangle, with the widest edge on the bottom. For example:

```John-Lasseter:~ jlasseter\$ java  triangle4 5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
```
5. Like the four triangle problems, your fifth and final program, diamond.java, takes an integer argument, n. This program should print out a (2n-1) by (2n-1) diamond shape. For example:

```John-Lasseter:~ jlasseter\$ java  diamond  5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
```

To summarize, there are five programs you need to write, all saved in a folder named lab2:

• triangle1.java
• triangle2.java
• triangle3.java
• triangle4.java
• diamond.java

You are to submit these source code files electronically and with a printed copy (I need to have paper to mark up and the digital file to run your program). Your five source files should be saved in a folder named hw2 and this folder should be copied copied into your folder in the class directory

`/classes/cs124/<your user name>/`

NOTE: Make sure you're actually copying it to this folder and not your own home folder at /afs/afs.hws.edu/home/<your user name>! This is an obvious source of confusion, and will lead you to think you've turned in your work to me when you haven't.

As in Homework #12, please respect my file name requests. I ask for precise names because it makes it easier for me to test your work. Thanks.

### Formatting Requirements

• Programs should be indented neatly and consistently: for a block inside of a { / }, indent a few spaces, and all statements at that level indented the same way.

• Each program must include at the top of the file a "header comment", giving the name of the program, its author, and the date of its creation (this is a standard practice in software development. For each file, your head should be in the following format:

```/*
*  triangle1.java   < or whatever the name of the program is >
*  CS 124, Fall 2014
*  Assignment #2
*  < the date, in the format you find most delightful >
*
*/```

### General

• Your code for both programs must be syntactically and semantically correct, which means that it has to compile successfully.

I cannot emphasize this enough. Any of these five programs that cannot be successfully compiled with javac will receive no credit. If you run into any trouble here, please ask me for help, which I am always happy to give (as are the TAs). I know this seems draconian, but I promise you'll thank me for it later.

• Naturally, your code for both programs must also be behaviorally correct, which means that it should print the right output in the right format. However, partial credit will be given for partial solutions. At a minimum, be sure to test on the examples you planned beforehand.

John H. E. Lasseter