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
A Method to the Madness Java's Work Horses CS 102-02 Lecture 4-1 April 20, 1998 CS102-02 Lecture 4-1 What’s up? • • • • What’s a method? Returning values from methods Java’s built-in classes Static members April 20, 1998 CS102-02 Lecture 4-1 What’s a Method? • Objects know how to do things • A method is a set of instructions for doing something • A method shouldn’t do too much – If the method’s code is more than half a page, it’s probably doing too much April 20, 1998 CS102-02 Lecture 4-1 “Methods, I Do Declare!” • The syntax for declaring a method is: Method name Parameter type & name public float Area(String shapeName) Access modifier April 20, 1998 Return type CS102-02 Lecture 4-1 Returning Values • Calling a method returns a value – Returned value could be a primitive type or a reference type – Objects and arrays are okay • Don’t have to return a value – Return type void • Why write a method that doesn’t return a value? April 20, 1998 CS102-02 Lecture 4-1 Returning with return • To return a value, use the syntax: return expression; • The return statement doesn’t have to be the last statement in the method • When a return is executed: – Current method finishes executing – Control jumps back to the line following the method invocation April 20, 1998 CS102-02 Lecture 4-1 Returning from Methods // Do some stuff sumOfDice = rollDice(); // jump into rollDice return workSum; // Do the next line switch (sumOfDice ) { April 20, 1998 CS102-02 Lecture 4-1 Returning an Expression • Returning a value return true; • Returning an expression return 9 + 12; return n * factorial(n-1); April 20, 1998 CS102-02 Lecture 4-1 Brown Paper Packages, Tied Up in Strings • Java lets you organize code into classes and packages • Packages are really just groups of related classes • Packages help prevent name collisions among developers of similar classes April 20, 1998 CS102-02 Lecture 4-1 Bundles of Java • Java has special rules about declaring classes to be part of a package – Usually classes are stored in directories which mimic the package name – java.awt.datatransfer classes are stored in a directory java/awt/datatransfer/ • If you don’t declare a package, your class is part of the default package April 20, 1998 CS102-02 Lecture 4-1 Reuse, Reuse, Reuse • Don’t write code someone else has written already • Java includes: – The core language – Libraries April 20, 1998 CS102-02 Lecture 4-1 Java’s Built-in Classes • The Applet Package (java.applet) – The all-important Applet class • The AWT Package (java.awt) – Java’s Abstract Windowing Toolkit, a library for building cross-platform graphical user interfaces – Includes sub-packages and classes for events, images and data transfer April 20, 1998 CS102-02 Lecture 4-1 More Built-in Packages • Beans (java.beans) – A component model for designing reusable software. • • • • IO package (java.io) Core Java classes (java.lang) Networking (java.net) Remote Method Invocation (RMI, in java.rmi) April 20, 1998 CS102-02 Lecture 4-1 Still More Packages • SQL (java.sql) – Classes for database access using SQL • Text (java.text) – Classes for manipulating text, especially international text • Utilities (java.util) – Utility data structures, date, time and more April 20, 1998 CS102-02 Lecture 4-1 Some Java Nitty-Gritty • With data members, every object has its own copy of data – Since objects are also called instances, data members are often called instance variables • All objects instantiated from a class share the same methods – Don’t need a copy for each object April 20, 1998 CS102-02 Lecture 4-1 One Class, One Copy • What if you want all objects of the same class to share a variable? • Use the static keyword – When you declare a variable static, you’re telling Java, “All objects of this class share this variable’s value.” April 20, 1998 CS102-02 Lecture 4-1 Using static • Include the static modifier in the declaration • Suppose we wanted to track the total number of employees created from an Employee class – Create a static vairable to track the total – Add one to the total in the constructor – Subtract one in the destructor April 20, 1998 CS102-02 Lecture 4-1 Employee Example public class Employee{ private String firstName; private String lastName; private static int employeeTotal; public Employee(SSN socialSecurity) { employeeTotal++; : } } April 20, 1998 CS102-02 Lecture 4-1