Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Notes: Chapter 2 First Java Programs Secure: Robust: Portable: Encapsulation – Enables objects to hide their implementation from other objects – a principle called information hiding. Preventing outside classes from changing field variables. Implies the idea of DO ONE THING AND DO IT WELL!! Instance Method: Static Method: Library: BASIC OUTLINE OF A JAVA APPLICATION: //Purpose: To convert a Fahrenheit temperature to Celcius // Name: Mrs. Spurgeon // Date: Nov. 1, 2007 import java.util.Scanner; public class TempConverter { public static void main (String [] args) { int fahrenheit; double celcius; System.out.println ("Temperature Converter"); Scanner reader = new Scanner (System.in); System.out.print ("Enter a fahrenheit temperature: "); fahrenheit= reader.nextInt(); celcius = (fahrenheit - 32.0) * 5.0/9.0; System.out.println ("The equivalent temperature in Celcius is " +celcius); } } Options to using an existing library or class: 1. Import the entire library: ____________________________ 2. Import just the classes you need: ____________________________ Website for Java API Documentation: http://java.sun.com/j2se/1.5.0/docs/api/ Displaying output: System.out.print (“Programming ”); System.out.println(“is”); System.out.println (“ cool\n” + “Have fun!”); Primitive Type: EIGHT primitive types in Java Type Size String Method Summary: Range import java.lang.String char charAt (int index) int compareTo(String s2) boolean equals (Object anObject) int indexOf(char ch) String substring (int begIndex) String substring (int beginIndex, int endIndex) Comparing two strings: s1.equals(s2) NOTE: Do not use == to compare strings! ________________________________ ________________________________ ________________________________ ________________________________ _____________________________ ________________________________ ________________________________ EasyReader and EasyFormat classes: (from www.skylit.com\javamethods) To open the standard input stream for reading keyboard input use EasyReader reader = new EasyReader(); reader is the name you give to the input stream (can be anything you like). Examples for reading data from the keyboard: int n = console.readInt(); // reads an integer double x = console.readDouble(); // reads a double Note: readInt, readDouble, readChar, and readWord methods do not consume the end of the line after reading the last item. Call readLine to get rid of the tail of the line (even if only the newline character is left) before calling readLine on the next line. Working with EasyFormat The EasyFormat class has static methods for placing items of different types into a string of a given "width" (length) and right-justified. Example: System.out.println("[" System.out.println("[" System.out.println("[" System.out.println("[" System.out.println("[" System.out.println("[" Output: + + + + + + EasyFormat.format('A', 5) EasyFormat.format("ABCDEF", 5) EasyFormat.format(567, 5) EasyFormat.format(-567, 5) EasyFormat.format(56.789, 5, 1) EasyFormat.format(-56.789, 5, 1) + + + + + + "]"); "]"); "]"); "]"); "]"); "]"); // // // // // // [ A] [ABCDEF] [ 567] [ -567] [ 56.8] [-56.8] Example program to write: Write a program that asks a user for a location and a number of feet. Calculate and print the equivalent number of miles, yards and inches. Graphics and GUIs: Windows and Panels Standalone GUI applications run in windows. – Containers for graphical components to be displayed to the user Windows have numerous properties. – Width and height – Title bar – Ability to be dragged or resized by the user By default, JFrame uses the border layout //creating an empty frame import javax.swing.*; //used to create a JFrame public class GUIWindow { public static void main (String[] args) { } //end of main } //end of GUIWindow Some commonly used JFrame methods