1 / 238

Think Java: Java Programming Language Part 1

Think Java: Java Programming Language Part 1. Chia James Chang cjameschang@yahoo.com Materials are based on: Professor Allen B. Downey’s “Think Java: How to Think Like a Computer Scientist” (10/16/2012 – 11/20/2012) (Update: 10/21/2012). Who Am I?.

maleah
Télécharger la présentation

Think Java: Java Programming Language Part 1

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. Think Java:Java Programming Language Part 1 Chia James Chang cjameschang@yahoo.com Materials are based on: Professor Allen B. Downey’s “Think Java: How to Think Like a Computer Scientist” (10/16/2012 – 11/20/2012) (Update: 10/21/2012) Think Java (TSG@ROLF)

  2. Who Am I? James has worked in the software industry for 20+ years and is currently working at Microsoft. He had worked for Yahoo!, Sun Microsystems, Rolm/IBM, and several startups and has used Java and other languages and technologies to develop various mobile, enterprises, security, and Big Data software. Think Java (TSG@ROLF)

  3. Freely received, freely give • “Freely you have received; freely give.” • Matthew 10:8 (New International Version) • “As we enjoy great Advantages from the Inventions of others, we should be glad of an Opportunity to serve others by any Invention of ours, and this we should do freely and generously.” • Benjamin Franklin, quoted in Benjamin Franklin by Edmund S. Morgan. Think Java (TSG@ROLF)

  4. Background • In 1999, Professor Allen B. Downey was teaching an introductory computer science class using the Java programming language at Colby College, Maine, and start writing this free book. • “Think Java: How to Think Like a Computer Scientist” by Allen B. Downey • http://greenteapress.com/thinkapjava/index.html • Each chapter is about ten pages, not including the exercises. It’s a free book! • This book about a way of thinking like a computer scientist. Think Java (TSG@ROLF)

  5. Think like a computer scientist • Computer scientists have an approach to problem-solving, and a way of crafting solutions, that is unique, versatile and powerful. • Hope that Professor Downey’s book and this class will give you a sense of what that approach is, and that at some point you will find yourself thinking like a computer scientist. • If you achieved it, all credits go to Professor Downey. • If not, it’s all my fault for not being able to teach. Think Java (TSG@ROLF)

  6. Outline - 1 Chapter 1: The way of the program (programming language, program, debug, first program...) Chapter 2: Variables and types (variables, assignments, operators...) Chapter 3: Void methods (floating-point, classes and methods, parameters...) Chapter 15: Object-oriented programming (object methods and class methods, inheritance...) Chapter 4: Conditionals and recursion (modulus operator, conditions, recursion...) Chapter 6: Value methods (return values, overloading...) Chapter 7: Iteration and loops (multiple assignment, while...) Chapter 8: Strings and things (characters, traversal, String...) Think Java (TSG@ROLF)

  7. Outline - 2 Chapter 9: Mutable objects (packages, instance variables, Objects...) Chapter 11: Create your own objects (class, constructors...) Chapter 12: Arrays (accessing elements, arrays and objects...) Chapter 13: Arrays of Objects (Card objects, arrays of cards...) Chapter 14: Objects of Arrays (Deck class, shuffling, sorting...) Chapter 5: GridWorld: Part 1 (College Board AP Computer Science Case Study) Chapter 10: GridWorld: Part 2 (College Board AP Computer Science Case Study) Chapter 16: GridWorld: Part 3 (College Board AP Computer Science Case Study) Think Java (TSG@ROLF)

  8. References • Think Java: How to Think Like a Computer Scientist” by Allen B. Downey • http://greenteapress.com/thinkapjava/index.html • Think Java Class @ TSG of River of Life Foundation website • http://www.techsamaritan.org/courses/jameschang/java/java-1.html • GridWorld Case Study (College Board AP Computer Science Curriculum) • http://www.collegeboard.com/student/testing/ap/compsci_a/case.html • Java SE 7 JDK (Windows, Unix/Linux, and Mac) • http://www.oracle.com/technetwork/java/javase/downloads/index.html • The Java Tutorials • http://docs.oracle.com/javase/tutorial/index.html • Eclipse IDE for Java EE Developers • http://www.eclipse.org/downloads/ Think Java (TSG@ROLF)

  9. Chapter 1: The way of the program Think Java (TSG@ROLF)

  10. The way of the program • Think like a computer scientist who combines some of the best features of. • Mathematicians: use formal languages to denote ideas • Engineers: design things, assembling components into systems and evaluating tradeoffs among alternatives • Scientists: observe the behavior of complex systems, form hypotheses, and test predictions. Think Java (TSG@ROLF)

  11. Problem-Solving • The single most important skill for a computer scientist is problem-solving. • What is problem-solving? • the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately • The process of learning to program is an excellent opportunity to practice problem-solving skills. Think Java (TSG@ROLF)

  12. What is a programming language? • High-level: Java, Python, C, C++, etc. • easier to program; portable; interpret or compile • Low-level: machine or assembly language • Java is both compiled and interpreted. Think Java (TSG@ROLF)

  13. What is a program? • A sequence of instructions that specifies how to perform a computation. • Instructions or statements perform: • input: get data • output: output data • math: perform math operations • testing: check for conditions, run statements • repetition: perform actions Think Java (TSG@ROLF)

  14. What is debugging? • Debugging: tracking down and correcting bugs • Three kinds of errors: • syntax errors: • Syntax refers to the structure of your program and the rules about that structure. • e.g. omitting the semi-colon at the end of a statement • run-time errors: • In Java, run-time errors (called exceptions) occur when the interpreter is running the byte code and something goes wrong. • e.g. an infinite recursion causes a StackOverflowException • logic errors and semantics: • The semantics, or meaning of the program, are wrong. • e.g. yielding an unexpected result Think Java (TSG@ROLF)

  15. Experimental debugging • One of the most important skills you will acquire in this class is debugging. • Debugging is one of the most interesting, challenging, and valuable parts of programming. • Debugging is like detective work. • Debugging is like an experimental science. As Sherlock Holmes pointed out, “When you have eliminated the impossible, whatever remains, however improbable, must be the truth.” (From A. Conan Doyle’s The Sign of Four). Think Java (TSG@ROLF)

  16. Java! Java! Java! Think Java (TSG@ROLF)

  17. Java • “Java: The World's Most Popular Programming Language” • The TIOBE Programming Community Index • “3 Billion Devices Run Java” • Oracle Think Java (TSG@ROLF)

  18. Java Buzzwords Think Java (TSG@ROLF)

  19. The first program • “Hello, World.” in Java class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World!”); } } • Java programs are made of class definitions class CLASSNAME { public static void main(String[] args) { STATEMENTS; } } Think Java (TSG@ROLF)

  20. Java on different OS • Running on different Operating Systems (OS) via Java VM editor HelloWorld.java javac HelloWorld.java java HelloWorld.class Java VM Java VM Java VM Java VM “Hello World!” Think Java (TSG@ROLF)

  21. Java Platform • Java Virtual Machine (JVM) • Java Application Programming Interface (API) Think Java (TSG@ROLF)

  22. Create and run Java program - 1 • Create a source file • vi HelloWorld.java • Compile the source file into .class file • javac HelloWorld.java • Output file: HelloWorld.class • Run the program • java –classpath . HelloWorld • Output message: “Hello World!” Think Java (TSG@ROLF)

  23. Create and run Java program - 2 • Use different Java IDEs or editors • In this class, I’ll use Eclipse to create and run Java programs. • Demo Think Java (TSG@ROLF)

  24. Summary & Exercises Think Java (TSG@ROLF)

  25. Chapter 2: Variables and types Think Java (TSG@ROLF)

  26. The way of program • Think like a computer scientist who combines some of the best features of. • Mathematicians: use formal languages to denote ideas • Engineers: design things, assembling components into systems and evaluating tradeoffs among alternatives • Scientists: observe the behavior of complex systems, form hypotheses, and test predictions. • The single most important skill for a computer scientist is problem-solving. • the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately • The process of learning to programis an excellent opportunity to practice problem-solving skills. Think Java (TSG@ROLF)

  27. The first program • “Hello, World.” in Java class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World!”); } } Think Java (TSG@ROLF)

  28. More printing • “Hello, World.” in Java class Hello1 { // Generates some simple output. public static void main(String[] args) { System.out.println(“Hello, world.”); // print one line System.out.println(“How are your?”); // print another } } • To display the output on one line, use print class Hello2 { // Generates some simple output. public static void main(String[] args) { System.out.print(“Goodbye, ”); System.out.println(“cruel world!”); } } // output: Goodbye, cruel world!. Think Java (TSG@ROLF)

  29. Variables • A variable is a named location that stores a value. • To declare or to create a variable String bob; String firstName; String lastName; • To declare multiple variables int hour, minute; Think Java (TSG@ROLF)

  30. Primitive Data Types

  31. Assignment • To store a variable with an assignment statement bob = “Hello.”; hour = 11; minute = 59; bob = “123”; // legal bob = 123; // not legal • When you declare a variable, you create a named storage location. • When you make an assignment to a variable, you give it a value. Think Java (TSG@ROLF)

  32. Printing variables • You can print the value of a variable using println or print: class Hello3 { public static void main(String[] args) { String firstLine; firstLine = “Hello, again!”; System.out.println(firstLine); int hour, minute; hour = 11; minute = 59; System.out.print(“The current time is “); System.out.print(hour); System.out.print(“:”); System.out.print(minute); System.out.println(“.”); // output: The current time is 11:59. } } Think Java (TSG@ROLF)

  33. Keywords • There are certain words that are reserved in Java. These words are called keywords. • The complete list is available at http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html , provided by Oracle. Think Java (TSG@ROLF)

  34. Java Reserved Words abstract assertboolean break bytecase catch char class const continuedefault do doubleelse enum extendsfalse final finally float forgotoif implements import in instanceof int interfacelongnative new nullpackage private protected publicreturnshort static strictfp super switch synchronizedthis throw throws transient try truevoid volatilewhile Think Java (TSG@ROLF)

  35. Operators • Operators are symbols used to represent computations like addition. • addition is +, subtraction is -, multiplication is *, and division is /. • Expressions can contain both variable names and numbers. • Variables are replaced with their values before the computation is performed. Think Java (TSG@ROLF)

  36. Operators • What is the result? minute = 59; System.out.println(minute/60); // 0.98333? • Integer division • The result is 0 because Java is performing integer division. • When both operands are integers (operands are the things operators operate on), the result is also an integer, and by convention integer division always rounds down. Think Java (TSG@ROLF)

  37. Order of operations • When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. • A complete explanation of precedence can get complicated, but just to get you started: • Multiplication and division happen before addition and subtraction. So 2*3-1 yields 5. • If the operators have the same precedence they are evaluated from left to right. So 59*100/60 yields 98. • Any time you want to override the rules of precedence you can use parentheses. So 2*(3-1) yields 4. Think Java (TSG@ROLF)

  38. Operators

  39. Operators

  40. Operators for Strings • In general you cannot perform mathematical operations on Strings, even if the strings look like numbers. the following are illegal bob – 1 “Hello”/123 bob * “Hello” • For Strings, the + operator represents concatenation, which means joining up the two operands. So “Hello, “ + “world.” yields “Hello, world.” Think Java (TSG@ROLF)

  41. Arithmetic operators + Addition (also used for String concatenation) x = 1 + 1; String helloWorld = “hello” + “world!”; - Subtraction x = 3 - 1; * Multiplication x = 2 * 1; / Division x = 4 / 2; % Remainder (Mode) x = 5 % 3;

  42. Unary operator + Unary plus x = +1; // x = x + 1; - Unary minus x = -1; // x = x – 1; ++ Increment (prefix or postfix) System.out.println(++x); // prefix System.out.println(x++); // postfix -- Decrement (prefix or postfix) System.out.println(--x); // prefix System.out.println(x--); // postfix ! Logical Complement boolean success = true; !success;

  43. Equality operator == Equal to if (x == y) System.out.println(“x == y”); != Not equal to if (x != y) System.out.println(“x != y”);

  44. Relational Operator > Greater than if (x > y) System.out.println(“x > y”); >= Greater than or equal to if (x >= y) System.out.println(“x >= y”); < Less than if (x < y) System.out.println(“x < y”); <= Less than or equal to if (x <= y) System.out.println(“x <= y”);

  45. Conditional operator && Conditional-AND if ((x == 1) && (y == 1) System.out.println(“x is 1 AND y is 1”); || Conditional-OR if ((x == 1) || (y == 1) System.out.println(“x is 1 OR y is 1”); ?: Tenary (shorthand for an if-then-else) int result = (x > y) ? x : y; • “Short-circuiting” behavior means that the second operand is evaluated only if needed.

  46. Bitwise Operator ~ Unary bitwise complement “00000000”  “11111111” << >> Signed left or right shift x << 2; y >> 2; >>> Unsigned right shift (shifts a zero into the leftmost position) & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR

  47. Composition • One of the most useful features of programming languages is their ability to take small building blocks and compose them. int percentage; percentage = (minute * 100) / 60; System.out.println(hour * 60 + minute); • WARNING: The left side of an assignment has to be a variable name, not an expression. Think Java (TSG@ROLF)

  48. Summary & Exercises Think Java (TSG@ROLF)

  49. Chapter 3: Void methods Think Java (TSG@ROLF)

  50. Floating-point • in Java, the floating-point type is called double, which is short for “double-precision.” double pi; pi = 3.14159; • Initialization: a combined declaration and assignment double pi = 3.14159; • Java distinguishes the integer value 1 from the floating-point value 1.0. int x = 1.1; // illegal double y = 1; // legal, Java automatically converts double z = 1 / 3; //what’s the answer? 0.333333 or 0? Think Java (TSG@ROLF)

More Related