CPSC 233 Tutorial 5 February 9 th /10 th, 2015. Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Programming
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
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.
Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS
 2005 Pearson Education, Inc. All rights reserved 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.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS
Classes, Objects, and Methods
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Control Structures if else do while continue break switch case return for.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Java Primitive Types Operators Basic input and output.
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.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Creating and Using Class Methods. Definition Class Object.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Classes - Intermediate
Methods.
Exam Review 10/01/2014 Happy October. The Exam  Will be in Canvas  Two parts  Part A is recall – closed book, closed notes ◦Quizzes, in class activity.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Functions + Overloading + Scope
Objects as a programming concept
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Methods and Parameters
Method Mark and Lyubo.
Starting Out with Java: From Control Structures through Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Unit-2 Objects and Classes
Classes, Objects, and Methods
Defining Classes and Methods
An Introduction to Java – Part II
An Introduction to Java – Part I, language basics
Lecture Notes – Week 3 Lecture-2
Group Status Project Status.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Programming Review 1
Session 2: Introduction to Object Oriented Programming
Defining Classes and Methods
Happy October Exam Review 10/01/2014.
Unit-1 Introduction to Java
Presentation transcript:

CPSC 233 Tutorial 5 February 9 th /10 th, 2015

Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type + Name Each object of a class has its own copy of the class instances.

Java Instance Variables public class SpeciesSecondTry { public String name; public int population; public double growthRate; public void writeOutput () { … } public int PredictPupulation (int years) { … }

Java Methods A method contains: Heading Name A method is invoked by its name Output Type Input Type Name Body Block {} Local Variables A series of statements defining the method functionality

Java Methods Output A method of a class should be invoked to: Return a value Perform a task without returning a value

Method Without Output Methods without Output Use the keyword void Perform some operations such as printing or changing the value for an instance variable within its block

Method With Output Method with output Define the type of output Use the keyword return to return the output

Method Input Input parameters (Formal parameters) serve as a blank space in the method These blank spaces are considered local variables for the method Input types: Primitive Types Byte – Short – Int – Long – Float – Double – Char Is String a primitive? Reference to an object Class name + Object name

Method Input Cont. When invoking a method using arguments, your arguments should have the same number, order and type as your formal parameters. Invoking PredictPopulation method:

Exercise Write a Dog class in Java with following characteristic: This class has one instance variable: Name An object of this class should be able to perform following operations: Set values for instance variable. Take the dog age as a primitive parameter and calculate the human age. Print out the instance variables and human age

Exercise Solution

Exercise cont. Write a class to test your Dog calss

Class Test

References Course Slides JAVA, An introduction to problem solving and programming