Phil Campbell London South Bank University Java 1 First Steps.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
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.
1 Classes and Objects in Java Basics of Classes in Java.
1 Inheritance Classes and Subclasses Or Extending a Class.
Classes and Objects in Java
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
1 Lecture 16/3/11: Contents Example of an array of user-defined objects: Car and Carr Constructor Providing functionalities for a user- defined object.
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Programming Methodology (1). Implementing Methods main.
Phil Campbell London South Bank University Using Java (2)
Computer Programming Lab(7).
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
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.
CSCI 160 Midterm Review Rasanjalee DM.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Using Classes to Store Data Computer Science 2 Gerb.
C++ Classes & Data Abstraction
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
April 20023CSG11 Electronic Commerce Java (1) John Wordsworth Department of Computer Science The University of Reading Room 129,
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Created by Ron Beglieter (based on the Java Tutorial) 1 What is Java? Java technology is both a programming language and a platform; Programming Language.
Starting Software Development A Beginning. You Learn Software Development By Doing Software development.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Electronic Commerce Java (1)
Chapter 12 – Object-Oriented Design
Object-Oriented programming for Beginners LEAPS Computing 2015
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Introduction to Programming G50PRO University of Nottingham Unit 9 : Classes and objects Paul Tennent
COMPUTER 2430 Object Oriented Programming and Data Structures I
Implementing Classes Yonglei Tao.
CompSci 230 Software Construction
CSC 113 Tutorial QUIZ I.
Implementing Classes Chapter 3.
Assignment 7 User Defined Classes Part 2
Example with Static Variable
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Basics of OOP A class is the blueprint of an object.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Object-Oriented Programming and class Design
Presentation transcript:

Phil Campbell London South Bank University Java 1 First Steps

Phil Campbell London South Bank University Java Is an Object Oriented Language which supports the creation of Object models discussed in the UML section. The next few slides show how to define a class in Java. We then discover how to use that class in a simple Java program

Phil Campbell London South Bank University Java is an Object Oriented Language Java is a compiled language Java is portable (Multi-platform) but.... You are not here to learn Java.... You are here to learn Software Development

Phil Campbell London South Bank University SimpleCounter The class called SimpleCounter holds a single value in an attribute called value When a new instance of the class is created, a value has to be supplied to give the counter an initial value The only methods available to the user of the class are: click() - which adds one to the value in the object and getValue() - which returns the number in the value attribute

Phil Campbell London South Bank University A Class

Phil Campbell London South Bank University // filename : SimpleCounter.class // // Demonstration of a **very** simple and not very // useful class // Phil Campbell ver v0.1 September 03 public class SimpleCounter extends Object{

Phil Campbell London South Bank University public class SimpleCounter extends Object{ private int value;

Phil Campbell London South Bank University // the constructor public SimpleCounter (int startValue){ value = startValue; } // End SimpleCounter()

Phil Campbell London South Bank University The constructor...places a newly created instance into a well defined initial state.

Phil Campbell London South Bank University // a simple method public void click( ){ value++; } // End click()

Phil Campbell London South Bank University public int getValue(){ return value; } // End getValue() return type

Phil Campbell London South Bank University public class SimpleCounter extends Object{ private int value; // the constructor public SimpleCounter (int startValue){ value = startValue; } // End SimpleCounter() // a simple action public void click (){ value++; } // End click() // a 'getter' public int getValue(){ return value; } // End getValue() } // End class SimpleCounter

Phil Campbell London South Bank University Demonstrating SimpleCounter count = new SimpleCounter( 5); count 5 public class SimpleCounterDemonstration extends Object{ public static void main( String[] args){ SimpleCounter count = null; reference variable object : an instance of the class SimpleCounter count: SimpleCounter value = 5

Phil Campbell London South Bank University An object is an instance of a class that has state and behaviour.

Phil Campbell London South Bank University Demonstrating SimpleCounter count 5 public class SimpleCounterDemonstration extends Object{ public static void main( String[] args){ SimpleCounter count = null; count = new SimpleCounter( 5); count.click( ); public void click ( ){ value++; } 6

Phil Campbell London South Bank University Demonstrating SimpleCounter count 5 public static void main( String[] args){ SimpleCounter count = null; count = new SimpleCounter( 5); count.click( ); public void click ( ){ value++; } 6 System.out.println( count.getValue( )) public int getValue(){ return value; } 6

Phil Campbell London South Bank University public class SimpleCounterDemonstration extends Object{ public static void main (String[] args){ SimpleCounter count = null; count = new SimpleCounter(5); System.out.print( "First value : "); System.out.println( count.getValue()); count.click(); System.out.print( "Second value : "); System.out.println( count.getValue()); count.click(); System.out.print( "Third value : "); System.out.println( count.getValue()); } // End of main } // End of SimpleCounterDemonstration count 5 output First value : 5 Second value: 6 Third value : 8 678

Phil Campbell London South Bank University The state of an object is the aggregate value of its attributes. Each time the value of an attribute is changed in an object, the object changes state.

Phil Campbell London South Bank University public class SimpleCounterDemonstration extends Object{ public static void main (String[] args){ SimpleCounter count = null; count = new SimpleCounter(5); SimpleCounter count2 = null; count2 = new SimpleCounter(10); count.click(); System.out.print( "First value : "); System.out.println( count.getValue()); count2.click(); count.click(); count2.click(); System.out.print( "Second value : "); System.out.println( count2.getValue()); } // End of main } // End of SimpleCounterDemonstration count 5 output First value : 6 Second value: count

Phil Campbell London South Bank University public class SimpleCounterDemonstration extends Object{ public static void main (String[] args){ SimpleCounter count = null; count = new SimpleCounter(5); SimpleCounter count2 = null; count = new SimpleCounter(10); count.click(); System.out.print( "First value : "); System.out.println( count.getValue()); count2.click(); count.click(); count2.click(); System.out.print( "Second value : "); System.out.println( count2.getValue()); } // End of main } // End of SimpleCounterDemonstration count 5 output First value : 11 count2 10 <----ERROR 11

Phil Campbell London South Bank University Words used so far class reference variable method state constructor inquiry method object instance Describes the contents and behaviour of a set of instances (objects) that share the same attributes, methods and relationships class

Phil Campbell London South Bank University Words used so far class reference variable method state constructor inquiry method object instance Used as the name of an instance. Can only refer to instances of a certain kind. Is not the instance itself. reference variable

Phil Campbell London South Bank University Words used so far class reference variable method state constructor inquiry method object instance Describes the behaviour of an object. Provides a way to interact with the data held in the object. method

Phil Campbell London South Bank University Words used so far class reference variable method state constructor inquiry method object instance The state of an object is the aggregate value of all of its attributes state set of all values in

Phil Campbell London South Bank University Words used so far class reference variable method state constructor inquiry method object instance Places a newly created instance into a well defined initial state constructor

Phil Campbell London South Bank University Words used so far class reference variable method state constructor query method object instance A method that returns a value indicating some aspect of the state of an instance inquiry method

Phil Campbell London South Bank University Words used so far class reference variable method state constructor inquiry method object instance An object is an instance of a class that has state, behaviour and identity object

Phil Campbell London South Bank University Words used so far class reference variable method state constructor inquiry method object instance Instantiation is the process of creating a physical example of a class... an object. instance

Phil Campbell London South Bank University The IDE Integrated Development Environment Editor pane File Chooser Interaction pane

Phil Campbell London South Bank University The IDE

Phil Campbell London South Bank University

Exercise 1 Colour hue : String first : Colour hue : "red" firstsecondthird "red" "blue" "green" getHue():String setHue(String theHue) Write the class Colour in Java

Phil Campbell London South Bank University public class Colour extends Object{ } // End class Colour Colour hue : String getHue():String setHue(String theHue) Object public String getHue(){ return hue; } // end getHue() private String hue; public Colour (String theHue){ hue = theHue; } // End constructor() public void setHue( String theHue){ hue = theHue; } // End setHue()

Phil Campbell London South Bank University public class Colour extends Object{ private String hue; public Colour (String theHue){ hue = theHue; } // End constructor() public String getHue(){ return hue; } // end getHue() public void setHue( String theHue){ hue = theHue; } // End setHue() } // End class Colour Colour hue : String getHue():String setHue(String theHue) Object Write a demonstration program for the Colour Class AttributeConstructorModifier Method Inquiry Method

Phil Campbell London South Bank University } // end main() } // end class ColourDemo public class ColourDemo extends Object{ public static void main ( String[] argv){ first = new Colour("Red"); System.out.println("First has colour " + first.getHue()); first.setHue( "Blue" ); System.out.println(" First now has " + first.getHue()); System.out.println("Changing the colour "); Colour first = null; first "Red" "Blue"

Phil Campbell London South Bank University Exercise 2 Rectangle height : double width : double Rectangle (h : double, w : double) area(): double perimeter(): double getHeight(): double getWidth(): double setHeight( aHeight: double) setWidth( aWidth: double) Write the Rectangle class in Java

Phil Campbell London South Bank University public class Rectangle extends Object{ private double height; private double width; // the constructor public Rectangle (double height, double width){ super(); this.height = height; this.width = width; } // End Rectangle() public double getHeight (){ return height; } // End getHeight() public double getWidth (){ return width; } // End getWidth() public void setHeight ( double arg){ height = arg; } // End setHeight()

Phil Campbell London South Bank University public void setWidth ( double arg){ width = arg; } // End getWidth() public double area(){ return height * width; } // End area() public double perimeter(){ return 2 * ( height + width); } public String toString(){ return "(" + height +"," + width + ") area= " + area() + " perimeter= " + perimeter(); } // End toString() } // End class Rectangle