lecture_2_1:

Elementary Programming: variables

comp-prog/lecture_2_1
Matt Gottsacker
Marquette University High School
Last modified: 08.28.2019

Programming as problem solving

So far, we have used Java to print things out. We have used it for some calculations, but nothing we can't do on a calculator. Now, we will start learning concepts that will enable us to formulate problems as programming problems.

Programming as problem solving

What is an algorithm?

"An algorithm describes how a problem is solved by listing the actions that need to be taken and the order of their execution."

It is a set of instructions to help plan a program.

It is a recipe.

Variables


int week = 1;

double profit = 736.0;
                    

  • Variables hold information and correspond to memory locations.
  • The information they hold can change...usually
  • Each variable has a:
    • type
    • size
    • name
    • value

Variables: type and size

Java variables can be of the following types and sizes:

type size (bits)
byte 8
short 16
int 32
long 64
float 32
double 64

Variables: names

  • Use descriptive variable names. Long names are okay.
  • Lowercase variable names, unless more than one word. Then, combine all words into one, and capitalize the first letter of each subsequent word. For example:
    • camelCasedVariableName

Variables: values

Once it is declared, the value of a variable can change...


int week;

week = 1;
week = 2;
                        

Variables: values

...unless it is a constant.


final int MAX_WEEKS = 5;
                        


Constants also have different naming conventions.

  • Prefaced with final keyword.
  • All caps.
  • Underscores between words.

end_