1 / 197

CMPS 135 Introduction to Programming

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)

Download Presentation

CMPS 135 Introduction to 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. CMPS 135 Introduction to Programming Instructor: Dr. Cong-Cong Xing

  2. Part I Overview of Computers and Programming

  3. 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

  4. 1. Computer Hardware System • Basic architecture Input devices CPU Output devices Storage devices

  5. Input devices • Enter data into computers • Ex: keyboard, scanner • Output devices • Observe computation results • Ex: monitor, printer

  6. CPU (Central Processing Unit) • Control unit: coordinate all computation tasks • ALU (arithmetic-logic unit): carry out computations

  7. Storage devices • Major storage device: memory (also called primary memory, RAM, 1st storage device) which stores info temporarily

  8. 2nd storage devices store info permanently. Ex: hard disks, floppy disk, optical disks (CDs, DVDs), U-drive

  9. Motherboard (main board): host memory, CPU, etc.

  10. 2. Problem Solving and Programming Problem Design Requirements Implementation Testing Specifications

  11. 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

  12. 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

  13. 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

  14. 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?

  15. 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

  16. Part II Welcome to Java

  17. 1. The First Program // my first Java program public class Greeting { public static void main (String args[]) { System.out.println(“Welcome to Java”); } }

  18. 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)

  19. 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

  20. 3. Compilation and Execution (GUI Dr. Java)

  21. 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

  22. (Why command line then?)

  23. 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

  24. 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.

  25. Cont’d

  26. Part III Basic Elements

  27. 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:

  28. A, a, Max, MAX, daysOfWeek (legal) • %right, 30days, #missing (illegal)

  29. 2. Data Types and Values

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. Cont’d • 3/2 hours = how many seconds ? 60*60*3/2 or 3/2*60*60 or neither

  36. 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

  37. 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

  38. Cont’d • Ex: evaluation tree of (a+b+c)/3 (a + b + c) / 3 + 1 + 2 3 / result

  39. 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

  40. 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!

  41. 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

  42. 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”, “_”, “ “

  43. 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);

  44. 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)

  45. 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.

  46. 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

  47. 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

  48. 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

  49. Cont’d • Data structure (dictionary of variables)

  50. Cont’d • Design (structure chart) • (How to read the chart? from left to right, from top to bottom)

More Related