Don’t wait until the last minute to get help
description
Transcript of Don’t wait until the last minute to get help
![Page 1: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/1.jpg)
Don’t wait until the last minute to get help
Tip #1Tip #1
![Page 2: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/2.jpg)
Bad things happen while learning a new skill. You will probably crash and burn on some programs.
Start early; give yourself time for mistakes.
Hey, I’ll still passif I can get enough
partial credit.
Tip #2Tip #2
![Page 3: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/3.jpg)
Don’t be too ambitious with your course load. You CANNOTCANNOT slack off (kaytarmak) in this class, even for a
few days.
Tip #3Tip #3
![Page 4: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/4.jpg)
Watch out for the “bigpicture”.
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.
Tip #4Tip #4
![Page 5: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/5.jpg)
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.
![Page 6: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/6.jpg)
Additional ResourcesSun also has afree ~1,000 pagebook on Java.
You shoulddownload orbookmark thisresource.
http://www.javasoft.com/docs/books/tutorial/index.htmlhttp://www.javasoft.com/docs/books/tutorial/index.html
![Page 7: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/7.jpg)
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
![Page 8: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/8.jpg)
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
![Page 9: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/9.jpg)
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.
![Page 10: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/10.jpg)
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).
![Page 11: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/11.jpg)
Sample Application (in a file called “HelloWorld.java”)
public class HelloWorld
{
public static void main(String argv[])
{
System.out.println(“Hello World!”);
}
}
![Page 12: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/12.jpg)
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.
Java File NamesJava File Names
Incorrect
![Page 13: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/13.jpg)
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.
Java File NamesJava File Names
![Page 14: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/14.jpg)
Big Picture Time
HelloWorld.java
public class HelloWorld{ public static void main(String argv[]){ System.out.println (“Hello World!”); }}
HelloWorld.class
0xCAFEBABE ...javac
javac HelloWorld.java java HelloWorld
![Page 15: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/15.jpg)
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.
Java File Structure
![Page 16: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/16.jpg)
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.
An Average Java File
![Page 17: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/17.jpg)
The Usual Way:
“Source Code”
OS-specificcompiler orinterpreter
OS-specific“Object Code”
Executeprogram
The Java Approach:
“Source Code”
Javacompiler
Generic“Byte Code”
OS-specificJVM
interpreter
OS-specific“Object Code”
Executeprogram
Java Portability
Demo.java javac Demo.java Demo.class
java Demo
![Page 18: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/18.jpg)
Built-in Data Types• 4 “atomic data
types” + String– Num (number)– Char (character)– Boolean – Ptr (pointer)– String
• Java: (6 importantimportant “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”)
Note: A String is Note: A String is NOT a primitiveNOT a primitive
![Page 19: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/19.jpg)
List of Data TypesPrimitive Type Default Value
boolean falsechar '\u0000' (null)byte (byte) 0short (short) 0int 0long 0Lfloat 0fdouble 0dvoid N/A
![Page 20: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/20.jpg)
Variable Declarations
• Java:– <datatype> <identifier>;
• or (optional initialization at declaration)– <data type> <identifier> = <init value>;
![Page 21: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/21.jpg)
Examplesint 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”;
More on these
assignmentexamples...
![Page 22: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/22.jpg)
Questions?
![Page 23: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/23.jpg)
Javadoc comment gets repeated twice in output,once above each listed variable!
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;
![Page 24: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/24.jpg)
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!
![Page 25: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/25.jpg)
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) . . .
![Page 26: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/26.jpg)
• Given: int theStart = 10;
float temp = 5.5f;
temp = temp + (float)theStart;
• What does theStart now hold?
Casting: Test Your Knowledge
Trickquestion
• Given: char c = ‘A’;
int x;
x = c;
• Legal?
Remember:Everything’s a number
at some level
15.5
65
![Page 27: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/27.jpg)
Operators• Assignment: =• Arithmetic: +, -, *, /, % (mod), and others
int numLect = 2;int numStudents = 583;int studentsPerLect;studentsPerLect = numStudents / numLect;
// gives 291 due to integer divisionint numQualPoints = 30;int numCreditHours = 8;float GPA;GPA = numQualPoints / numCreditHours;
// gives 3.0 due to integer divisionsomeIntVar = someIntVar * someFloatVar // gives compile-time error
![Page 28: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/28.jpg)
Test Your Knowledge• Here’s the problem:
int iVar = 10;float fVar = 23.26f;// gives compile-time erroriVar = iVar * fVar;
• Which solution works best?
iVar = (int) iVar * fVar
iVar = (int) (iVar * fVar)
iVar = iVar * (int) fVar
1
2
3
Lesson: write code that’seasily understood.
iVar = (int) ((float) iVar * fVar)
4
SOLUTIO
N
SOLUTIO
N
VARIETY PACK
VARIETY PACK
232
Same Compile Error
232
230
![Page 29: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/29.jpg)
Shorthand Operatorscounter = 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.
![Page 30: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/30.jpg)
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
![Page 31: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/31.jpg)
1. C-style comments with /* */; no nesting
2. C++ style comments beginning //
3. A unique "doc comment" starting with /** ...*/ Fun with comments:
/*/
/* // */
///////////////////
/* ========= */
Some Comments on CommentsSome Comments on Comments
worthless
Never closed
Good for blocks
Lesson: Comments should be helpful; don’t worry aboutcompiler tricks with syntax.
![Page 32: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/32.jpg)
Commenting Factoids
Lesson: Java encourages clear code through the type of operators and comments it allows!
• Watch for comments that open, but never close:
int x, y; /* * Here, we declare the
* the point coordinates.
// int z; */
![Page 33: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/33.jpg)
/** * <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)
Javadoc
![Page 34: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/34.jpg)
@author @param@return @exception @deprecated // jdk 1.1 @since // jdk 1.1@serial // jdk 1.2
@see <class name> @see <full-class name> @see<full-class
name#method.name>@version
• 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.
Javadoc (Cont’d)
![Page 35: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/35.jpg)
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...
![Page 36: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/36.jpg)
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
![Page 37: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/37.jpg)
Printing (cont’d)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.)
![Page 38: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/38.jpg)
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!
Printing (cont’d)Printing (cont’d)
Don’t use (needed for autograder)
![Page 39: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/39.jpg)
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( )
![Page 40: Don’t wait until the last minute to get help](https://reader035.fdocuments.us/reader035/viewer/2022062500/5681543d550346895dc23fae/html5/thumbnails/40.jpg)
Questions