This week we are reviewing the material we've covered
over the first part of the term.
- Activity #1
-
Create a program call Silly.
Have it:
- Print out the message This is just a silly program.
- Print out your name and A-number, identified as the author.
- Read a positive integer from the user (with a suitable prompt).
- Echo back that integer value.
This is just a silly program.
By Mark Young (A00000000)
Enter a positive integer: 10
10
- Activity #2
-
Revise the program so that it keeps reading positive integers
until the user enters a number less than or equal to zero.
Use the sentinel loop idiom described in class.
This is just a silly program.
By Mark Young (A00000000)
Enter a positive integer: 10
10
Enter another positive integer: 15
15
Enter another positive integer: 3
3
Enter another positive integer: 0
- Activity #3
-
Revise the program again
so that instead of just printing out the number,
it says whether the number is even or odd.
(Hint:
The number is even if its remainder when divided by two
is zero;
otherwise it is odd.)
This is just a silly program.
By Mark Young (A00000000)
Enter a positive integer: 10
10 is even
Enter another positive integer: 15
15 is odd
Enter another positive integer: 3
3 is odd
Enter another positive integer: 0
- Activity #4
-
Revise the program again.
For even numbers,
have the program print the numbers from 1 to half of the number entered.
This is just a silly program.
By Mark Young (A00000000)
Enter a positive integer: 10
10 is even
1 2 3 4 5
Enter another positive integer: 15
15 is odd
Enter another positive integer: 3
3 is odd
Enter another positive integer: 0
- Activity #5
-
Revise the program one more time.
For odd numbers,
have the program call a method named printTriangle.
The method says that it will print a triangle of the given height.
This is just a silly program.
By Mark Young (A00000000)
Enter a positive integer: 10
10 is even
1 2 3 4 5
Enter another positive integer: 15
15 is odd
I will print a triangle of height 15
Enter another positive integer: 3
3 is odd
I will print a triangle of height 3
Enter another positive integer: 0
- Activity #6
-
Last revision!
Have the method printTriangle
actually print the triangle of the given height.
This is just a silly program.
By Mark Young (A00000000)
Enter a positive integer: 10
10 is even
1 2 3 4 5
Enter another positive integer: 15
15 is odd
I will print a triangle of height 15
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
Enter another positive integer: 3
3 is odd
I will print a triangle of height 3
*
**
***
Enter another positive integer: 0
Hint:
The way to print a triangle is very similar to the way to print a rectangle
(see the program DrawRectangle).
There is only one difference:
the number of stars on each line
is not equal to the width of the rectangle;
it is equal to the number of the line
(one star on line 1,
two stars on line 2,
and so on).
So how does the code from DrawRectangle need to be changed?