Title: Algorithms
1Algorithms
- Series of mathematical or variable manipulations
- Integer a,b,c
- a 12
- b 22
- c a b (what is c?) 12 22 264
2An example.
- Describe how to tie a shoe
- How to dial a phone
- Give a pill to a dog
3Programs Complicated, Concatenated Algorithms
- integer Total, LoopCounter
- Total 0
- LoopCounter 0
- Start
- Add 2 to Total
- Add 1 to LoopCounter
- Go Back to Start Until Total 12
- What is LoopCounter?
Total 2 4 6 8 10 12
LoopCounter 1 2 3 4 5 6
done
test for Total12, done here
4Written in Code
- int LoopCounter, Total
- LoopCounter 0
- Total 0
- while (Total lt 12)
-
- Total Total 2
- LoopCounter LoopCounter 1
-
semicolon
no semicolon (because brackets follow)
open bracket
odd-looking algebra
close bracket
5A computer Program
public class Lab2MainClass public static void
main( String args ) . your
code here .
- A computer file.
- In your Lab work area.
- Written in an editor.
- filename.java
- gt mkdir Lab2
- gt cd Lab2
- /Lab2gt pico Lab2MainClass.java
6Unix vs. Java
- Unix
- - At the terminal prompt
- Conversations with the computer to build files
- clear
- ls
- set prompt gt
- mkdir
- cd
- cd directoryName
- pico filename
- rm filename
- Java
- A program that runs
- In a file
- not Unix
- public class name
- public static void main
- - brackets
7Basic Java Program Framework
- public class Lab2MainClass
-
- public void someBehavior( input variables )
-
-
-
your main program name
brackets ALWAYS paired
8Comments
- included in your program
- read by humans, not the computer
- used to explain what you intend the code to do
9Commenting code
- / anything between delimiters /
- // anything after two slashes
- Header - comments at the beginning of a program
- // one line only
- /
- CS113
- Author Harry Truman
- Date September 7, 2010
- /
10Readability
- Indentation
- Skipped lines
- Brackets
- Comments
- Pages Functional paragraphs
- Meaningful variable names
11Java Commands
- go in the brackets
- tells the computer what to do, one step at a time
- mathematical, algorithmic
- follows a strict syntax
12example
- public class MyProgram
-
- public void test( )
-
- int X
- X 14
- System.out.println( X )
-
-
- on the computer
- gt javac MyProgram.java
- gt java MyProgram
- 14
- gt
13Display
- System.out.println( anything in quotes )
- System.out.println( anyVariable )
14The Assignment Statement
- public void name ( )
-
- int A
- A 4
-
- NOT A equals 4
- instead A is assigned the value 4
semicolon, brackets, main must be Java
15the Assignment
- int x
- x 14
- x x 1
- what is x?
- ( same as x which is a java shortcut )
Java
16Keywords vs. myWords
- Keywords are Java-defined
- public
- static
- void
- main
- String
- args
- class
- int
- double
- many more
- variables, file names are your choice
- new class names
- new method names
- greeting
- Lab2MainClass
- x, y, z
- myName
- Convention
- lowerUpper
17Structure of a class (well stop calling it a
program)
- // introductory comments
- public class className extends Parent
-
- public outputs name ( inputs )
-
- // variables
- // algorithms
- // outputs (i.e. System.out.println( )
) - // end method
- // end class
18A very simple program contained in file
AnyName.java
- public class AnyName
-
- public static void main(String args )
-
- System.out.println("The main method in the
AnyName class") -
-
19Writing a Java Class
- Write the main class, in a file with the same
name - public class AnyName in AnyName.java
- must contain a
- public static void main (String args)
- Compile it javac AnyName.java
- produces AnyName.class
- 4. Run the program java AnyName
20Run Java Programs within a terminal window
21Alternation if, else
- Integer x
- String y
- If (x 0)
- y X is zero
- else
- y X is not zero
no semicolon must be English
22Proper Java
- int x
- String displayText
- if (x 0) // note comparison equal
-
- displayText X is zero
-
- else
-
- displayText X is not zero
- // end if x is zero
- System.out.println( displayText )
optional
23- public class Factorialpublic static void main(
String args ) long Product 1 //
holds the running factorial int FactNum
25 // number to find the factorial of int
x 0 // increment variable while ( x lt
FactNum ) x x1 Product
Product x System.out.println( x "
factorial " Product ) if (Product lt
0) System.out.println("Err
or! Number too large.") System.exit(
0 ) // end while loop
// end main // end class
24New stuff
- long Product 1
- - bigger int
- System.out.println( x " factorial " Product
) - - concatenate Strings
- System.exit( 0 ) - stop the program
25casting
- We want to avoid mixing math operations between
incompatible types - What happens when we WANT to multiply a double
an int? - Cast
- ( double ) x changes x temporarily to a
double - ( int ) product changes product temporarily to
an int
26public static
- public can be used throughout the program
- static guaranteed unique name
27A Java Method
- Methods are often called routines, subroutines,
functions or procedures - A single function from start to finish
- Surrounded by curly-brackets ...
- Can be used many times (called) from all over
- Allows intelligent Transfer Of Control
- Many methods and variables are collected into a
class - A Class is a Java program that does one job, and
draws on many methods and variables to accomplish
it. Classes eventually become objects
28The main method
- public class Lab2MainClass
-
- public static void main( String args)
-
- String Greeting
- Greeting Hello
- System.out.println( Greeting )
-
29public static void ExampleMethod( )
System.out.println(Hello)
30Using (Calling) a method
- in main ExampleMethod( )
- public class Lab2MainClass
-
- public static void main( String args)
-
- ExampleMethod( )
-
- public static void ExampleMethod( )
-
- System.out.println(Hello)
-
-
Note parenthesis
31Our First Complete Java Class (i.e. collection of
Methods and variables).
class
public class SimpleClassProgram public
static String Greeting public static
void main( String args )
sayHello( ) System.out.println(
done ) public static void
sayHello( ) Greeting
Hello System.out.println( Greeting )
public variables
method1main
call
return
method2
32public static
- variables that are public static can be used by
any method. - methods that are public static can be called from
any method (not just main), and can be used by
other programmers.
33using another authors public class and method
- import javax.swing.JOptionPane
- // JOptionPane has many useful methods
- in main
- String userInput
- userInput JOptionPane.showInputDialog( type
something)
used by method
sent from method
34Exchanging values with main
- methods can return the result of a calculation
(as a service to main) - public static int addNumbers( ) // not void
-
- int x, y, z
- x 1
- y 2
- z x y
- return( z )
-
35Calling a method that returns a value
- public static void main( String args)
-
- int b
- b addNumbers( ) // note b
- System.out.println( b )
-
36Java Dot Notation
- Used to separate larger categories from detailed,
smaller instances (more when we study Objects) - System.out.println( )
- System is a library of code someone else wrote
(imported), a large collection of classes and
methods - out is a subset of System, as opposed to in, a
class - println is a specific method in the out subset
of the System library of programs
37import statement
- import javax.swing.JOptionPane
- public class myClass
- .....
- javax - a library of programs
- swing - a library within a library
- JOptionPane - a class (has methods we can use)
38Terms
- Class a type, like int or boolean or float,
that designates a conglomerate of other types,
variables, code snippets (method), anything,
always regarded as a whole. A collection of
methods and variables (best studied after
Objects) - Public within a class, free and unrestricted
access - Static within a class, there will only be one
method or variable with this name. - Void a method with no output back to the main
39More Terms
- main always the beginning of a (concatenated)
program. The computer runs this as the first
method of a program. - String args human readable stuff (more later)
40Structure of a Variable Type
- type VariableName
- examples
- int x,y,z
- String Greeting
-
- used by all methods within a class
- public static double myAccountBalance
41Structure of a Method
- domain static return type Name ( )
-
- / useful code here /
- // matching, closing brackets
- example
- public static void main (String args )
-
-
42How to call a method
- Calling a void method
- MyMethod( )
- Calling a method with a return int variable -
- i.e. public static int MyMethod( )
-
- return(6)
-
- from main
- int LocalVariable
- LocalVariable MyMethod( )
-
43The main method
- The computer only runs the main method.
- All support methods must be called from main.
- The computer jumps to the support methods as they
are called, runs them, then returns. - At the closing bracket of main, the computer
stops.
44Example in file NewClass.java
- public class NewClass
-
- public static String signOn
- public static boolean testFlag
- public static void main( String args )
-
- testFlag MyMethod( )
- if (testFlag true)
-
- System.out.println(signOn)
- System.out.println(testFlag)
-
-
- public static boolean MyMethod( )
-
- signOn Please Log In
- return( true )
45Compiling and running
- gt javac NewClass.java
- gt ls
- NewClass.class NewClass.java
- gt java NewClass
- Please Log In
- true
46Summary
- Three operations (so far)
- Concatenation
- Alternation if / else
- Transfer of Control methods
- one to go Iteration
- Methods and Variables