1 / 31

DON’T PANIC!!

DON’T PANIC!!. Lots of new notions coming in these slides Don’t worry if not all of it makes perfect sense We’ll meet most of this stuff again in detail later Do worry if none of it makes any sense You should get the general picture now

lacey
Download Presentation

DON’T PANIC!!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DON’T PANIC!! • Lots of new notions coming in these slides • Don’t worry if not all of it makes perfect sense • We’ll meet most of this stuff again in detail later • Do worry if none of it makes any sense • You should get the general picture now • Now brace yourself … stop me if you get confused … ask questions … throw money …

  2. 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 parts define what objects in the class look like. • Every class is automatically in existence when the program runs.

  3. Classes and Objects • An object is an instance of a class, and is created using the new operator. • The non-static part of the class defines what each object looks like. • Many instances (objects) can be created from a class … no limit except reality • An object contains information and functionality of a “thing”, e.g., Account, Vehicle, Employee, etc.

  4. Classes’ and Objects’ Components • Classes (and thus also objects) are composed of methods and data values • Data values store information • Methods do things, and also have their own local data

  5. The class name appears on top of the icon. The object’s name appears on top of the icon. An icon for a class is the rectangle. An icon for an object is the rounded rectangle. The class name is placed inside the object icon. Graphical Representation Account SV129 Account

  6. Employee Before you can create instances of a class, the class must be defined. The dotted line shows the instance-of relationship. Steve Bill Andy The class name can be omitted since it is clear which class these objects belong to . Employee Employee Employee Instance-of Relationship

  7. Visibility Modifiers: public and private • The modifiers public and private designate the accessibility of objects’ and class’ data values and methods • If a component is declared private, nothing outside the class can access it. • If a component is declared public, anything outside the class can access it.

  8. In general, be private (military demotion) • Make class components private whenever you can • This supports the notion of encapsulation, which makes for more robust software development

  9. Class and Instance Data Values • A class data value (indicated by the static modifier) is used to maintain information shared by all instances or aggregate information about the instances. • An instance data value is used to maintain information specific to individual instances. • Make instance data values private always

  10. minimum balance There is one copy of minimum balance for the whole class and shared by all instances. SV506 SV129 SV008 100.00 Account Account Account current balance current balance current balance 1304.98 908.55 354.00 Sample Data Values Account All three Account objects possess the same instance data value current balance.

  11. Data Type reference primitive long String byte short Applet MessageBox double char InputBox int HiLo boolean etc. float Primitive and Reference Data Values Primitive variables contain values Reference variables point at objects

  12. minimum balance account prefix 100.00 6427 Account SV129 Account current balance opening balance 908.55 100.00 Variable and Constant Data Values There are two types of data values: A variable whose value can change over time. A constant whose value must remain fixed over time. • Constants are indicated by the final modifier • Non-final public class data values will give you warts

  13. Methods • Methods have code (to do stuff) and data • A method defined for a class is called a class method (indicated by the static modifier) and a method defined for an object is called an instance method.

  14. Method Data = Local Variables • A local variable is a variable that is declared within a method. • Local variables are accessible only in the method in which they are declared. • The final and static modifiers do useful things to local variables - more about this later

  15. Messages • To instruct a class or an object to do something, we a message to one of its methods • Values passed to a method when sending a message are called arguments or parameters of the message. • The (formal) parameters of a method are local variables that receive the message parameters • Methods can return one data value to the calling method

  16. Message deposit with the argument 250.00 is sent to chk-008. chk-008 Account deposit 250.00 deposit Message name is usually omitted in the diagram. 250.00 deposit Sending a Message

  17. This message has no argument. chk-008 Account getMonthlyFee monthly fee The method returns the value monthly fee back to the message sender. Getting an Answer

  18. Account getAverageBalance average balance The average balance of all accounts is returned. Calling a Class Method

  19. Program Components • A Java file is composed of • comments, • import statements, and • class declarations.

  20. Comment Program Component: Comment // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } }

  21. Import Statement Program Component: Import Statement // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } }

  22. Class Name The name of the class we want to import. Use asterisks to import all classes. Package Name Name of the package that contains the classes we want to use. More Examples Import Statement Syntax and Semantics <package name> . <class name> ; e.g. javabook . InputBox; import javabook.*; import java.awt.image.ColorModel; import com.drcaffeine.galapagos.*;

  23. Class Declaration Program Component: Class Declaration // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } }

  24. Method Declaration Program Component: Method Declaration // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } }

  25. Modifier Modifier Return Type Method Name Parameter Method Body Method Declaration Elements public static void main ( String[ ] args ) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); }

  26. Statements • Method bodies contain statements • Simple statements end with a ; • Compound statements are enclosed in {}s public static void main (String[] args) { int someData = 0; if (someData == 27) { System.out.println(“Cosmic rays!”); someData = 0; } }

  27. Parameter Local Variables Sample Method public double fromDollar( double dollar ) { double amount, fee; fee = exchangeRate - feeRate; amount = dollar * fee; return amount; }

  28. Files and Classes • A Java program file ends with .java • There must be one public class per file • It must have the same name as the file • One public class (i.e., one file) must have the main method

  29. Simple Java Programs • Simple Java programs can be written in just the one file, containing • One public class (with the main method) • Other class methods and final data values as required • Such programs do not create any objects, but simply run class methods (starting with the main method) and use primitive data.

  30. center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } } Comment Import Statements Class Name Method Body Template for Simple Java Applications

  31. DON’T PANIC!! • We’ll write some programs without creating objects, i.e., you’ll have to think about only classes (and their methods and data values) • We’ll write some programs that create objects from pre-existing classes, i.e., you’ll use objects before you have write code to define them. • Then we’ll write programs that define and create their own objects

More Related