AP Computer Science 2015/02/09. Learning Objectives 1.List the usual visibility for constructors, fields, and methods. 2.Explain: a.how local variables.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Abstract Data Type Fraction Example
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
EXAMPLE 1 Writing Equivalent Fractions. EXAMPLE 1 Writing Equivalent Fractions Write two fractions that are equivalent to. Writing Equivalent Fractions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Simple Sorting Algorithms
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Multiplying Fractions  Step 1:  Look for common terms that can cancel  Step 2:  Look at cancelling signs (if possible)  Step 3:  Multiply the numerator.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Adding/Subtracting Unlike Fractions Section 3.6 Numerical and Algebraic.
Bell Ringer 9/21/15 1. Simplify 2x 2 6x 2. Add Subtract 2 _
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
6-2:Solving Rational Equations Unit 6: Rational and Radical Equations.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Algebraic Fractions  Know your rules  Anything raised to the 0 power = 1  Negative exponents can be moved to the opposite and made positive (that is,
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Monday Schedule Monday: Rational Equations Tuesday: Rational Equations Wednesday: In class Activity, Factoring Quiz Thursday: Quiz Friday:
Exponent Rules. Parts When a number, variable, or expression is raised to a power, the number, variable, or expression is called the base and the power.
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Non-static classes 1.  a utility class has features (fields and methods) that are all static  all features belong to the class  therefore, you do not.
CS1054: Lecture 11 More Sophisticated Behavior (contd..)
 When a number, variable, or expression is raised to a power, the number, variable, or expression is called the base and the power is called the exponent.
Bell Ringer 2/9/15 1. Simplify 3x 6x 2. Multiply 1 X Divide 2 ÷ Add Subtract 2 _
Written by: Dr. JJ Shepherd
5 Minute Check. Fluency Tuesday, Dec 1 Lesson 5.5.b Compare and Order Rational Numbers.
Non-Static Classes What is the Object of this lecture?
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Rational Expressions – Equivalent Forms
Intro To Classes Review
Exponent Rules.
Rule Exercises Status of the Ball Definitions and Rule 15
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CLASS DEFINITION (> 1 CONSTRUCTOR)
Exponent Rules.
Objectives Simplify expressions with exponents..
Goals 1. I can use the division properties of exponents to evaluate powers and simplify expressions. I can use the division properties of exponents to.
Dividing monomials.
Slope and Rate of Change
Topic 28 classes and objects, part 2
Multiplying and Dividing Rational Expressions
More on Classes and Objects
Essential Questions What is a denominator? What is a numerator?
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Simple Classes in Java CSCI 392 Classes – Part 1.
PreAP Computer Science Quiz Key
JAVA CLASSES.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Agenda Inheritance Polymorphism Type compatibility Homework.
What to expect this week
Chapter 11 Classes.
Presentation transcript:

AP Computer Science 2015/02/09

Learning Objectives 1.List the usual visibility for constructors, fields, and methods. 2.Explain: a.how local variables "shadow" class fields b.how this can be used to access a shadowed field 3.Explain: a.what are class invariants, and b.how they relate to constructors, accessors, and mutators 4.Code a class using class invariants.

Review: Visibility ●public: anything can access it ●private: only the class can access to it

Usual Visibility Class PartVisibility Constructors public Fields private Methods public

Usual Visibility: Exceptions ●Class constants opublic static final NUM_LINES = 7; ●"Helper" methods o Decomposing a task into smaller sub-tasks o The method/task is public o The "helper" methods/sub-tasks are private

Shadowing public class Sample { private int x; public Sample() { x = 2; } public void f(int x) { x = 3; }

Shadowing Exercise: 1.Create an accessor for class field x. 2.What is the value of the field x after f(3) is called? Is it 2 or 3?

Shadowing ●Answer: 2 ●Why? o Local variables "shadow" class fields. o Any reference to x refers to the local variable, not the class field.

Shadowing This is wrong: public class Sample { private int x; public Sample(int x) { x = x; }

Shadowing This is right: public class Sample { private int x; public Sample(int x) { this.x = x; }

Shadowing ● this.x will always give you the class field o (Except for static fields - more on that later) ●Even if a local variable is also named x

Class Invariants ●Definition of invariant: never changing ●Sample invariants o = 4 o Death and taxes ●Sample non-invariants o Weather o High school relationships

Class Invariants ●A DiceRoller class has: o a field int lastRoll o an accessor int getLastRoll() o a mutator void rollDice() ●Invariant: lastRoll is a number from 1 to 6 o Does not matter how many times the dice are rolled ●You can make your own invariants!

Class Invariants Rules: 1.Invariant is true in the initial state a. Constructors establish the initial state 2.The invariant is true after a state change a. Mutators can change the state 3.Fields should be private a. Only mutators can change the state 4.(Accessors don't change the state)

Lab Create a Fraction class (starter code, tests provided) State: ●Numerator ●Denominator Invariants: 1.Fraction is always reduced 2.Denominator is always positive 3.Zero is not allowed as denominator

Exit Quiz, Homework For the Fraction class: 1.List the constructor(s). 2.List the accessor(s). 3.List the mutator(s). Homework: ●Wednesday: Self-checks ●Monday: Fraction lab