Presentation is loading. Please wait.

Presentation is loading. Please wait.

Set & Get Methods ICS 111: Introduction to Computer Science I

Similar presentations


Presentation on theme: "Set & Get Methods ICS 111: Introduction to Computer Science I"— Presentation transcript:

1 Set & Get Methods ICS 111: Introduction to Computer Science I
William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa "There is no reason anyone would want a computer in their home." Ken Olson, president, chairman and founder of Digital Equipment Corp., 1977 11/20/2018 © 2007 William Albritton

2 Last Assignment Go over last assignment
General steps for writing a class definition Class name at top Write the data fields Write the constructor Write the methods General steps for writing main() method of client program, which uses class definition Instantiate (create) an object of the class definition Call the methods of the class definition Display the results of the methods 11/20/2018 © 2007 William Albritton

3 Accessor Methods Simple methods that get a data field
Usually start with “get” Have return values but no parameters Used to return data to the calling method Name name = new Name("Nami","Suzuki"); String firstName = name.getFirst(); //firstName is "Nami" public String getFirst(){ return first; } 11/20/2018 © 2007 William Albritton

4 Mutator Methods Simple methods that set a data field
Usually start with “set” Have parameters but no return value Used to change the value of a data field Name name = new Name("Nami","Suzuki"); name.setFirst("Nancy"); String herName = name.toString(); //herName is "Nancy Suzuki" public void setFirst(String firstP){ first = firstP; } 11/20/2018 © 2007 William Albritton

5 Class Name Class Name has several methods that are typical methods of other classes Constructor to initialize data fields of object toString() method to output the data fields Access (get) method to access each data field individually Mutator (set) method to change each data field individually See Name.java & NameClient.java 11/20/2018 © 2007 William Albritton

6 Class Exercise 1 Write class Point & a client program that uses objects of class Point The Point class definition should have Two integer data fields (for x & y) A constructor A toString() method A mutator (set) & accessor (get) method for both x & y (so 4 methods) The client program’s main() method should Instantiate (create) an object of class Point Call the methods of class Point Output the results of the methods 11/20/2018 © 2007 William Albritton

7 Class Fraction Class Fraction has several methods that are typical methods of other classes Constructor to initialize data fields of object toString() method to output the data fields Access (get) method to access each data field individually Mutator (set) method to change each data field individually See Fraction.java & FractionClient.java 11/20/2018 © 2007 William Albritton

8 Constructor & Parameters
Your formal & actual parameters have to have the same type, and the same number of parameters For example, these statements do not compile Fraction fraction1 = new Fraction("5", "3"); Fraction fraction2 = new Fraction(12.3, 3.98); Fraction fraction3 = new Fraction(5, 6, 7); 11/20/2018 © 2007 William Albritton

9 Class Exercise 2 List all the variables in Exercise2.java (class Car & client program) For each variable, write the following: The kind of variable (data field, parameter, or local variable) The scope (specific line numbers) 11/20/2018 © 2007 William Albritton

10 Class Kurochan Class Kurochan has several methods that are typical methods of other classes Constructor to initialize data fields of object draw() method to display the drawing Access (get) method to access each data field individually Mutator (set) method to change each data field individually See Kurochan.java & KurochanClientApplet.java & kurochan.html The client program uses get() & set() to draw several Kurochans diagonally 11/20/2018 © 2007 William Albritton

11 Class Exercise 3 What is the output of the following program?
Exercise3.java 11/20/2018 © 2007 William Albritton


Download ppt "Set & Get Methods ICS 111: Introduction to Computer Science I"

Similar presentations


Ads by Google