Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.
Classes and Objects in Java
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
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.
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.
TCU CoSc Introduction to Programming (with Java) Getting to Know Java.
Object Oriented Software Development
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes, Objects, and Methods
Writing Classes (Chapter 4)
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Introduction to Object-Oriented Programming
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Introduction to programming in the Java programming language.
Classes and Objects in Java
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
01/24/2005 Introduction to Programming with Java, for Beginners A Closer Look at Constructors and Methods.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Chapter 3 Implementing Classes
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Content Programming Overview The JVM A brief look at Structure
Object-Oriented Programming: Classes and Objects
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Java Primer 1: Types, Classes and Operators
Classes and Objects in Java
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Introduction to Programming with Java, for Beginners
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes, Objects, and Methods
Classes and Objects in Java
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Introduction to Object-Oriented Programming
Classes, Objects and Methods
Announcements Assignment 2 and Lab 4 due Wednesday.
Classes and Objects in Java
CMSC 202 Exceptions.
Classes and Methods 15-Aug-19.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure

1 Object-Oriented Programming Model Design problems/programs such that they correspond to real world entities Entity a.k.a Object is something that has real existence Examples: person, dog, car, student, bank account Objects: Model the parts of a problem. They have: Data/State: information about that object Behaviors: describe how the object behaves Classes Describes common features to all objects Templates for creating objects

2 Example 1: Student Object Data: name, address, major, courseList Behavior: change major, add/drop a course

3 Example 3: Dog Objects

4 OOP in Java A Java program is a collection of objects A class is an abstract description of objects Classes contain Data Fields that hold the data for each object  Data is stored in instance variables Methods that describe the actions/behaviors the object can perform Constructor(s) that tell how to create a new object of this class

5 Class Structure class Classname { //Data Fields … //Constructor.. //Methods.. } Classname and Filename should be same i.e. Classname.java Start and end of class declaration is indicated by curly braces i.e. { } Compiler complains if this is missing

6 Classes contain Data Definitions Classes describe the data held by each of its objects Also known as instance variables class Dog { String name; int age;...rest of the class... } Data usually goes first in a class

7 Classes contain Data Definitions (contd..) Data can also describe the relationship of the object to other objects Example: a checkingAccount might have An accountNumber (used as an ID number) A balance (the internal state of the account) An owner (some object representing a person)

8 Classes always contain Constructors A constructor is a piece of code that “constructs,” or creates, a new object of that class If you don’t write a constructor, Java defines one for you (behind the scenes) i.e. default constructor Usually a constructor is written to initialize an object’s state i.e. data fields Default constructor initialize object’s state default value for that type. E.g. for type int, the default value is zero

9 Constructor: Initialize Data Example class Dog { String name; int age; Dog(String Name, int Age) { name = Name; age = Age; } … //rest of the class } Important: Constructor name is same as ClassName (This part is the constructor)

10 Creating Objects Declare a variable of appropriate type to hold the object E.g. For Dog object we a need a variable of type Dog Use the constructor of Dog to create a Dog instance and store it in the variable Known as invoking the constructor Dog d1; d1 = new Dog(); make a new object Dog d2; d2 = new Dog(“Fido”, 5); make a new object

11 Classes contain Methods A class may contain methods that describe the behavior of objects Methods contain one or more statements that describe a particular behavior A block of code, which when used performs certain instructions Different from control block structures like if, while  Can receive inputs  Can return an output

12 Examples of Object Behavior Query Methods: ask an object about its state What’s your name? Age? Favorite band? Command Methods: change an object’s state Withdraw $100 from my bank account  my bank balance changes

13 Example of Method class Dog {... void bark() { //instructions for barking System.out.println(“Woof”); } } Methods usually go after the data & constructor Statement

14 Making object do something To make a dog object bark, in Java we write d1.bark(); d1 is dog object and we ask a this particular dog to bark, it says “Woof” Terminology:  Sending message to dog object  Commanding or Asking the dog to something

15 General Method Declaration return_type methodName(0+ parameters){..} Method Input receive 0 or more inputs. E.g. bark() has no inputs Inputs may be passed so that method can use these values for some purpose A method specifies its expected inputs via a list of “formal parameters” (type1 name1,type2 name2, …) Input void bark(String barkSound){ System.out.println(barkSound); }

16 General Method Declaration (contd..) return_type methodName(0+ parameters){..} Method Output A method may output nothing or one thing If it outputs nothing, then its return_type is void void setDogAge(int dogAge){ age = dogAge; } Output

17 General Method Declaration (contd..) Method Output If it outputs one thing: Its return_type is non- void  i.e. must be of certain type It then must have a return statement that returns a value The value returned must match return type int getDogAge(){ return age; } age is of type “int” and hence return type is int

18 Calling/Invoking a Method on an Object Calling/Invoking a method on an Object Is a programming terminology for asking or making an object to perform some behavior In general objectName.methodName(0 + parameters) In a method call, the number, order, and type of arguments must match the corresponding parameters Examples: d1.bark(); d1.bark(“Woof grrr”);

19 Methods can contain other methods Example: class Dog {... void bark() { System.out.println("Woof"); } } E.g. System.out.println(String s) String s is the input to the method Other programmer's wrote printing program and called it class System

20 Reusing already written methods Java is famous for reusing already written programs Benefits: Do not need to reinvent the wheel Java provides a huge library of useful programs that can be reused Comes along with the JDK tool kit Some libraries are part of the language and can be simply used Some libraries (utilities) need to specified with an import statement More on this later lectures

21 Methods may contain temporary data Data described in a class exists in all objects of that class Example: Every Dog has its own name and age A method may contain local temporary data that exists only until the method finishes E.g: void wakeTheNeighbors( ) { int i = 50; // i is a temporary variable while (i > 0) { bark( ); //using its own class method i = i – 1; } }

22 Temporary /Local vs. Instance Variables Temporary/local variables are known From the point of declaration until the end curly brace of the block in which they are declared In contrast, instance variables are Declared outside of any method Known to all methods in the class in which they are declared

23 Class Methods in General Methods consist of statements (or instructions) causes the object to do something Combination of literals and, operators Control Structures - e.g. while, if Temporary variables Methods from other classes Methods from its own class

24 Asking Object about its data directly It may possible to ask a object about its data without querying the object i.e. calling the method ObjectName.DataField; >Dog d1 = new Dog(“Fido”,5); >String dogName = d1.name; >System.out.println(dogName); Fido But this depends on accessibility of the data More on this in slide 28

25 Putting it all together When you want to create standalone Java application program one of the classes should contain the main method Method main is a special method, not part of any object, that is executed in order to get things started such as: Creating Objects Ask objects to do something or interact with another object

26 Main Method public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); // creates a Dog fido.bark(); fido.setAge(6); } Method main is of return type void Will explain the keyword public (on slide 28) Learn about static and String [] args in later lectures

27 OOP Recap Class: a template for creating objects An object is an instance of a class Example: One Dog class Multiple Dog objects Lord of the Rings Simulation  One Human class, multiple Human objects  One Elf class, multiple Elf objects  One Orc class, multiple Orc objects

28 A Counter class example

29 Complete Counter class public class Counter { private int count; public Counter () { count = 0; } public int currentCount () { return count; } public void incrementCount () { count = count + 1; } public void reset () { count = 0; } } Counter class declaration constructor methods Data field (instance variable) query command ?

30 Counter Object Behaviors Counter c= new Counter(); If you want to increment count value of object c c.incrementCount(); If you want see the count value of object c c.currentCount(); If you want to reset count value of object c c.reset();

31 Accessibility Level Data fields/instance variables of class and class itself can also have accessibility_level Examples: public void incrementCount(){.. }, private count ; public: makes the method/data field accessible from outside the class Private: not accessible outside the class In default case: accessible if within same directory Accessibility_level appears before the return type of method the type of variable

32 Complete Counter Program public class Counter { private int count; public Counter () { count = 0; } public int getCount () { return count; } public void incrementCount () { count = count + 1; } public void reset () { count = 0; } public static void main(String[ ] args) { Counter c = new Counter();  int whatIsCount = 0;  c.incrementCount(); whatIsCount = c.currentCount(); System.out.println(whatIsCount);  c.reset(); whatIsCount = c.currentCount();  System.out.println(whatIsCount); } } // ends the class

33 General Java Program A program consists of one or more classes Typically, each class is in a separate java file File Class Variables Constructors Methods Variables Statements

34 Writing and Running Programs in OOP When you write a program You are writing classes and all the things that go into classes Your program typically contains commands to create objects and make them do something When you run a program It creates objects, and those objects interact with one another and do whatever they do to cause something to happen

35 Advantages of OOP OOP is conducive to good design and efficient redesign Most changes affect only a few classes