For each question, first THINK it through. Use paper.
If uncertain, feel free to look online for help BEFORE programming anything.
Once set on an answer/solution, program it/type in the code. See what happens.
1. Write a Java class named Triangle that contains a method called draw() that displays the following pattern.
Design the program in such a way that it will be easy to enlarge the pattern to 10 rows and 19 columns.
Make sure your Provide a main(String[] args) within a program named TestTriangle to test your solution.
*
***
*****
*******
*********
2. What is printed after the following lines of code are executed?
(THINK ABOUT THE ANSWER... come up with an answer, then test it.)
int j = 0;
for(int i = 0; i < 100; i++)
j = j++;
System.out.println("The value of j is: " + j);
So why is this?
3. Given the following code, what is printed?
(DO NOT USE THE COMPUTER. Figure it out, test your answers.)
int end = Integer.MAX_VALUE;
int start = end - 100;
int loops = 1;
for(int i = start; i <= end; i++)
{
System.out.println("loop: " + loops);
loops++;
}
System.out.println("Value of loops: " + loops);
4. Given the following code, what is printed?
(DO NOT USE THE COMPUTER. Figure it out, test your answers.)
int end = Integer.MAX_VALUE;
int start = end - 100;
int sum1 = 0; int sum2 = 0; int sum3 = 0;
for(int i = start; i < end; i++)
{
System.out.println("loop: " + i);
sum1 += (i-start);
sum2 += (end - start);
sum3 += (end - i);
}
System.out.println("Value of sum1: " + sum1);
System.out.println("Value of sum2: " + sum2);
System.out.println("Value of sum3: " + sum3);