Algorithms - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Algorithms

Description:

human-readable text. always in quotations. Java: String greeting = 'Hello World' ... Readability. Indentation. Skipped lines. Brackets. Comments. Pages ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 47
Provided by: cseBu
Category:

less

Transcript and Presenter's Notes

Title: Algorithms


1
Algorithms
  • Series of mathematical or variable manipulations
  • Integer a,b,c
  • a 12
  • b 22
  • c a b (what is c?) 12 22 264

2
An example.
  • Describe how to tie a shoe
  • How to dial a phone
  • Give a pill to a dog

3
Programs 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
4
Written 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
5
A 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

6
Unix 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

7
Basic Java Program Framework
  • public class Lab2MainClass
  • public void someBehavior( input variables )

your main program name
brackets ALWAYS paired
8
Comments
  • included in your program
  • read by humans, not the computer
  • used to explain what you intend the code to do

9
Commenting 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
  • /

10
Readability
  • Indentation
  • Skipped lines
  • Brackets
  • Comments
  • Pages Functional paragraphs
  • Meaningful variable names

11
Java Commands
  • go in the brackets
  • tells the computer what to do, one step at a time
  • mathematical, algorithmic
  • follows a strict syntax

12
example
  • 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

13
Display
  • System.out.println( anything in quotes )
  • System.out.println( anyVariable )

14
The 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
15
the Assignment
  • int x
  • x 14
  • x x 1
  • what is x?
  • ( same as x which is a java shortcut )

Java
16
Keywords 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

17
Structure 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

18
A 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")

19
Writing 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

20
Run Java Programs within a terminal window
21
Alternation if, else
  • Integer x
  • String y
  • If (x 0)
  • y X is zero
  • else
  • y X is not zero

no semicolon must be English
22
Proper 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

24
New stuff
  • long Product 1
  • - bigger int
  • System.out.println( x " factorial " Product
    )
  • - concatenate Strings
  • System.exit( 0 ) - stop the program

25
casting
  • 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

26
public static
  • public can be used throughout the program
  • static guaranteed unique name

27
A 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

28
The main method
  • public class Lab2MainClass
  • public static void main( String args)
  • String Greeting
  • Greeting Hello
  • System.out.println( Greeting )

29
public static void ExampleMethod( )
System.out.println(Hello)
30
Using (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
31
Our 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
32
public 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.

33
using 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
34
Exchanging 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 )

35
Calling a method that returns a value
  • public static void main( String args)
  • int b
  • b addNumbers( ) // note b
  • System.out.println( b )

36
Java 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

37
import 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)

38
Terms
  • 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

39
More 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)

40
Structure of a Variable Type
  • type VariableName
  • examples
  • int x,y,z
  • String Greeting
  • used by all methods within a class
  • public static double myAccountBalance

41
Structure of a Method
  • domain static return type Name ( )
  • / useful code here /
  • // matching, closing brackets
  • example
  • public static void main (String args )

42
How 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( )

43
The 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.

44
Example 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 )

45
Compiling and running
  • gt javac NewClass.java
  • gt ls
  • NewClass.class NewClass.java
  • gt java NewClass
  • Please Log In
  • true

46
Summary
  • Three operations (so far)
  • Concatenation
  • Alternation if / else
  • Transfer of Control methods
  • one to go Iteration
  • Methods and Variables
Write a Comment
User Comments (0)
About PowerShow.com