Cse 1341 honors
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

CSE 1341 Honors PowerPoint PPT Presentation


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

CSE 1341 Honors. Professor Mark Fontenot Southern Methodist University Note Set 13. Utility Classes. Some classes don’t make objects They contain a collection of methods but don’t represent an object Perfect Example: java.lang.Math

Download Presentation

CSE 1341 Honors

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


Cse 1341 honors

CSE 1341 Honors

Professor Mark Fontenot

Southern Methodist University

Note Set 13


Utility classes

Utility Classes

  • Some classes don’t make objects

  • They contain a collection of methods but don’t represent an object

  • Perfect Example: java.lang.Math

    • Instead of creating a Math object, we just call the methods directly from the class

int a = 2, b = 5;

double val = Math.pow(a, b);

Call the pow function direction from Math class –

no object is needed.


Utility classes1

Utility Classes

  • So you don’t have to create an object of type Math before you can use these methods.

  • These methods are called static methods.

    • They don’t operate on data that is specific to the instance of a particular object

int a = 2, b = 5;

double val = Math.pow(a, b);

Call the pow function direction from Math class –

no object is needed.


Creating your own class of static methods

Creating Your Own Class Of Static Methods

public class RobotHelper {

public static void turnRight() {

// code here to turn your robot to the right

}

public static void turnLeft() {

//code here to turn your robot to the left

}

public static void forwardToBump () {

//code here to go forward until bump sensor is activated

}

public static void forwardInches(int distance) {

//Move forward distance inches

}

}

RobotHelper.java

-Notice that this class has no main method;

it just has a bunch of static methods.

-How do we access them, then?


Accessing static methods

Accessing Static Methods

public class RobotDriver {

public static void main (String [] args) {

RobotHelper.turnLeft();

RobotHelper.forwardInches(10);

RobotHelper.turnLeft();

RobotHelper.forwardToBump();

}

}

Access the static methods from a class

by using the following pattern:

<class name>.<static method name>

What other static methods have we seen?


Breakout 1

Breakout 1


Classes objects

Classes/Objects

  • Class represents:

    • pattern by which objects are created (blueprint)

    • interface to communicate with objects of that type.

Student s = new Student();

In order to execute this line of code,

there has to be a class Student declared

somewhere.


Class diagram

Class Diagram

  • Part of the UML (Unified Modeling Language) that indicates the data members and methods that are part of a class

  • Generic – not specific to Java

Student

- Name:string

- age:int

+ setName(n : string)

+ getName() : String

+ setAge(a : int)

+ getAge() : int

+ birthday()


Class student

Class Student

Name of the class

Student

Data Members

- Name:string

- age:int

+ setName (n : string)

+ getName() : String

+ setAge(a : int)

+ getAge() : int

+ birthday()

Methods / Interface

  • private

  • +public

  • This is enough information to “stub out” the class

    • Sort of like writing the shell… methods are there, but they

    • don’t have anything in the body yet.


How it all works

How it all works

//Create a new student object

Student s = new Student();

//set students name to something

s.setName(“John Doe”);

//Set the students age to something

s.setAge(23);

“”

s

0


How it all works1

How it all works

//Create a new student object

Student s = new Student();

//set students name to something

s.setName(“John Doe”);

//Set the students age to something

s.setAge(23);

John Doe

s

0


How it all works2

How it all works

//Create a new student object

Student s = new Student();

//set students name to something

s.setName(“John Doe”);

//Set the students age to something

s.setAge(23);

John Doe

s

23


How it all works3

How it all works

Student s = new Student();

s.setName(“John Doe”);

s.setAge(23);

Student t = new Student();

t.setName(“Sue Doe”);

John Doe

s

23

Sue Doe

t

0


Breakout 2

Breakout 2


  • Login