Introduction to Computer Science I

Similar documents
Lecture 7: Classes and Objects CS2301

How to engineer a class to separate its interface from its implementation and encourage reuse.

A A B U n i v e r s i t y

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

by Pearson Education, Inc. All Rights Reserved. 2

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Introduction to Classes and Objects

Fundamentals of Programming Session 25

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CSE 142 Su 04 Computer Programming 1 - Java. Objects

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Object Oriented Programming. Java-Lecture 1

Fundamentals of Programming Session 23

Computer Programming C++ Classes and Objects 6 th Lecture

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Computational Expression

1.00 Lecture 8. Using An Existing Class, cont.

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Object Oriented Design

Computational Expression

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Cpt S 122 Data Structures. Introduction to C++ Part II

Computational Expression

Chapter 6 Introduction to Defining Classes

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Software Design and Analysis for Engineers

Computational Expression

This exam is open book. Each question is worth 3 points.

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

3.1 Class Declaration

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Java Primer 1: Types, Classes and Operators

CS 231 Data Structures and Algorithms, Fall 2016

CHAPTER 7 OBJECTS AND CLASSES

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

COP 3330 Final Exam Review

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Implementing an ADT with a Class

Updates. Office B116E Office Hours : Friday 1 p.m. 2 p.m. Also by appointment Office Hours Purpose:

Lecture 02, Fall 2018 Friday September 7

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Programming overview

Computational Expression

Chapter 6 Lab Classes and Objects

C++ Important Questions with Answers

Computational Expression

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Javadocing in Netbeans (rev )

Lesson 12: OOP #2, Accessor Methods (W03D4)

Chapter 4 Java Language Fundamentals

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

UML IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

JAVA- PROGRAMMING CH2-EX1

APCS Semester #1 Final Exam Practice Problems

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Non-instantiable Classes

Embedding Graphics in JavaDocs (netbeans IDE)

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Java Foundations Certified Junior Associate

Functions and Recursion

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Programming with Objects

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Unit 4 - Inheritance, Packages & Interfaces

CHAPTER 7 OBJECTS AND CLASSES

CSE 113 A. Announcements - Lab

Chapter 7 Classes & Objects, Part B

Chapter 1 Introduction to Computers, Programs, and Java

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 15: Object Oriented Programming

CS 101 Spring 2006 Final Exam Name: ID:

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Software and Programming 1

Introduction to Programming Using Java (98-388)

Creating Classes and Objects

Chapter 4 Defining Classes I

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Introduction to Classes and Objects

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Protection Levels and Constructors The 'const' Keyword

Object Oriented Programming in C#

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Computational Expression

Declarations and Access Control SCJP tips

How to define your own classes that implement abstractions. How to pass information to methods and how methods return values.

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

Transcription:

Introduction to Computer Science I Classes Janyl Jumadinova 5-7 March, 2018

Classes Most of our previous programs all just had a main() method in one file. 2/13

Classes Most of our previous programs all just had a main() method in one file. Most programs are composed of the main() method for the primary class and one or more additional, supporting classes. 2/13

Classes Most of our previous programs all just had a main() method in one file. Most programs are composed of the main() method for the primary class and one or more additional, supporting classes. These additional classes do not have main() methods, but have other methods to implement behaviors. 2/13

Classes Most of our previous programs all just had a main() method in one file. Most programs are composed of the main() method for the primary class and one or more additional, supporting classes. These additional classes do not have main() methods, but have other methods to implement behaviors. Remember: Classes are templates for objects Objects are composed of data members (attributes) and member methods (behaviors) Objects respond to messages 2/13

Defining a Method Think about creating methods in terms of three things: 1. Parameters - Parameters/arguments/values to send to a method? - What does the method need so that it can do its job? 3/13

Defining a Method Think about creating methods in terms of three things: 1. Parameters - Parameters/arguments/values to send to a method? - What does the method need so that it can do its job? 2. Task - What does the method do? 3/13

Defining a Method Think about creating methods in terms of three things: 1. Parameters - Parameters/arguments/values to send to a method? - What does the method need so that it can do its job? 2. Task - What does the method do? 3. Return/Results - What does the method produce as a result? - What is it s final answer? - Does it even need to return anything? 3/13

4/13

Gradebook class // Define class GradeBook with a method displaymessage public class GradeBook { // method to display a welcome message // to the GradeBook user public void displaymessage () { System.out.println("Welcome to the Grade Book!"); } } 5/13

Gradebook class This class by itself is not useful in that you can compile it to a.class file, but you cannot execute it with the JVM since there is no main() method. We will need to create a separate class that will use or instantiate an object of this class. 6/13

GradebookMain class // Now we have a.java file that uses the GradeBook class, // like creating an int variable. This creates or // instantiates a new variable or object // called mygradebook of the GradeBook class. public class GradeBookMain { public static void main ( String args[] ) { GradeBook mygradebook = new GradeBook (); mygradebook.displaymessage(); } } 7/13

GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered 8/13

GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered Middle compartment: - Attributes (data members) 8/13

GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered Middle compartment: - Attributes (data members) Bottom compartment: - Behaviors (member methods), method name, followed by parentheses - Plus (+) sign indicates public member method 8/13

GradeBook class with argument Bottom compartment: - Behaviors (member methods) - method name, followed by parentheses - Plus (+) sign indicates public member method - Parameters are listed in the parentheses, name first followed by a colon and the data type - Data type is language-independent since the UML is used by multiple languages 9/13

GradeBook class with instance variables Middle Compartment: - Attributes (data members) - Minus (-) sign indicates private member method - Data member name followed by colon and data type 10/13

GradeBook class with instance variables Middle Compartment: - Attributes (data members) - Minus (-) sign indicates private member method - Data member name followed by colon and data type Bottom compartment: - Parentheses followed by colon and return data type 10/13

Constructor A special method that is used to initialize a newly created object. All Java objects have a constructor, even if you don t specifically code it. 11/13

Constructor A special method that is used to initialize a newly created object. All Java objects have a constructor, even if you don t specifically code it. Default constructor created by the compiler: public Dog () { } Constructor is called just after the memory is allocated for the object. Constructors may contain code that is run when the object is created (initializes the object. A constructor must have the same name as the class its in. 11/13

GradeBook class with a constructor Indicate a constructor using constructor before name Constructor usually listed first 12/13

Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook. 13/13

Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook. Reference variables contain a reference to an object of the specified class. 13/13

Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook. Reference variables contain a reference to an object of the specified class. Reference variables allow you send messages to objects, such as mygradebook.displaymessage(). 13/13

Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook. Reference variables contain a reference to an object of the specified class. Reference variables allow you send messages to objects, such as mygradebook.displaymessage(). Primitive-type variables are not objects, so cannot receive messages. 13/13