Download presentation
Presentation is loading. Please wait.
Published byRudolf Anthony Modified over 6 years ago
1
Computer Programming Methodology Introduction to Java
COS 130 Computer Programming Methodology Introduction to Java
2
Sign in sheet
3
Algorithm Like a Recipe, but . . . Strictly defines the order of steps
“In mathematics and computing, an algorithm is a procedure (a finite set of well-defined instructions) for accomplishing some task which, given an initial state, will terminate in a defined end-state.” - Wikipedia Like a Recipe, but . . . Strictly defines the order of steps
4
Program “A computer program is a list of instructions
to be executed by the computer.” - Wikipedia Programs implement algorithms Programs are executable by “machines” Programs group instructions into a unit of work
5
Programs Exist in 2 Forms
Source Designed to be understood by people Letters, words Executable Designed to drive the computer Binary – 1’s and 0’s
6
Computing a 2 Step Process
Compilation Compiler Source Executable Execution Executable Computer Data In Data Out
7
Java Recent language developed by Sun
Primarily OO, procedural programming also supported (somewhat awkwardly) Most “popular” computer language Tiobe Software’s TPC index ( Write once, run everywhere
8
First Java Program A name you pick Capitalization important Must call file the same name public class Hello { public static void main(String args[]) System.out.println(“Hello World”); } Starts a java file All code inside class We will only use 1 Braces group code They always come in pairs Special Method Your program starts running here Make it just like this We will always have exactly one An instruction to print something Your program will do this Statements always end in a semicolon Or a brace
9
Literals Do not change while program is running
Must edit and re-compile program to change
10
Note: ‘A’ is not the same as “A”
Literals (cont.) String Literal Group of characters (letters) Inside quotes “Hello World” Character Literals One character (letter) Inside single quotes Example: ‘A’ Note: ‘A’ is not the same as “A”
11
Literals (cont) Integer Literals Floating Point Literals
Numbers with no decimal point Just by itself Example: 7 Floating Point Literals Numbers with a decimal point Example: 3.14 Note: 7 is not the same as 7.0 Note: 7 is not the same as “7” Note: 7 is not the same as ‘7’
12
Variable Declarations
Sets aside space in memory Labels the space with a name Declaration sets variable for a particular type – numbers, letters, etc. Once type is set it can not be changed Variable must be declared before it can be used
13
Name rules First letter: A-Z a-z Remaining letters: A-Z a-z 0-9
May be as long as you want Capitalization is important May not include spaces
14
int declaration int numStudents;
Declares a variable to hold an integer number Start variable names with a small letter This variable will be named numStudents It will set aside 1 32bit word of space It can hold values between -2,147,483,648 and +2,147,483,647
15
A Shortcut When we create a variable we can also set it to a particular value This is called “initialization” Example: int maxStudents = 30;
16
Integer Operators + Addition - Subtraction * Multiplication / Division
% Remainder
17
Division and Remainder
What is 5 / 9 ? What is 5 % 9 ? 5 What is 28 / 9 ? 3 What is 28 % 9 ? 1
18
Integer Expressions Use the integer operators on int variables and literals Operators are applied left to right Multiplication, Division, and Remainder are done before addition and subtraction What is * 30 ? 307
19
Integer Expression (cont)
To control the order things happen use parens “(“ and “)” What is (7 + 10) * 30 ? 510 Notice that parens do not imply multiplication What is (7 + 10) 30 ?
20
With Variables How many seats are left in a class?
maxStudents - curEnroll Where does the result go? How do we save the result? How do we use the result?
21
= Equal Sign? NO!
22
Assignment Operator = maxStudents = 30; Left side must be a variable
The value from the right is put in the spot in memory named on the left . . . 30 maxStudents
23
maxStudents – curEnroll
Example seatsLeft = maxStudents – curEnroll ; . . . 33 curEnroll . . . -3 -3 seatsLeft . . . 30 maxStudents
24
Example How to increase a number by 1 count = count + 1;
This is a very common case, be sure you understand it. This is mathematical nonsense.
25
More Data Types long double boolean
26
long What if I need a number that is too big to fit into an int?
long population; Use pretty much like an int long popNorthAmerica, popSouthAmerica, popNewWorld; . . . popNewWorld = popNorthAmerica + popSouthAmerica;
27
Caution An int will fit into a long
But, a long will not fit into an int long myLong; int myInt; . . . myLong = myInt + 17; // this is ok myInt = myLong + 1; // this will not work
28
What if I need a fraction?
float and double give you numbers that have places to the right of the decimal point float pi = 3.14; Using float is a bad idea double pi = 3.14; Use double instead!
29
Arithmetic with doubles
double area; double pi=3.14; double radius=7.5; ….. area = pi * (radius * radius);
30
Simple conversion No! No! No! int myInt=7; long myLong=77;
double myDouble=7.77; No! myInt = myLong; myLong = myInt; myInt = myDouble; myLong = myDouble; myDouble = myInt; myDouble = myLong; No! No!
31
Note: true is not the same as “true”
boolean A variable that can be true or false boolean isVegetarian; isVegetarian = true; isVegetarian = false; Note: true is not the same as “true”
32
Variable Declarations (Review)
Sets aside space in memory Labels the space with a name Declaration sets variable for a particular type – numbers, letters, etc. Once type is set it can not be changed Variable must be declared before it can be used
33
Comments Comments are a way to stick notes to other people (and your self) in your program code The compiler skips over comments Therefore, no trace of your comments goes into the executable
34
Comments (cont) Two slashes comments out the rest of the line Example:
numStudents = 30; // current enrollment
35
Comments (cont) A slash asterisk comments out every line until an asterisk slash is found Example: /* Current enrollment */ numStudents = 30;
36
Comments (cont) What if we do this: /* Current enrollment
numStudents = 30; */
37
Comments (cont) Use comments to put your name on your programs
Use comments to identify what your program does
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.