CS 200 Creating Classes Jim Williams, PhD
Week 12 Team Lab: File Input and Output Exam 2 Review BP1 Master Mind No recording or picture taking Come to office hours to review booklet BP1 Master Mind BP2 Adventure Story Lecture: Creating Classes
Adventure Story Tips
BP1 Notes BP1 Grading Results Regrade Requests (1 week from yesterday) If very small changes (e.g., 1 line) may result in many more points, may be worth it to discuss with the grader. Grader will take off a couple points per change but may earn more points since more tests pass.
Course Transition P1-P7: Small procedural programs using objects BP1-BP2: Larger programs using Procedural Programming BP2: just use through Chap 11 material. Now: Introduce Creating Classes and Object-Oriented Programming Continue with OOP in CS 300
Procedural Programming: Steps a program must take to reach a state Object-Oriented Programming: Group related data and methods together Objects are data fields manipulated through predefined methods https://en.wikipedia.org/wiki/Comparison_of_programming_paradigms
Class vs Instance http://buggyandbuddy.com/wp-content/uploads/2016/08/owl-horiz..png
Class as a Template A template for creating objects/instances. attributes/fields constructors public, private, protected, <package> accessors/getters mutators/setters this: implicit parameter to every instance method
Fields/Attributes Variables in classes outside methods Class variables: static keyword Instance/object variables: no static keyword class Exam { String studentsName = ""; static String examName = "Exam 1"; }
Memory Allocation Class variables (static) once when class is loaded into VM Instance variables (no static) once for each new instance created class Exam { String studentsName; static String examName; }
How Many of Each Variable? capacity: 1 contents: 2 capacity: 2 capacity: 0 contents: 1 class Cup { static int capacity; //ounces String contents; } in a method: Cup c1 = new Cup(); Cup c2 = new Cup();
Member Variables also called attributes, fields Two kinds class (static) instance (non-static) Demo
Visibility of Members Members include methods and fields public: accessible if class is protected: accessible within package and by descendants <package>: no visibility modifier means visible within package only. private: visible only within the class
Accessed outside the class? public class Cup { private static int capacity; public String contents; } capacity: yes contents: yes contents: no capacity: no
Constructors: special methods Called once when creating a new instance. After memory is allocated, constructor typically initializes fields. new Exam() is creating new instance of Exam and calling the no-arg constructor.
Constructors: special methods Same name as class. No return type or void. class Exam { String studentsName; static String examName = "Exam 2"; Exam() { studentsName = "blank"; }
Which are Constructors for Cup? class Cup { Cup() { } Cup(String contents) { } void Cup(String contents) { } Full() { } } all first 2 first 3 first 2, last 1
Accessor/Getter Methods Methods to retrieve information Provide read access to information within the instance/object. String getName() { } String name() { } boolean isHappy() { }
Mutator/Setter Methods Allow user to change data/state of an object. Provide write access to data. void setName( String newName) { } void name( String name) { } void happy( boolean happy) { }
this implicit parameter class Car { String owner; void setOwner( String owner) { this.owner = owner; } in main: Car mine = new Car(); mine.setOwner("me");
Encapsulation Enclosing something Internal/Developer vs External/Users access Use visibility and getters and setters to allow appropriate user access Data should be Private Provide getters/accessors for read access Provide setters/mutators for write access Provide constructors to initialize on creation
Demo Creating a class with instance variables Instantiating a class Writing and calling a constructor Writing and calling a getter/accessor
Rectangle Design a Rectangle class Fields: width & height as double with default of 1.0 and private Constructors: no-arg constructor & a constructor with specified width and height, public Methods: getArea() and getPerimeter(), public Draw a UML diagram for the class then implement the class. Write a Shapes program that: Creates 2 rectangles (4 by 10) and (3.5 by 25.4) Display width, height, area and perimeter public class Rectangle { //implicitly extends Object //instance variables aka fields and attributes private double height; private double width; private static int count = 0; //class variable aka class fields and attributes public Rectangle() { //no-arg constructor this( 1.0, 1.0); //call the 2-arg constructor } public Rectangle( double size) { //constructor this( size, size); //call the 2-arg constructor public Rectangle(double height, double width) { //constructor this.height = height; this.width = width; count++; //class (static) variables can be accessed from instance methods and constructors public double getArea() { //derived field return height * width; public double getPerimeter() { //another derived field return height * 2 + width * 2; public double getHeight() { return this.height; //setter/mutator for height that validates any changes to height public void setHeight(double newHeight) { if ( newHeight > 0.0) { this.height = newHeight; //accessor for class field public static int getCount() { //instance fields cannot be accessed from class (static) methods //as which instance isn't defined - no implicit this parameter return count; //overriding the same toString() method inherited from Object public String toString() { return "Rectangle height=" + height + " width=" + width; import java.util.ArrayList; public class Shapes { public static void main(String[] args) { //user of the Rectangle class //since this Shapes class is in the same package (default package) //as the Rectangle class then all non-private members of Rectangle //would be accessible. ArrayList<Rectangle> list = new ArrayList<>(); list.add( new Rectangle()); list.add( new Rectangle( 4.0, 10.0)); System.out.println( "count: " + Rectangle.getCount()); for ( Rectangle shape : list) { System.out.println( shape.getArea());