Ch1 - Output   .   Ch2 - Sequence    .   Ch3 - Variables    .    Ch4 - Input

Ch5 - If decisions    .    Ch6 - While loops   .    Ch7 - Functions

JT Home Page


Chapter 4

Input - Output

Introduction

The task of the majority of programs is to read some input data and process it to give some output data (i.e. results). In this chapter, we will look at the simplest form of input-output ( I/O for short ) involving keyboard for input and screen for output. Here, we recap output, and cover input.

Output - print, printLine, alert

The print() function can display a string of characters enclosed in double-quote marks on the screen, as in:


print("You made an error - try again");

note the semicolon - it is a statement, so it needs a terminator.

Alternatively, we can use printLine(), which prints its data, then moves to the start of the next line. So:


	printLine("John");
	print("Smith");

would produce

John
Smith

A differnt kind of output can be performed via alert(), as in


alert("hello");

a box pops up with hello in it. The user clicks OK, signifying that they have seen the message, and are dismissing it.

Displaying results

What we really need is to be able to display the results of calculations - e.g. the values of variables and expressions.

Here is a complete example, which we will extend to include input.

Problem: A trailer is to carry 30 mountain bikes, each of which weigh 18.75 Kg. What is the total load?

Firstly,what type of output is required? In principle, a plain number answers the question, e.g:


	562.5

but a more meaningful format is:
	Total load is --- kg.

The actual numbers aren't important, because we are discussing layout, and anyway the numbers may change.

In JT, we might code this as follows:

The program shows variables being used with output. We can also use expressions (calculations), as in:

printLine(x+3);


Problem: Alter each print to printLine, and run the program. note the different output.

5.4 Input - getInt  ,  getDouble,    getString

To get some data from the user into a variable, we have getInt, getDouble, getString. We use them as follows:
int age=0;
age = getInt("How old are you?");

A box pops up, and the user can enter data. We must provide a suitable prompt, giving the user some idea of the input that is needed. We assign the value to a variable. This means that programs are more flexible - we can write them without knowing the values in advance.

If things are going wrong, and you wish to halt your program rather than enter some data, type quit

We will amend the bike program, making it request its data. We also ask for your name, to show getString in use:

Note that we could use alert to display the answer:


alert(totalWeight);

but in JT, an alert can only display a single item, hence the output is not explained by extra text. Alerts are mainly used for warnings and error messages - because the user can not ignore them.

Problems

Write a program which asks the user for the dimensions of a rectangle, and which then displays the area.

It should display the result in the form:


length is 2.0 cm
breadth is 3.1 cm
area is 6.2 cm sq.

Add an alert which says "Here are your results..."

Ch1 - Output   .   Ch2 - Sequence    .   Ch3 - Variables    .    Ch4 - Input

Ch5 - If decisions    .    Ch6 - While loops   .    Ch7 - Functions

JT Home Page