1 / 65

Introduction to Java

Introduction to Java. IST 311 / MIS 307. Java. Three flavors: Java Standard Edition (Java SE) – client-side standalone application or applets Java Enterprise Edition (Java EE) – server-side applications like servlets & JavaServer Pages (JSP)

katharynj
Télécharger la présentation

Introduction to Java

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. Introduction to Java IST 311 / MIS 307

  2. Java • Three flavors: • Java Standard Edition (Java SE) – client-side standalone application or applets • Java Enterprise Edition (Java EE) – server-side applications like servlets & JavaServer Pages (JSP) • Java Micro Edition (Java ME) – mobile devices like cell phones

  3. Java • Java SE 6 – latest version • Java Development Toolkit (JDK) • JDK 1.6 (Java 6 or JDK 6) • Set of programs for developing & testing Java programs from command line • Integrated Development Environment (IDE) • Eclipse for editing, compiling, building, & debugging programs

  4. Java • Examples in textbook include line numbers. Feature which may be turned on or off in Eclipse. • Source code • Right click to bring up list • Select Preferences • Click on link to Text Editors • Check mark on Line Numbers • Click OK

  5. Objects • An object has structure • Attributes (characteristics) • Behaviors or operations it can carry out (actions)

  6. Class Definition • A Java program is written as a class definition • A Java application is a class that contains a main( ) method

  7. Class Definition • Import Declaration • Statements to identify the predefined classes used in a Java program • Import declarations must appear before the class declaration • Most Java API’s begin with either “java” (core classes) or “javax” (optional classes) import javax.swing.JOptionPane; import java.io.*;

  8. Class Definition • Class Definition has two parts: • Class Header • Class’s name; must be the same name as the .java file • Accessibility as public or private • Pedigree which specifies where it fits in the Java class hierarchy; default is Object class • Class Body • Enclosed within curly brackets { } • Contains instance variables to store data • Contains methods for the actions or behaviors

  9. Class Definition • Identifier – name for a class, method, or variable • Must begin with a letter, A to Z, or a to z, an underscore (_), or a dollar sign ($); don’t use $ for your identifiers • May be followed by any number of letters, digits, 0 to 9, an underscore, or dollar sign • May not use a Java keyword (see Appendix A, pg. 709) • Java is case sensitive taxrate vs. TaxRate

  10. Class Definition • Data Types – two types in Java • Primitive Data Types (pg.32, Table 2.2) boolean char byte short int long float double • Objects or Reference Variables– programmer created through a class definition • String is an object and therefore a data type

  11. Class Definition • Variables – named storage location in memory where a value of a given data type may be stored

  12. Start Lab 2

  13. Class Definition • Method Definition • Code that carries out a specific task or behavior of the class • Method heading and body • Heading contains • the name • accessibility as public, private, or protected • type of data returned by the method • list of parameters

  14. Class Definition • Method Definition • Body contains • Executable statements System.out.print(“Hello.”): • Local variable declarations • Computations or assignment statements sum = num1 + 5; • Calls to other methods • Return statements return sum;

  15. Flow of Control: Methods

  16. Input from Keyboard • Input is complicated task • Things can go wrong • User types a letter instead of a number • File could be missing • Java has three I/O streams at startup • System.in : refer to standard input device (keyboard) • System.out : refer to standard output device (display) • System.err

  17. Input from Keyboard • java.util.Scanner class has a list of methods for data types(pg. 27) • nextByte( ) • nextInt( ) • nextDouble( ) • next( ) • nextLine( ) Scanner input = new Scanner(System.in); double radius = input.nextDouble();

  18. Scanner Class • Selects chunks of data from input stream • Stops at the delimiter • Default delimiter = white space • Space, tab, return, newline characters • What happens when type full name in program?

  19. Output • In Java, any source or destination for I/O is considered a stream of bytes or characters • To perform output, we insert bytes into the stream • I/O is handled through methods that belong to classes contained in: • java.io package

  20. Output • java.io.PrintStream class contains methods • print( ) • println( ) //prints a line feed after data • System.out andSystem.err can be used to write things to the console • System.out.print(“Hello”);

  21. Math

  22. Assignment Statements • Value are placed into variables using assignment statement • Assignment operator = • Data type of variable on left must be compatible with data type on the right int x = 1; x = 1; x = x + 1;

  23. Constants • A constant is a final variable associated with the class rather than with its instances • final – declare a variable that has a value that never changes; must be initialized; named using uppercase by convention public final int EATING = 0;

  24. Object Instantiation • Define objects (classes) • To use the object it must be instantiated or invoked • We will do this in the main( ) method • Object instantiation is an example of a reference variable Lab1 x;// reference variable declared x = new Lab1( );//object instantiated x.user_interface( );//method call

  25. Naming Conventions • Choose descriptive names with straightforward meanings • First word lowercase and capitalize first letter of subsequent words showInputDialog • Capitalize first letter of class name ComputeArea

  26. Proper Indentation & Spacing • Consistent indentation style makes programs clear & easy to read, debug, and maintain int i= 3+4 * 4; inti = 3 + 4 * 4; Bad style Good style

  27. Block Style • Next-line style • End-of-line style – Java API standard • Both are acceptable; pick one and stick with it

  28. Testing and Debugging • Syntax Errors • Error messages from the compiler • Fatal or Warnings • Run-Time Errors • Cause the program to terminate abnormally • Logic Errors • Mistakes in algorithm used to solve problem

  29. Homework 1

  30. Numeric Operations

  31. Numeric Operations • 4 + 5.0 * 6 • (4 + 5) * 6 • 4 + 5 % 3 • 9 % 2 * 7 / 3

  32. Increment & Decrement Operators • When used in isolation, K++ is the same as ++K int k; k = k + 1;

  33. Increment & Decrement Operators

  34. Shorthand Operators CAUTION: There are no spaces in the shorthand operators.

  35. Character Data • Java supports Unicode character set enables characters in a wide variety of languages • ASCII character set is 7-bit code, defines 0 to 127 for the letter, digits, and special characters • First 128 characters of Unicode match ASCII character set, making them backward compatible

  36. Character Data • Cast operation such as int to convert one type of data to another type char ch = ‘a’; System.out.println( ch);//displays a System.out.println( (int)’a’);//displays 97

  37. Escape Sequences System.out.println(“He said “Jave is fun””); • Syntax error from second quotation character. • Overcome this by using Escape Sequence characters. System.out.println(“He said \“Jave is fun\””); He said “Java is fun”

  38. Escape Sequences

  39. Format Output • java.text.NumberFormat class • Represent numbers as dollar amounts, percentages, and other formats import java.text.NumberFormat; NumberFormat dollars = NumberFormat.getCurrencyInstance( ); System.out.println(dollars.format(5129.95)); $5,129.95

  40. Format Output import java.text.NumberFormat; NumberFormat percent = NumberFormat.getPercentInstance( ); percent.setMaximumFractionDigits(2); System.out.print(percent.format( rate / 100.0);

  41. Format Output • java.text.DecimalFormat class • Specify the number of decimal places to display import java.text.DecimalFormat; DecimalFormat threeDigits = new DecimalFormat(“0.000”); System.out.println( threeDigits.format (number));

  42. Format Output • print( ) and println( ) cannot directly format certain outputs • Default output of floating-point numbers is typically up to 6 decimal place & 15 decimal places for double • Align output in certain columns • Use printf( ) method

  43. Format Output System.out.printf(formatSting, argumentList); • formatString = string specifying the format of the output • argumentList = list of arguments that are constant values, variables, or expressions; multiple arguments separated with commas

  44. Format Output

  45. Format Output int count = 5 double amount = 45.56 System.out.printf(“count is %d and amount is %f” , count, amount); count is 5 and amount is 45.560000 Pg. 96, Table 3.9 for examples

  46. Flow of Control

  47. Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition

  48. Selection • Simple if Statement • Nested if / else Multiway Statement • switch Statement • Alternative way to write a multiway selection structure • Involves a test of equality • Logical expression being tested must be a primitive integral type byte, short, int, char, long, or boolean

  49. boolean Data Type • Java provides six comparison operators to use when comparing two values

  50. boolean Data Type • You can compare characters; however comparison is to ASCII character sets • Equality comparison operator is two equal signs == not a single equal sign used for assignment statement • Result of comparison is a boolean value: true or false

More Related