CompSci 6 3.1 Announcements  Read Chap 3 for next time  Homework will be posted  Consulting Hours will start soon.

Slides:



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

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Written by: Dr. JJ Shepherd
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Classes CS 21a: Introduction to Computing I First Semester,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Object-Oriented Programming James Atlas June 12, 2008.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Non-Static Classes What is the Object of this lecture?
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
CompSci 100e Program Design and Analysis II January 27, 2011 Prof. Rodger CompSci 100e, Spring20111.
Java: Base Types All information has a type or class designation
Information and Computer Sciences University of Hawaii, Manoa
Andrew(amwallis) Classes!
Re-Intro to Object Oriented Programming
Creating Your Own Classes
Chapter Topics The Basics of a C++ Program Data Types
ITM 352 Data types, Variables
Java: Base Types All information has a type or class designation
OOP Powerpoint slides from A+ Computer Science
Objects as a programming concept
Java Primer 1: Types, Classes and Operators
Documentation Need to have documentation in all programs
Basic Elements of C++.
Chapter 3: Using Methods, Classes, and Objects
Basic Elements of C++ Chapter 2.
Java Programming with BlueJ
Chapter 2 Using Objects.
An Introduction to Java – Part II
Creating Objects in a Few Simple Steps
String Methods: length substring
Implementing Non-Static Features
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
JAVA CLASSES.
Methods, Data and Data Types
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
COS 260 DAY 6 Tony Gauvin.
Announcements Lab 5 was due today Program 3 due Monday by 12pm
AP Java Unit 3 Strings & Arrays.
Introduction to Computer Science and Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CompSci Announcements  Read Chap 3 for next time  Homework will be posted  Consulting Hours will start soon

CompSci Objects, Classes, Methods  Classes define  the state (data), usually private  behavior (methods) for an object, usually public  Many objects can be created based on one class.  Method – sequence of instructions that access and/or manipulates the data of an object  Accessor method – access, don’t change data  Mutator method – changes the data

CompSci Example - class Chicken  State  weight, height, name  Behavior (methods)  Accessor methods ogetWeight(), getHeight(), getName()  Mutator methods oeat() – adds weight, adds some height if under 12.0 osick() – lose weight ochangeName()  Syntax Note: in code method name always followed by parentheses

CompSci Method Features  Return values  Methods can return information o Accessor methods require that o Have return type in header specifying type of info o Use: w = chick.getWeight();  Parameters  Methods may receive information thru parameters o Mutator methods usually require that o Method header includes parameter definition in parentheses o Use: chick.newName("Elsa");  May have both parameters and return values

CompSci Constructing Objects - new  The new operator  Used to create objects  Create three chickens  “Fred”, weight 2.0, height 3.8  “Sallie Mae”, weight 3.0, height 4.5  “Momma”, weight 6.0, height 8.3  Use Chicken constructor Chicken one = new Chicken(2.0, 3.8, "Fred"); Chicken two = new Chicken(3.0, 4.5, "Sallie Mae"); Chicken three = new Chicken(6.0, 8.3, "Momma");

CompSci Primitive Types  Java builds in primitive types for dealing with numbers  Eight primitive types; only use few at first o int - holds whole numbers only o double – can deal with fractions  think scientific notation  These are not objects o They have no methods  Constants: o int – no decimal point: 256 o double – contains decimal point:

CompSci Another Class - String  Use the API methods for String class  To get to API from CompSci 6 web page o Click on “resources”  Print length of Chicken one ’s name.  length() is a method in String class. System.out.println(one.getName() + " has " + one.getName().length() + " letters. " );

CompSci Object References  Variable of type object – value is memory location

CompSci Assign: one = two;  Now they reference the same object