Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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.

 

 

Java, Programming

  • Category:- Java
  • Reference No.:- M9455066

Have any Question?


Related Questions in Java

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assignment - method in our madnessthe emphasis for this

Assignment - "Method in our Madness" The emphasis for this assignment is methods with parameters. In preparation for this assignment, create a folder called Assign_3 for the DrJava projects for the assignment. A Cityscap ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As