Lab 4: Random Numbers, While Loops, and For Loops in Java, Exercises of Guiding Electromagnetic Systems

Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.

Typology: Exercises

2019/2020

Uploaded on 01/11/2020

samia-khan-2
samia-khan-2 🇵🇸

4.8

(4)

11 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
LAB 4
TOPICS:
Random numbers
While loops
For lops
Exercise 1:
In BlueJ, create a new project called Lab4
Create a class in that project called LotsaLoops
Write a method in that class called makeRand
In makeRand, create a Random object named gen
Set an integer variable num to a random value in the range 0 to 99, then print
it as follows:
From 0 to 99: xxx
Compile the code and run the method. Run it several times.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Lab 4: Random Numbers, While Loops, and For Loops in Java and more Exercises Guiding Electromagnetic Systems in PDF only on Docsity!

LAB 4

TOPICS:

• Random numbers

• While loops

• For lops

Exercise 1:

  • In BlueJ, create a new project called Lab
  • Create a class in that project called LotsaLoops
  • Write a method in that class called makeRand
  • In makeRand, create a Random object named gen
  • Set an integer variable num to a random value in the range 0 to 99, then print it as follows: From 0 to 99: xxx
  • Compile the code and run the method. Run it several times.

SOLUTION:

Figure 1

Exercise 2:

  • Compute and print values in the following ranges (inclusive) -- do the computation right in the println statement.
  • Remember, the general form is gen.nextInt(numberInRange) + startingPoint;

SOUTION:

From 1 to 20: xxx

From 5 to 10: xxx

Figure 6

From 700 to 800: xxx

Exercise 3:

  • Generate a random number in the range 1 to 1000 and store it in a variable named myVal
  • Then use a while loop to check to see if myVal is outside of the "good" range 400 to 500 (see if it's less than 400 or greater than 500)
  • If it is, print the bad value and generate another value in the range 1 to 1000.
  • Stay in the loop until you get a value in the good range (400 – 500)
  • Print the good value after you fall out of the loop.
  • Run the method multiple times.

SOLUTION:

OUTPUT:

Figure 11 Exercise 4:

  • Now write a for loop that executes 20 times. Use a control variable called count that runs from 1 to 20.
  • Each time through the loop, generate and print a random number in the range 50 to 100:

SOLUTION:

OUTPUT:

Exercise 6:

  • Now create a new method called moreForLoops (static, no parameters, no return value).
  • In this method, write a for loop that prints the integers from 40 to 50 all on the same line separated by a comma and a space: 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  • For now, it's ok to print a comma after the last value.
  • How many times does the loop body execute? ANS: It executes 11 times.

SOLUTION:

Figure 16

OUTPUT:

Exercise 8:

  • Add another blank println statement to start further output on a new line.
  • Now use a for loop to compute the sum of the integers from 70 to 362.
  • Each time through the loop, add one value to a running sum (declare sum and initialize it to 0 before the loop).
  • Print the results after the loop completes: The sum of the integers from 70 to 362 is xxx

SOLUTION:

Figure 20

OUTPUT: