Introduction to Objects A way to create our own types.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

Procedural programming in Java
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Liang Chapter 6 Variable scope and the keyword this.
A simple program: Area of a circle Problem statement: Given the radius of a circle, display its area Algorithm: 4. Display area To store values, we need.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
Applications in Java Towson University *Ref:
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.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Procedural programming in Java Methods, parameters and return values.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Classes - Intermediate
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes
Chapter 7 User-Defined Methods.
using System; namespace Demo01 { class Program
Introduction to Computer Science / Procedural – 67130
Lesson 2: Building Blocks of Programming
Objects and Classes Creating Objects and Object Reference Variables
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Chapter 7 Objects and Classes
Presentation transcript:

Introduction to Objects A way to create our own types

Type and Variables Until this point we were only able to create simple types, actually call “primitive” types –integer –double –float –char –String (actually not primitive)

We want to do better Bank Account Math Calculator Stock Car Vending machine Telephone Model “whatever” as software Make a computer act like one

Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Consider methods Take a known process Package it for reuse if ((a >= b) && (a>=c)) max = a else if ((b >= a) && (b>=c)) max = b else max = c; int maxof3(int a, int b, int c){ int max; if ((a >= b) && (a>=c)) max = a else if ((b >= a) && (b>=c)) max = b else max = c; return max; }

How are object like this? Take a group of methods and data and package those for reuse. a( ) b( ) c( ) int i int k a( ) b( ) c( ) int i int k class Testclass

int i; int j; void a() {…} void b() {…} void c() {…} class TestClass{ int i; int j; void a() {…} void b() {…} void c() {…} }

How do we decide what goes inside? The problem will guide us. The things we put inside will define –What the object will do – How we can interact with it These things will be the “Bank Account”s, “Student”s, etc

Let’s start simple A circle

What are some of the attributes of a circle? Radius (most obvious) Color Border Position

How do we interact with a circle? Change its size Move it Ask it for its area … depending on the problem’s needs

Let’s start with a simple Circle class Just a radius –No borders or colors A means of asking it for its area. This will serve as the basis (a type or class) for creating lots of circles

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } Heading for the class

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } A property of each circle

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } A method named Area that will calculate the area of that specific circle

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } A constructor Used to initialize the circle Let’s see how in the next slide

Creating circles Circle circle1 = new Circle(10); Radius:10 circle1

Creating circles Circle circle1 = new Circle(10); radius:10 circle1 Circle circle2 = new Circle(15); radius:15 circle2

Asking about area Circle circle1 = new Circle(10); System.out.println(circle1.Area()); radius:10 circle1 Circle circle2 = new Circle(15); System.out.println(circle2.Area()); radius:15 circle2 Each circle will return it’s own area

What’s the difference? Circle circle1 = new Circle(10); Circle circle1; Creates a REFERENCE Like having a telephone number for a friend.. a means to find them. But this one is a contact without a number.

What’s the difference? Circle circle1 = new Circle(10); Circle circle1; Creates the object and defines the reference to the object In this case, circle1 actually refers to a real Circle.

Only a reference.. No circle Circle circle1; circle1

A reference with a circle Circle circle1 = new Circle(10); Radius:10 circle1

Put it Together! public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String[ ] args) { Circle circle1 = new Circle(10); System.out.println(circle1.Area()); Circle circle2 = new Circle(15); System.out.println(circle2.Area()); }

Do I need new() ? Can I define a variable and just reference from the main? How about this? … TRY IT… public class Circle() { double radius=5; public static void main(String args[]) { System.out.println(radius); } Error: non-static variable radius cannot be referenced from a static context You never “new()”ed one. No radius exists.

Only a reference.. No circle Circle circle1; circle1

Do I need new() ? Here there is no Area() or radius defined. Because NO new() has occurred! public class Circle() { double radius=5; double Area() { double this area = radius*radius*Math.PI; } public static void main(String args[]) { System.out.println(Area()); } Error : nonstatic method can not be referenced from static method

This one creates the object. Then a radius and Area() exists to use… no errors. public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { Circle circle1 = new Circle(10); System.out.println(circle1.Area()); Circle circle2 = new Circle(15); System.out.println(circle2.Area()); } These are created when you new()

public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i); Circle circle1 = new Circle(10); System.out.println(circle1.Area()); } Why can I declare “i” like this in the main, but not radius in the previous example? This is legal!

public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i); Circle circle1 = new Circle(10); System.out.println(circle1.Area()); } Because main is static. Static needs more explanation.

Conclusion: “ new ” before using static is coming!