Presentation is loading. Please wait.

Presentation is loading. Please wait.

Business Programming Concepts II

Similar presentations


Presentation on theme: "Business Programming Concepts II"— Presentation transcript:

1 Business Programming Concepts II
Java Objects MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj, All rights reserved

2 Objectives Get started on Object Oriented Programming
Learn how to use non-static methods in classes Let’s get started!

3 Objects Functional decomposition or Divide & Conquer:
Take a large task, and break it up into methods (static methods) -Write a main method that calls on these other methods, which in turn may call on other methods. We can also divide our methods into multiple classes, and call them. Think about the TEXTIO class. We used the methods in that (written by someone else) within our own class. Think of our MyCustomerData class in Assignemnt 3. It was used by the TestMyCustomerData class. We can create & use many such classes. Commonly used classes are also called APIs (Application Programming Interface)

4 Objects Static Methods:
Called (invoked) using <classname>.<methodname> E.g., TextIO.getln(); Non-Static Methods: -Cannot be called using <classname>.<methodname> -Instead we need to use <objectname>.<methodname> E.g., MyWord.length() where MyWord is a String object. -An object is created by allocating memory. We can create multiple objects of the same class when running one program Each object will occupy a different portion of main memory, and will have a different name.

5 Objects Non-Static Methods:
A class can have non-static methods as well as static methods. The static methods will be invoked using <classname>.<methodname> The non-static methods can only be invoked by declaring objects of that class. Objects: Objects have methods (non-static) as well as fields (variables). Every time an object is created of a class, memory is allocated for all the non-static class variables (aka member variables) in the class. The non-static methods can access static & non-static variables in their code. The static methods can access only static variables in their code. Why? The object is an instance of its class The non-static class variables are also called instance variables. Static methods and variables are sometimes called class methods or class variables, since they belong to the class, and not to any object (don’t need to create an object to call them). Example: Classes c1 and Testc1

6 Objects

7 Fun In Class Assignment
-Create a class called Students.java that has four instance variables: name,test1, test2 and test3. Default values are “ZZZZZ” for name and -1.0 for the 3 test scores. -Create methods: void setName(String namePar) void setTest1(double test1Par) String getName() double getTest1() And similarly for test2 and test3. That allow us to set & get values for these variables. The set methods are also called mutator methods The get methods are also called accessor methods.

8 Objects Note: This is very similar to arrays. An array name is a reference to the array.

9 Objects When the object variable is created, before new is used, it points to no memory. This is called pointing to null. We can explicitly set an object name to null std = null;//std point to no memory and can test if(std==null){ } If an object points to null, we cannot reference its instance methods or variables. -Doing so would result in a null pointer exception.

10 Objects What if we set std1 = null; ?

11 Objects

12 Objects For more detailed reading:


Download ppt "Business Programming Concepts II"

Similar presentations


Ads by Google