C Chegg.com x C Chegg Study | Guided Solutior x ork #3 courses/328018/assignment
ID: 3750837 • Letter: C
Question
C Chegg.com x C Chegg Study | Guided Solutior x ork #3 courses/328018/assignments/3168404 Steps: (3 points) Create anew project in Eclipse (like how you did with Homework #1) but name this one "Homework3" 1) 2) (2 points) Create a package named "Main" for the project (also like in Homework #1) 3) (2 points) Create a Java source code file named "Main.java" (also like in Homework #1) 4) (2 points) Open "Main java" in your Code Editor window and copy /paste the boilerplate code from the ..Hello, World" assignment into your "Main Java" file in Homework #3. (On Windows, Ctrl + C is copy and Ctri+ V is paste) 5) (I points) Remove the "System.out printin"Hello, World...)" line from the boilerplate code so you start with an empty "main" method 6) (2 points) Declare an integer variable named "i" and initialize it to 0 7) (3 points) Declare a constant integer named max" and initialize it with a value of 10 8) (12 points) Create a "while" loop that evaluates as long as "i" is less than "max" 9) (I points) Add the line "System.out.printin(Integer.toString()." (don't type quotation marks!) inside the while loop brackets 10) (2 points) Underneath the previous line, use a Unary operator to increment "" by I 11) (5 points) Run the program and observe the console. Observe what the program does and add comments above the while loop code explaining to me what the while loop does (comments are done using either 1) // on each line OR 2)/ Comment between these! that your code works by running it and viewing the results in console) code works by running it and viewing the results in console) 12) (8 points) Below that, create the same routine but using the "do/ while" looping style (verify 13)(7 points) Below that, create the same routine but using the "for looping style (verify that your Q Search EAB SafesearchExplanation / Answer
package Main;
public class Main {
public static void main(String[] args) {
int i = 0;
final int max = 10;
/*
* ------WHILE LOOP-------
* While loop iterates 10 times by printing the value of the i.
* The value of i is converted from integer to String and is printed.
* In each iteration the value of i is incremented by 1 using unary operator.
* While loop iterates until the value of i is less than value of max.
* Once the value of i is equal to or greater than the value of max, loop stops iterating i.e. the condition is failed so it comes out of the loop
*/
System.out.println("----while lopp----");
while(i<max) {
System.out.println(Integer.toString(i));
i++;
}
/*
* -----Do-WHILE LOOP-----
*
* In the while loop above, since the value of i was reached 10 after the last iteration.
* In the do-while loop, it just prints the value of the i as 10 for the first iteration and comes out of the loop in the
* next iteration as the value of i is more than value of max. In do-while loop the condition is checked at the end so the
* first iteration is always executed.
*/
System.out.println("---do-while loop----");
do {
System.out.println(Integer.toString(i));
i++;
}while(i<max);
/*
* -----FOR LOOP-----
* the syntax of for loop is for(initialization statement; condition statement; update statement).
* so here we are reinitializing the value of i to 0 and iterating it until the value of i is less than value of max.
* the output is different as compared to output of while loop because in for loop we are incrementing the value of i by 1
* here we are incrementing the value of i by 2 i.e. once after printing the value of i and once in the for loop.
* So in for loop values of i are printed as even numbers.
*
*/
System.out.println("----for loop-----");
for(i=0;i<max;i++) {
System.out.println(Integer.toString(i));
i++;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.