1 / 14

CSE 1341 Honors

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

domani
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 13

  2. 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.

  3. 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.

  4. 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?

  5. 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?

  6. Breakout 1

  7. 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.

  8. 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()

  9. 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.

  10. 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

  11. 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

  12. 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

  13. 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

  14. Breakout 2

More Related