1 / 40

Don’t wait until the last minute to get help

Tip #1. Don’t wait until the last minute to get help. Tip #2. Hey, I’ll still pass if I can get enough partial credit. Bad things happen while learning a new skill. You will probably crash and burn on some programs. Start early; give yourself time for mistakes. Tip #3.

annona
Télécharger la présentation

Don’t wait until the last minute to get help

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. Tip #1 Don’t wait until the last minute to get help

  2. Tip #2 Hey, I’ll still pass if I can get enough partial credit. Bad things happen while learning a new skill. You will probably crash and burn on some programs. Start early; give yourself time for mistakes.

  3. Tip #3 Don’t be too ambitious with your course load. You CANNOT slack off (kaytarmak) in this class, even for a few days.

  4. Tip #4 Watch out for the “big picture”. Don’t forget this is a programming course, not a Java course. It’s dangerous to hide from the programming part of the course. You may be crushed on the final.

  5. Bottom Line • If you’re not adequately prepared for this course: • Option 1: Get prepared • Option 2: Drop Now...Do us both a favor! • Will have take home quiz to test your knowledge of the prerequisites.

  6. Additional Resources Sun also has a free ~1,000 page book on Java. You should download or bookmark this resource. http://www.javasoft.com/docs/books/tutorial/index.html

  7. Introduction to Java • What Java is: • A tool for programming well • Portable across any hardware platform that has a JVM interpreter • Relatively easy to learn if you have a good foundation • An object-oriented language • What Java is not: • “The Ultimate Programming Language” • HTML or another web-content language • Only useful for web applets • Just Another Vacuous Acronym

  8. Introduction to Java (cont’d) • Strengths of Java: • A real language, in demand in industry • Portability • Comparatively easy to learn • Difficult to destroy a machine with it ;-) • Advanced built-in GUI/Graphics features • Weaknesses of Java: • Slow: interpreted and OO • GUI/Graphics via “Least Common Denominator” approach (due to platform independence) • Awkward/annoying syntax obscures some concepts and principles

  9. Java’s Popularity • Keys to Java’s Popularity: • An OO language that’s relatively simple. • Virtual Machine approach, allowing transportability to various different kinds of computers (operating systems). • Presence of JVM as part of Web browsers, encouraging movement of Java programs over the Web.

  10. Structure of Java Programs • Applications (“normal” computer programs): • Create one or more Java source files • Compile each source file into a class file • Thus an application will consist of a bunch of these class files. [Not a single executable i.e. .exe] • Send one class file to the Java system • It must have a method (module) called main: public static void main(String[ ] argv) ( Get used to weird looking stuff! ) • The main method controls program flow (but the OO orientation means that it starts a process that features decentralized control).

  11. Sample Application (in a file called “HelloWorld.java”) public class HelloWorld { public static void main(String argv[]) { System.out.println(“Hello World!”); } }

  12. Java File Names Source code files must have the ".java" extension. The file name should match the class name. This naming convention is enforced by most reasonable compilers. Thus, an improperly named java file, saved as "myTest.java": class test { ... } Compiled byte code has the ".class" extension. Incorrect

  13. Java File Names Source code files must have the ".java" extension. The file name should match the class name. This naming convention is enforced by most reasonable compilers. Thus, a properly named java file, saved as "Test.java": class Test { ... } Compiled byte code has the ".class" extension.

  14. Big Picture Time HelloWorld.java HelloWorld.class 0xCAFEBABE ... javac public class HelloWorld { public static void main(String argv[]){ System.out.println (“Hello World!”); } } java HelloWorld javac HelloWorld.java

  15. Java File Structure • Java Files: • 1. Consist of the optional package statement, • 2. followed by any necessary import statements • 3. followed by the class name, • 4. followed by any inheritance • and interface declarations. • 5. Note: if the file defines more than one class or interface, only one can be declared public, and the source file name must match the public class name.

  16. An Average Java File • Thus: • package fatih.edu.ceng217; • import java.util.*; • import fatih.edu.ceng217.lecturenotes.*; • import netscape.javascript.JSObject; • import netscape.javascript.JSException; • public class SplayTree implements TreeType, TreeConstants • { ... • }// SplayTree • Note the globally unique • package name. Without a • package specification, • the code becomes part of an • unnamed default package in • the current directory.

  17. Java Portability The Usual Way: “Source Code” OS-specific compiler or interpreter OS-specific “Object Code” Execute program The Java Approach: “Source Code” Java compiler Generic “Byte Code” Execute program OS-specific “Object Code” OS-specificJVM interpreter Demo.java javac Demo.java Demo.class java Demo

  18. 4 “atomic data types” + String Num (number) Char (character) Boolean Ptr (pointer) String Java: (6 important “primitives” + String) int (integer) long (long integer, 2x bits) float (real number) double (real number, 2x bits) char (character, use single quotes: ‘b’) boolean String (Java is case sensitive, so capitalize first letter here: String, not string; use double quotes: “a string”) Built-in Data Types Note: A String is NOT a primitive

  19. List of Data Types Primitive Type Default Value boolean false char '\u0000' (null) byte (byte) 0 short (short) 0 int 0 long 0L float 0f double 0d void N/A

  20. Variable Declarations • Java: • <datatype> <identifier>; • or (optional initialization at declaration) • <data type> <identifier> = <init value>;

  21. Examples More on these assignment examples... int counter; int numStudents = 583; float gpa; double batAvg = .406; char gender; char gender = ‘f’; boolean isSafe; boolean isEmpty = true; String personName; String streetName = “North Avenue”;

  22. Questions?

  23. Assignment • Java allows multiple assignment. • int theStart, theEnd; • int width = 100, height = 45, length = 12; • This tends to complicate javadoc comments, however: • /** • * Declare cylinder’s diameter and height • */ • int diameter = 50, height = 34; Javadoc comment gets repeated twice in output, once above each listed variable!

  24. Examples • Note that whole integers appearing in your source code are taken to be ‘ints’. So, you might wish to flag them when assigning to non-ints: float maxGrade = 100f; // now holds ‘100.0’ double temp = 583d; // holds double precision 583 float anotherTemp = 5.5; // ERROR! // Java thinks 5.5 is a double • Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but we should prefer L): float maxGrade = 100F; // now holds ‘100.0’ long x = 583l; // holds 583, but looks like 5,381 long y = 583L; // Ah, much better!

  25. Primitive Casting • Conversion of primitives is accomplished by (1) assignment with implicit casting or (2) explicit casting: • int total = 100; • float temp = total; // temp now holds 100.0 • When changing type results in a loss of precision, an explicit ‘cast’ is needed. This is done by placing the new type in parens: • float total = 100f; • int temp = total; // ERROR! • int theStart = (int) total; • We will see much, much more casting with objects (later) . . .

  26. Casting: Test Your Knowledge • Given: char c = ‘A’; int x; x = c; • Legal? Trick question • Given: int theStart = 10; float temp = 5.5f; temp = temp + (float)theStart; • What does theStart now hold? 15.5 Remember: Everything’s a number at some level 65

  27. Operators • Assignment: = • Arithmetic: +, -, *, /, % (mod), and others int numLect = 2; int numStudents = 583; int studentsPerLect; studentsPerLect = numStudents / numLect; // gives 291 due to integer division int numQualPoints = 30; int numCreditHours = 8; float GPA; GPA = numQualPoints / numCreditHours; // gives 3.0 due to integer division someIntVar = someIntVar * someFloatVar // gives compile-time error

  28. Test Your Knowledge iVar = (int) (iVar * fVar) iVar = (int) iVar * fVar iVar = iVar * (int) fVar iVar = (int) ((float) iVar * fVar) SOLUTION VARIETY PACK • Here’s the problem: int iVar = 10; float fVar = 23.26f; // gives compile-time error iVar = iVar * fVar; • Which solution works best? 3 230 4 1 232 232 2 Lesson: write code that’s easily understood. Same Compile Error

  29. Shorthand Operators counter = counter + 1; //OR: counter++; counter = counter - 1; //OR: counter--; counter = counter + 2; //OR: counter+=2; counter = counter * 5; //OR: counter*=5;Last two examples: it’s “op” then “equals” (e.g., +=2), not “equals” then “op” (e.g., isn’t =+2) • We will see examples with recursion where the shorthand operator potentially causes a problem.

  30. Documentation & Comments • Three ways to do it: // Double slashes comment out everything until the end of the line /* This syntax comments out everything between the /* and the */. (There are no nested comments as in C++. */ /** * This is syntax for Javadoc comments (similar to second style * of commenting, but allows for HTML formatting features. */ • For CENG217, use Javadoc comments

  31. Some Comments on Comments worthless Good for blocks Never closed • 1. C-style comments with /* */; no nesting • 2. C++ style comments beginning // • 3. A unique "doc comment" starting with /** ...*/ • Fun with comments: • /*/ • /* // */ • /////////////////// • /* ========= */ Lesson: Comments should be helpful; don’t worry about compiler tricks with syntax.

  32. Commenting Factoids • Watch for comments that open, but never close: int x, y; /* * Here, we declare the * the point coordinates. // int z; */ Lesson: Java encourages clear code through the type of operators and comments it allows! A syntax highlighting editor is your friend!

  33. Javadoc /** * <PRE> * Get the name. * Returns the name at a * specified array location. * </PRE> * @param i the index of the array to * be retrieved. * @return strName the name * @see Employees#isEmployed() called to * verify employment */ public String getName (int i) { if (myEmpl.isEmployed()) return myArray[i]; else return "Nada"; } // getName(int)

  34. Javadoc (Cont’d) • You may include HTML tags (but avoid structuring tags, like <H1>, etc.) • Javadoc comments should immediately preceed the declaration of the class, field or method. The first sentence should be a summary. Use the special javadoc tags--@. When '@' tags are used, the parsing continues until the doc compiler encounters the next '@' tag. @see <class name> @see <full-class name> @see<full-class name#method.name> @version @author @param @return @exception @deprecated // jdk 1.1 @since // jdk 1.1 @serial // jdk 1.2

  35. Constants • Java: • public final static <type> <IDer> = <value>; • public final static int MIN_PASSING = 60; • public final static float PI = (float) 3.14159; • Details on “why this syntax” to come soon...

  36. Printing to Screen • Java: • System.out.println(<argument>); • System.out.println( ); // prints blank line • System.out.println(5); // prints 5 • System.out.println(“Hello World”); // prints Hello World • “println” vs. “print” in Java: • println includes “carriage return” at end, next print or println on new line • print causes next print or println to begin at next location on same line

  37. Printing (cont’d) • When starting Java, there are at least three streams created for you: System.in // for getting input System.out // for output System.err // for bad news output • These are InputStream and PrintStream objects • Note: For Win95/NT System.out is "almost" identical to System.err they both display to the screen (the one exception is when using DOS redirect, >, where System.out is redirected, while System.err is still put to the screen.)

  38. Printing (cont’d) System.out.println ("This line is printed out") System.err.println ("This is the error stream's output"); These are both instances of the PrintStream class. There are other methods you might find useful in these classes: System.out.flush(); System.out.write(int); System.out.write(byte[] buf, int offset, int length); // eek! Don’t use (needed for autograder)

  39. Summary • Java Basics: Summary • Java Programs • JVM, applications & applets • Entry point is main() or init() • Java Primitives and Operators • Primitive data types • Operators: straightforward except for = and == • Your new best friend: System.out.println( )

  40. Questions

More Related