CompSci 100e Program Design and Analysis II January 27, 2011 Prof. Rodger CompSci 100e, Spring20111.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Written by: Dr. JJ Shepherd
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
UML Basics & Access Modifier
Writing Classes (Chapter 4)
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
CompSci 100e Program Design and Analysis II January 18, 2011 Prof. Rodger CompSci 100e, Spring20111.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
CompSci 6 Introduction to Computer Science Sept 29, 2011 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not.
Using Java Class Library
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
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.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Written by: Dr. JJ Shepherd
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
CompSci 100 Prog Design and Analysis II Sept 14, 2010 Prof. Rodger CompSci 100, Fall
Objects, If, Routines. Creating Objects an object is a fundamental software element that has a state and a set of behaviors. Ex: bank account or student.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
CompSci Announcements  Read Chap 3 for next time  Homework will be posted  Consulting Hours will start soon.
Java: Base Types All information has a type or class designation
Written by: Dr. JJ Shepherd
Re-Intro to Object Oriented Programming
Java: Base Types All information has a type or class designation
OOP Powerpoint slides from A+ Computer Science
Intro To Classes Review
Computer Programming Methodology Luminance
CompSci 230 Software Construction
Classes A class is a blueprint of an object
The Random Class and its Methods
Operator Overloading
Defining Classes and Methods
An Introduction to Java – Part II
Creating Objects in a Few Simple Steps
Object Oriented Programming (OOP) LAB # 5
Implementing Non-Static Features
Classes Lecture 7 from Chapter /1/11.
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)
© A+ Computer Science - Classes And Objects © A+ Computer Science -
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Defining Classes and Methods
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Classes and Objects CGS3416 Spring 2019.
Class rational part2.
What to expect this week
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CompSci 100e Program Design and Analysis II January 27, 2011 Prof. Rodger CompSci 100e, Spring20111

Announcements Apt-0127 due tonight Apt-0201 due Tuesday Lab 02 Friday and Monday – Thesaurus APT CompSci 100e, Spring20112

Object, Classes, Methods Classes define – the state (data), usually private – behavior (methods) for an object, usually public There can be many objects created based on a class. Method – sequence of instructions that access the data of an object – Accessor – access, don’t change data – Mutator – changes the data CompSci 100e, Spring20113

Example - class Chicken State – weight, height, name Behavior (methods) – Accessor methods getWeight, getHeight, getName – Mutator methods eat – adds weight, adds some height if under 12.0 sick – lose weight changeName CompSci 100e, Spring20114

Constructing Objects - new 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 100e, Spring20115

Object References Variable of type object – value is memory location CompSci 100e, Spring20116

one = two; Now they reference the same object System.out.println(one.getName() + “ has “ + one.getName().length() + “ letters.”); System.out.println(two.getName() + “ has “ + two.getName().length() + “ letters.”); CompSci 100e, Spring20117

Parts of a Class State – Data Constructors – Initialize state when object is created Accessor methods – Accessing data Mutator methods – Modify data – change the state CompSci 100e, Spring20118

Class Example Chicken class – Chicken.java – Defines state and behavior of Chicken Farm class – Farm.java – Creates Chickens with “new” – Invokes the Chicken constructor – Calls chicken methods on Chickens to access or change state CompSci 100e, Spring20119

What happens here? How many Chickens are constructed? Chicken x, y; Chicken z = new Chicken(1.0, 2.1, “baby”); x = new Chicken(10.3, 8.1, “ed”); y = new Chicken(6.2, 6.3, “mo”); Chicken temp = x; x = y; y = temp; z = x; CompSci 100e, Spring201110

Generating Random Numbers java.util.Random class in Java library Random generator = new Random(); Methods: – int nextInt(int n) - returns integer from 0 up to but not including n – double nextDouble() – returns double between 0.0 and 1.0 int num = generator.nextInt(10); CompSci 100e, Spring201111

Examples Go over Chicken.java, Farm.java Go over Skier.java, SkiRace.java, RunSkiRace.java CompSci 100e, Spring201112