Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2.

Slides:



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

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.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 6: A First Look at Classes
UML Basics & Access Modifier
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Sadegh Aliakbary Sharif University of Technology Fall 2011.
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Starting Out with Java: From Control Structures through Objects.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Defining a ClasstMyn1 Defining a Class A class is a template that defines the form of an object. It specifies both the data and the code that will operate.
Salman Marvasti Sharif University of Technology Winter 2015.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Some odds and ends about Classes and Objects. 6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Page 1 – Autumn 2009Steffen Vissing Andersen SDJ I1, Autumn 2009 Agenda: Java API Documentation Code Documenting (in javadoc format) Debugging.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Chapter 3 Classes & Objects
A first Look at Classes.
Introduction to Classes
Chapter 6: A First Look at Classes
CS 200 Creating Classes Jim Williams, PhD.
What is an object? An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the.
Introduction to Classes
Chapter 4: Writing classes
Chapter 10 Thinking in Objects
Unit-2 Objects and Classes
Today’s topics UML Diagramming review of terms
Lecture 5- Classes, Objects and Methods
Object Oriented Programming in java
CS 200 Creating Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Lecture 8 Object Oriented Programming (OOP)
Chapter 6: A First Look at classes
Presentation transcript:

Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2 Classes and Objects

Page 4.2 – Autumn 2010Steffen Vissing Andersen Checkpoints from Chapter 2 Checkpoints , p p , p p p p. 71 Review Questions page 105 Question 1-12, 15-21

Page 4.3 – Autumn 2010Steffen Vissing Andersen Strings String is an Object Data Type (a Class) The Class String is defined in package java.lang Strings can be concatenated just by adding them public class TestStrings { public static void main(String[] args) { String name1 = "Bob" String name2 = "Pete"; System.out.println("Hello " + name1 + " and " + name2); }

Page 4.4 – Autumn 2010Steffen Vissing Andersen import java.util.Scanner; public class StringTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter name of city: "); String city = input.nextLine(); System.out.println(“Number of letters: " + city.length()); System.out.println("Uppercase: " + city.toUpperCase()); System.out.println("Lowercase: " + city.toLowerCase()); System.out.println("First character: " + city.charAt(0)); } Using Strings – an example

Page 4.5 – Autumn 2010Steffen Vissing Andersen UML Class Diagram

Page 4.6 – Autumn 2010Steffen Vissing Andersen UML Class Diagram

Page 4.7 – Autumn 2010Steffen Vissing Andersen public class Message { private String message; public Message(String message) { this.message = message; } public String getMessage() { return message; } Message class

Page 4.8 – Autumn 2010Steffen Vissing Andersen Message class – UML Class Diagram

Page 4.9 – Autumn 2010Steffen Vissing Andersen public class MessageTest { public static void main(String[] args) { Message oneMessage = new Message("Hello world!"); System.out.println(oneMessage.getMessage()); } Using the Message class

Page 4.10 – Autumn 2010Steffen Vissing Andersen Object Oriented Concepts Objects Attributes Behaviour Object Oriented Analysis and Design - OOAD What? – Analysis of Requirements How? – Design Object Oriented Programming - OOP Implementation (Writing Code) Testing

Page 4.11 – Autumn 2010Steffen Vissing Andersen Characteristics of Objects Objects in Real Life Can perform certain Actions Actions can have effect on other Objects in the World Have a certain State at a certain Time Objects in an OO-Programming Language Can perform Actions (has Operations/Methods) Can perform Actions on other Objects (Collaboration) Have a certain State at a certain Time (Values stored in Attributes/Fields)

Page 4.12 – Autumn 2010Steffen Vissing Andersen Class The general Description of all Objects of a given Type (Example: “Car”, “Person”, “House”, “Ball”, …) The Template for all Objects of the same Class Object Represent ‘things’ from the real World (Example: “the red Car in the Car Park”, “the Person to your right” Classes and Objects –color : String Car – name : String Person color : String = "red" myCar : Car name : String = "Peter" peter : Person +getColor() : String +getName() : String

Page 4.13 – Autumn 2010Steffen Vissing Andersen Some Observations of Objects Encapsulation The Data in an Object are hidden for the outside world and can only be accessed inside the Object The Data in an Object is accessed through the Objects Methods/Operations Abstraction The Details about functionality are hidden in the Object (Class) itself

Page 4.14 – Autumn 2010Steffen Vissing Andersen UML versus Java UML Java public class Person { // fields private String name; private String address; private int ssn; private int age; // methods void eat() { } void walk(int speed) { } AttributesFields OperationsMethods Parameters Arguments Data Type +walk(speed : int) +eat() -name : String -address : String -ssn : int -age : int Person

Page 4.15 – Autumn 2010Steffen Vissing Andersen Class Rectangle (1/3) public class Rectangle { private double length; private double width; public Rectangle(double len, double w) { length = len; width = w; } public void setLength(double len) { length = len; } -Same Name as Class Name -No return type Constructor

Page 4.16 – Autumn 2010Steffen Vissing Andersen Class Rectangle (2/3) public void setWidth(double w) { width = w; } public void set(double len, double w) { length = len; width = w; } public double getLength() { return length; }

Page 4.17 – Autumn 2010Steffen Vissing Andersen Class Rectangle (3/3) public double getWidth() { return width; } public double getArea() { return length * width; } } // end of class Rectangle

Page 4.18 – Autumn 2010Steffen Vissing Andersen Rectangle – UML Class diagram

Page 4.19 – Autumn 2010Steffen Vissing Andersen RectangleDemo public class RectangleDemo { public static void main(String[] args) { Rectangle box = new Rectangle(10.0, 20.0); System.out.println("The length is " + box.getLength()); System.out.println("The width is " + box.getWidth()); System.out.println("The area is " + box.getArea()); }

Page 4.20 – Autumn 2010Steffen Vissing Andersen RectangleDemo Class Diagram Object Diagram Rectangle box = new Rectangle(10.0, 20.0);

Page 4.21 – Autumn 2010Steffen Vissing Andersen Constructors – Creating Objects Default constructor – a no-argument constructor Ex.: Rectangle rect1 = new Rectangle(); Constructors with arguments Ex.: Rectangle rect2 = new Rectangle(10.0); Ex.: Rectangle rect3 = new Rectangle(10.0, 20.0); Implementing Constructors If no Constructors are explicitly defined by the programmer (you) then a default constructor is provided by the compiler