Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Java Planning our Programs Flowcharts Arithmetic Operators.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
 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.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
COMP More About Classes Yi Hong May 22, 2015.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Object Oriented Software Development
Classes Java is an Object-Orented Programming language, and one of the main advantages of OOP is code reuse. Code reuse: write code only once, put it in.
Hello AP Computer Science!. What are some of the things that you have used computers for?
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
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 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
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.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
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 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Algorithms and Computer Programming. Algorithms algorithm is an ordered sequence of instructions for solving a problem. Look at it as a building blocks.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Classes - Intermediate
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.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Defining Classes. Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class.
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.
Functions + Overloading + Scope
Class Inheritance Part I
Concepts of Object Oriented Programming
Java Applet Programming Barry Sosinsky Valda Hilley
Chapter 5 Classes.
Functions CIS 40 – Introduction to Programming in Python
Method Mark and Lyubo.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Static Methods 14-Nov-18.
Starting Out with Java: From Control Structures through Objects
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Functions Christopher Harrison | Content Developer
Workshop for Programming And Systems Management Teachers
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Week 4 Lecture-2 Chapter 6 (Methods).
Chapter 10: Void Functions
Methods/Functions.
References Revisted (Ch 5)
Object-Oriented Programming and class Design
Introduction to Methods and Interfaces
Presentation transcript:

Java Classes Methods Objects

Classes

Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class we would be declaring a class Every program must have a main class in order to work, it is the class that the JVM uses to execute the program

Why are classes used? Classes are used to started off a program Classes in Java are also used to create objects (we will talk about objects later on) A class specifies 1. The data it contains 2. The code that uses the data

Classes as Building Blocks A classes are the building blocks to programs For example and architect draws a plan of the house before it is build, the class is the plan

Advantage of Classes Classes provide reusability in programs This means that a class can be used over and over again For example we have a ready made class called the ‘Keyboard.class’, we use this over and over again when we want the program to read an input from the keyboad

Activity Create a program and name the class myAddingClass The code within the class should be able to add two numbers The output should be the result of the addiion

Methods

What is a method? Programs are used to solve complex problems and are MUCH longer than the ones we create at school These programs are split up into a number of sections to make it easier to code and to read These sections are called methods

… A method holds its own specific task which is then joined to other methods to form a complete process A method contains the actions that the program will actually perform So first we create a class and within the class we have a number of methods

Example Lets say we have a program to hold student records The program could have the following methods 1. A method to calculate if a mark is a pass or fail 2. A method to calculate the average marks 3. A method to draw up a graph of the grades

Why do we use Methods? Splitting a problem into different methods makes it much easier to program Methods avoids having to have double code as if we need to use a method twice you would just need to call it

Our First Program As we can see we have one class called FirstProgram We have one methods which is the main method class FirstProgrsam{ public static void main (String args[]) { System.out.println(“hello World”); }

How do we Identify Methods? We know that a block of code is a method as we would see the following syntax The parameter list would be what the method will be using or a statement of when the method should start method name (parameter list) { // body of the method }

Calling Methods In Java when we say we are calling a method we mean that we are asking the program to perform the tasks of a method This is done to save duplicate codes in our program This makes it easier to write and understand codes

//using a methods //calling methods Class callingMethods{ //method findArea static int findArea (int lnegth, int width) { return length * width; } public static void main (String args[]){ //calling method findArea System.out,println(“Area = “ +findArea(5,3); System.out,println(“Area = “ +findArea(5,4); System.out,println(“Area = “ +findArea(5,5); }

Static Methods The method in the previous program was a static method They do not make use of instance variables Static methods take the parameters and compute/calculate something

Public Methods A public method is a method that could be used by other classes In other words another class within the program would be able to use the public method by simply calling them

Void Methods A void method is basically a method that contains the keyword ‘void’ Void methods perform an actions but do not output any sort of result

Instance Methods This is the default type of method Instance methods use instance variables (a set variable) These methods are associated with objects of the program

Objects

What is an Object? In life we have objects all around us such as tables, chairs, desks ect… Every objects has its attributes such as a table has four legs and a straight table top. All tables have this in common – common attributes Objects also have a behaviour for examples dogs bark, eat and breath. All dogs have these behaviours in common

Object Oriented Programming Java is an object oriented programming language, programs in java also take into account different objects with attributes and behaviours For example if we want to draw a triangle on Java we would give it the attributes of the objects – size and colour We could also give it a behaviour and make it move around the screen

Object characteristics Objects have two main characteristics; 1. Attributes = how the object looks and what it is - set as an instance variable in Java 2. Behaviour = what action it would do – set as a method in Java

Creating a Program We will be using an architects plan to find out the following certain attributes of different rooms So a common object we have is a room, so we would need to create an object room using a class.

//create a type room by listing the attributes of a room class Room{ double width; double height; double length; } class HomeDemo1{ public static void main (String args[]){ //creating an object of type room //object is a room with the name bedroom1 Room bedroom1 = new Room(); double volume; //give bedroom1 its attributes bedroom1.width = 4; bedroom1.height = 3; bedroom1.length = 5; //find the volume of the room volume = bedroom1.width * bedroom1.height *bedroom1.length; System.out.println("Volume of Bedroom1 = " + volume); }

Outputs What is the output of the program? Volume of Bedroom1 = 60.0 Which is the class that is creating the data type for the room? Class Room { double width; double height; double length }

Creating two Objects In the next program we will be creating two objects The two objects in this program will be; 1. Bedroom1 (width 5, height 3, length 5) 2. A Kitchen (width 3, height 3, length 4) Create a NEW program to output the volume of both bedroom1 and the kitchen

We need a Method As we can see in out second program the following code is repeated twice; When we have repeated code we realise that we need to create a method, this would eliminate the repeated code volume = bedroom1.width * bedroom1.height * bedroom1.length;

Our new Method Our new method would need to work out the volume We will name this method ‘getVolume’ to understand what the method will be doing In the class Room write the following method //method to calculate the volume double getVolume(){ volume = width * height * length; return volume; } STEP ONE

Eliminating Code Remove the following lines of code from your program; STEP TWO volume = bedroom1.width * bedroom1.height * bedroom1.length; volume = kitchen.width * kitchen.height * kitchen.length; Double volume; 1 2 3

Replacing Code Replace the following code; With; STEP THREE System.out.println("Volume of Bedroom1 = " + volume); System.out.println("Volume of kitchen = " + volume); System.out.println("Volume of Bedroom1 = " + bedroom1.getVolume()); System.out.println("Volume of Kitchen = " + kitchen.getVolume());

Add a Living Room Add a living room to your program with the following attributes; 1. Width = 5 2. Height = 3 3. Length = 6

Area Create a method called getArea This method should return the area So now your program should 1. Hold three objects 2. Two methods 3. Output the Volume of all objects 4. Output the Area of all objects Area = ((width * height) * 2) + ((length * height * 2) + (width * length));