140 likes | 239 Views
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
E N D
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 • 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 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 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 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?
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 • 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 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 //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 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 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 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