LAB 4: LOOPS. Loops are constructs that control repeated executions of a block of statements. The concept of looping is fundamental to programming. Java provides three types of loop statements: while loops, do-while loops, and for loops.

LAB SHEET 4.1: The while Loop ESTIMATED TIME

:

45 minutes

OBJECTIVE

:

To write program for executing statements repeatedly using a while loop.

REQUIREMENTS

:

JCreator 5.00 Pro

PROCEDURE

:

Read the question and write the program. Then run the program and get the correct output.

CONCLUSION

:

The while loop checks the loop-continuation-condition first. If the condition is true, the loop body is executed; if it is false, the loop terminates.

DISCUSSION / RESULT / EXERCISE (Conversion from miles to kilometers) Write a program that displays the following table (note that 1 mile is 1.609 kilometers): Miles 1 2 ... 9 10

Kilometers 1.609 3.218 14.481 16.090

1

LAB SHEET 4.2: The do-while Loop ESTIMATED TIME

:

45 minutes

OBJECTIVE

:

To write loops using do-while statements.

REQUIREMENTS

:

JCreator 5.00 Pro

PROCEDURE

:

Read the question and write the program. Then run the program and get the correct output.

CONCLUSION

:

The do-while loop is called posttest loop because the condition is checked after the loop body is executed.

DISCUSSION / RESULT / EXERCISE You have three identical prizes to give away and a pool of 10 finalists. The finalists are assigned numbers from 1 to 10. Write a program to randomly select the numbers of 3 finalists to receive a prize. Make sure not to pick the same number twice. For example, picking finalists 3, 6, 2 would be valid but picking 3, 3, 11 would be invalid because finalist number 3 is listed twice and 11 is not valid finalist number. Random number generation is not discussed in this course, but for this problem you can insert the following line of code to generate a random number between 1 and 10: int num = (int) (Math.random() * 10) +1;

2

LAB SHEET 4.3: The for Loop ESTIMATED TIME

:

45 minutes

OBJECTIVE

:

To write loops using for statements.

REQUIREMENTS

:

JCreator 5.00 Pro

PROCEDURE

:

Read the questions and write the program. Then run the program and get the correct output.

CONCLUSION

:

The for loop generally is used to execute a loop body a predictable number of times; the number is not determined by the loop body.

DISCUSSION / RESULT / EXERCISE (Displaying the ASCII character table) Write a program that prints the characters in the ASCII character table from ‘!’ to ‘~’. Print ten character per line. The ASCII table is shown in Appendix B.

3

LAB SHEET 4.4: Nested Loops ESTIMATED TIME

:

45 minutes

OBJECTIVE

:

To write nested loops.

REQUIREMENTS

:

JCreator 5.00 Pro

PROCEDURE

:

Read the question and write the program. Then run the program and get the correct output.

CONCLUSION

:

The nested for statement is used very often because it is ideally suited to process tabular data.

DISCUSSION / RESULT / EXERCISE (Math: combinations) Write a program that displays all possible combinations for picking two distinct numbers from integers 1 to 9: 1 2 1 3 ... 9 7 9 8

4

lab 1: introduction to programming

looping is fundamental to programming. Java provides three types of loop statements: while loops, do-while loops, and for loops. LAB SHEET 4.1: The while Loop. ESTIMATED TIME ... get the correct output. CONCLUSION. : The while loop checks the loop-continuation-condition first. If the condition is true, the loop body is ...

80KB Sizes 1 Downloads 211 Views

Recommend Documents

Introduction-To-Networks-Lab-Manual-V5-1-Lab-Companion.pdf ...
3. Page 3 of 3. Introduction-To-Networks-Lab-Manual-V5-1-Lab-Companion.pdf. Introduction-To-Networks-Lab-Manual-V5-1-Lab-Companion.pdf. Open. Extract.

Introduction to Lab 2
Sep 15, 2010 - http://www.it.uu.se/edu/course/homepage/realtid/ht10/lab2. Lab report ... Based on OSEK (industry standard for automotive embedded systems).

Introduction to Lab 2 (LEGO)
Sep 15, 2010 - Flash the custom firmware ... Custom FW using fwflash-jh. ▻ Original FW using fwflash- .... Application: Distance and touch sensor sensing. 1.

Introduction to Java Programming
LiveLab is a programming course assessment and management system. Students can .... B MySQL Tutorial. C Oracle Tutorial. D Microsoft Access Tutorial. E Introduction to Database Systems. F Relational Database Concept. G Database Design ...... In 1954,

Introduction to Java Programming
problem-driven complete revision new problems early console input hand trace box multidimensional arrays. Sudoku problem simplified basic GUI earlier .... T Networking Using Datagram Protocol. U Creating Internal ..... the outset, it is helpful to re

Lab#1
Work environment. • Logging on the lab PC: Linux. • Lab Server: zoltar.redes.upv.es netcat usage( see Appendix A). • connect as a client to zoltar.redes.upv.es o www.upv.es: echo (7) daytime (13). • use netcat for a full connection, both serv

Lab # 1
Solution is in page 2. Lab 3. Problem 1: Fashion factory ... First Semester: 2013-2014 bi ? 288 – 57.6

Introduction to Answer Set Programming
Carleton University. Answer Set Programming. F. Gagnon 08 ... In C++ and Java, both logic and control have to be specified: – Logic is usually the hard (and ...