2.04k likes | 2.39k Views
CMPS 135 Introduction to Programming. Instructor: Dr. Cong-Cong Xing. Part I. Overview of Computers and Programming. 0. Java. High level programming language Developed by James Gosling (and his team) at Sun Microsystems in 1991. Formally born in 1995 Naming: oak (tree) Java (coffee)
E N D
CMPS 135 Introduction to Programming Instructor: Dr. Cong-Cong Xing
Part I Overview of Computers and Programming
0. Java • High level programming language • Developed by James Gosling (and his team) at Sun Microsystems in 1991. • Formally born in 1995 • Naming: oak (tree) Java (coffee) • High popularity in industry and academia
1. Computer Hardware System • Basic architecture Input devices CPU Output devices Storage devices
Input devices • Enter data into computers • Ex: keyboard, scanner • Output devices • Observe computation results • Ex: monitor, printer
CPU (Central Processing Unit) • Control unit: coordinate all computation tasks • ALU (arithmetic-logic unit): carry out computations
Storage devices • Major storage device: memory (also called primary memory, RAM, 1st storage device) which stores info temporarily
2nd storage devices store info permanently. Ex: hard disks, floppy disk, optical disks (CDs, DVDs), U-drive
2. Problem Solving and Programming Problem Design Requirements Implementation Testing Specifications
Problem: problem statement • Requirements: complete understanding of the problem (what needs to be done) • Specs: explicit list of input(s), output(s), relevant formulas (if any), data structures, and methodology
Design: algorithm (a step-by-step procedure showing how to solve the problem) • Implementation: coding of design in the chosen language (ex: Java) • Testing: try the best to ensure the program works correctly
3. Overview of Programming Languages • Machine language (lowest level) • Native language for computers • Binary numbers • The only language that computers can understand • Ex: 00011100111100010 (meaning?) • Assembly language (low level) • 2-to-4 letter commands • Ex: ADD R2 R4 (meaning?) • Assembly programs needs to be translated into machine programs by assembler
High-level languages • English-like constructs • Ex: x = a+b (meaning?) • Examples of high-level languages • Pascal, C, C++, Fortran, Java, ML, Haskell • How can computer understand high-level languages?
4. Processing a High-level Language editor Source file Compile Source file Object file Fix err err Linker/ loader executable in RAM More Obj files
Part II Welcome to Java
1. The First Program // my first Java program public class Greeting { public static void main (String args[]) { System.out.println(“Welcome to Java”); } }
2. Dissection of the First Program • What is the output of the program? • Welcome to Java (is displayed on monitor) • How to get the output? • Compile and execute the program • Java is case-sensitive. Main, main, and MaIn are different. • Line by line interpretation (informally)
Comments. Ignored by compiler. Starts the def of class Greeting “public” and “class” are key words “Greeting” can be changed // my first Java program public class Greeting { public static void main (String args[]) { System.out.println(“Welcome to Java”); } } Starts the def of method main Necessary for every Java program Every “word” is fixed Prog execution starts here Printout “Welcome to Java” On monitor Signifies the begin and end Class Greeting and Method main
Compilation and Execution (command line) • Every java program must be save in a file ended with .java • Ex: Greeting.java • Compilation: javac <filename> • Ex: javac Greeting.java • Execution: java <filename (w/o .java)> • Ex: java Greeting
4. Java Packages • Many predefined classes are grouped together to form various packages. Ex: class JOptionPane is in package javax.swing • How to use packages? • Syntax: import <name of package> • Ex: print “welcome to java” again
Direct compiler to load JOptionPane import javax.swing.JOptionPane; public class GreetingAgain { public static void main (String args[]) { JOptionPane.showMessageDialog(null, “Welcome to Java”); System.exit(0); } } Print “welcom to Java” in a Pop-up window (See next slide) Terminate prog Successfully. Required for GUI.
Part III Basic Elements
1. Identifiers • Names that users make up for describing data and programs • Ex: Greeting (a class name) • Rule to make identifiers • A letter (a – z or A – Z) followed by a combination of letters and digits (0 – 9) • More examples:
A, a, Max, MAX, daysOfWeek (legal) • %right, 30days, #missing (illegal)
3. Variables • Special identifiers that hold certain type of data • Each variable corresponds to a memory location (or memory cell) • Ex: a 2 variable a holds 2 • b 1.23 variable b holds 1.23 • Value of variables may be changed (by programmers) at any time • Each variable must have a data type
4. Declaration of variables • Syntax • <type> var1, var2, …, varn; • Ex: • int a, b, c; --------------(a) • double A; --------------(b) • Meaning: (a) declares variables a, b, and c are of type int. (b) declares variable A is of type double • Every variable must be declared first before it can be used
5. Arithmetic Expressions • Math formulas expressed in Java • Addition operator: + • In math: a+b • In Java: a+b • Subtraction operator: - • In math: a-c • In Java: a-c • Multiplication operator: * • In math: b×c • In Java: b*c
Cont’d • Division operator: / • In math: a÷b • In Java: a/b • Modulo operator: % • In math: a mod b (the remainder of a÷b) • In Java: a%b • Ex: 7%4 = 3
Cont’d • Note: a/b, when both a and b are integers, the result of a/b is also an integer (the integer part of the quotient, no rounding). Otherwise, the result is a real. • Ex: 7/4 = 1 2/4 = 0 2.0/4 = 0.5 2/4.0 = 0.5 2.0/4.0 = 0.5
Cont’d • 3/2 hours = how many seconds ? 60*60*3/2 or 3/2*60*60 or neither
Cont’d • More examples math Java (a+b+c)÷3 (a+b+c)/3 a+b-c÷2 a+b-c/2 (b2-4ac) ÷2a (b*b-4*a*c)/(2*a) (a+b)(a-b) ÷(a2-b2) fill in
6. Precedence of Operators • From highest to lowest: ( ) * / % + - • For operator of the same level: innermost first, from left to right • Consistent with math high low
Cont’d • Ex: evaluation tree of (a+b+c)/3 (a + b + c) / 3 + 1 + 2 3 / result
Ex: evaluation tree of (b*b-4*a*c)/(2*a) (b * b – 4 * a * c) / (2 * a) 1 2 5 * * * 3 * 4 - 6 / result
7. Assignment Statements • Syntax: <var> = <expression>; • Ex: a = 1; b = 2; b = a+b-2; a = a+1; b = b – 1; • Meaning: evaluates the expression on the right first and stores the result of evaluation into the variable on the left (a memory cell). • Note: = does not mean “equal”! • Types on both sides of = MUST match!
8. Keywords • Reserved words by Java. They have special meanings to Java and should be avoided by users. • Examples: int, double, boolean, char, while, if, for, void, static
9. Input and Output • String type: another data type in Java. Any double quoted sequence of symbols are of type String. • Ex: “123”, “ab”, “ab45H$%”, “1”, “_”, “ “
Cont’d • Input statement: reads data into programs • Syntax: import javax.swing.JOptionPane; <var> = JOptionPane.showInputDialog(“___”); note: <var> must be of type String • Meaning: prompts and reads data into variable var • Convert string type variable vstr to int or double type • <var> = Integer.parseInt(vstr); • <var> = Double.parseDouble(vstr);
Cont’d • Ex: the following statements String astr; int a; astr=JOptionPane.showInputDialog(“enter a value for a”); a = Interger.parseInt(astr); pops up a window and prompts for input and converts astr (String type) to a (int type)
Cont’d • Output statement: display computation results on monitor. • Syntax: System.out.println(“msg”+var); or System.out.print(“msg”+var); • Meaning: print msg and the value of variable var on the monitor. • with ln, line is changed. w/o ln, line is not changed.
Cont’d • Ex: the statements a = 1; b =2; System.out.println(“value of a is: ”+a); System.out.println(“value of b is: ”+b); Produces the following on monitor: value of a is: 1 value of a is: 2
Cont’d • Ex: the statements a = 1; b =2; System.out.print(“value of a is: ”+a); System.out.println(“value of b is: ”+b); Produces the following on monitor: value of a is: 1value of b is: 2
10. Case Study • Problem: write a Java program that prompts and inputs a radius of a circle and computes the area of the circle. • Requirements: users enter the radius, program outputs the corresponding area. • Specification: • Input(s): radius of a circle • Output(s): area of the circle • Relevant formula: area = pi*radius*radius
Cont’d • Data structure (dictionary of variables)
Cont’d • Design (structure chart) • (How to read the chart? from left to right, from top to bottom)