Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Include at least 1 URL reference to resources used (APA format)

Needs to be 2 paragraphs long.

Read this section below and do some research to see other uses for the break statement.

What are some of these other uses?

What can happen if you don't use a break statement?

How about using a break in a switch statement, show how you can really have a problem if you skip the break statement in a switch.
This section supplements the loop material you studied in Chapter 4.

In Chapter 4, we introduced you to the use of the break statement inside a switch statement. It terminates the switch statement and transfers control to the next statement after the switch statement. In addition, you can use the break statement inside a while , do , or for loop. It does the same thing as when it's in a switch statement. The break terminates the immediately enclosing loop and transfers control to the next statement after the bottom of the loop. We say "immediately enclosing" because you can have a break that's nested inside multiple loops. The break gets associated with the loop that immediately surrounds it.

The DayTrader program in Figure 12.9 illustrates what's called "day trading." It's a form of gambling in which people buy and sell stock on the stock market every day in hopes of making money off shortterm stock movements. This program keeps track of a day trader's stock balance over a three-month period (for day 1 to 90). The original balance is $1,000. In our simple model, at the beginning of each day, the day trader retains half the initial balance in savings and invests the other half in the stock market. The money returned at the end of the day equals the investment times a random number between 0 and 2. Thus, the money returned ranges anywhere from zero to double the original investment. Each day, the day trader adds the money returned to the balance in savings. If the balance ever goes below $1 or above $5,000, the day trader quits. Before examining the break statement in Figure 12.9, look at the (day - 1) argument in the final printf statement. This is after the for loop, so the scope of day needs to be bigger than the scope of the for loop. That's why we declared it before the for loop with the other local variables.

But why did we subtract 1 in the printf statement? Because the day++ operation in the third compartment of the for loop header increments day one extra time, after the transaction that drives the balance to a terminating value. If we had forgotten to subtract 1 in the printf statement, that would be an off-by-one error.

Now look at the DayTrader program's break statement. If the balance ever gets outside the $1 to $5,000 range, program control jumps immediately to the next statement below the for loop. If you run the program several times, you'll see that sometimes this causes the loop to terminate before day reaches 90. You'll get a different result each time you run the program because this program uses Math.random to generate a random number in the range between 0.0 and 1.0.

Attachment:- Java.rar

Java, Programming

  • Category:- Java
  • Reference No.:- M92396226
  • Price:- $25

Priced at Now at $25, Verified Solution

Have any Question?


Related Questions in Java

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 ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assessment instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

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 ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

  • 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