Lesson 3 Computers and Programming Student Resources



Download 85.34 Kb.
Date29.01.2017
Size85.34 Kb.
AOIT Introduction to Programming

Lesson 3

Computers and Programming

Student Resources

Resource

Description

Student Resource 3.1

Worksheet: Converting a User Password to Binary Machine Representation

Student Resource 3.2

Reading: Program Execution Process

Student Resource 3.3

Worksheet: Steps in the Program Execution Process

Student Resource 3.4

Worksheet: Firstname Program in Python and Other Programming Languages

Student Resource 3.5

Reading: High-Level Programming Languages

Student Resource 3.6

Worksheet: High-Level Programming Languages

Student Resource 3.7

Writing Assignment: What I Know about Programming Languages

Student Resource 3.8

Worksheet: Computers and Programming Defining Format

Student Resource 3.9

Four-Quadrant Reflection: Knowing a Programming Language and Knowing How to Program

Student Resource 3.1

Worksheet: Converting a User Password to Binary Machine Representation

Student Names:_____________________________________________________ Date:___________

Directions: Working with a partner, use the conversion table below to convert a “strong” user password in text (for example, AtrV*496, which has a mixture of upper- and lowercase letters and a special character) into binary machine representation that could be read by a computer. Then exchange your machine representation with another team and have that team convert the machine representation back into text. Remember to write only the machine representation on this page so that you can give it to another team to “decode.”

Conversion Table for Translating Text to Machine Representation (and Vice Versa)

00100000 (blank character)

! 00100001

" 00100010

# 00100011

$ 00100100

% 00100101

& 00100110

' 00100111

( 00101000

) 00101001

* 00101010

+ 00101011

, 00101100

- 00101101

. 00101110

/ 00101111

0 00110000

1 00110001

2 00110010

3 00110011

4 00110100

5 00110101

6 00110110

7 00110111

8 00111000

9 00111001

: 00111010

; 00111011



< 00111100

= 00111101

> 00111110

? 00111111

@ 01000000

A 01000001

B 01000010

C 01000011

D 01000100

E 01000101

F 01000110

G 01000111

H 01001000

I 01001001

J 01001010

K 01001011

L 01001100

M 01001101

N 01001110

O 01001111

P 01010000

Q 01010001

R 01010010

S 01010011

T 01010100

U 01010101

V 01010110

W 01010111

X 01011000

Y 01011001

Z 01011010

[ 01011011

\ 01011100

] 01011101

^ 01011110

_ 01011111

` 01100000

a 01100001

b 01100010

c 01100011

d 01100100

e 01100101

f 01100110

g 01100111

h 01101000

i 01101001

j 01101010

k 01101011

l 01101100

m 01101101

n 01101110

o 01101111

p 01110000

q 01110001

r 01110010

s 01110011

t 01110100

u 01110101

v 01110110

w 01110111

x 01111000

y 01111001

z 01111010

{ 01111011

| 01111100

} 01111101

~ 01111110


Write the machine representation for a password here:



Student Resource 3.2

Reading: Program Execution Process



c:\users\mika\documents\my documents\pearson\2012\june\16\programming_lesson3_presentation_root_061512.jpg

In this presentation, we will use the Python Circle program to illustrate how a program runs from start to finish. This whole process is called the program execution process.



slide2

The execution process described here would be very similar but not precisely the same for other Python programs or programs written in other high-level languages (for example, in C or Java). The Circle program opens a drawing window and draws a red circle (both the drawing line and the fill are red).



slide3

To better picture the process about to be described, think for a minute about what you expect the visible results to be. That will help you visualize what is going on during each step of the process.



slide4

The execution process has been divided into a series of six steps. The key software and hardware components that participate in the steps are listed. The user selects Run > Run Module in the first step.

The Python shell has a menu that allows the user to run a Python program. By clicking Run Module, the user instructs Python to run the program that is in the IDLE window.

slide5

The Python shell calls the parser. The parser is an internal component that has a very important job. It reads in and parses the program. Parsing means that it reads the high-level code for semantic accuracy and breaks statements down into meaningful syntactic units. This is all in preparation for building an execution “table.”



slide6

Notice that the “if” decision statement (“If syntax error”) is shown in a diamond-shaped flowchart symbol.



slide7

If there are no syntax errors, the parser builds the execution table, which contains a description of the program’s statements, one by one. They are arranged so that the program can execute efficiently. The table contains information like the statement type. For example, it tells whether the statement is a function, an assignment statement, or print. If it is a function, it tells what the arguments are.



slide8

The executor is part of the Python interpreter. Its job is to execute (or run) the program. It uses the information in the execution table that the parser built. Based on this information, the executor calls the internal machine code functions one after the other, and these machine code functions run the program.



slide9

When the executor reaches the last statement, done(), it returns control to the shell. The shell goes into a wait state (which means it will no longer accept input from the keyboard). It will respond only to the drawing window being closed or to a manual shutdown command (for example, Ctrl+C).



slide10

After the program execution ends, the drawing window is closed. If the user reruns this program or opens another one and runs it, then another drawing window opens, and the sequence starts all over again.



slide11

Steps in the program execution process (a high-level view focusing on the Circle program):

Before the program execution starts, IDLE must be open, and the user must specify the program to be run (Circle).


  1. In the Python shell, using the mouse, the user selects Run > Run Module.

  2. The shell calls the parser, which reads in and parses the program.

  3. If the parser detects a syntax error, it stops running, returns an error message to the screen, and returns control to the shell.

  4. If there are no syntax errors, the parser builds the execution table, which contains a description of the statements, one by one, arranged so that the program can execute efficiently.

  5. Based on the information in the execution table, the executor calls the appropriate internal machine code functions (one by one) to run the program. It draws the circle.py red circle.

  6. When the executor reaches the last statement, done(), it returns control to the shell. The shell goes into a wait state (it will no longer accept input from the keyboard). It will respond only to the drawing window being closed or to a manual shutdown command (for example, Ctrl+C).

After the program execution ends, the drawing window is closed. If the user reruns this program or opens another one and runs it, another drawing window opens, and the sequence starts all over again.

Student Resource 3.3

Worksheet: Steps in the Program Execution Process

Student Name:_____________________________________________________ Date:____________

Directions: Each of the boxes below represents a step in the program execution process. Use this page to take notes about the steps during the presentation “Program Execution Process.” Then, using the information and the notes you took during the presentation, complete the second page of this worksheet. On that page, be sure to explain the steps of the program execution process using complete sentences. The first step has been done for you as an example.


Start



2. Read and parse program.



1. Run module.

3. If error, display error message and exit.

4. Build execution table.

6. End program and wait for drawing window to close.

5. Execute program.

End
Steps in the Program Execution Process

Describe each step in the program execution process in a complete sentence.

1.In the “Run module” step, the user selects Run > Run Module in Python’s IDLE interface to instruct the Python shell to run the program.
2.

3.

4.

5.

6.

Student Resource 3.4

Worksheet: Firstname Program in Python and Other Programming Languages

Student Names:_____________________________________________________ Date:___________

Directions: Below is the Firstname program in Python. On subsequent pages is the Firstname program in other programming languages: C, Fortran, Java, PHP, and C#. Follow these steps to complete the worksheet:


  1. As a group, look at the Firstname program in Python and try to understand it. Write some observations, as indicated.

  2. Have each group member pick one programming language (C, Fortran, Java, PHP, or C#). Look at the Firstname program in that language and try to understand it. Then fill in the comments for the program in your chosen language. The first comment line in each program has been done for you as an example.
    Tip: All five programs do the same thing and produce the same result. They all prompt the user to enter her first name, and then they print “Hello,
    firstname” using whatever name was typed in.

  3. Each group member should then fill in the Venn diagram, noting the similarities and differences between his chosen language and Python.

  4. Reconvene as a group and compare your Venn diagrams.

Firstname Sample Program in Python

Read through this program carefully and try to analyze what is going on in each line of code (and each line of code is doing more than one task). “Talk it through” as if you were describing it to another person.

#

# firstname.py



# Prompts for user first name and prints "Hello, firstname"

# Copyright 2012 National Academy Foundation. All rights reserved.

#
# prompt the user for a value and assign input into variable "firstname"

firstname = raw_input("What is your first name?")


# print text "hello" along with the current value of variable "firstname"

print("Hello, " + firstname + "!")



Write a few observations about the Python program here:

For example, try to answer questions like these: What special character “delimits” a comment (that is, “sets off” the beginning and end of the comment) in Python? How does the program prompt the user for her first name? What is the name of the command that prints the “Hello” greeting on the screen? Where do you think Python stores the first name that the user types in?



Firstname Program in C

The output from this program is the same as the output from the Python program, but there are many differences in the way the two programs are written. Do the same kind of observation and analysis for this program as you did for the Python program.

Write in comments indicating what each line of code is doing. If you aren’t sure, just make an educated guess. The first line of code has already been commented as an example.

/* This C program reads the user's name and prints out a greeting */


/* include definitions of C input/output (I/O) routines */

#include


int main()

{

/* */



char firstname(100);

/* */


printf("What is your first name?");

/* */


scanf("%s,firstname);

/* */


printf("Hello%s\n",firstname);

/* */


return 0;
}

Syntactic Comparison of Python and C

Write how Python and C are different and how they are the same. Two sample observations have been included as examples.

How is C different from Python?

How is Python different from C?

Python has no end character for statements.

C statements end with a semicolon.

How are Python and C the same or similar?



Firstname Program in Fortran

The output from this program is the same as the output from the Python program, but there are many differences in the way the two programs are written. Do the same kind of observation and analysis for this program as you did for the Python program.

Write in comments indicating what each line of code is doing. If you aren’t sure, just make an educated guess. The first line of code has already been commented as an example.

C

C This Fortran program reads the user's name and prints out a greeting



C
! create ("declare") the firstname variable

CHARACTER firstname*100

!

READ (*,*) firstname



!

PRINT *,"Hello",firstname

!

END


Syntactic Comparison of Python and Fortran

Write how Python and Fortran are different and how they are the same.

How is Fortran different from Python?

How is Python different from Fortran?

How are Python and Fortran the same or similar?



Firstname Program in Java

The output from this program is the same as the output from the Python program, but there are many differences in the way the two programs are written. Do the same kind of observation and analysis for this program as you did for the Python program.

Write in comments indicating what each line of code is doing. If you aren’t sure, just make an educated guess. The first line of code has already been commented as an example.

//

// This Java program reads the user's name and prints out a greeting



//
// include definitions of the Java input/output routines

import java.io.Console;


public class HelloJava {

//

public static void main(String[] args) {



//

Console con = System.console();

//

String firstname=con.readLine("What is your first name?; ");



//

System.out.println("Hello," + firstname);


}
}

Syntactic Comparison of Python and Java

Write how Python and Java are different and how they are the same.

How is Java different from Python?

How is Python different from Java?

How are Python and Java the same or similar?



Firstname Program in PHP

The output from this program is the same as the output from the Python program, but there are many differences in the way the two programs are written. Do the same kind of observation and analysis for this program as you did for the Python program.

Write in comments indicating what each line of code is doing. If you aren’t sure, just make an educated guess. The first line of code has already been commented as an example.

/* This PHP program reads the user's name and prints out a greeting */


// prepare to read in information

$fp=fopen("php://stdin","r"):

//

print("What is your first name?");



//

$firstname=fgets($fp);

//

fclose($fp);



//

print("Hello" . $firstname . "\n");

//

return;
?>



Syntactic Comparison of Python and PHP

Write how Python and PHP are different and how they are the same.

How is PHP different from Python?

How is Python different from PHP?

How are Python and PHP the same or similar?



Firstname Program in C#

The output from this program is the same as the output from the Python program, but there are many differences in the way the two programs are written. Do the same kind of observation and analysis for this program as you did for the Python program.

Write in comments indicating what each line of code is doing. If you aren’t sure, just make an educated guess. The first line of code has already been commented as an example.

//

// This C# program reads the user's name and prints out a greeting



//
// include definitions of the C# input/output routines

using System;

public class HelloCSharp {

//

public static void Main() {



//

Console.WriteLine("What is your first name? ");

//

string firstname=Console.ReadLine();



//

Console.WriteLine("Hello," + firstname);


}
}

Syntactic Comparison of Python and C#

Write how Python and C# are different and how they are the same.

How is C# different from Python?

How is Python different from C#?

How are Python and C# the same or similar?



Student Resource 3.5

Reading: High-Level Programming Languages

History of Programming Languages

Origin of Programming Languages

The first digital computers were programmed in machine representation, and each machine had its own unique machine instruction language. Creating a program was a laborious task of entering 0s and 1s into computer memory by wiring a patch panel (which is a little like an old-fashioned telephone switchboard) or by punching holes in a paper tape. This made programming error prone and limited the size of the program you could create. As an example, here is machine representation to add two registers to one another and store the result in a third register (essentially calculating a = b + c):

000000 00001 00010 00110 00000 100000

You can imagine how easy it would be to make a mistake if you had to write a computer program this way!

The 1950s saw the development of translator programs (called assemblers), which allowed programmers to write machine representation for a computer in a text file with text abbreviations (called mnemonics, because their names helped you remember what they did), replacing the machine representation. For the IBM 7094 computer (which existed in the early 1960s), the assembler was called FAP. FAP allowed a programmer to write code like this:

LDQ X

FMP Y


STO PROD

This tiny program multiplies the contents of X and Y and stores the product in PROD. The code means this:



  1. Load X into the Q register.

  2. Multiply X by Y using floating-point arithmetic.

  3. Store the result (the product of the two numbers) in the PROD variable.

As you can see, this code is easier to create than the machine representation shown earlier.

Low-Level Languages Today

Assemblers still exist today. Intel Assembler allows you to create machine representation for Intel processors. There is also z/OS Assembler for the IBM mainframes. Today, assembler languages are used mostly to write specialized code inside operating systems. Machine representation and assembler languages are unique to a particular type of hardware.

First High-Level Languages and How They Were Used

The next step in making it easier to write computer programs was the invention of the compiler. The compiler allows the programmer to write a program in terms of the problem to be solved instead of having to worry about the details of how the computer hardware needs to be programmed to solve it. The two early compiler languages that became popular were Fortran, for scientific problems, and COBOL, for writing business applications. In the Fortran language, the earlier FAP program would be written like this:

PROD = X * Y

Later High-Level Languages and How They Were Used

Although Fortran and COBOL still exist as computer languages, efforts were made to create general-purpose languages that combined the capabilities of both scientific and business languages. One of the first such languages was PL/I, developed by IBM in the 1960s.

The Unix operating system and command-line tools were all written using the C language. C is a general-purpose language that has been implemented in a standard way on almost all modern computers.

An advantage of writing programs in a high-level language is that you can run the same program on different computers without having to change it (or with only minimal changes).

Modern Languages and How They Are Used

The advent of the Internet in the 1990s caused an explosion in computer languages of all sorts. There are now many languages for various purposes, for example:

General-purpose programming languages: C, C++, C#, Java, Pascal

Languages used to create web server applications: Java, PHP, Python, Ruby, Perl, C#, .NET languages

Languages used to create web browser applications: Java, JavaScript, ActionScript, Flex, C#, .NET languages

Interpreted vs. Compiled Languages

When a programmer creates a program that can run on a computer, the statements in the language are either compiled or interpreted. A compiler reads and parses the statements in a source program and turns them into machine representation that can be directly executed by the computer. In Windows, these are stored on disk as EXE files. Each time the programmer makes a change to the source program, it has to be compiled again to produce a new EXE file.

An interpreter reads the program source code each time the program is to be executed. It parses the program into a set of tables or hierarchies called byte code, and then it interprets and executes the byte code one statement at a time. If a language is interpreted, it is not necessary to compile it before running it. But since the program is interpreted each time it is run, it will usually not run as fast as an equivalent program written in a compiled language.

Python Programming Language

Python is a general-purpose, high-level programming language that is relatively easy to learn. Python is an interpreted language.

Python was first released in 1991. It was named after the BBC television series Monty Python’s Flying Circus. Python has a wide variety of uses. It can be used for system programming, web-based scripting, and database programming, as well as for creating graphical user interfaces, games, and graphics.

If you choose Python programming as a career, you might work at Google, Yahoo!, NASA, Pixar, YouTube, or Industrial Light and Magic.

Firstname Program in Python

# This Python program reads the user's name and prints out a greeting

# prompt for user name and read it into the firstname variable

firstname = raw_input("What is your first name? ")

# print out the greeting

print("Hello, " + firstname + "!")



C Programming Language

C is a compiled programming language that was developed in 1972 at Bell Telephone Laboratories. It was originally used to develop the Unix operating system, and it has also been used for developing the Windows, Macintosh, and Linux operating systems. It is also sometimes used to develop application programs.

C is a relatively simple and easy-to-learn programming language, when compared to some others, but it is very powerful. Much of the capability of C is provided by the C library, which consists of a standard set of functions. Programs written in C are generally easy to port from one operating system to another.

If you choose C programming as a career, you might work for Microsoft, Apple, or IBM.

Firstname Program in C

/* This C program reads the user's name and prints out a greeting */

/* include definitions of C input/output (I/O) routines */

#include

int main()

{

/* create ("declare") variable for user name */



char firstname(100);

/* prompt for user name */

printf("What is your first name?");

/* read in user name */

scanf("%s", firstname);

/* print out the greeting */

printf("Hello %s \n", firstname);

/* end the program */

return 0;

}

C++ and C# Programming Languages

C++ (pronounced “C plus plus”) is regarded as a middle-level language, since it has a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup in 1979 as an enhancement to the C language. It was originally named C with Classes and was renamed C++ in 1983. C++ is a compiled, object-oriented, procedural language. Visual C++ is a popular C++ compiler from Microsoft. C++ has enjoyed widespread usage for writing Windows GUI programs.

Microsoft Visual C# (pronounced “C sharp” is part of Microsoft’s .NET programming framework. It is a modern object-oriented programming language that is heavily influenced by Java.

Firstname Program in C#

//

// This C# program reads the user's name and prints out a greeting



//

// include definitions of the C# input/output routines

using System;

public class HelloCSharp {

//

public static void Main() {



//

Console.WriteLine("What is your first name? ");

//

string firstname=Console.ReadLine();



//

Console.WriteLine("Hello," + firstname);

}

}

Fortran Programming Language



Originally developed in the 1950s for scientific and engineering applications, Fortran is still in use today. Fortran was one of the first compiled languages.

Fortran is a blend word derived from “IBM Mathematical Formula Translating System.” It was originally written as FORTRAN.

Here is an example of a Fortran statement; notice that it looks a lot like algebra:

X = -A + 2*B*SQRT(B**2 - 2*A)

If you choose Fortran programming as a career, you might work at an oil company, a research laboratory, or the federal government.

Firstname Program in Fortran

C This Fortran program reads the user's name and prints out a greeting

! create ("declare") the firstname variable

CHARACTER firstname*100

! prompt for the user name

PRINT *,"What is your first name? "

! read in the user name

READ (*,*) firstname

! print out the greeting

PRINT *,"Hello ", firstname

! end the program

END

Java Programming Language

Java is an interpreted programming language developed by Sun Microsystems and released in 1995.

Java is commonly used for developing website back-end applications that perform tasks such as processing orders submitted on the web. It is also used to create applications that need to be portable to multiple operating system environments, such as Windows, Linux, and Mac.

Java uses statement syntax similar to C. Because of its huge libraries, Java can be more time-consuming to learn than other programming languages.

If you choose Java programming as a career, you might work at IBM, Sun Microsystems, or the IT department of a medium-sized to large company. Java is usually used heavily by any organization doing web programming.

Firstname Program in Java

// This Java program reads the user's name and prints out a greeting */

// include definitions of the Java input/output routines

import java.io.Console;

public class HelloJava {

// define the entry to the main program

public static void main(String[] args ) {

// get access to keyboard input

Console con = System.console();

// prompt for user name and read it into firstname

String firstname = con.readLine("What is your first name?: ");

// print out the greeting

System.out.println("Hello, " + firstname);

}

}

PHP Programming Language



PHP is an interpreted language especially suited for producing dynamic web pages. It was originally developed in 1994. PHP can be deployed on most web servers, works in most operating systems, and can be used with many database systems.

Most of the basic parts of the PHP language are similar to or the same as C. PHP has hundreds of base functions and thousands more from extensions that allow you to process page requests on a web server. Proponents claim that PHP is installed on more than 20 million websites and 1 million web servers.

If you choose PHP programming as a career, you might work at Facebook, Wikipedia, or Yahoo!.

Firstname Program in PHP



/* This PHP program reads the user's name and prints out a greeting*/

// prepare to read in information

$fp = fopen("php://stdin", "r");

// prompt for user name

print("What is your first name? ");

// read user name into $firstname

$firstname = fgets($fp);

//stop reading information

fclose($fp);

// print out the greeting and name

print("Hello " . $firstname . "\n");

// end the program

return;


?>

Student Resource 3.6

Worksheet: High-Level Programming Languages

Student Name:______________________________________________________ Date:___________

Directions: Using information in Student Resource 3.5, Reading: High-Level Programming Languages, fill out the matrix below. Part of the matrix has already been filled in to help you get started.

Language

General Characteristics

Origin

Usage

Career Opportunities

Python

Easy to learn

Interpreted


1991







C














Fortran











Oil company

Research lab

Java







Website applications

Operating system–independent apps






C#













PHP













Student Resource 3.7

Writing Assignment:
What I Know about Programming Languages


Student Name:_______________________________________________________ Date:___________

Directions: In this assignment, you will write a short piece that shows some of the information you have learned about programming languages in this lesson. Since we are using Python in this course, use Python as a base and compare other languages to it.

Your writing assignment should include the following:

Compare and contrast Python to at least two other programming languages. Your comparison should include information such as the origins of the languages, how they are used, and the career possibilities they offer.

Explain what you expect to be able to do with Python after you learn it. Think about the skills that you will have and how you might apply them in the real world.

Make sure that you have an introductory sentence that tells what you are going to talk about, and remember to include a conclusion that summarizes your piece.

Make sure your assignment meets or exceeds the following assessment criteria:

The written piece demonstrates knowledge of the Python programming language, including its origins, its usage, and the career options for people who are proficient in Python.

The written piece accurately and completely compares and contrasts Python with at least two other programming languages.

The written piece provides concrete and detailed examples of how people who are proficient in Python can use their skills.

The assignment is neat and legible and does not contain spelling or grammatical errors.

Student Resource 3.8

Worksheet: Computers and Programming
Defining Format


Student Names:______________________________________________________ Date:___________

Directions: Using the Defining Format table below, define the following terms related to the program execution process. First, enter a category for each term. In the next column, list two or three characteristics of the term.

Term

Category

Characteristics

(Example)

Software



is code

that

is written in a programming language

controls the operation of a computer

sometimes runs an application, such as Microsoft Word



sometimes runs an operating system, such as Microsoft Windows

Hardware








High-Level Language







Machine Representation







Interpreter








Compiler







Student Resource 3.9

Four-Quadrant Reflection: Knowing a Programming Language and Knowing How to Program

Student Name:_______________________________________________________ Date:___________

Directions: Write at least two English-language statements in each quadrant of the matrix below. Two examples have been filled in for you.

Use each of the following words at least once: bug (or error), debug, syntax, semantics, output.




Know a programming language (can write program statements, one by one)

Know how to program (can write a complete program that produces meaningful results)

“I already can . . .”

Use the correct syntax for the Turtle Graphics turtle.circle() function.


Use Turtle Graphics to draw colored circles as output.

“I need to learn how to . . .”









Copyright © 2009–2012 National Academy Foundation. All rights reserved.


Download 85.34 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2020
send message

    Main page