In this IB Computer Science lesson you will be learning about:
Computer programs work by manipulating data through user input or mathematical calculations.
There are different types of data that we can get from the keyboard or use in our programs. Java gives different types of data the following names
int – Positive and Negative integers (whole numbers)
double – Positive and Negative real (decimal numbers)
boolean – A true or false value
char – A single Character
String – a set of characters joined together (word or sentence)
During a programs operation data must be stored and manipulated in the computer’s memory. The computer needs to know how much space to allocate in its memory every time you read data from the keyboard or do a calculation. Each one of the data types above will take a different amount of memory so you must think and plan ahead about what types of data you will be using in your program.
Java doesn’t let you as the programmer access memory locations directly, it forces you to define a variable to represent that data location. A variable in computer science is exactly like a variable in math. It is a letter or word that is used to represent a piece of data.
You should follow the naming conventions for variables outlined below
In order to use a variable, you must declare the type and the name of a variable before you can use it in your program
In Java, start with the data type followed by the variable name, and ending with a semicolon.
It is also possible to declare multiple variables of the same type on a single line by starting with the data type, then listing the variables separated by commas, and ending with a semicolon.
To assign a value to a variable: start with the variable name, then an equal sign, followed by the value you wish to assign, end with a semicolon.
Variables must be assigned the same of type of value that they were declared to hold. This is because when you declare a variable it creates a certain amount of space in the computers memory for that piece of data.
There is one exception:
Notice that Strings are assigned values inside a set of “ “ double quotation marks and characters are assigned values inside a set of ‘ ‘ quotation marks
It is also possible to assign a value to a variable at the same time it is declared
Sometimes we have to wait to assign values to variables because they will be entered by the user when the program runs.
Input is actually a very complicated aspect for computer programming languages. There are many different ways to do accomplish these tasks in Java. The standard way to do this is using methods contained inside the Java IO Library package.
In Java, a source of input data is called an input stream and the output data is called an output stream. Think of these streams like this:
You have already seen one system stream – System.out in the last lesson
The system stream that deals with input from the keyboard is called System.in
The data a keyboard sends to a program is character data, even when the characters include the digits ‘0’ through ‘9’.
In order to do this with Java you need to create a link in software to the System.in data stream. The class with the methods to do this is called InputStreamReader
A typical line to create this link would look as follows:
InputStreamReader inStream = new InputStreamReader (System.in ) ;
This line constructs an InputStreamReader object and gives it the name inStream
Now we need a way to actually read in the data that is actually typed into the keyboard
To make this task as simple as possible we will use a class called BufferedReader to read in the data from the input stream we just linked to.
A typical line would look something like this
BufferedReader input = new BufferedReader(inStream);
This line creates a BufferedReader object named input that links to the input stream object inStream created before.
Think of this process as an assembly line:
Putting that all together in code will like this.
You will notice I had to add a couple of lines of code
The import statement loads the InputStreamReader and BufferedReader class into the program
The throws IOException tells java to ignore its error checking on user input when the program is running.
A typical line to read in data would be
dataIn = input.readLine();
Here dataIn would be a String variable that was created and input is the BufferedReader object that you created earlier. ALL data read from the keyboard using this method is treated as a STRING
Example:
Let’s write a program that asks the user to enter their first name and then enter their last name and we will store that data in memory
Programming Strategy:
What if you wanted to read in a number from the keyboard instead
Let’s write a program that ask the user to enter an integer value and a decimal value
Programming Strategy:
This seems like a lot of code to write for such a simple task, there are a few shortcuts we can take to reduce the number of lines of code
Java works a lot like BEDMAS in Math Class, it performs internal brackets first and works outward
The following example illustrates how to print the value of variables to the screen
The following example shows how to output both text and variables to the screen at the same time
The plus symbol in the print statements is performing a String Concatenation. Basically its combining numerical data and String data together to form a longer String.
Check out our programing in python courses that focus on high school level coding.
These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions
Return To International Baccalaureate Computer Science Main Page