Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Warmup Lesson 2.2 (parameters, etc)

Similar presentations


Presentation on theme: "Agenda Warmup Lesson 2.2 (parameters, etc)"— Presentation transcript:

1 Agenda Warmup Lesson 2.2 (parameters, etc) Guided Practice (2.2 Assignments) (Time Permitting): Lesson 2.3 Closure Activity Students will be able to: Understand what accessor methods and mutator methods are, and how they differ Understand what an immutable class is Create parameters in order to make their methods more useful See how today's lesson fits into the unit and the course as a whole

2 Warmup What is an instance variable? What are they a.k.a.?
What is required when creating an instance variable? Why would a method be declared with the word void? Why would a method be declared with the word double? Is this method valid? public int theNumber( ) { int x = 3; return x; }

3 Accessor & Mutator Methods
An Accessor Method is a method that returns the value of a private instance variable. Accessor Methods are sometimes called “getters,” and usually begin with get. (they don’t have to, this is just a “Java naming convention.”)

4 Mutator Methods set or change the value of a private instance variable, and are sometimes called “setters.” They usually begin with set, according to Java naming conventions, but some do not. Demo: CLASSDemo

5 Immutable Class An Immutable Class is a class that does not contain mutator methods. The word immutable means “cannot be changed.” String is an immutable class. When you create a String, it is immutable in the sense that you can’t change its individual characters. For example, this is impossible: String x = “abc”; x.charAt(0) = “m”;

6 Parameters Some methods can be more flexible (therefore useful) if we “send” or “pass” them input values Values that are passed to a method are called parameters A method must specify the type of parameters it can receive, but the client program that calls the method does not have to do this The names of parameters in the client & the class are rarely the same. In fact, it’s best to make them different. Demo: ParametersDemo, ParamClient

7 Important rules about methods:
If possible, avoid putting a return statement inside an if statement. Sometimes, it is most efficient to do so, but usually it is best to avoid it. The same rule applies to loops: usually, not always, you want to avoid putting a return statement inside a loop. We will eventually see RARE exceptions to these first 2 “rules.” A method can only return one value. No exceptions to this rule. If a method receives multiple parameters, the order that they are sent/received does matter. One method cannot use another method’s parameters. If a method receives int x as a parameter, no other method will recognize x.

8 Assignments Create a class called Die. It has 1 method, getRoll( ), which receives 1 parameter. This parameter is the number of sides the die has. In the method, randomly roll the die, and return (***but don’t display***) the result. Next, create a client, DieClient, that asks how many sides the die has (error check: 4+), then rolls the die once (by calling the method) and displays the result. 2. Create another client called BoxCars. It rolls a pair of 6-sided dice 1000 times (you must do this by using, but not changing at all, the Die class). Display all the rolls (should look like “you rolled a __ and a __”) and count how many times “box cars” (a pair of 6’s) occurs, as well as how many times snake eyes occurs. 3. see next slide

9 3. Change your code in P243num5, so that you roll the dice by calling the getRoll( ) method from your Die class. Do not change the Die class at all; there is no reason to.


Download ppt "Agenda Warmup Lesson 2.2 (parameters, etc)"

Similar presentations


Ads by Google