PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

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.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
COMP More About Classes Yi Hong May 22, 2015.
PART 2 Part 2: The Material. With the introduction of what an object is, now we are ready to learn the CONSTRUCTOR concept. And when we want to make use.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and 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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
PART 3. You have been using static methods in your programs before and methods are the building blocks of procedural programming. In this part of the.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Objects and Classes Mostafa Abdallah
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.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
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.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
OOP Basics Classes & Methods (c) IDMS/SQL News
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Class Fundamentals BCIS 3680 Enterprise Programming.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
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.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Topic: Classes and Objects
Concepts of Object Oriented Programming
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
Variables ICS2O.
Java Programming Function Introduction
OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Building Java Programs
Java Programming Function Introduction
Presentation transcript:

PART 1 Part 1: The Material

Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming. A procedural program is written as a list of instructions, telling the computer, step-by-step, what to do: Define a variable, read a number, multiply by 4, display something. A typical program that you have written probably looks like: Part 1: The Material

class Application { static double pi = 3.14; static void main(String args[]) { int radius = 3; String color = “red”; double area = calculate_area(radius); System.out.println(“The “ + color + “ circle with radius “ + radius + “has area “ + area + “.”); } static double calculate_area(int r) { return (double) r * r * pi; } The application starts by the first statement of the main method, and each line is executed sequentially. If there is a method call, then the code of the method gets executed. Later, the call again returns to the main application code Part 1: The Material

class Application { static double pi = 3.14; static void main(String args[]) { int radius = 3; String color = “red”; double area = calculateArea(radius); System.out.println(“The “ + color + “ circle with radius “ + radius + “has area “ + area + “.”); } static double calculateArea(int r) { return (double) r * r * pi; } Notice the use of the keyword static. It is used to define the variables which are global to the application. Furthermore, it is there in the declaration of each function. Global Variable Part 1: The Material

But, what does static mean? This will be best understood after you learn what an object is. Now we will learn the very basics of object-oriented programming and objects. Part 1: The Material

As you know, the program you write in Java is inside a class. The classes you have been defining so far (the procedural programs) can be named as static classes since each method and global variable is defined to be static. There is also another type of class which we will call a non- static class. class Classname { // Body of the class // Your code goes here. } Part 1: The Material

Java Classes static non-static (this you already know) (this is what you will learn) Non-static classes will allow you to define objects. So the question is: What is an object and why is it useful? Part 1: The Material

So far you have seen data types such as int, double, String and boolean. Although these are essential, they are not convenient to store complicated data. What do we mean by this ??? Suppose in your program you wanted to store the title, author and the year published of a number of books. How would you do this? Here are some alternatives: Part 1: The Material

Solution 1: Create one String variable for each book that holds the title, author and the year. (We will call title, author and year the fields of the book.) Example: Problem: Accessing each field is not easy. For example, changing the year would be cumbersome. String book1 = “Lord of the Rings, J.R.R. Tolkein, 1955”; Part 1: The Material

Solution 2: For each book, create a String variable for the title, another String variable for the author and an int variable for the year. Example: Problem: For each book, you need to create three variables! What if there were 10 fields and you wanted to store 10 books? You would have to define 100 variables. String book1Title = “Lord of The Rings”; String book1Author = “J.R.R. Tolkein”; int book1Year = 1955; Part 1: The Material

Wanted Solution: “Create” a data type called Book which encapsulates three variables, String title, String author, int year, such that the following hold. 1)Accessing each of these variables is easy. 2)We only create one Book variable (object) for each book we want to store, regardless of the number of fields. author Book Class title year Think of the Book class as a box that includes three elements. Java allows you to do this. Part 1: The Material

How? First we have to create the Book data type. This is where we use the non-static class. Let’s create a Book data type now. This is a non-static class. The definitions of the variables do not contain the word static. class Book { String title; String author; int year; } Part 1: The Material

By creating this class, we have defined a new data type called Book which encapsulates three variables: title, author, year. Now let’s see how we can use this Book data type in our static class containing the main method. Defining a Book variable: The variable is called b1 and it is of type Book Observe that this is no different from defining an int or a String. class Application { public static void main(String args[]) { Book b1; } int s; String x Part 1: The Material

Initializing a variable: Or simply, Here “new Book();” creates a new Book object which has initial default values for title, author and year. These values are title = “”, author = “”, year = 0. The equality sign represents an assignment. Now the b1 variable points to a new book object. Book b1; b1 = new Book(); Book b1 = new Book(); Part 1: The Material

Accessing the fields of the book and modifying their values: We have to use the dot operator. b1.title and b1.author are String variables. b1.year is an int variable. You can play with these variables just like any other String or int variable. b1.title = “Lord of the Rings”; b1.author = “J.R.R. Tolkein”; b1.year = 1955; b1.title += “, The Return of the King”; b1.year++; System.out.println(b1.title); System.out.println(b1.year); Part 1: The Material

Creating another Book object: Each Book object we create has its own variables title, author and year. So b1.title has nothing to do with b2.title. Thus manipulating b1.title would not change b2.title and vice versa. String title String author int year String title String author int year b1b2 Book b2 = new Book(); b2.title = “In Cold Blood”; b2.author = “Truman Capote”; b2.year = 1965; Part 1: The Material

Returning an object: The return type of a method can be an object and thus you can return an object in that method. class Application { static void main(String[] args) { Book b1 = createBook(“Lord of the Rings”, “J.R.R. Tolkein”, 1955); System.out.println(b1.title); System.out.println(b1.author); System.out.println(b1.year); } static Book createBook(String t, String a, int y) { Book b = new Book(); b.title = t; b.author = a; b.year = y; return b; } Part 1: The Material

Passing an object as a parameter: In an application, you may have objects as parameters. class Application { public static void main(String[] args) { Book b1 = new Book(); b1.title = “Lord of the Rings”; b1.author = “J.R.R. Tolkein”; b1.year = 1955; print_info(b1); } static void print_info(Book b) { System.out.println(“Title: ” + b.title); System.out.println(“Author: ” +b.author); System.out.println(“Year: ” + b.year); } Part 1: The Material

Review of Concepts Two types of classes: static and non-static. Non-static classes enable one to create objects. Objects allow you to encapsulate more than one variable in one object variable. The fields of an object are accessed using the dot operator. Each object you create has its own fields independent from the fields of other objects that may be defined. Objects can be returned in a method or can be passed to methods through parameters (just like int or String variables). Part 1: The Review

Review of Syntax Non-static class (defining a new data type) Creating an object Accessing the fields of an object using the dot operator class Book { String title; String author; int year; } Book b1 = new Book(); b1.title = “Lord of the Rings”; b1.author = “J.R.R. Tolkein”; b1.year = 1955; Part 1: The Review

Exercise Write a non-static class called Student that has three fields: String firstName, String lastName and int id. Complete the createNewStudent() method below. It is supposed to create a new student object with the given values of first name, last name and id number. Then this student should be returned. class Application { static void main(String[] args) { Student std = createNewStudent(“James”, “Newsted”, ); System.out.println(std.firstName + “ ” + std.lastName + “, ” + id); } static Student createNewStudent(String fname, String lname, int i) { // your code goes here } Part 1: The Exercise

Solution class Student { String firstName; String lastName; int id; } static Student createNewStudent(String fname, String lname, int i) { Student s = new Student(); s.firstName = fname; s.lastName = lname; s.id = i; return s; } Part 1: The Solution