Computer Programming-1 CSC 111 Chapter 1 : Introduction
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
Computer Organization
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)
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.
Program Execution steps output data input data executing program Keyboard Screen
Example 1: Area and Perimeter of a rectangle Input Length width Process Area = length*width Perimeter = 2*( length + width) Output Area Perimeter
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
Example 3: Area and Perimeter of a circle Input Radius PI Process Area = PI * Radius * Radius Perimeter = 2 * PI * Radius Output Area Perimeter
Test and Debug the Program Check the program, Compile the program. Run the program. Debug the program using a debugger.
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
Typical Java Environment Verify Verifier ensures bytecodes do not violate security requirements Execute Interpreter translates bytecodes into machine language
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.
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
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
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)
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.
Characteristics of Java (2) Platform-independent Portable Architecture neutral ”Write-once, run-anywhere”
Characteristics of Java (3) Secure The bytecode verifier of the JVM : checks untrusted bytecode controls the permissions for high level actions.
A Simple Program 1 // Hello.java 2 Import javax.swing.* 3 public class Hello { 4 5 // main method 6 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
A First Program in Java: Printing a Line of Text 1 // 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
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 3 public class Hello {
A Simple Program: Printing a Line of Text 3 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
A Simple Program: Printing a Line of Text 3 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 6 public static void main( String args[] ) {
A Simple Program: Printing a Line of Text 6 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)
A Simple Program: Printing a Line of Text 8 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 ;
A Simple Program: Printing a Line of Text 10 } // 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