Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Programming section 3 2 Importing classes purpose packages CLASSPATH creating and using a package.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture 5: Objects and Classes, cont’d Continue with the Player example Introduce GOAL as a final static variable that applies to all objects of the class.
Problem Solving #3: JVM ICS Outline Review of Key Topics Review of Key Topics Problem 1 Problem 1 Problem 2 Problem 2 Problem 3 Problem 3 Problem.
Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How.
UML Basics & Access Modifier
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Chapter 4 Objects and Classes.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
Methods Review. 2. Class-wide vs. local variables. 3. Why C# bans global variables. 4. Nested blocks. 5. Scope of identifiers.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
10-Nov-15 Java Object Oriented Programming What is it?
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC Programming I Lecture 6 September 4, 2002.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
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 6962: Server-side Design and Programming JSF DataTables and Shopping Carts.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
1 Project 3 String Methods. Project 3: String Methods Write a program to do the following string manipulations: Prompt the user to enter a phrase and.
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.
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
Class Fundamentals BCIS 3680 Enterprise Programming.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Classes and Objects Introduced
Creating Your Own Classes
Introduction to programming in java
Chapter 7 User-Defined Methods.
Inheritance ITI1121 Nour El Kadri.
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS1101: Programming Methodology Recitation 7 – Exceptions
The dirty secrets of objects
CS Week 14 Jim Williams, PhD.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Encapsulation and Constructors
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Java Classes and Objects 3rd Lecture
Class Examples.
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)
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Today in COMP 110 Brief static review The Math Class Wrapper Classes
Using java libraries CGS3416 spring 2019.
Agenda Warmup Lesson 2.2 (parameters, etc)
Classes “Absolute C++” Chapter 6
CSG2H3 Object Oriented Programming
Presentation transcript:

Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…

Problems with Homework 1 In some cases, misunderstanding of what to do, pls come to talk to me after class How to convert the input strings for the old and new base to int: –not Integer.parseInt –not Math.pow –can just call str2int –or, special method for converting base since it cannot be more than 2 characters Bugs: easy to check correctness by converting output string back to original base Program was supposed to keep prompting for input until user enters empty string Nice output format is very helpful Identifier names are important The way errors are detected is important: does not have to be complicated, but should not be misleading to user

How we can best learn I propose to show the best program that was submitted, we test it, look at the code, comment on its good and bad points Idea is not to put anyone down, but to learn to be a better programmer I also expect to learn things from you during this course… How do you like this idea? Rule about homework: talking to people is good, acknowledging it is even better, but copying (or editing) other students’ solutions is absolutely forbidden (even with their permission)

Classes All Java programs are organized into classes Most classes have instance variables and instance methods, allowing us to construct objects of the class Exception: some classes just have static variables and static methods, such as the Math class. You cannot construct an object of the Math class.

An Example: the Player class int heads; // data field (instance variable) Player() { // constructor // initialize heads to 0 } void flip() { // instance method // increment heads with probability 1/2 }

Constructing 2 Player objects Player jack = new Player(); Or Player jack; jack = new Player(); Player jill = new Player(); while jack.heads < 10 & jill.heads < 10 { jack.flip(); jill.flip(); }

Private vs Public Considered good practice to make data fields of an object private Then they can only be accessed or changed by methods of the same class Methods that do this are called accessor and mutator methods (getter and setter) methods respectively Likewise private methods cannot be called outside the class Public data fields and methods can be called from anywhere; those that are neither public nor private can be called anywhere from with the same package

Object variables are really references What does the following do: Player jack = New Player(); Player jill = New Player(); jill = jack; while jack.heads < 10 & jill.heads < 10 { jack.flip(); jill.flip(); }