CompSci 125 Lecture 05 Programming Style, String Class and Literals, Static Methods, Packages
Homework Update HW2 Due 9/13
Programming Assignment Update p1: Traffic Applet due Sept 21
Programming Style
Review: Two Audiences for Commercial Software 1. Java: You need to correctly express your intentions to the Java Runtime Environment (JRE) 2. Other Programmers: You need to explain what you wrote, how it works and perhaps why you did it this way to other programmers
Code Health Commercial source code lives for years after its written after you have been promoted or moved to another project Other programmers will live with your source code You will become known by the code you leave behind Healthy code is easy to repair Healthy code is easy to enhance
Programming Style Contributes to Code Health Programming Style refers to the appearance of source code Ugly code may work correctly But not for long! Ugly code can become error prone. http://developers.sun.com/sunstudio/products/archive/whitepapers/java-style.pdf
Programming Style: Indentation public static void main(string[] args) {!!//Statements! }!
Programming Style: Identifier Names Terse names usually acceptable for throw-away variables whose meaning is obvious and scope is small: int i;! double x;! Descriptive names for everything important/far-reaching: int employeeage;! double altitudeinfeetabovegroundlevel;!
Programming Style: Identifier Capitalization Names of variables and methods begin with lower-case: int enginerpm;! public static int getnumberofcylinders() { }! Capitalize first letter of class names: public class Dragon { }! Use all capital letters for constant names: final double PI=3.1415926536;!
String Class and Literals
Review: Objects and their Reference Variables An object is an instance of a class constructed in memory An object has an address but not a name Usually, we won t know the address of an object We locate objects with reference variables The value of a reference variable is an address of an object
Sometimes we say, A reference variable points to an object String employeename = new String( Jim Conrad );! employeename Jim Conrad
Aliases: More than one variable may reference a single object String employeename = new String( Jim Conrad );! String instructorname = employeename;! employeename Jim Conrad instructorname
Aliases: Each Invocation of a constructor builds a new instance of the class String employeename = new String( Jim Conrad );! String instructorname = new String( Jim Conrad );! employeename Jim Conrad instructorname Jim Conrad
Java Implements a Short-cut for Building String Literals String employeename = Jim Conrad ;! employeename Jim Conrad
But the short-cut doesn t always build a new String object!!! String employeename = Jim Conrad ;! String instructorname = Jim Conrad ;! employeename Jim Conrad instructorname
Garbage Collection (Re-cycling) of Unreferenced Objects String employeename = new String( Jim Conrad );! String instructorname = new String( Jim Conrad );! instructorname = employeename;! employeename Jim Conrad instructorname Jim Conrad
Null Reference String employeename = new String( Jim Conrad );! String instructorname = new String( Jim Conrad );! instructorname = null;! employeename instructorname Jim Conrad Jim Conrad An Unreferenced object
String Methods
Review Methods Methods define the behaviors of an object An object is an instance of some class and implements the methods defined in that class A method consists of a sequence of Java instructions to be performed by an object A constructor is a special method that builds a new object
Methods Defined in the Built-In String Class The Built-In String class defines many methods Textbook Section 3.2 lists some These methods determine what a String object can do, its behaviors http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/string.html
Example String Methods String(String s)!!//constructs new String object! char charat(int index)!//extracts char at position! int length()!!//retrieves number of chars! boolean equals(string s)!//true if content identical to s!
Invoking Methods on a String Object String s = ABC ;! String q = Kyle ;! int numberofcharactersins = s.length();! char charat0 = s.charat(0);! char charat1 = s.charat(1);! char charat2 = s.charat(2);! boolean siskyle = s.equals( Kyle );! boolean qiskyle = q.equals( Kyle );!
Packages
Built-In Java Classes Organized into Packages Java includes hundreds of pre-defined classes These classes are organized into packages Collectively known as the Java Application Programming Interface (API) You must import a class to use it in your program Except classes in java.lang are automatically imported import java.util.scanner;!//imports Scanner class! import java.util.*;!!//imports entire java.util package!
The import Statement import java.util.random;! Package Name
The import Statement import java.util.random;! Class Name
Documentation http://docs.oracle.com/javase/6/docs/api Often easier to google for what you want google java String google java Scanner google java Graphics google java Color
Packages java.util.scanner: Text parser java.util.random: Generates random numbers
static Methods Methods ordinarily implement behaviors of an object Static Methods are defined in and associated with a class Not associated with any object! Useful for mathematical functions And a Java application s main method
java.lang.math The Math class defines only static methods! Math.abs(n) --- Absolute value of int n Math.pow(x,y) --- x y Math.sin(x) --- Trig function, sin(x) where x is in radians Math.sqrt(x) --- Square root of x