CS 200 Creating Classes Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
UML Basics & Access Modifier
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Writing Classes (Chapter 4)
Object Oriented Programming: Java Edition By: Samuel Robinson.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Information Systems Engineering
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Topics Instance variables, set and get methods Encapsulation
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Fundamentals BCIS 3680 Enterprise Programming.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
1 More About Derived Classes and Inheritance Chapter 9.
Java: Base Types All information has a type or class designation
Abstract Classes, Abstract Methods, Override Methods
Advanced Programming in Java
Re-Intro to Object Oriented Programming
Topic: Classes and Objects
Classes (Part 1) Lecture 3
Inheritance ITI1121 Nour El Kadri.
Java: Base Types All information has a type or class designation
Final and Abstract Classes
A first Look at Classes.
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
Advanced Programming in Java
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS Week 14 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
CS 200 More Classes Jim Williams, PhD.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Implementing Non-Static Features
Advanced Programming Behnam Hatami Fall 2017.
CS 302 Week 9 Jim Williams.
Today’s topics UML Diagramming review of terms
CS 200 Additional Topics and Review
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CS 200 Objects and ArrayList
Object-Oriented Programming
Object Oriented Programming in java
Chapter 11 Inheritance and Polymorphism
CS 200 More Classes Jim Williams, PhD.
Final and Abstract Classes
CS 200 Creating Classes Jim Williams, PhD.
CS 200 Objects and ArrayList
Lecture 8 Object Oriented Programming (OOP)
CSG2H3 Object Oriented Programming
Chapter 6: A First Look at classes
Presentation transcript:

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());