An intro to programming - PowerPoint PPT Presentation

About This Presentation
Title:

An intro to programming

Description:

Make a new variable of type int and call it 'lastGrade' ... Use single quotes for char, double quotes for string ... Calculates the car's average miles/gallon ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 29
Provided by: matthew133
Category:

less

Transcript and Presenter's Notes

Title: An intro to programming


1
An intro to programming
  • The basic nitty-gritty thatll make you scratch
    your head and say, This is programming?

2
What is a program?
  • A program is computer coding that
  • Takes input
  • Performs some calculation on the input
  • Displays output

3
A program is a function!
  • Both take inputs and give outputs based on some
    rule
  • Both make calculations
  • Both take arguments

4
A program is a NOT function!
  • In a function, there is exactly one output for
    every input
  • Some computer programs will have different
    outputs for the same input
  • Example a virtual set of dice

5
The client
  • The user of the program is sometimes called the
    client
  • The client doesnt have to know whats going on
    inside the black box to use the program
  • Do you know exactly how Microsoft Word works?

6
Using outside sources
  • Many programs must consult outside sources to
    perform the correct calculations on their input

7
Writing programs
  • You need
  • Knowledge of a coding language
  • A compiler translates your language into
    machine code (0s and 1s the computer can
    understand)

8
What is pseudocode?
  • Pseudocode is programmers shorthand for actual
    code
  • It mimics actual programming languages
  • There are no rules of pseudocodeyet

9
A simple pseudocode program
A program that 1) asks the users age in human
years, 2) calculates the users age in dog years,
and 3) tells the user what that age is.
  • program DogYears
  • write How old are you?
  • let humanAge INPUT
  • let dogAge humanAge 7
  • write humanAge
  • end program

PROGRAM CONTROL STATEMENT ?
MISCELLANEOUS OUTPUT ?
INPUT ?
CALCULATION BASED ON INPUT ?
OUTPUT ?
PROGRAM CONTROL STATEMENT ?
10
A few good pseudocode rules
  • The name of the program begins with a capital
    letter
  • Each line ends in a semicolon
  • All variable names begin in lowercase
  • We use an asterisk () to represent
    multiplication (also use , -, /, )
  • program DogYears
  • write How old are you?
  • let humanAge INPUT
  • let dogAge humanAge 7
  • write humanAge
  • end program

11
Write your own pseudocode program
  • Write a program in pseudocode that
  • Asks the client what grade they just finished
  • Calculates how many years of high school the
    client has remaining.
  • Outputs this answer.

12
A sample program
  • program Graduation
  • write What grade did you just complete?
  • let lastGrade INPUT
  • let yearsLeft 12 lastGrade
  • write lastGrade
  • end program

13
Three parts of programs
  • Variables, to store data
  • Calculation statements, to handle input,
    calculations, and output
  • A user interface, which allows the client to use
    the program

14
Variables primitive types
  • Different types depending on the kind of data
  • In Java, some types includeboolean for true or
    falseint for integerschar for lettersdouble
    for real numbersstring for strings
  • These are called primitive types because they are
    built into the language

15
Variables - objects
  • In some languages, including Java, users can
    define their own variable types, called objects
  • An object is a collection of primitive types
  • Example an object representing a cat might
    contain a double variable for its weight, a
    string variable for its name, and a boolean
    variable for if its been neutered or spayed

16
Variables - objects
  • In some languages, including Java, users can
    define their own variable types, called objects
  • An object is a collection of primitive types
  • Example a string is an object that holds many
    char variables, so a string is any set of
    letters a word or a sentence

17
Java
  • Java was released in 1995 by Sun Microsystems.
  • It is based on the language C.
  • Java is platform-independent, object-oriented,
    and easy to use on the Internet.

18
Graduation in Java
  • import extra.
  • public class Graduation
  • public static void main(String args)
  • Std.out.println( "What grade did you just
    complete?" )
  • int lastGrade
  • lastGrade Std.in.readInt()
  • int yearsLeft
  • yearsLeft 12 - lastGrade
  • Std.out.println( "You have " yearsLeft "
    years of high school left.")

Write this
Make a new variable of type int and call it
lastGrade
Assign to lastGrade the value that the user
inputshope its an integer
Make a new variable of type int and call it
yearsLeft
Use calculations to assign lastGrade a value
based on yearsLeft
19
Declaring variables
  • We create variables for our program to use by
    declaring them
  • Declaring a variable is like saying, Computer,
    save some space in RAM for this variable
  • Declaring is done ONCE before using the variable
  • a variable does NOT assign a value to the variable

20
How to declare a variable
  • Write the variables type
  • Leave a space
  • Write the variables name (start with a lowercase
    letter, its good style)
  • Exampleschar faveLetterint daysdouble
    foodMassstring name

21
How to assign a value to a variable
  • Write the variables name
  • Write a single equals sign ()
  • Use single quotes for char, double quotes for
    string
  • Write the variables value and a semicolon
  • ExamplesfaveLetter qdays 42foodMass
    33.42name Matthew

22
Declaring and assigning at the same time
  • You can, if you choose, declare variables and
    assign values to them at the same time
  • Write the variables type, then its name, and
    then its value
  • Exampleschar faveLetter qint days
    42double foodMass 33.42string name
    Matthew

23
Input the easy way
  • Because input in Java is not easy, we use a
    package called extra that assists with input.
  • To input using extra
  • Declare a variable
  • Assign an input statement as its value
  • The input statement should begin with
    Std.in.readInt() reads an integerreadChar()
    reads a characterreadLine() reads a stringetc

24
Output always easy
  • To output, write Std.out.println()
  • In the parens, write a variable name, a string,
    or any combination of variables and strings
    connected with plus () signs

25
Input and output examples
  • int days
  • Days Std.in.readInt()
  • String name
  • Std.out.println(What is your name?)
  • Name Std.in.readLine()

26
An exercise
  • Write a program that
  • Calculates the number a miles has traveled during
    the last year
  • Calculates the cars average miles/gallon
  • You should ask the client for the average number
    of miles per day s/he drives and the total
    gallons of gas s/he uses in one year

27
A solution
  • import extra.
  • public class Miles
  • public static void main(String args)
  • Std.out.println( How many miles per day do you
    drive?" )
  • double milesPerDay
  • milesPerDay Std.in.readDouble()
  • Std.out.println( How many gallons of gas did
    you use last year? )
  • int gallonsUsed
  • gallonsUsed Std.in.readDouble()
  • double totalMiles milesPerDay 365
  • double avgMPG totalMiles / gallonsUsed
  • Std.out.println( "You drove totalMiles
    miles last year and averaged avgMPG
    miles per gallon.)

28
Another exercise
  • Write a program that
  • Calculates the amount of change you should
    receive when you go shopping and the type of
    bills you should receive it in
  • You should ask the client for the price of the
    article and the amount of cash s/he actually paid
Write a Comment
User Comments (0)
About PowerShow.com