Similarity between programming with variables and solving algebraic equations

A variable in a program is like the proverbial 'x' in Algebra. It is a symbol which can hold different values.

When you used to solve for 'x' in an algebraic equation, you used to write the expression involving 'x'.

Then you would mentally simplify the expression, write down the simplified expression.

Repeat the step till you reach the final value of 'x'.

Programming with variables is quite similar - the difference being that instead of you, the computer executes the steps.

Wow!  Isn't that cool?

But how does the computer know how to solve the variable.

Here comes your role as a programmer.

You need to provide all the steps needed to solve the expression in your program, so that the computer can execute those steps and present the result.

Its time to take a practical demonstration of the concepts discussed so far.  So we go back to solving the Order Capture Problem for your 9 p.m. Pizza Delivery.

What information I need to capture when some one rings for a Pizza delivery?

• I have to capture name of the Student and the room where Pizza has to be delivered.
• There are 10 dorms in the campus - D1 to D10.
• Each dorm has 4 wings(E, W, N, S) - D1-E to D10-S.
• Each wing has 10 rooms - D1-E-1 to D10-S-10.
• There are 400 rooms in total.

b> Which one of the 400 rooms do I need to deliver Pizza ?

Lets define a few variables to solve this problem.

1. Variable to store name, we call it "name" and it is of datatype String class. We will examine datatypes in next section. For the time being let us assume that String class datatype helps to store a named memory location which can store name of a person.

2. Variable to store Dorm number - let us call it "dorm_number" and its datatype is int. Int is a primitive datatype, but we will learn the concepts of primitive and user defined datatypes in a later section.

3. Variable to store Dorm Wing - let us call it "dorm_wing" and its datatype is char. Char is again a primitive datatype which can store characters like 'A', 'a', ...

4. Variable to store room number - let us call it "room_number" and its datatype is int.

Note: A variable's name is also referred to an Identifier. In other words you identify the variable in your program by its identifier.

So the variable declaration looks like:

String name;

int dorm_number;

char dorm_wing;

int room_number;

We have used 3 different datatypes - String, int and char to store these variables. The choice of these datatypes will be clear when we go through the section on Primitive Data Types.

Now we have to read inputs' of these 'variables' from the console window using a Java built in datatype called the Scanner Class. Just like the System.out  Class which we have been using to print text to the console or the screen, we use the Scanner class to take input from the console.

The concept of Class and  methods will be clear when we get into details of Object Oriented Programming. For the time being, let us use these as presented in the sample code.

The Scanner class is a class in java.util, which allows the user to read values of various types.

Methods in scanner  allow us to read in String or numeric values from the keyboard.

The following is a sample code for reading and storing the input 'variables'  and printing them back to the console so that we can take out printout of the order labels.

Primitive Data Types

Computers work on data.

There are innumerable types of data

• numeric (payment, discount, years,..)
• non-printable (e.g. bytecode generated by the Java Compiler)

just to name a few.

Programs handle data.

Conceptually any data handled by a computer can be expressed as a string of 0's and 1's like the following Binary representation "1011110110000000"

The smallest unit of data that can be addressed/accessed/retrieved/stored by a program is 8 bits of data called 1 Byte.

Now some data can be stored in 1 byte, while others need more than 1 byte. Lets see this with a series of examples for Numeric Datatypes:

Example Usage: Variable

byte 1 Byte Integers in the range of -128 to +127  Daily outside temperature
short 2 Bytes Integers in the range of -2^15 to +2^15 Total Kilometers covered by a car in a month
int 4 Bytes Integers in the range of -2^31 to +2^31 Distance between earth and moon
long 8 Bytes Integers in the range of -2^63 to +2^63 Number of hamburgers produced by a leading fast food chain in a year
float 4 Bytes Floating-point numbers in the range of ±3.4 x 10^-38 to ±3.4 x 10^38 ,with 7 digits of accuracy paycheck
double 8 Bytes Floating-point numbers in the range of ±1.7 x 10^-308 to ±1.7 x 10^308,with 15 digits of accuracy  atomic mass/distance or astronomic distance

The boolean Datatype

Used for variables which can hold one of the two possible values : true or false.

Boolean variables are  useful for evaluating conditions which can be either true or false.

The char Datatype

Variables defined as the char datatype is used for storing characters as we have seen in our Pizza Delivery Problem where the Wings of the Dorm are stored as a single character 'E' or 'W' or 'N' or 'S'. Note the use of single quotes in assigning values to char datatype variables.

Note: Even though you can access 1 Byte of data,  yet programmatically you can still modify a single bit in those 8 bits

Primitive Datatypes are the building blocks (as defined in any programming language) for storing and retrieving data

For more detailed understanding of the datatypes  please refer to

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html