COMP 110-001 Classes Yi Hong May 22, 2015. Announcement  Lab 2 & 3 due today.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference l Parameter passing Classes, Objects, and Methods.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Classes, Objects, and Methods
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Chapter Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS
COMP More About Classes Yi Hong May 22, 2015.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Writing Classes (Chapter 4)
Introduction to Object-Oriented Programming
CSC 212 Object-Oriented Programming and Java Part 1.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
COMP Mid-Term Review Yi Hong May 27, 2015.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Day 5. Task: Implement your vending machine code into the program with JOptionPane I/O (modify it) it to me.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
COMP Review of Chapter 1 & 2
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Programming Logic and Design Seventh Edition
Classes and OOP.
Yanal Alahmad Java Workshop Yanal Alahmad
Defining Classes and Methods
CS2011 Introduction to Programming I Objects and Classes
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
COP 3330 Object-oriented Programming in C++
Defining Classes and Methods
Introduction to Object-Oriented Programming
Announcements Assignment 2 and Lab 4 due Wednesday.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

COMP Classes Yi Hong May 22, 2015

Announcement  Lab 2 & 3 due today

Review  Q1: What are the three types of loops? What are their differences?  Q2: Write a program that maintains the balance of an account Ask for a balance-update from user in each iteration Positive value: deposit Negative value: withdraw If the balance-update is 0 or the balance goes below 0, exit from loop and print out the remaining balance

Sample Code for Q2

num++ v.s. ++num  num++ does num = num + 1;  So does ++num. But, there is a difference int num1 = 5; System.out.println(num1++); Outputs num1 (5), then +1 int num2 = 5; System.out.println(++num2); +1, then outputs num2 (6)

Today  Classes

Classes and Objects  Java programs (and programs in other object-oriented programming languages) consist of objects of various class types  Objects can represent objects in the real world Automobiles, houses, employee records  Or abstract concepts Colors, shapes, words

Object Oriented Programming (OOP)  Object: Attributes + Methods  Class: the blueprint of objects of the same type Person name, contact Student student ID, program, year Teacher employee ID, department, rank S1 name=“Alan”, contact=“919-…..”, program = biostat, year = 1st S1 name=“Alan”, contact=“919-…..”, program = biostat, year = 1st S2 name=“Anna”, contact=“919-…..”, program = CS, year = 1st S2 name=“Anna”, contact=“919-…..”, program = CS, year = 1st T1 name=“Yi”, contact=“919-…..”, dept = CS, rank = no rank T1 name=“Yi”, contact=“919-…..”, dept = CS, rank = no rank T2 name=“Marc”, contact=“919-…..”, program = biostat, rank = assoc prof T2 name=“Marc”, contact=“919-…..”, program = biostat, rank = assoc prof Class Objects Superclass Subclass

OOP in Practice  Import class if necessary E.g.: import java.util.*;  Create object Class_Type variable_name = new ClassType(…); E.g.: Scanner keyboard = new Scanner(System.in); Polygon treeTop = new Polygon();  Access object members (attribute or method) int inputNumber = keyboard.nextInt(); treeTop.setColor(Color.green);

 A class is the definition of a kind of object A blueprint for constructing specific objects Specifies an object’s attributes and defines its behaviors as methods Class  Today, we will talk about how to create our own classes

UML (Unified Modeling Language) Automobile - fuel: double - speed: double - license: String + accelerate(double pedalPressure): void + decelerate(double pedalPressure): void Class name Data Methods (actions)  Use a UML class diagram to help design a class

Objects, Instantiation Object Name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Object Name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Object Name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF” Instantiations, or instances, of the class Automobile

 Classes specify the data type, what kind of data the objects have  Important: classes usually do not have data; individual objects have data.  But, a class can have variables that are static as well as methods that are static.  Static variables and static methods belong to a class as a whole and not to an individual object (more discussion later) Objects

 Each Java class definition goes in its own, it is in a separate file  ClassName  save the file as ClassName.java  E.g.: Student.java includes the class Student Class Files and Separate Compilation

 What happens when you compile a.java file?.java file gets compiled into a.class file Contains Java bytecode The same filename except for.class instead of.java  You can compile a Java class before you have a program that uses it  Don’t worry about the compilation in this course as Eclipse does it automatically Class Files and Separate Compilation

Example: Class Student Class Name: Student - Name - Year - GPA - Major - Credits - GPA sum + getName + getMajor + printData + increaseYear How: increase year by 1 + calcGpa How: average grades - : private + : public In this lecture, we focus on public first, we will discuss about private members later

Example: Class Student Class Name: Student - name: String - year: int - gpa: double - major: String - credits: int - gpaSum: double + getName(): String + getMajor(): String + printData(): void + increaseYear(): void + calcGpa(double grade): void

public class Student { public String name; public int classYear; public double gpa; public String major; //... public String getMajor() { return major; } public void increaseYear() { classYear++; } Defining a Class Class name Data (instance variables) Methods Instance variables and methods are members of a class

 Data defined in the class are called instance variables public String name; public int classYear; public double gpa; public String major; Instance Variables public: no restrictions on how these instance variables are used (more details later – public is actually a bad idea here) Data type: int, double, String… variables

public class Student { public String name; public int classYear; public double gpa; public String major; // … public String getMajor() { return major; } public void increaseYear() { classYear++; } Using Instance Variables Inside the Class Definition

 Create an object jack of class Student Student jack = new Student(); Scanner keyboard = new Scanner(System.in);  Create an object keyboard of class Scanner Creating an Object Create an object Return memory address of object Assign memory address of object to variable

public static void main(String[] args) { Student jack = new Student(); jack.name = “Jack Smith”; jack.major = “Computer Science”; System.out.println(jack.name + “ is majoring in ” + jack.major); Student lily = new Student(); lily.name = “Lily Chase”; lily.major = “Biology”; System.out.println(lily.name + “ is majoring in ” + lily.major); } Using public Instance Variables Outside a Class jack.name and lily.name are two different instance variables because they belong to different objects

 Instance variables Declared in a class Confined to the class Can be used in any method in this class  Local variables Declared in a method Confined to the method Can only be used inside the method 23 Local / Instance Variables public class Student { public String name; public int classYear; public String major; public void printInfo(){ String info = name + “:” + major + “:” + classYear; System.out.println(info); } public void increaseYear(int inc) { classYear += inc; }

public class Student { public String name; public int classYear; public String major; public void printInfo() { String info = name + “: ” + major + “: ” + classYear ; System.out.println(info); } public void increaseYear(int inc) { classYear += inc; info = “info changed a bit”; } } 24 An Example Java will not recognize info ✗

public class Student { public String name; public int classYear; public String major; public void printInfo() { String info = name + “: ” + major + “: ” + classYear ; System.out.println(info); } public void increaseYear(int inc) { classYear += inc; String info = “classYear updated”; System.out.println(info); } 25 An Example The two variables, info, will not affect each other This will become more clear after we discuss code block later

 Methods  Code block Next Class