Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
Advertisements

Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference l Parameter passing Classes, Objects, and Methods.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
ECE122 Feb Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their.
Classes, Objects, and Methods
© Marty Hall, Larry Brown Web core programming 1 Basic Object-Oriented Programming in Java.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
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.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
COMP More About Classes Yi Hong May 22, 2015.
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Classes, Objects, and Methods
Writing Classes (Chapter 4)
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
CSC 142 Computer Science II Zhen Jiang West Chester University
COMP 150: Introduction to Object-Oriented Programming Lecturer: Dr. AJ Bieszczad 4-1 Chapter 4 l Class and Method Definitions l Information Hiding and.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Objects and Classes Mostafa Abdallah
Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
CSC 142 Computer Science II Zhen Jiang West Chester University
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.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Class Structure 15-Jun-18.
Instance Method Review - CIS 1068 Program Design and Abstraction
CSC240 Computer Science III
CSC240 Computer Science III
Class Structure 16-Nov-18.
Classes, Objects, and Methods
Defining Classes and Methods
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Classes Lecture 7 from Chapter /1/11.
CS2011 Introduction to Programming I Methods (II)
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Core Concepts.
Class Structure 25-Feb-19.
Instance Method – CSC142 Computer Science II
Defining Classes and Methods
Classes, Objects and Methods
Announcements Assignment 2 and Lab 4 due Wednesday.
Presentation transcript:

Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS

Object-Oriented Programming Objects ◦They are all around us ◦Cars, people, trees… Each object can perform certain actions ◦Independently or interacting with other objects ◦These actions are called methods Each objects has characteristics or attributes ◦Example: a car has static attributes like its color and dynamic attributes like its speed A class defines the type or kind of object ◦Example: all car objects could belong to the automobile class ◦A blueprint Objects belonging to a particular class have similar attributes, but are not the same!

A Class Outline The Unified Modeling Language can be used describe classes -fuel: double -speed: double -license: String Automobile +accelerate(double pedalPressure) +decelerate(double pedalPressure)

A Dog Class Use UML to describe such a class ◦What attributes would it have? ◦What methods?

public class Dog { public String name; public String breed; public int age; public void writeOutput() { System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } public int getAgeInHumanYears() { int humanAge = 0; if (age <= 2) { humanAge = age * 11; } else { humanAge = 22 + ((age-2) * 5); } return humanAge; } Instance variables Variables in the body of the class Can be accessed by all class methods SYNTAX public class Class_Name { Instance_Variable_Declarations... Method_Definitions }

public class DogDemo { public static void main(String[] args) { Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; balto.writeOutput(); Dog scooby = new Dog(); scooby.name = "Scooby"; scooby.age = 42; scooby.breed = "Great Dane"; System.out.println(scooby.name + " is a “ + scooby.breed + "."); System.out.print("He is " + scooby.age + " years old, or "); int humanYears = scooby.getAgeInHumanYears(); System.out.println(humanYears + " in human years."); } Create a new Dog object using Java’s new operator new allocates a new set of instance variables for the object A class does not need to have instance variables Public instance variables can be set/read from other classes

public class Dog { public String name; public String breed; public int age; public void writeOutput() { System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } public int getAgeInHumanYears() { int humanAge = 0; if (age <= 2) { humanAge = age * 11; } else { humanAge = 22 + ((age-2) * 5); } return humanAge; } Method not returning any value Method returning an int

public class DogDemo { public static void main(String[] args) { Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; balto.writeOutput(); Dog scooby = new Dog(); scooby.name = "Scooby"; scooby.age = 42; scooby.breed = "Great Dane"; System.out.println(scooby.name + " is a “ + scooby.breed + "."); System.out.print("He is " + scooby.age + " years old, or "); int humanYears = scooby.getAgeInHumanYears(); System.out.println(humanYears + " in human years."); } Invoking a method Invoking a method and saving its return value Defining methods SYNTAX: public Return_Type Method_Name(Parameters) { Statements }

Defining Methods public void writeOutput() { System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } Method heading Method body

Defining Methods public void writeOutput(int unusedArgument1, String unusedArgument2) { double unusedLocalVariable; System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } Method heading Method body Method arguments Local variables

Understanding A Method Call Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; balto.writeOutput(); Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; System.out.println("Name: " + balto.name); System.out.println("Breed: " + balto.breed); System.out.println("Age in calendar years: " + balto.age); System.out.println("Age in human years: " + balto. getAgeInHumanYears()); System.out.println();

Defining Methods that Return Values public int getAgeInHumanYears() { int humanAge = 0; if (age <= 2) { humanAge = age * 11; } else { humanAge = 22 + ((age-2) * 5); } return humanAge; } Return value Method return type Return statement SYNTAX: return Expression; TIP Make your code more readable by using only one return statement

Using return in void Methods You can use it without a return value to alter control flow public void showLandPortion() { if (population == 0) { System.out.println("Population is zero."); return; // Ends here to avoid division by zero. } double fraction = 6.0 / population; System.out.println("If the population were spread "); System.out.println("over 6 continents, each " + "individual"); System.out.println("would have a fraction of its "); System.out.println("continent equal to " + fraction); } TIP Generally should be avoided. Code is cleared when using if.. else

Using the this Keyword Referring to an object’s public instance variables Object_name. Variable_name The this keyword can be used to refer to an object’s own instance variables from within its methods Not necessary in this case, but can help improve readability Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; public void writeOutput() { System.out.println("Name = " + this.name); System.out.println("Population = " + this.population); System.out.println("Growth rate = " + this.growthRate + "%"); }

Local Variables Local variables are only valid within the method they are declared Local variables can also be declared within a block or compound statement (between { }) ◦Only valid within that block We have seen local variables and instance variables. Are there any other kind of variables? public void showNewBalance() { double newAmount; newAmount = amount + (rate / 100.0) * amount; } System.out.println("With interest added, the new amount is $" + newAmount); }

Method Parameters Parameters can be used to write generic/customizable methods Within the method it can be used as a local variable Supplying arguments when calling methods can be done ◦Using constants myNumber.exponentiation(2); ◦Using other variables ◦int exponent = 4; ◦myNumber.exponentiation(exponent); Arguments are automatically cast to the right type, if the parameter is larger than the supplied value ◦byte→short→int→long→float→double class Number { public long base; public long exponentiation(int power) { long result = 1; for (int i = 0; i < power; i++) { result *= base; } return result; }

Primitive Types are Passed Using call-by-value Defining method parameters of primitive types is equivalent to declaring a local variable int exponent = 4; myNumber.exponentation(exponent); System.out.println(exponent); What will this print? class Number { public long base; public long exponentiation(int power) { long result = 1; while (power-- > 0) { result *= base; } return result; }