Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming CIS 421 Web-Based Java Programming.

Similar presentations


Presentation on theme: "Java Programming CIS 421 Web-Based Java Programming."— Presentation transcript:

1 Java Programming CIS 421 Web-Based Java Programming

2 Course Text ä Text: Computing with Java: Programs, Objects, Graphics Alternate 2 nd Edition Art Gittleman Scott Jones Publishers ISBN: 1-57676-074-X

3 Prerequsites, Tests, Public area ä ä Prerequisites: Cis237, Mat 126, and QPA of 2.90 ä ä Tests:Two midterms, one final. ä ä Public directory: /export/home/public/spiegel/cis421

4 Turnin script ä Programming Projects are submitted electronically using turnin script ä See turnin handout ä Must setup turnin once ä To invoke: ä turnin421.... ä each item submitted separated by whitespace ä no limit to # items ä You should receive a confirmation e-mail containing an ls –l listing of files submitted (can include files previously submitted) ä Files submitted may be overwritten by subsequent turnin of files with same name

5 Overview of Java ä Java Features ä How Java Works ä Applications vs Applets ä Program-Driven vs Event Driven ä Graphical User Interfaces (GUI)

6 Java Features ä Simple, Object-Oriented, Familiar ä Robust and Secure ä Architecture Neutral and Portable ä High Performance ä Interpreted, Threaded, and Dynamic

7 Simple, OO, Familiar ä Its simplicity comes from the fact that there are no pointers in Java. Therefore, the programmer does not have to manage pointers and the resultant problems that pointers bring. ä All programs in Java are based on objects ä Java uses the familiar syntax and fundamental control structures of C/C++

8 Robust and Secure ä Robust programs run without crashing due to programming errors, erroneous input, or failure of external devices. Java has many checks at compile-time and provides run-time exception handling to deal with unexpected events. ä Security, especially across the internet, requires careful measures, which are implemented in Java

9 Architecture Neutral and Portable ä Java programs run on a variety of processors using various operating systems ä Portability depends not only on architecture but also on implementation. Java specifies the language carefully to reduce implementation dependencies.

10 High Performance ä Java versions continually increase performance capabilities. ä In network applications, communication delays usually far exceed performance delays.

11 Interpreted, Threaded, and Dynamic ä Interpreted, not compiled. ä Threaded – capable of multi_tasking and concurrent processing, even across the internet ä Dynamic linking to library code as it needs it. ä Java is ideally suited for general, interactive, and network programming

12 How Java Works JavaSourceCode JavaCompiler JavaByteCode Java Interpreter For Processor 1 Java Interpreter For Processor 2

13 Setting up Unix for Java ä Make a directory called java in your home directory. ä Put this java directory in your CLASSPATH in.cshrc with the command (this is one long line) setenv CLASSPATH “.:$home/java:whatever_is_there_already“ ä this allows packages starting from your java subdirectory ä Put the directory /usr/j2sdk1.4.0_03/bin in your path variable in the.login file. ä Give the commands source.login and source.cshrc to make the changes effective. ä These changes are applied automatically on subsequent logins

14 Testing Changes acad > java -version java version "1.4.0_03" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_03-b04) Java HotSpot(TM) Client VM (build 1.4.0_03-b04, mixed mode) acad >

15 Applications vs Applets ä Applications run like programs written in other languages. They usually are standalone, running at the command line. ä Applets are usually small applications that are embedded in a web-page and run on the internet in a web browser.

16 Building an Application ä Edit the source file either in your favorite editor, or IDE, and save it as.java ä The file name must be the same as the one and only public class name in the file ä Compile by giving the command javac.java at the command line of a DOS or Unix window. ä Run the program by giving the command java java

17 hello.java // File: hello.java // Compiler: Sun javac // Compile: javac hello.java // Executable: hello.class // Execute: java hello // // Purpose: Prints usual first // program

18 hello.java public class hello { public static void main(String[] args) public static void main(String[] args) { int x = 3; int x = 3; System.out.println(“Hello, World!” + System.out.println(“Hello, World!” + “\nx = “ + (x) ); “\nx = “ + (x) ); }}

19 Application Programs ä The file name must be the same as the one and only public class in the file. ä This public class must have a static method called main (Why static??) public static void main( String[] args) { local declarations and statements local declarations and statements}

20 Exercise ä ä Setup your Unix account to compile and run a Java Application program. Test it with the HelloWorld program hello.java. ä ä The example is in the java/hello subdirectory in the instructor’s public area

21 Basic Java Programming ä The C/C++ component ä Comments – C/C++ style ä Identifiers and keywords ä Types, variables, expressions ä Control structures ä Functions ä System output ä Console input ä Packages

22 Identifiers and Keywords ä A Java identifier must start with a letter, followed by 0 or more letters and/or digits. Java is case-sensitive. ä Keywords cannot be used as user-identifiers. See text for a list of keywords. ä Style – recommended & preferred; consistency is a must! ä Class names begin with a capital letter ä Variable names begin with a lower case letter ä Function names begin with a verb which is lowercased. ä Constants are all upper case. ä Multiple word names are lower case except for the beginning of each word component.

23 Examples ä Request would be a class name ä myRequest would be a variable name ä getRequest() would be a function (method) name ä THE_REQUEST would be a constant. ä The Java standard style convention should be followed in our programming.

24 Standard Types ä char – ascii or unicode ä boolean – true or false ä Numerical types – various sizes of numbers

25 Numerical Types ä Standard numerical types in Java are typesizeleast valuegreatest value ________________________________________ byte8-128127 short16-3276832767 int32-21474836482147483647 long64-2 63 2 63 -1 float*32~ -3.4 x 10 38 ~ 3.4 x 10 38 double* 64~ -1.7 x 10 308 ~ 1.7 x 10 308 * 7 and 15 digit accuracy respectively

26 Variables and Expressions ä Java follows the syntax of C/C++ for expressions and assignment. ä The operators for the standard types are the same as those for C/C++ ä Remember that = is assignment and == is equal relational operator. ä You should NOT use = in a cascading manner.

27 Control Structures The control structures are the same as C/C++ ä if ä switch ä for ä while ä do – while Note: unlike C/C++, where the expression can evaluate to int, the test expression MUST be of type boolean

28 Functions (Methods) ä In Java there are no independent functions ä A function (method) is always a member function of some class. ä The syntax is very similar. modifier(s) resulttype name( ) { local declarations local declarations and statements and statements } // the modifier is public, private, or protected, and can also be prefaced static // the modifier is public, private, or protected, and can also be prefaced static

29 System Output ä Output is generated by using streams. The stream classes are defined in the standard Java package java.io. ä The class System in the package java.lang contains three different streams for use in Java programs: System.inthe keyboard System.outthe screen System.errthe screen ä System.out.println( any string)

30 Examples of Output ä To print an object, the object should have overloaded the method toString that is inherited from the Class Object. ä Standard types have this method. ä System.out.println(“The value of x = “ + x ); ä The + is the concatenation operator for strings.

31 System Input ä System input is quite complicated, so many authors provide a package of IO functions for the standard types. ä We prefer to avoid these. A class InputReader is in the public java directory for text input. ä More fundamental IO will be discussed later ä Java is made for GUIs, particularly components such as TextFields, Menus. etc;

32 Examples of input ä helloYou.java ä helloInt.java ä java/hello directory in public area

33 Things that are different ä String concatenation is the operator + ä It takes two operands which are “stringable”, that is any operand that is a string or has the toString method overloaded for that type. ä System.out.println(76 + “trombones”);

34 Packages ä Java organizes code into packages which correspond to directories in the file system. ä Each Java class is contained in a package. ä The default package is. (the current directory) ä The System class is found in java.lang ä The Applet class is found in java.applet

35 Comments ä When calling methods of the same class, we do not need to use the class name as a prefix. ä When calling methods of another class, we use the class name or object of that class as a prefix.

36 Objects ä Still declared as a class ä No separate sections ä Each declaration specified with access ä public, private, protected ä static declarations part of class, but not object ä non-static declarations part of instantiated objects only


Download ppt "Java Programming CIS 421 Web-Based Java Programming."

Similar presentations


Ads by Google