1 / 51

Introduction to Computer Programming

Introduction to Computer Programming. Pre-AP Computer Science, Cycle 5. Language of a Computer. Computers are electronic devices; they work by sending electronic signals Electronic signals represent information in binary , using 0’s and 1’s This binary language is called machine language

tirzah
Download Presentation

Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer Programming Pre-AP Computer Science, Cycle 5

  2. Language of a Computer • Computers are electronic devices; they work by sending electronic signals • Electronic signals represent information in binary, using 0’s and 1’s • This binary language is called machine language • A single 0 or 1 is called a bit of data • Bit = binary digit • A sequence of 0’s and 1’s is called binary code

  3. Bytes? • A sequence of 8 bits = 1 byte 10011101 • Kilobyte (KB)  1024 bytes • Megabyte (MB)  1024 KB • Gigabyte (GB)  1024 MB • Terabyte (TB)  1024 GB • Petabyte (PB)  1024 TB BIT BYTE

  4. Binary into Letters • Binary code is translated into numbers, letters, and punctuation • Most common system: ASCII • American Standard Code for Information Interchange • 7 bits, or 128 different characters • ‘A’  1000001 • Another common system: Unicode • 16 bits, or 65,536 different characters • Useful for languages other than English • ‘A’  0000000001000001

  5. Evolution of Programming Languages wages = rate * hours • Machine Code 100100 010001 100110 010010 storage locations 100010 010011 math operations • Assembly Language LOAD rate MULT hours STOR wages • High-level Language (C++, Java, Python, etc) wages = rate * hours;

  6. Translating High-Level Languages to Machine Code • Remember, computers only understand machine code • Any other code must be translated back to machine code in order for the computer to execute the program

  7. Translating to Machine Code

  8. Java Compiler and IDE • Compiler (JDK 7) • http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html • Accept the license agreement • Download Java SE Development Kit 7u51 for Windows x64 (last one in the first table) • IDE/Editor (jGRASP) • http://spider.eng.auburn.edu/user-cgi/grasp/grasp.pl?;dl=download_jgrasp.html • Scroll down, then download/install jGRASP 2.0.0_07 for Windows

  9. Test your Compiler public class MyFirstJavaProgram { public static void main(String[] args) { System.out.println(“Hello world!”); } } • Run the code by clicking on the red running man

  10. Warm-up: February 18th • Why do you need to translate high-level programming languages into machine language? • Why would you prefer to write a program in a high-level programming language rather than a machine language?

  11. Programming is Problem Solving PAP Computer Science, Cycle 5

  12. Programming is Problem Solving • Most computer programs set out to solve a problem • Programming well requires good problem-solving skills • Problem-solving skills can be developed through various techniques

  13. Algorithm Development • Analyze the problem • Really understand what it’s asking for • Outline the project requirements • What information do I need to bring in? • What information needs to be stored? • What information needs to be outputted? • Design your algorithm • Implement the algorithm • Verify the algorithm (test it) • Maintain and improve program

  14. Algorithm: Perimeter and Area of a Rectangle Formulas: perimeter = 2*(length+width) area = length * width • Get the length of the rectangle • Get the width of the rectangle • Find the perimeter using the perimeter equation • Find the area using the area equation • Output the length, width, perimeter, and area of the rectangle

  15. Computers are stupid! • They only do EXACTLY what you tell them to do • They take NO information for granted • They do not make assumptions • They do not automatically fill in gaps in instructions • They do not stop unless told to do so • EVERY step must be outlined for them

  16. Demonstration: Robot Ms. Alexander • Take out a sheet of paper • On the paper, write the steps necessary for Ms. Alexander to walk from the door to her chair and sit down • Robot Ms. Alexander only knows how to take steps, bend her knees, and turn

  17. Algorithm: Number-Guessing Game • The computer picks a randomly generate number between 1 and 100 • The user tries to guess the number • If the user’s guess is smaller than the number, the computer says “too small, guess again” • If the user’s guess is larger than the number, the computer says “too large, guess again” • The process repeats until the user guesses the correct number

  18. Algorithm: Number-Guessing Game • Generate a random number • Save the random number in a variable called num • Repeat the following steps until the player has guessed the correct number: • Prompt the user to enter a guess • If guess = num, print “correct!” • Otherwise, if guess < num, print “Too small! Guess again!” • Otherwise, if guess > num, print “Too large! Guess again!”

  19. Structured Programming • Divide larger, more complex programming problems into smaller, more manageable problems • Write code to solve the smaller problems, then use those answers to solve the large problem • Also called top-down design, bottom-up design, stepwise refinement, and modular design

  20. Example: Programming a TV Remote Problem: We want to control a TV using a remote with many different buttons Number keys code Volume keys code Channel keys code

  21. Object-Oriented Programming • Analyze a problem to determine what the major components are, and create objects to represent these components • Determine how these objects should be characterized, and how they should interact with each other • Each object is given characteristics (properties) • Each object is given abilities (methods)

  22. Example: Video Game Rentals • Two main objects: video game and the customer • Video game object • Data: title, genre, rating, # in stock, price • Operations: be scanned, inc/dec stock as copies are rented/returned, be shelved • Customer object • Data: age, gender, renting history • Operations: rent a game, return a game, browse, search by…

  23. Structured vs OOP • Structured • Better for mathematical functions • Better for simple, single-step, or few-step problems • Object-Oriented • Better for more complicated problems • Better for problems that use large amounts of related information • C, COBOL, FORTRAN, BASIC  structured programming languages • C++, Java, Ruby, Python, Perl  OOP programming languages

  24. Warm-up: February 19 • Develop an algorithm for the following problem: Find the radius of a circle given the circle’s circumference. Formula: circumference = 2*pi*radius

  25. Warm-up: February 19 • Develop an algorithm for the following problem: Find the radius of a circle given the circle’s circumference. Formula: circumference = 2*pi*radius • Input the circumference and save in a variable • Calculate radius by dividing the circumference by (2*3.141) • Output the radius

  26. Basic Elements of a Java Program PAP Computer Science, Cycle 5

  27. Example Code //***************************************** // Author: Rachel Alexander Date: 2/19/14 // This code displays three lines of text //***************************************** public classASimpleJavaProgram { public static void main(String[] args) { System.out.println(“Happy Wednesday!”); System.out.println(“Sum of 2 and 3 = “ + 5); System.out.println(“7 + 8 = “ + (7+8)); } }

  28. Comments //***************************************** // Author: Rachel Alexander Date: 2/19/14 // This code displays three lines of text //***************************************** public classASimpleJavaProgram { public static void main(String[] args) { System.out.println(“Happy Wednesday!”); System.out.println(“Sum of 2 and 3 = “ + 5); System.out.println(“7 + 8 = “ + (7+8)); } }

  29. Comments • It’s good programming practice to begin each code you write with a comment listing the author of the program, the data last modified, and a short description of what the code does • You should also comment key statements in a program • Single line comments: //This is a comment • Multi-line comments: /* This comment can span many lines. */

  30. Classes //***************************************** // Author: Rachel Alexander Date: 2/19/14 // This code displays three lines of text //***************************************** public classASimpleJavaProgram { public static void main(String[] args) { System.out.println(“Happy Wednesday!”); System.out.println(“Sum of 2 and 3 = “ + 5); System.out.println(“7 + 8 = “ + (7+8)); } }

  31. Classes • Classes are how we create objects in Java • Recall, Java is an object-oriented programming language • Each code we write must exist inside of a class • This class we named ASimpleJavaProgram • Public vs private classes will be discussed later

  32. Methods //***************************************** // Author: Rachel Alexander Date: 2/19/14 // This code displays three lines of text //***************************************** public classASimpleJavaProgram { public static void main(String[] args) { System.out.println(“Happy Wednesday!”); System.out.println(“Sum of 2 and 3 = “ + 5); System.out.println(“7 + 8 = “ + (7+8)); } }

  33. Methods • Code within classes must be written inside of a method • Methods are what cause actions to be performed in our Java code • Each class can only have one “main” method, but unlimited amounts of other named methods

  34. Example Code //***************************************** // Author: Rachel Alexander Date: 2/19/14 // This code displays three lines of text //***************************************** public classASimpleJavaProgram { public static void main(String[] args) { System.out.println(“Happy Wednesday!”); System.out.println(“Sum of 2 and 3 = “ + 5); System.out.println(“7 + 8 = “ + (7+8)); } }

  35. Output • System.out.println( ) is used to output information to the user • You can output text, variables, lists, or even math Example: System.out.println(“Happy Wednesday!”); Happy Wednesday!

  36. Output • You can output mutiple types of expressions in one line by combining them with the ‘+’ operator Example: System.out.println(“The sum of 2 and 3 = “ + 5 ); The sum of 2 and 3 = 5 Example: System.out.println(“7 + 8 = “ + (7 + 8)); 7 + 8 = 15

  37. Your Turn! Write a program that produces the following output: ****************************** * Programming Assignment 1 * * PAP Computer Science * * Author: YOUR NAME * * Date: February 19, 2014 * ****************************** CODE TEMPLATE: public classASimpleJavaProgram { public static void main(String[] args) { System.out.println(“TEXT GOES HERE”); } }

  38. Warm-up: Thursday, Feb 20 • Name one way in which commenting code can be helpful. • What is the command for generating output?

  39. Warm-up: Thursday, Feb 20 • Name one way in which commenting code can be helpful. • Notes/reminders to yourself and others • Declare the author of the code/date last modified • Explain key lines of code • What is the command for generating output? • System.out.println( )

  40. Warm-Up: Friday, Feb 21 • Correct the 6 errors in the following code: public CLASS warmup { public static void main(String[] args) { System.Out.PrintIN(“Hello!) } }

  41. Warm-Up: Friday, Feb 21 • Correct the 6 errors in the following code: public classwarmup { public static void main(String[] args) { System.out.println(“Hello!”); } }

  42. Advanced Output Pre-AP Computer Science, Cycle 5

  43. Recall… • Output is performed using the System.out.println() command • Example: System.out.println(“Hello world!”); • However, this is not the only command for output

  44. Output Expressions • System.out.println( ) • Outputs onto a new line • System.out.print( ) • Outputs on the same line • System.out.printf( ) • Outputs with special formatting rules

  45. Printlnvs Print System.out.println(“Hello”); System.out.println(“there.”); Hello there. System.out.print(“Hello”); System.out.print(“there.”); Hello there.

  46. Outputting Different Data Types • Strings are enclosed in double quotes. • System.out.println(“Hello!”); • Characters are enclosed in single quotes • System.out.println(‘A’); • Numbers/variables are not enclosed in quotes • System.out.println(7); • System.out.println(4 + 12); • System.out.println(answer);

  47. White Spaces • Typically, Java ignores “white spaces” • Spaces • Enters • Tabs • When outputting, some “white spacing” is allowed, while others are not allowed.

  48. White Spaces System.out.print(“It’s sunny.”); System.out.println(“Let’s go outside.”); It’s sunny. Let’s go outside. System.out.println(“It’s sunny.” + “Let’s go outside.”); It’s sunny. Let’s go outside. System.out.println(“It’s sunny. Let’s go outside.”); ERROR MESSAGE

  49. Escape Sequences • Escape sequences are used to output typically reserved characters • Slashes • “Enters” • Quotations • Tab • Escape sequences always begin with a forward slash (\) • This is found ABOVE THE ENTER KEY

  50. Common Escape Sequences

More Related