Three most primary forms of loops: the while loop, the do..

Assignment Help JAVA Programming
Reference no: EM13164948

explored the world of loops we have accepted the three most primary forms of loops: the while loop, the do...while loop (even though Python does not use this loop), and the for loop. The for loop has approximately three primary implementations that will be reviewed in the next class period. To finish the chapter formally we will discuss the sentinel-controlled loop and the nested loop.

The Sentinel-Controlled Loop

Loops by themselves are indeterminate except for counting loops. That simply means that we will not know how many times the loop will execute. Sometimes there will be a programming situation in which we need the user to input as many data entries and then enter a special value to signify the end of the input session. The special value that is used is known as the sentinel. The goal of the loop is to execute the loop until that one particular sentinel value is executed. Our first approach would be to program like this.

print("Welcome to this accumulating program")

SENTINEL = -555

entry = 0
total = 0

while entry != SENTINEL:
entry = int(input("Enter a number (Enter -555 to quit): ")
total += entry

print("The total of the numbers entered: ", total)

If you were to program this and execute this, you would see one small problem. Even though it is syntactically correct (to my knowledge), the total will always be short by 555. Why? The sentinel value will have to be accounted for and added back to the total. In my estimation and that of programming experts everywhere, this is not a great programming practice. This also defeats the purpose of using it effectively.

The best way to use sentinel-controlled loops is to have the sentinel on be concerned with stopping the loop but not being a part of the data. In fact (in type disciplined languages) the sentinel should be of the data type but not a part of the data set. Since, for this course, we use Python, we can get away with even a string for the sentinel [even though that is highly unsafe]. Let's revise the above code to understand what should take place.

print("Welcome to this accumulating program")

SENTINEL = -555

total = 0
entry = int(input("Enter a number (Enter -555 to quit): ")

while entry != SENTINEL:
total += entry

entry = int(input("Enter another number (Enter -555to quit): ")

print ("The total of the numbers entered: ", total)

This programming approach is a much more disciplined approach. First the code requires the user to input the first entry before the loop. This is known as the priming input. If the first entry is the sentinel, the loop will not execute, otherwise, the first entry is processed and then all remaining inputs are required inside the loop. I call this the looping input. In this program the limitation is that -555 (or whatever you set the sentinel to) cannot be processed as data. It ends the loop. It is best when choosing a sentinel to pick a value that is far from the data set.

Your task:

Take this code and keep count of how many entries (inputs the user makes). Without trying to use arrays [since I know that you will search Google], display the average of the entries, the highest value entered and the lowest value entered.

Nested Loops

Loops are conditional structures that test a condition, execute a set of statements and then recheck the condition to see if it still exists. You are to permitted to nest loops like you can nest decisions but you have to be aware that there is a different outcome when you nest loops. Since a loop is inside another loop, the complete execution of the inner has to occur before the iteration of the outer loop. Think about minutes and seconds. We have to count 60 seconds before we can get one minute. We have to count another 60 seconds to get to two minutes. After 60 minute iterations we have accomplished an hour iteration.

for hours in range (24):
for minutes in range (60):
for seconds in range (60):
print (hours, " : ", minutes, " : ", seconds)

Now write this program and watch the execution. You should be impressed at how many executions can occur in just four lines of code. In fact that was 24 * 60 * 60 -> 86,400 executions. WOW! You should also notice how the seconds increase through all its executions before one minute iteration.

Your next task:

When I was younger we used to have composition note books that had multiplication tables that ran from 1 through 12. So you would see:

1
-----------
1 x 1 = 1
1 x 2 = 2
.
.
.
1 x 12 = 12

2
-----------
2 x 1 = 2
2 x 2 = 4
.
.
.
2 x 12 = 24
.
.
.
12
--------------
12 x 1 = 12
12 x 2 = 24
.
.
.

12 x 12 = 144

You are to use two nested loops to output the multiplication like the one printed above from 1 to 12 (both multiplicand and multiplier). Try to get your format to be like the output as well.

 

 

Reference no: EM13164948

Questions Cloud

How many grams of sodium carbonate present after reaction : A solution containing 3.50 of sodium carbonate is mixed with one containing 5.00 of silver nitrate. How many grams of sodium carbonate are present after reaction?
A method that takes a two-dimensional array : A method that takes a two-dimensional array of int's as a parameter and searches the array for the second parameter, returning true if the second parameter matches any of the integers in the array, and false otherwise.
How many grams of carbon dioxide are produced : how many grams of carbon dioxide are produced when 2.50g of sodium hydrogen carbnate reacts with excess citric acid according to the equation: 3NaHCO3 + H3C6H5O7 -> Na3C6H5O7 + 3CO2 + 3H2O.
Neurophysiological and evolutionary : Provide a framework for the theoretical concepts associated with Donald Hebb the Neurophysiological and Evolutionary write 200-300 cite references
Three most primary forms of loops: the while loop, the do.. : explored the world of loops we have accepted the three most primary forms of loops: the while loop, the do...while loop (even though Python does not use this loop), and the for loop. The for loop has approximately three primary implementations that w..
How many wheels are left over : A manufacturer of bicycles has 4819 wheels, 2305 frames, and 2250 handlebars. How many wheels are left over? How many frames are left over? How many handlebars are left over? Which part limits the production of bicycles?
Plan the testing required for the system : Plan the testing required for the system. You should consider unit, integration, and system testing in your test plan and determine who should participate in the testing.
The mode of a list of values is the score : For this project you will write a program to compute the arithmetic mean (average), median, and mode for the values read in from TopicFin.txt. The program results will be written to TopicFout.txt.
The program should ask the user to enter the student'' answer : Your program should store these correct answers in an array. (Store each question's correct answer in an element of a string array.) the program should ask the user to enter the student's answers for each of the 20 questions, which should be stored i..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Java program that will add the corresponding elements

Develop a Java program that will add the corresponding elements of two 1-dimensional arrays X and Y to produce the 1-dimensional array Z. Arrays X, Y, Z have the same dimension [M].

  Write java program to select pine for furniture company

Write down the Java program for the furniture company. Ask user to select P for Pine, O for Oak or M for Mahogany.

  Afterwards a way for the user to input

And so on and so forth then afterwards a way for the user to input that they finished a particular task on the list. After the user has input that they have finished a particular task the program should be print "Good Job!" or "Keep it up!"

  Program that prompts the user to enter the year and display

Write a program that prompts the user to enter the the year and first day of the year and displays the calendar table for the year on the console. For example , if the user entered the year 2013, and 2 for tuesday, January 1, 2013, your program shoul..

  Create your own date class

You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program:

  Create three classes for a customer

a travel manager and have a job of buying a travel package for a customer. You must create three classes for this programming challenge: Customer, Ticket, and TravelManagerDemo.

  Create an array of integers at random

create an array of integers at random with a range of 0 through 1000 in Java... any hints?

  Design java program to enter name in input dialog box

Design the program called MailingLabel.java Program must ask user to enter name and address in input dialog box with each component of address seperated by comma.

  Java class to accept a user-s hourly rate of pay

Write a class that accepts a user's hourly rate of pay and the number of hours worked. Display the user's gross pay, the withholding tax (15% of gross pay), and the net pay (gross pay - withholding).

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  Design an object-oriented java application

You are to design an object-oriented Java application to let the user play a dice game that uses two dices. The player bets on a value and the dices roll

  Create a web application for sheridan restaurant

Create a web application for Sheridan Restaurant Reviews that contains the following functionality - Display a table of all restaurants based on the city entered by the user

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd