Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming-1 CSC 111

Similar presentations


Presentation on theme: "Computer Programming-1 CSC 111"— Presentation transcript:

1 Computer Programming-1 CSC 111
Chapter 1 : Introduction

2 What Is a Computer? Computer Computer programs
Executes statements (computations/logical decisions) Computer programs A sequence of instructions to be performed by a computer Hardware :Physical devices of computer system Software: Programs that run on computers

3 Computer Organization

4 Computer Organization II
Input unit (Mouse, keyboard) Output unit (Printer, monitor, audio speakers) Memory unit (Retains input and processed information) Central processing unit (CPU) which consists of: Secondary storage unit (Hard drives, floppy drives)

5 The Programmer’s Algorithm
An algorithm is a series of step by step instructions that produces a solution to a problem The programmer’s algorithm: Define the problem. Plan the problem solution. Code the program. Test and debug the program.

6 Program Execution steps
output data input data executing program Keyboard Screen

7 Example 1: Area and Perimeter of a rectangle
Input Length width Process Area = length*width Perimeter = 2*( length + width) Output Area Perimeter

8 Example 2: Sum and Average of 5 numbers
Input five number x1, x2, x3, x4, x5 Process Sum = x1+x2+x3+x4+x5 Average = Sum/5 Output Sum Average

9 Example 3: Area and Perimeter of a circle
Input Radius PI Process Area = PI * Radius * Radius Perimeter = 2 * PI * Radius Output Area Perimeter

10 Test and Debug the Program
Check the program, Compile the program. Run the program. Debug the program using a debugger.

11 Typical Java Environment
Java programs undergo five phases Edit Programmer writes program (and stores program on disk as .java file) Compile Compiler creates bytecodes from program (.class file generated from the .java file) Load Class loader stores bytecodes in memory

12 Typical Java Environment
Verify Verifier ensures bytecodes do not violate security requirements Execute Interpreter translates bytecodes into machine language

13 The Java Virtual Machine
The class Loader, the Bytecode Verifier and Interpreter constitute the Java Virtual Machine (JVM). JVM is platform specific. The interpreter translates the bytecodes into specific machine commands.

14

15 Phase 1 Editor Phase 2 Compiler Phase 3 Class Loader Bytecode Verifier
Primary Memory . Disk Editor Compiler Class Loader Program is created in an editor and stored on disk in a file ending with .java. Compiler creates bytecodes and stores them on disk in a file ending with .class. Class loader reads .class files containing bytecodes from disk and puts those bytecodes in memory. Phase 1 Phase 2 Phase 3 Bytecode Verifier Bytecode verifier confirms that all bytecodes are valid and do not violate Java’s security restrictions. Phase 4 Interpreter Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes. Phase 5

16 Running Programs The Java compiler produces bytecode not machine code
Bytecode is converted into machine code using a Java Interpreter Bytecode is runnable on any computer that has a Java Interpreter installed

17 Java Class Libraries Classes Java contains class libraries
Include methods that perform tasks Return information after task completion Used to build Java programs Java contains class libraries Known as Java APIs (Application Programming Interfaces)

18 Characteristics of Java
Object-Oriented Combines data and behavior into one unit  objects Provides Data abstraction and encapsulation Decompose program into objects. Programs are collections of interacting and cooperating between objects.

19 Characteristics of Java (2)
Platform-independent Portable Architecture neutral ”Write-once, run-anywhere”

20 Characteristics of Java (3)
Secure The bytecode verifier of the JVM : checks untrusted bytecode controls the permissions for high level actions.

21 A Simple Program 1 // Hello.java 2 Import javax.swing.*
3 public class Hello { 4 5 // main method public static void main( String args[] ) { JFrame myWindow ; myWindow = new JFrame() ; myWindow.setSize(300,200); myWindow.setTitle(“ Hello !!!!!!”); myWindow.setVisible(true); } // end main } // end class Hello

22 A First Program in Java: Printing a Line of Text
// Welcome1.java Comments start with: // Comments ignored during program execution Documents code Provides code readability Traditional comments: /* ... */ /* This is a traditional comment. It can be split over many lines */ Note: line numbers not part of program, added for reference

23 A Simple Program Blank line Begins class declaration for class Hello
2 Blank line Makes program more readable Blank lines, spaces, and tabs are white-space characters Ignored by compiler Begins class declaration for class Hello Every Java program has at least one user-defined class Keyword: words reserved for use by Java class keyword followed by class name Naming classes: capitalize every word MyClassName public class Hello {

24 A Simple Program: Printing a Line of Text
public class Hello { Name of class called identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Examples: Hello1, $value, _value, button7 7button is invalid Java is case sensitive (capitalization matters) a1 and A1 are different

25 A Simple Program: Printing a Line of Text
public class Hello { Saving files File name must be class name with .java extension Hello.java Left brace { Begins body of every class Right brace ends declarations (line 12) Part of every Java application Applications begin executing at main Parenthesis indicate main is a method Java applications contain one or more methods public static void main( String args[] ) {

26 A Simple Program: Printing a Line of Text
public static void main( String args[] ) { Exactly one method must be called main Methods can perform tasks and return information void means main returns no information For now, mimic main's first line Left brace begins body of method declaration Ended by right brace } (line 10)

27 A Simple Program: Printing a Line of Text
System.out.println( “Hello Every Body!" ); Instructs computer to perform an action Prints string of characters String - series characters inside double quotes White-spaces in strings are not ignored by compiler System.out Standard output object Print to command window Method System.out.println Displays line of text Argument inside parenthesis This line known as a statement Statements must end with semicolon ;

28 A Simple Program: Printing a Line of Text
} // end main Ends method declaration Ends class declaration Can add comments to keep track of ending braces Lines 7 and 8 could be rewritten as: Remember, compiler ignores comments Comments can start on same line after code 12 } // end class Hello


Download ppt "Computer Programming-1 CSC 111"

Similar presentations


Ads by Google