banner



How To Clear Bluej Terminal Window

In this example, we want to explore how one writes and compiles a more complicated program than the one discussed for programming assignment 1.�� In this example, we will try and implement programming exercise 2.6 on page 196.�� In this assignment one wants to ask the user to give three integers and one wants to output the average of the three numbers.(The program asks for 5, but we will simplify it to 3. Again, when one clicks on the �Bluej� icon, one will get:

As discussed in the first programming assignment, one must create a new project, which I have called �Program2�.I have filled in the �readme.txt� file as follows:

------------------------------------------------------------------------

This is the project README file. Here, you should describe your project.

Tell the reader (someone who does not know anything about this project)

all he/she needs to know. The comments should usually include at least:

------------------------------------------------------------------------

PROJECT TITLE: Average

PURPOSE OF PROJECT: To compute the average of three integers

VERSION or DATE:July 26, 2006

HOW TO START THIS PROJECT:Right click on the "FindAverage" class.

AUTHORS:your name here

USER INSTRUCTIONS: Type in three numbers, each followed by clicking on the �OK�

I then create the new classes �FindAverage� using the �New Class� button:

Then type in the name of FindAverage� in the box with the �Class� Radiobutton clicked.(We will talk about some of the other choice later in the quarter.)One will get:

I have right-clicked on the box of FindAverage to get the menu show above. I will choose Open Editor as my choice and it will open up the file.Inside it I will find a prototype class which I remove. I then type in the progam. I have typed in below the program:

Next one must compile the program to make sure there are no errors.To do this, just click on the �compile� button in the upper right hand corner.�� However, you will almost never have a program without syntax errors the first time you type in a class.Even if copying a program directly from the book, one will usually make some typos.If you do get errors, then you must correct them.�� For example, in the above code I get the following error when I try to compile it:

An error message is generated.The shaded line indicates in which line the error occurred.Here, it is saying it expected to find a �;�, which indicates the end of a statement, and it did not find it.I forgot to put in the �;� after the last Integer.parseInt.So when I correct this by typing in the missing �;� and after I click the �Compile� button again, the following appears:

So, there are no compiler (syntax) errors for this class.(A syntax error is an error in the grammar of the language.It means that one has not obeyed the rules of grammar of the language. One of the rules of Java is that a statement must be terminated by a �;�.)Notice that the FindAverage box in BlueJ no longer has cross-hatched lines through it. Notice that before I compiled FindAverage , it appeared with cross-hatched lines through it.(See the second figure from the top.)When it no longer has cross-hatched lines through it, this means that it has been compiled correctly.

If you do not understand the compiler error message given, you can click on the �?� button.This sometimes gives an explanation of the error.Some times it will report no information given and just repeat the error message.If you do not know what to do after the error message, you should ask the tutor in the labs what to do.If you are at home, send a �zip file� to me with the contents of the project and I will try and resolve it.

To run the program one must get back to the project window:

It will be one of the tabs at the bottom of the screen.(It is the one with the �Blue J�� in the picture above.)Then to run the program, one �right clicks� on the class �FindAverage�, yielding:

One highlights the �void main( args)� by putting the cursor over it:

When one clicks on this highlighted item, one gets the following:

One just clicks on the �Ok� button, which tells the machine to run the program.When one does this, one gets the following:

Note: Sometimes one can not see the Dialog Box, because it is behind the project window.If you do not see the Dialog Box, then close the project window by clicking on the �-� box (which is above the �Help� label in the upper right hand corner) to see if the Dialog Box is hidden behind the project window.

The program is now running and it has output the Dialog Box, which is waiting for the user to type in some input.It has prompted the user to type in a number.One types in a number:

After typing in the number �1� and clicking on the �OK� key, one gets the second Dialog Box:

(This time the Dialog Box should be in front of the project window.).We type in a �2�:

(We note that rather than clicking on the �OK� button, one can just hit the �Enter� key on the keyboard after typing in the �2�.)After clicking on the �OK� button, the third Dialog Box appears:

We type in a �4�:

But after clicking the �OK� box, we get the following output:

Opps, it did not work!The answer has appeared in the �Terminal Window�.(The �Terminal Window� is where the �System.out.println� prints its output.)This is called a semantic error.That is to say it is an error in meaning.A syntax error is an error in grammar: you have not followed the rules of the language.These are fairly easy to correct because the compiler tells you the error and where it occurs.

A semantic error is an error in the logic of the program (usually referred to as a �bug�).So we must re-look at the program.We see that rather than printing out the average, the program has just printed out the string �(a+b+c)/3�.The mistake is that we want the average of a, b, and c to be computed and not the string of �(a+b+c)/3�.If we look at the example of the program listing on page 74 of the text, we see our error.We should have had the � after the �:� and not after the �3�.So we go back to the source code of the class FindAverage (We get there by double clicking on the FindAverage class in the project window. See the picture above this):

We then change the position of the �to be placed after the �: � :

Notice the �+ (a + b + c)/3� is no longer green. (This indicates that it is no longer part of the String literal.)

We click on the �Compile� button again.(Every time we change a line of code in a program, we must re-compile it.).�� There are no errors:

We return to the project window.We can do this one of two ways: (1) Click on the �Close� button for the �Average� window or (2) click on the �BlueJ: p...� tab at the bottom of the screen.When we get back to the project window, we see the following:

Now we can run the program as before.Right click on the class �FindAverage� and click on the �OK� button.�� Again, the three dialog boxes appear, and we respond the same as before: �1�,�2�, and �4� for the three input numbers.Again the �Terminal Window� window appears with the answer:

Notice that the previous output is still there.This is the default for the terminal window. All previous output is kept there until it is explicitly removed.(Please see the last page of this document to see how to remove previous output.)

But it is still not exactly right.We wanted �2.3� (7/3) and we only got �2�.This is not quite an error but not quite correct either.The problem is that in Java (as in C++) when we divide integers by integers, the answer will also be an integer.So the remainder is thrown away. (See page 47 of the text for some examples of this.) To get a real number (called �float� or �double� in Java), we must convert the result into a float.There are two ways to do this, the easiest being to replace �3� by �3.0�.(See Exercise 4 on page 106 of the text for an example of doing this.)The other way is to cast the computation as a (double) where an example of this is on page 48 of the text.)So we now have the following code:

We must compile it again.After we do this, run the program (by clicking on the �main� tab and the �Ok� button), and give the input of �1�, �2�, and �4�, respectively.We then get the following output:

The output is now correct.(The last 5 at the end is just an artifact of both Java and the machine it is running on.)If we wanted just to get �2.3� or �2.33�, we would have to use the formatting as discussed in on pages 124-126.)

If one did not want the results from previous executions of the program to appear in the terminal window, one clicks on the �Options� menu tab, and one will have:

Then choose the �Clear� item.(This should be done before running the program.)

If one does not like the output of 2.3333333333333335 and would like a nicer representation, the class DecimalFormat allows us to do that.An object of DecimalFormat lets one choose the formatted output that one wants.For example, if one wants an output of 3 decimal places to the right of the decimal point and at least one digit to the left of the decimal point, its format is �0.###�.The �0� means that a digit must be place there, and the �#� means that one must put a digit there, but only 3 digits will be put there unless some necessary accuracy will be lost.E.g., if one creates the format of �####.##�, then one wants 4 digits to the left of the decimal point, and two to the left.Thus all of the output will be aligned this way.So a 3 digit number will take 4 places to store it, with the 4 digit being a blank space. However, if one wanted to put a number which exceeded 4 digits to the left of the decimal point (e.g., 12231), then the field would be expanded to output this number. One would not want only the �2231� to be printed.So we modify our program as follows:

We import java.text.DecimalFormat , which is where the class DecimalFormat is stored.Then we create an object of type DecimalFormat , with the output of 3 places to the right of the decimal point, and at least one digit to the left.(So if the average was less than zero, a zero would be put there.).When we run the above program, we get the output of:

Note that DecimalFormat is a special case (a subclass) of NumberFormat.An example of NumberFormat is used to get an output with currency, e.g., $10.31.

How To Clear Bluej Terminal Window

Source: https://condor.depaul.edu/ggordon/courses/224/211doc/UsingBlueJ3.htm

Posted by: wakefieldthedis1939.blogspot.com

0 Response to "How To Clear Bluej Terminal Window"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel