© 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.

Slides:



Advertisements
Similar presentations
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
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.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Objects and Classes Writing Your Own Classes A review.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
 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.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
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.
UML Basics & Access Modifier
Introduction to Objects A way to create our own types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
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.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
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.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
1 Object-oriented design requires that we interact with a problem in much the same way that we interact with our world – we treat it as a set of separate.
Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Classes and Objects in Java
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
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Creating and Using Class Methods. Definition Class Object.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Classes - Intermediate
Topics Instance variables, set and get methods Encapsulation
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Building Java Programs Interfaces reading: , 16.4.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
1 More About Derived Classes and Inheritance Chapter 9.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Abstract Classes, Abstract Methods, Override Methods
Functions + Overloading + Scope
Java Memory Management
Java Memory Management
CompSci 230 Software Construction
Object Oriented Programming (OOP) LAB # 5
Implementing Non-Static Features
Classes Lecture 7 from Chapter /1/11.
class PrintOnetoTen { public static void main(String args[]) {
Methods/Functions.
Abstract Data Types Abstraction is to distill a system to its most fundamental parts. Applying the abstraction paradigm to the design of data structures.
Corresponds with Chapter 5
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Introduction to Methods and Interfaces
Presentation transcript:

© 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 dimensions length width

© Vinny Cahill 3 Identifying the classes The first step is to identify what classes will be required l What kinds of entity does our program deal with? l Put another way, what types of object will the program use? l Or, what classes will we need? We will need a class whose instances represent rectangles

© Vinny Cahill 4 Defining a class in Java l The syntax of a Java class declaration is as follows: public class { } attributes are usually called instance variables in Java

© Vinny Cahill 5 Class Rectangle 1 l The first step is to choose a name for the class: public class Rectangle { }

© Vinny Cahill 6 Class Rectangle 2 We want two instance variables called length and width l Both will be integer numbers an integer is a positive or negative whole number public class Rectangle { private int length; private int width; } here we define two instance variables of type int we give the name of the type and then the name of the instance variable

© Vinny Cahill 7 Defining an instance variable in Java l The syntax of a Java instance variable declaration is as follows: private ;

© Vinny Cahill 8 Defining a method in Java l Typically, a method performs a calculation and returns a result l We need to describe both: The type of result returned The calculation to be performed l So, a method definition has two parts: A heading that describes how to use the method A body that describes how the method works

© Vinny Cahill 9 Defining a method’s heading 1/2 Consider some of the methods in the Terminal class number = terminal.readInt(“Enter number:”); terminal.println(“Hello World”); l The corresponding headings are public int readInt(String prompt) public void println(String message)

© Vinny Cahill 10 Defining a method’s heading 2/2 l The syntax of a Java method heading is as follows: l The syntax of each Java parameter definition is: The result type may be void if no result is produced public ( )

© Vinny Cahill 11 Defining a method’s body The body of the method is just a list of variable declarations and statements just as in main l The syntax of a Java method declaration is as follows: public ( ) { ;... ; }

© Vinny Cahill 12 Class Rectangle 3 public int calculateArea() { return this.length * this.width; } public int calculatePerimeter( ) { return (2 * this.length) + (2 * this.width); } both methods return an int as their result calculating the result involves multiplying the length by the width We want two methods called calculateArea and calculatePerimeter

© Vinny Cahill 13 Class Rectangle 4 class Rectangle { private int length; private int width; public int calculateArea() { return this.length * this.width; } public int calculatePerimeter() { return (2 * this.length) + (2 * this.width); }

© Vinny Cahill 14 Defining a constructor is Java We also need a method to initialise the length and width instance variables of each new object that we create l Methods that initialise new objects are called constructors l The syntax of a Java constructor declaration is as follows: public ( ) { ;... ; } Look, no result type! The name of the method is the same as the name of the class

© Vinny Cahill 15 In our constructor we need to initialise length and width l Our constructor needs to have two parameters giving these values These two parameters are also of type int Class Rectangle 5 public Rectangle(int l, int w) { this.length = l; this.width = w; } storing a value into a variable uses assignment “ this.width = w ” means store (assign) the value of w in(to) variable width

© Vinny Cahill 16 Class Rectangle 6 /* A class whose instances represent rectangles */ public class Rectangle { private int length; // used to store the length of the rectangle private int width; // used to store the width of the rectangle /* declare a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) { this.length = l; // store the value of l into length this.width = w; // store the value of w into width } /* declare a method to calculate the area of a rectangle */ public int calculateArea() { return this.length * this.width; } /* declare a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() { return (2 * this.length) + (2 * this.width); }

© Vinny Cahill 17 Functions and Parameters l Given f(x) = x 2 + 4x + 13 l What is f(10)? l What is f(20)? l What is f(30)? x is the “formal parameter” of function f 10 is an “actual parameter” of function f

© Vinny Cahill 18 Class Rectangle /* A class whose instances represent rectangles */ public class Rectangle { private int length; // used to store the length of the rectangle private int width; // used to store the width of the rectangle /* declare a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) { this.length = l; // store the value of l into length this.width = w; // store the value of w into width } /* declare a method to calculate the area of a rectangle */ public int calculateArea() { return this.length * this.width; } /* declare a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() { return (2 * this.length) + (2 * this.width); } “public” parts of Rectangle

© Vinny Cahill 19 /* A class whose instances represent rectangles */ public class Rectangle { /* a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) /* a method to calculate the area of a rectangle */ public int calculateArea() /* a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() } Class Rectangle We refer to the collection of method headings as the interface of the class

© Vinny Cahill 20 Class Rectangle “public” parts of Rectangle public class Rectangle { private int length; // used to store the length of the rectangle private int width; // used to store the width of the rectangle private int area; // used to store the area of the rectangle private int perimeter; // used to store the perimeter of the rectangle /* declare a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) { this.length = l; // store the value of l into length this.width = w; // store the value of w into width this.area = this.length * this.width; // calculate the area this.perimeter = (2 * this.length) + (2 * this.width); // calculate the perimeter } /* declare a method to calculate the area of a rectangle */ public int calculateArea() { return this.area; } /* declare a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() { return this.perimeter; }

© Vinny Cahill 21 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); }

© Vinny Cahill 22 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } “Square” On the screen

© Vinny Cahill 23 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 10 “Square” 10 public Square(int l) { return this.length = l; }

© Vinny Cahill 24 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } public Square(int l) { return this.length = l; } 20

© Vinny Cahill 25 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 1020

© Vinny Cahill Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } calculateArea! public int calculateArea() { return this.length * this.length; } 100

© Vinny Cahill 27 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 1020

© Vinny Cahill Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } calculateArea! public int calculateArea() { return this.length * this.length; } 400

© Vinny Cahill 29 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 1020

© Vinny Cahill 30 Circles Want to write a Java class describing objects that represent circles radius Might want to ask a circle for: its area the length of its circumference