Download presentation
Presentation is loading. Please wait.
Published byDenzel Brown Modified over 10 years ago
1
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well 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
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. Make things private whenever you can That helps with encapsulation
6
Class and Instance Data Values Class data (indicated by the static modifier) is used to maintain information shared by all instances or aggregate information about the instances. Make class data private whenever you can Instance data is used to maintain information specific to individual instances. Make instance data private always
7
Primitive and Reference Data Values primitive variables contain values Reference variables point at objects byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type
8
Variable and Constant Data Values There are two types of data values: Account SV129 minimum balance 100.00 current balance 908.55 account prefix 6427 opening balance 100.00 A constant whose value must remain fixed over time. A variable whose value can change over time. Constants are indicated by the final modifier Non-final public static data can give you warts
9
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. Every program has one static method called main. Thats where the program starts.
10
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.
11
Calling Methods To instruct a class or an object to do something, we call one of its methods Values passed to a method are called arguments or parameters of the message. The (formal) parameters of a method are local variables that receive the parameters Methods can return one data value
12
Calling a Method Call deposit with the argument 250.00. chk-008 deposit deposit 250.00
13
Getting an Answer This call has no argument. chk-008 getMonthlyFee monthly fee The method returns the value monthly fee.
14
Program Components A Java file is composed of comments, import statements, and class declarations.
15
//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 ); } Program Component: Comment Comment
16
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 ); } Import Statement
17
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 ); } Class Declaration
18
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 ); } Method Declaration
19
Method Declaration Elements public static void main (String[ ] args){ MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Modifier Return Type Method Name Parameter Method Body
20
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; }
21
Sample Method public double fromDollar( double dollar ) { double amount, fee; fee = exchangeRate - feeRate; amount= dollar * fee; return amount; } Parameter Local Variables
22
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
23
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.
24
Template for Simple Java Applications 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
25
DONT PANIC!! Well write some programs without creating objects, i.e., youll have to think about only classes (and their methods and data values) Well write some programs that create objects from pre-existing classes, i.e., youll use objects before you have write code to define them. Then well write programs that define and create their own objects
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.