Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science / Procedural – 67130

Similar presentations


Presentation on theme: "Introduction to Computer Science / Procedural – 67130"— Presentation transcript:

1 Introduction to Computer Science / Procedural – 67130
Unit 1 Course Mechanics The fundamentals of Procedural Programming (in Java) Basic Program Structure

2 Two Courses, One Goal 67130 starts with Procedural Programming, moves in Week 4 to Object Oriented Programming 67101 starts with Object Oriented Programming 67130 has one extra hour of lecture each week, and at least one extra targil at beginning The two share targilim from middle of courses Same Final Exams in both courses! 67130 67101

3 Entering Students’ Prior Knowledge in Computer Science
Number of Students Amount of Background

4 Entering Students’ Prior Knowledge in Computer Science
Previous Years: Course was aimed here Number of Students Amount of Background

5 Entering Students’ Prior Knowledge in Computer Science
Previous Years: Course was aimed here Number of Students Amount of Background Too much time spent on known material

6 Entering Students’ Prior Knowledge in Computer Science
Previous Years: Course was aimed here Number of Students Amount of Background Course moved very fast, not enough time on fundamentals Too much time spent on known material

7 This Year: 2 Courses, Each Tuned to Background
67130 67101 Number of Students More time spent on programming basics Amount of Background Move directly to object oriented programming

8 Again: Two Courses, One Goal
The two share targilim from middle of courses Same Final Exams in both courses! The goal is to have students at the same point for DAST, 2nd Semester 67130 67101

9 First Month: Procedural Programming Basics
67130 starts with the fundamentals of Procedural Programming (in Java) Basic Program Structure – Week 1 Variables – Week 1 Conditionals, Loops – Week 2 Procedures – Week 3 Top-down Design – Week 4 After four weeks, we move on to Object Oriented Programming (in Java)

10 Course Mission Statement
“To give the student the tools to develop correct, efficient, well-structured, and stylish programs, and to build a foundation for further studies in Computer Science.” -An Introduction to Computer Science with Java Kamin, Mickunas, and Reingold

11 Course Mission Statement
“To give the student the tools to develop correct, efficient, well-structured, and stylish programs, and to build a foundation for further studies in Computer Science.” -An Introduction to Computer Science with Java Kamin, Mickunas, and Reingold

12 Division of Units Between Course Goals (roughly ½ and ½ in lecture time)
3 2 15 1 14 16 5 6 4 18 19 17 8 9 7 20 11 12 10 13 “To give the student the tools to develop correct, efficient, well-structured, and stylish programs, and to build a foundation for further studies in Computer Science.” -An Introduction to Computer Science with Java Kamin, Mickunas, and Reingold

13 Course Mechanics All information can be found at: Lectures, Feldman Bet, Mondays and Tuesdays, 12:00 – 13:45 Tirgulim: Tuesdays (14:00 or 16:00), or Wednesdays (14:00 or 16:00) – Sprinzak (first 4 weeks in computer lab, downstairs) TAs: Oren and Aviv do tirgulim. Ezra is in charge of targilim graders. Dana will teach part of course Extra Help (lab assistants) Problem Sets, Final Exam, Office Hours

14 Resources Slides can be viewed online Books An Introduction to Computer Science with Java, Kamin, Mickunas, and Reingold (“Karel++” and “Karel”, Bergin, Stehlik, Roberts, Pattis)

15

16 Each layer of the course has a different role
Course Structure Each layer of the course has a different role High-level concepts, overall structure Lectures Detailed coverage of concepts, examples Tirgulim Very detailed problems, practical solutions Targilim YOU!

17 On Problem Sets (and Exams)
NO COPYING!

18 On The Information Sheet You Received Today
“Every student must solve the targil on their own.” "על כל סטודנט לפתור את התרגיל בעצמו."

19 You ARE NOT ALLOWED To Copy Files!!!

20 You ARE NOT ALLOWED To Be Told What to Write in the Program

21 What IS Allowed? Discussion is allowed, even encouraged – the result of it should be captured in your brains (NOT on paper, nor electronic) You can talk as much as you want and at any level of detail about a problem, but you are not allowed to take any written material from each other You are required to report with whom you have discussed the targil (“discussed”, not just fixed a misspelled variable)

22 You are required to report with whom you have discussed the targil
List people with whom you’ve discussed the targil in a comment at the beginning of the targil

23 IF SOMEONE COPIES FROM YOU, BOTH OF YOU ARE RESPONSIBLE!

24 What this Course is Not This is not a course on Java, even though we spend time learning this language It’s a course introducing Computer Science, which also means introducing programming (principles are better understood when they are concrete) Java is a means to what we want to accomplish, not an end in itself

25 Course Mission Statement
“To give the student the tools to develop correct, efficient, well-structured, and stylish programs, and to build a foundation for further studies in Computer Science.” -An Introduction to Computer Science with Java Kamin, Mickunas, and Reingold

26 Programming Language Instructions to the computer are given in a special language It has a vocabulary, punctuation marks, and rules of grammar The language allows us to write brief and unambiguous programs

27 A Simple Java Program class Hello { public static void main (String[ ] args) { // Say hello System.out.println("Hello!"); } }

28 A Simple Java Program class Hello { public static void main (String[ ] args) { // Say hello System.out.println("Hello!"); } }

29 Java Cares About File Names
Type the program into a file named Hello.java (case matters) Java demands that the class name (here, “Hello”) must also be the base name of the file into which you put it The extension of the file must be .java Hence, Hello.java Each Class requires a separate file

30 Steps to Nirvana Type the program into a file named Hello.java (case matters; really) Compile it (you’ll learn how in the tirgul) javac Hello.java Execute it (you’ll learn how in the tirgul) java -ea Hello The program displays the message Hello! on the screen

31 Program Development Edit program Compile program Execute program
Evaluate results

32 Compilation Many high-level languages, like C, Pascal, or C++, must be compiled before they are run Compilation means translation into the native language of the computer Each computer (Wintel, Macintosh, Sun Sparc, etc.) has its own native language, so compiling the same program for each computer would create something different The statement “x = 3 + 2;” would be translated into a sequence of low-level operations

33 Compilation C, Pascal, or C++ program Source code Compilation
Machine code for some specific machine

34 Basic Program Structure
class Hello { public static void main (String[ ] args) { // This is our first example program System.out.println("Hello!"); } }

35 The Way You Might Type It (sometimes I’ll do it this way, too)
import intro.utils.*; import java.io.*; public class Hello { public static void main (String[ ] args) throws IOException { // Say hello System.out.println("Hello!"); } }

36 Take it Apart import some classes class heading method heading comment
import intro.utils.*; import java.io.*; public class Hello { public static void main (String[ ] args) throws IOException { // Say hello System.out.println(“Hello!”); } } class heading method heading comment print out “Hello!”

37 Basic Program Structure
class Hello { public static void main (String[ ] args) { // This is our first example program System.out.println("Hello!"); } }

38 Programs Contain Procedures (also sometimes called Methods)
System.out.println("Hello!") is a procedure call System.out.println is the procedure’s name (one of Java’s standard procedures) "Hello, world!" is the procedure’s argument (a string, it goes between parentheses) System.out.println prints out the string followed by a carriage return

39 Another (similar) program
class Hello { public static void main (String[ ] args) { // This is our second example program System.out.println("Welcome,"); System.out.println("introcsp class!"); } }

40 The Generic Java Program
class FirstRun { // This is where new procedures can be // written public static void main (String[ ] args) { // This is where standard and new // procedures can be used } // New procedures can be also be written // here }

41 Programming and Procedures
In the course, we learn about Java, but it is just one language among many One way of looking at “Programming” means writing and learning about procedures

42 Reserved Words and Identifiers
Class heading names the class, i.e., the program class is a reserved word, one of many in Java (like public, static, and void) The word Hello in the heading is an identifier, a word the programmer chooses to use as a name An identifier is a letter followed by any series of letters or digits Java does distinguish between upper and lower case letters

43 Syntax Charts and Style
identifier digit letter letter “a letter followed by any series of letters or digits”

44 Comments Comments explain what is going on Comments are explanatory notes, placed after // on a single line, or between /* and */ across multiple lines The computer ignores them We don’t ignore them! Use them! ALWAYS!

45 Comments Across Multiple Lines
class Hello { public static void main (String[ ] args) { /* This is our first example program. It * is not a particularly interesting * program, but whose fault is that? * I ask you? */ System.out.println("Hello!"); } }

46 Statements Java procedures are sequences of statements. A statement might assign a value to a variable, or be an executable statement that prints output System.out.print("Please type the temperature (deg C): "); temperature = SimpleInput.in.readInt(); There are of course other kinds of statements, conditionals (if, if/else), and iterative statements (like for loops and while)

47 The Statement Part of the Program
The System.out.println procedure call is a statement The program can contain more than one statement as long as they are terminated by semicolons (Java’s statement terminator), as we saw before

48 Whole Java program gets a syntax chart, too
You can find “syntax diagrams” that completely describe the Java syntax (e.g., what follows what, where the curly braces go, what can follow a particular reserved word) in the book: Java Software Solutions – Foundations of Program Design (3rd Edition), by Lewis and Loftus in Appendix L, pages 747 through 759.

49 The Golden Rule of Style
Java does not care about formatting, but the following is a badly written program (though it will run): class Hello{public static void main(String[ ] args){System.out.println("Hello!");}} “Programs should be as easy to read and understand as they are for computers to execute… Above all, be consistent.” When working with others, be consistent with the environment!

50 The Essence of Procedural Programming
Developing an algorithm, a sequence of steps to solve the problem Planning procedures that implement the algorithm’s steps Parameterizing the procedures to make them as general as possible Coding and commenting the procedures Testing the final program

51 Design; Structure; Order
“The only salvation lies in structure” Stepwise refinement is the key. State the big goal first, then restate it in terms of smaller and smaller subgoals (top-down design).

52 Programming Errors Programming errors – in any programming language – can be classified into four broad categories: Lexical Errors Syntactic Errors Execution Errors Intent Errors

53 Lexical Errors A lexical error occurs whenever we use a word not in the vocabulary: class Hello { public shrdlu void main (String[ ] args) { // This is a lexical error program System.out.println("Hello!"); } } Lexical Error

54 Syntactic Errors A syntactic error occurs when we use incorrect grammar or punctuation: class Hello public static void main (String[ ] args) { // This is a syntactic error program System.out.println("Hello!"); } } Syntactic Error, missing first {

55 Execution Errors Execution errors occur when the computer is unable to execute a method successfully and stops (“crashes”): class Hello { public static void main (String[ ] args) { // This is an execution error program System.out.println(9/0); } } An Execution Error will occur (cannot divide by 0)

56 Intent Errors The worst of all. The computer successfully completes the program, but not the task… An intent error may occur early in a program and lead to an execution error later on. Then, we must trace backward from the instruction that caused the crash, to find the instruction that started the computer in the wrong direction.

57 Intent Error Uses wrong formula to compute area. Prints out "27"…
class Hello { public static void main (String[ ] args) { // This is an intent error program System.out.println("The area of"); System.out.println("a square with"); System.out.println("side 3:"); System.out.println(3*3*3); } } Uses wrong formula to compute area. Prints out "27"…

58 Bugs and Debugging All types of errors are known as “bugs”
Debugging means removing errors from a program Lexical and Syntactic errors can be discovered when (trying to) compile Execution errors can be discovered when (trying to) run the program Intent errors can sometimes be hardest to find

59 Lexical and Syntactic errors
Program Development Edit program Lexical and Syntactic errors Compile program Execution errors Execute program Intent errors Evaluate results

60 Unit 1 Extra Slide Material Compilation Managed Code

61 Why We Like Java Java is Object Oriented to the core
Java is, in certain key ways, simpler than other Object Oriented languages (like C++) Java is well-suited to the Internet Java is cross-platform Java’s popularity creates its own momentum

62 Compilation Many high-level languages, like C, Pascal, or C++, must be compiled before they are run Compilation means translation into the native language of the computer Each computer (Wintel, Macintosh, Sun Sparc, etc.) has its own native language, so compiling the same program for each computer would create something different The statement “x = 3 + 2;” would be translated into a sequence of low-level operations

63 Compilation C, Pascal, or C++ program Source code Compilation
Machine code for some specific machine

64 Interpretation Other languages, like Lisp and SmallTalk, are interpreted They don’t get translated directly into machine language Instead, they are given to an interpreter, which is a program that performs each operation in the program on the computer The statement “x = 3 + 2;” would cause some sequence of operations to be carried out

65 Interpretation Lisp or SmallTalk program Source code
Read by an Interpreter Lisp or SmallTalk Interpreter for a Specific Machine Machine instructions for some specific machine

66 Some Differences Compilation and Interpretation differ in many ways
When machine code is generated/activated At compile time with compilation At run-time with interpretation The kind of machine code that is generated / activated Compilation can do optimizations The flexibility of development Interpreters are more flexible because you don’t have to recompile when you make changes to a program

67 Compilation can do optimizations
Let’s say you had the following lines of code: y = 7; w = 4; x = y + w; A reasonable compiler will realize what is happening and replace the last line with machine code that puts 11 into x An interpreter might carry out all the last line’s sub-operations…

68 Where does Java fit? Compilation is efficient, but machine-specific
Interpretation is flexible, but inefficient Java wanted to be efficient, but also flexible, and most important: PLATFORM INDEPENDENT (same program running on all computers) So Java is compiled (sometimes twice) and interpreted...

69 Compilation and Interpretation
A Java program can basically exist in one of two states: Source code (that’s what you are typing in) Byte code, a .class file (a translation of the original code to something closer to the machine’s language – but not any real machine’s language)

70 Compilation and Execution
public class Hello { public static void main... Source code (Hello.java file) Compilation javac Hello.java op7 op342 op213 op431 Byte code, binary file (Hello.class file) OR Compiler Execution java Hello Machine Code Interpreter

71 Java Byte Code (.class file)

72 The Java Virtual Machine
The Java Virtual Machine is the interpreter that reads Java byte codes and carries out real machine operations To get Java running on any kind of computer, just implement the Java Virtual Machine Presto! Java is platform independent, and flexible (interpreted), but still reasonably efficient (because Java byte code is close to machine code)

73 Managed Code This approach in the design of Java is called “managed code” Managed code means a program (like the one you write) must be executed with a runtime environment installed in the same machine – your program cannot run without it Other examples of managed code include Visual Basic and .NET's Common Language Runtime (CLR) Compiled C or C++ programs are examples of unmanaged code “There has been a switch in the last four years toward using managed code. I'd say maybe half of the stuff that gets done now is in managed code.” - Bill Gates, CNet interview,


Download ppt "Introduction to Computer Science / Procedural – 67130"

Similar presentations


Ads by Google