Download presentation
Presentation is loading. Please wait.
1
CS 200 Creating Classes Jim Williams, PhD
2
Week 12 Team Lab: File Input and Output Exam 2 Review
No recording or picture taking Come to office hours to review booklet BP1 Grades (soon) Verifying Requirements Lecture: Creating Classes
3
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
4
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
5
Class vs Instance
6
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
7
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"; }
8
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; }
9
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();
10
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
11
Accessed outside the class?
public class Cup { private static int capacity; public String contents; } capacity: yes contents: yes contents: no capacity: no
12
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.
13
Constructors: special methods
Same name as class. No return type or void. class Exam { String studentsName; static String examName = "Exam 2"; Exam() { studentsName = "blank"; }
14
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
15
Accessor/Getter Methods
Methods to retrieve information Provide read access to information within the instance/object. String getName() { } String name() { } boolean isHappy() { }
16
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) { }
17
BP1 Notes BP1 Grading Results (emailed Tuesday)
Regrade Requests (1 week from Tuesday) 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.
18
this implicit parameter
class Car { String owner; void setOwner( String owner) { this.owner = owner; } in main: Car mine = new Car(); mine.setOwner("me");
19
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
20
Demo Creating a class with instance variables Instantiating a class
Writing and calling a constructor Writing and calling a getter/accessor
21
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());
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.