Agenda Warmup Lesson 2.2 (parameters, etc)

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
Access to Names Namespaces, Scopes, Access privileges.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Classes Java is an Object-Orented Programming language, and one of the main advantages of OOP is code reuse. Code reuse: write code only once, put it in.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Loops and Iteration for Statements, while Statements and do-while Statements.
Classes CS 21a: Introduction to Computing I First Semester,
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
10-Nov-15 Java Object Oriented Programming What is it?
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Building java programs, chapter 3 Parameters, Methods and Objects.
Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Agenda Warmup Prep for Core #1 Lesson G2 (RGB, Loops in Graphics)
Agenda Warmup Finish 2.4 Assignments
Agenda Warmup AP Exam Review: Litvin A2
Classes.
Namespaces, Scopes, Access privileges
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Using local variable without initialization is an error.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Classes, Objects, and Methods
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes, Encapsulation, Methods and Constructors (Continued)
Arrays in Java What, why and how Copyright Curt Hill.
Java Lesson 36 Mr. Kalmes.
Unit 3 Test: Friday.
Namespaces, Scopes, Access privileges
Chapter 6 – Methods Topics are:
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Lecture 5- Classes, Objects and Methods
Welcome to AP Computer Science A!
Welcome to AP Computer Science A!
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Classes CS 21a: Introduction to Computing I
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Barb Ericson Georgia Institute of Technology Oct 2005
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Classes and Objects CGS3416 Spring 2019.
Agenda Warmup Lesson 2.8 (Overloading constructors, etc)
Lec 17 Using Nested Loops and Objects in an Applet Class
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Day 11 The Last Week!.
Getting Started in Python
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

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

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; }

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

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

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”;

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

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.

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

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.