Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I
Day1 on Thursday, before lab1

2 Java Class Everything in Java is inside a class Class
Data with functions on the data Other features Fields: Data items inside a class Methods: Functions inside a class main(): test bed function

3 Lab0 public class IntegerList { private final int MAX_SIZE = 50;
private int intArray[] = new int[MAX_SIZE]; private int valCount = 0; public boolean addValue ( int intValue ) . . . public int find( int target ) public void print() public static void main( String [] args ) IntegersList list = new IntegersList(); int inputValue, target, index; Scanner sc = new Scanner(System.in); inputValue = sc.nextInt(); list.addValue(inputValue); }

4 Procedural Programming in C++
const int MAX_SIZE = 50; // Function prototypes boolean addValue ( int[] s[], int& size, int xValue ); int find( const int[] s[], int size, int target ); void print(const int[] s[], int size); int main() { int intArray[] = int[MAX_SIZE]; int valCount = 0, xVal; cin >> xVal; if ( addValue( intArray, valCount, xVal) ) cout << “Value ” << xVal << “ is added.”; else cout << “Value ” << xVal << “ is not added.”; . . . return 0; }

5 Features of OOP Abstraction Data Encapsulation (Data Hiding)
Inheritance Polymorphism

6 Abstraction Abstract Data Type (ADT) Mathematical model
Data plus operations on that data Abstraction Java class to implement ADT

7 Data Encapsulation Private data fields Public methods
The data can only be manipulated via methods. Methods Implementation Hiding the details (users don’t know) Change it when needed Implementer and User

8 Data Encapsulation The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

9 Features of OOP Abstraction Data Encapsulation (Data Hiding)
(Inheritance) (Polymorphism)

10 Lab0 public class IntegerList { private final int MAX_SIZE = 10;
private int intArray[] = new int[MAX_SIZE]; private int valCount = 0; public boolean addValue ( int intValue ) . . . public int find( int target ) public void print() public static void main( String [] args ) IntegersList list = new IntegersList(); int inputValue, target, index; Scanner sc = new Scanner(System.in); inputValue = sc.nextInt(); list.addValue(inputValue); }

11 Creating Java Classes in NetBeans
Creating a project in NetBeans Copying files Adding new class files to a project File / New File Category: Java File Type: Java class Class Name Location

12 public class Student { private String firstName, lastName; private float gpa; public boolean setGPA (float value) if ( value < 0 || value > 4.0 ) return false; gpa = value; return true; } public float getGPA() return gpa; ...

13 Private vs. Public // Most fields are private
private String firstName, lastName; private float gpa; // Most methods are public public boolean setGPA (float value) ... public float getGPA()

14 // A variable of class Student
// can reference a Student object Student s; // Class instance (object) is created in memory // Variable s points to the object s = new Student(); s.setGPA(3.0F); // The default is double System.out.println(“GPA: ” + s.getGPA()); // float is converted to string and // appended (+) to “GPA: ”

15 // A variable of type class Student // Can reference a Student object
Student s; // Class instance (object) is created in memory // Variable s points to the object // s = new Student(); s.setGPA(3.0F); System.out.println(“GPA: ” + s.getGPA()); // Will it work? No object!

16 Variables and Memory num1 num2 int num1, num2; num1 = 4;
The same for all non-class data types In C++ and Java 4 5 Need to initialize!

17 References and Objects
int num1, num2; num1 = 4; num2 = num1 + 1; Student s; s is a reference to class Student, or a pointer to a Student object. It keeps the addresses where the object is stored s = new Student(); Objects are allocated in Heap memory. Variables are in Stack. num1 num2 4 5 s gpa lastName firstName

18 Java (OOP) Programming
Create classes Declare class variables Create class instances (objects) Call methods

19 Due 10 pm, Friday, September 15
Prog0: 15 Points Due 10 pm, Friday, September 15

20 Building/Labs Access Open: 7 am – 5 pm Using your card: 5 pm – 11 pm
Closed: 11 pm – 7 am

21 Lab0 Due 10 pm, Friday, September 8 Zero differences! Any questions?


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google