CIS3023: Programming Fundamentals for CIS Majors II
This presentation is the property of its rightful owner.
Sponsored Links
1 / 34

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on
  • Presentation posted in: General

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Objects and Classes (contd.). Course Lecture Slides 19 May 2010. Ganesh Viswanathan. Objects and Classes.

Download Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cis3023 programming fundamentals for cis majors ii summer 2010

CIS3023: Programming Fundamentals for CIS Majors II

Summer 2010

Objects and Classes (contd.)

Course Lecture Slides

19 May 2010

Ganesh Viswanathan


Objects and classes

Objects and Classes

Credits: Adapted from CIS3023 lecture slides (Spring 2010) by Dr SeemaBandyopadhyay,University of Florida, Gainesville.


Object instantiation

Object Instantiation

  • Declaring and Creating object in two steps

  • Declaring and Creating object in single step

  • // <ClassName> <objectRefVar>;

  • Circle myCircle;

  • myCircle = new Circle();

  • // <ClassName> <objectRefVar> = new <ClassName>();

  • Circle myCircle = new Circle();


Trace code

Trace Code

animation

Declare myCircle

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle();

yourCircle.radius = 100;

myCircle

no value


Trace code cont

Trace Code, cont.

animation

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle();

yourCircle.radius = 100;

myCircle

no value

Create a circle


Trace code cont1

Trace Code, cont.

animation

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle();

yourCircle.radius = 100;

myCircle

reference value

Assign object reference to myCircle


Trace code cont2

Trace Code, cont.

animation

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle();

yourCircle.radius = 100;

myCircle

reference value

yourCircle

no value

Declare yourCircle


Trace code cont3

Trace Code, cont.

animation

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle();

yourCircle.radius = 100;

myCircle

reference value

yourCircle

no value

Create a new Circle object


Trace code cont4

Trace Code, cont.

animation

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle( );

yourCircle.radius = 100;

myCircle

reference value

yourCircle

reference value

Assign object reference to yourCircle


Accessing objects

Accessing Objects

  • Referencing the object’s data:

  • Invoking the object’s method:

// objectRefVar.data

myCircle.radius

//objectRefVar.methodName(arguments)

myCircle.getArea()


Trace code cont5

Trace Code, cont.

animation

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle( );

yourCircle.radius = 100;

myCircle

reference value

yourCircle

reference value

Assign object reference to yourCircle


Trace code cont6

Trace Code, cont.

animation

Circle myCircle = new Circle(5.0);

Circle yourCircle = new Circle();

yourCircle.radius = 100;

myCircle

reference value

yourCircle

reference value

Change radius in yourCircle


Value type vs reference type

Value Type vs. Reference Type


Copying variables

Copying Variables


Information hiding

Information Hiding

  • In a well designed OO application, a class publicizeswhat it can do i.e. its method signatures

  • but hides the internal details both of

    • how it performs these services (method bodies) and

    • the data (attributes) that it maintains in order to support these services


Access modifiers

Access Modifiers

class Circle {

private double radius;

public Circle() {

this(1.0);

}

public Circle(double newRadius) {

radius = newRadius;

}

public double getArea() {

return radius * radius * 3.14159;

}

}

Typically:

Attributes are declared private

Methods are declared public


Visibility modifiers

Visibility Modifiers

  • public

    • The class, data, or method is visible to any class

  • private

    • The data or methods can be accessed only by the declaring class.


Accessing private attributes

Accessing private attributes

public class Driver {

public static void main(String[] args)

{

Circle s1, s2;

s1 = new Circle(14);

s2 = new Circle(7);

System.out.println(“Radius = “, s1.radius);

outcome = s2.getArea();//ok!

}

}

Illegal:because attribute radius is hidden or private!


Accessing private attributes1

Accessing private attributes

  • How can code in any other class access them?

    • Programmer can provide methods to get and set them.


Get set methods

Get/Set Methods

  • Get method or Accessor

    • A method that returns the value of an attribute

      e.g., getUFID( ) { …

      return studentUFID;

      }

  • Set method or Mutator

    • A method that changes the value of an attribute

      e.g., getUFID( ) { …

      return studentUFID;

      }

  • Typically, have one get method and one set method per attribute.


Example class code

Example Class Code

public class Circle {

private double radius;

public double getRadius() {

return radius;

}

public void setRadius(double radius){

this.radius = radius;

}

public Circle() { this(1.0); }

public Circle(double r) { setRadius(r); }

public getArea() { return 3.14*radius*radius; }

}


Example driver code

Example Driver Code

public class Driver {

Circle c1 = new Circle(14);

Circle c2 = new Circle(7);

System.out.println(c1.getRadius()); //ok!

c1.setRadius(5); //ok!

boolean outcome = s2.getArea();//ok!

}


Cis3023 programming fundamentals for cis majors ii summer 2010

Some more code

public class Circle {

private double radius;

public double getRadius() {

return radius;

}

public void setRadius(double r){

if (r>0) radius = r;

}

public Circle() { this(1.0); }

public Circle(double r) { setRadius(r); }

public getArea() { return 3.14*radius*radius; }

}


Still more code

Still more code

public class Student {

private String name;

private String ssn;

private float gpa;

public Student(inti, String n) {

setSsn(i); setName(n); setGpa(0.0f);

}

// other constructors and methods, not shown here

public String getName () { return name; }

public void setName(String newName) { name = newName;}

public String getSsn () { return ssn; }

public void setSsn(String s) { ssn = s;}

public float getGpa () { return gpa; }

public void setGpa(float newGpa) { gpa = newGpa;}

}


Lots more code

Lots more code

public class Student {

private String name;

private String ssn;

private float gpa;

private intnumDsFs;

public Student(inti, String n) {

setSsn(i); setName(n); setGpa(0.0f); setNumDsFs(0);

}

// other methods, not shown here

public booleanisOnProbation() { if(numDsFs > 3) return true;

else return false; }

public String getName () { return name; }

public void setName(String newName) { name = newName;}

public String getSsn () { return ssn; }

public float getGpa () { return gpa; }

public void setGpa(float newGpa) { gpa = newGpa;}

public void setNumDsFs(int n) { numDsFs = n; }

}


Benefits of information hiding

Benefits of Information Hiding

  • Allows Data Validation

  • Allows control over the level of access given for an attribute

  • Simplifies Code Maintenance


Class attributes

Class attributes

  • Each instanceof a class (called an object) has a copy of the attributes

    • Changing an attribute in one object doesn’t affect the attribute of another object


Class attributes cont

Class Attributes, cont.

  • Sometimes you may want some data to be shared among all instances.

  • Example: we want all Student objects to have a shared access to the total student enrollment count at the university.


Static attributes

Static Attributes

public class Student {

private String name;

private String ssn;

private float gpa;

private staticinttotalNumStudents = 0;

public Student(inti, String n) {

setSsn(i); setName(n); setGpa(0.0f);

totalNumStudents++;

}

// other constructors, accessors/mutators, and methods, not shown here

public intgetTotalNumStudents() {

return totalNumStudents;

}

}


Static attributes cont

Static Attributes, cont.

  • A static attributeis one whose value is shared by all instances of that class.

  • It in essence belongs to the class as a whole.


Static attributes cont1

Static Attributes, cont.

// Client code:

Student s1 = new Student();

Student s2 = new Student();

Student s3 = new Student();

System.out.println(s1.getTotalNumStudents());

System.out.println(s2.getTotalNumStudents());

System.out.println(s3.getTotalNumStudents());

All of these println statements will print the value 3.


Static methods

Static Methods

public class Student {

private String name;

private static inttotalNumStudents = 0;

// other attribute details omitted ...

public staticintgetTotalNumStudents() {

return totalNumStudents;

}

}

  • // Client code

  • Student s1 = new Student();

    Student s2 = new Student();

    Student s3 = new Student();

    System.out.println(Student.getTotalNumStudents());

    System.out.println(s1.getTotalNumStudents());


Static methods1

Static Methods

  • may only access static attributes

  • class Student {

  • private String name; // NOT static

  • private staticinttotalStudents;

  • public static void print() {

  • System.out.println(name + " is one of " +

  • totalStudents + " students."); // ILLEGAL!

  • }

  • }


Get more info

Get more info!

  • Value Types and Reference Types:http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html

  • Creating and initializing objects:http://docstore.mik.ua/orelly/java-ent/jnut/ch03_02.htm

  • (Online) Search keywords:

    • JAVA classes and objects

    • JAVA pass by reference or value


  • Login