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

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
GUIs Part 4 CS221 – 4/17/09. Professional Assignments Assignment #2 – Download and install Visual Studio 2008 Professional Trial Software –
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Principles of Object-Oriented Software Development The language Java.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
University of Houston-Clear Lake Proprietary© 1997 Summary of Chapter 3 A class is a collection of data and methods that operate on that data. An object.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Lecture Notes – Classes and Objects (Ch 7-8) Yonglei Tao.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
23-October-2002cse JavaIntro © 2002 University of Washington1 Intro to Java CSE 413, Autumn 2002 Programming Languages
School of Computer Science & Information Technology G6DICP - Lecture 14 Class Libraries.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Package Advantage of Java Package
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Variables in Java A variable holds either
Inheritance-Basics.
Java Basics Packages.
IFS410: Advanced Analysis and Design
The Building Blocks Classes: Java class library, over 1,800 classes:
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Programs and Classes A program is made up from classes
Packages & Random and Math Classes
Chapter 6 Objects and Classes
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Interfaces,Packages and Threads
Presentation transcript:

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

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

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

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

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?

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

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 ) {

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

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

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

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

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

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)

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

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

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.”

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

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++; : }