280 likes | 335 Views
Introduction to Java. 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. Objects. An object has structure
E N D
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
Objects • An object has structure • Attributes (characteristics) • Behaviors or operations it can carry out (actions)
Class Definition • A Java program is written as a classdefinition • A Java application is a class that contains a main( ) method
Class Definition • ImportDeclaration • 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.*;
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
Class Definition • Identifier – name for a class, method, or variable • Must begin with a letter, A to Z, or a to z, an underscore (_) • May be followed by any number of letters, digits, 0 to 9, an underscore • May not use a Java keyword • Java is case sensitive taxrate vs. TaxRate
Class Definition • Data Types – two types in Java • Primitive Data Types boolean char byte short int long float double • Objects or Reference Variables– programmer created through a class definition • Stringis an object and therefore a data type
Class Definition • Variables – named storage location in memory where a value of a given data type may be stored
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
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;
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
Input from Keyboard • java.util.Scanner class has a list of methods for data types • nextByte( ) • nextInt( ) • nextDouble( ) • next( ) • nextLine( ) Scanner input = new Scanner(System.in); double radius = input.nextDouble();
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?
Output • In Java, any source or destination for I/O is considered a streamof 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
Output • java.io.PrintStream class contains methods • print( ) • println( ) //prints a line feed after data • System.outandSystem.errcan be used to write things to the console • System.out.print(“Hello”);
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;
Constants • A constant is a finalvariable 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;
Object Instantiation • Define objects (classes) • To use the object it must be instantiatedor 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
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
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
Block Style • Next-line style • End-of-line style – Java API standard • Both are acceptable; pick one and stick with it
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