Presentation is loading. Please wait.

Presentation is loading. Please wait.

April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS 102-02 Lecture 4-1 Java's Work Horses.

Similar presentations


Presentation on theme: "April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS 102-02 Lecture 4-1 Java's Work Horses."— Presentation transcript:

1 April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS 102-02 Lecture 4-1 Java's Work Horses

2 April 20, 1998CS102-02Lecture 4-1 What’s up? What’s a method? Returning values from methods Java’s built-in classes Static members

3 April 20, 1998CS102-02Lecture 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

4 April 20, 1998CS102-02Lecture 4-1 “Methods, I Do Declare!” The syntax for declaring a method is: public float Area(String shapeName) Access modifier Return type Method name Parameter type & name

5 April 20, 1998CS102-02Lecture 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?

6 April 20, 1998CS102-02Lecture 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

7 April 20, 1998CS102-02Lecture 4-1 Returning from Methods // Do some stuff sumOfDice = rollDice(); // jump into rollDice return workSum; // Do the next line switch (sumOfDice ) {

8 April 20, 1998CS102-02Lecture 4-1 Returning an Expression Returning a value return true; Returning an expression return 9 + 12; return n * factorial(n-1);

9 April 20, 1998CS102-02Lecture 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

10 April 20, 1998CS102-02Lecture 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

11 April 20, 1998CS102-02Lecture 4-1 Reuse, Reuse, Reuse Don’t write code someone else has written already Java includes: –The core language –Libraries

12 April 20, 1998CS102-02Lecture 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

13 April 20, 1998CS102-02Lecture 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)

14 April 20, 1998CS102-02Lecture 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

15 April 20, 1998CS102-02Lecture 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

16 April 20, 1998CS102-02Lecture 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.”

17 April 20, 1998CS102-02Lecture 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

18 April 20, 1998CS102-02Lecture 4-1 Employee Example public class Employee{ private String firstName; private String lastName; private static int employeeTotal; public Employee(SSN socialSecurity) { employeeTotal++; : }


Download ppt "April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS 102-02 Lecture 4-1 Java's Work Horses."

Similar presentations


Ads by Google