Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

Using Classes Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Not Glasses, Classes!!!
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
© 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.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Chapter 2 types, variables, methods Pages Horstmann.
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.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Two: Using Objects.
Chapter Two: Using Objects. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters.
Chapter 2 – Using Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
POS 406 Java Technology And Beginning Java Code
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
© A+ Computer Science - Chicken yeller = new Chicken();
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Chapter 3 Implementing Classes
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
Chapter Goals To learn about variables
Chapter 2 Not Glasses, Classes!!! Using Classes
Chapter 3: Using Methods, Classes, and Objects
Chapter 2 Using Objects.
Corresponds with Chapter 7
AN INTRODUCTION TO OBJECTS AND CLASSES
Each object belongs to a class
Parameter: an input to a method
Corresponds with Chapter 5
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Today The anatomy of a class Attributes Methods  Method calls, return values Constructors Importing existing classes RectangleDemo API documentation

Goals for today To (better) understand the concepts of classes and objects To be able to create objects To be able to call methods To be able to browse the API documentation

Chapter 2 Classes and Objects

Objects and Classes Object: entity that you can manipulate in your programs (by calling methods) Each object belongs to a class. For example, System.out belongs to the class PrintStream Figure 3: Representation of the System_out object

The Anatomy of a Class Package and import statements Class signature Attributes Methods Note! Main is a method package... import... public class ClassName { } public String getArtist(String n){... } public String artist; private int numberOfHits; private void computeNumHits(){... }

The Anatomy of a Class Your code goes here! package... import... public class ClassName { } public String getArtist(String n){... } public String artist; private int numberOfHits; private void computeNumHits(){... }

Attributes Attributes are variables (data) that are valid for the whole object Typical examples: names, status information Attributes can be public (known outside) or private (known only within the object)‏ Attributes can be objects themselves Attributes define the state of any given object public String artist; private int numberOfHits; private int numberOfRecords; private Album bestAlbum;

Methods Method: Sequence of instructions that accesses the data of an object You manipulate objects by calling its methods Class: Set of objects with the same behavior The class determines legal methods Continued… String greeting = "Hello"; greeting.println() // Error greeting.length() // OK

Methods Method signature: specification of what goes into the method, and what comes out (i.e., what is returned from the method)‏ Method body: the actual code that does all the work Methods can have local variables public String getArtist(String name) { String defaultArtist = “Sting”; if(name.equals(“Prince”)) { return “The artist formerly known as “ + name; } return defaultArtist; }

Method signature public String getArtist(String name) { String defaultArtist = “Sting”; if(name.equals(“Prince”)) { return “The artist formerly known as “ + name; } return defaultArtist; } Method name Access specifier Return type Argument list

Invoking Methods public static void main(String[] args) { System.out.println(“Who released Purple Rain?”); System.out.println(getArtist(“Prince”)); System.out.println(“Who released Nothing Like the Sun?”); System.out.println(getArtist(“not Prince”)); } public String getArtist(String name) { String defaultArtist = “Sting”; if(name.equals(“Prince”)) { return “The artist formerly known as “ + name; } return defaultArtist; } returnValue = methodName(arguments);

Return Values Figure 6: Invoking the length Method on a String Object

Passing Return Values You can also use the return value as a parameter of another method: Not all methods return values. Example: Continued… System.out.println(greeting.length()); println()‏

Passing Return Values Figure 7: Passing the Result of a Method Call to Another Method

A More Complex Call replace method carries out a search-and- replace operation As Figure 8 shows, this method call has  one implicit parameter: the string "Mississippi"  two explicit parameters: the strings "issipp" and "our"  a return value: the string "Missouri" Continued… river.replace("issipp", "our") // constructs a new string ("Missouri")‏

A More Complex Call Figure 8: Calling the replace Method

Method Definitions If method returns no value, the return type is declared as void A method name is overloaded if a class has more than one method with the same name (but different parameter types) public void println(String output) // in class PrintStream public void println(String output) public void println(int output)‏

Rectangular Shapes and Rectangle Objects Objects of type Rectangle describe rectangular shapes Figure 9: Rectangular Shapes

Rectangular Shapes and Rectangle Objects A Rectangle object is not, in fact, a rectangular shape–it is an object that contains a set of numbers (attributes) that describe each individual rectangle, along with a number of operations (methods) that can be applied to it Figure 10: Rectangular Objects

Constructing Objects Detail:  The new operator makes a Rectangle object  It uses the parameters (in this case, 5, 10, 20, and 30 ) to initialize the data of the object  It returns the object Usually the output of the new operator is stored in a variable Rectangle box = new Rectangle(5, 10, 20, 30); new Rectangle(5, 10, 20, 30)‏

Constructing Objects The process of creating a new object is called construction The four values 5, 10, 20, and 30 are called the construction parameters Some classes let you construct objects in multiple ways new Rectangle() // constructs a rectangle with its top-left corner // at the origin (0, 0), width 0, and height 0

Syntax 2.3: Object Construction new ClassName(parameters)‏ Example: new Rectangle(5, 10, 20, 30) new Rectangle()‏ Purpose: To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object

Accessor and Mutator Methods Figure 11: Using the translate Method to Move a Rectangle

Implementing a Test Program Write a new class Supply a main method Inside the main method, construct one or more objects Call methods in the objects Display the results of the method calls

Importing Packages Don't forget to include appropriate packages:  Java classes are grouped into packages  Import library classes by specifying the package and class name:  You don't need to import classes in the java.lang package such as String and System import java.awt.Rectangle;

Syntax 2.4: Importing a Class from a Package import packageName.ClassName; Example: import java.awt.Rectangle; Purpose: To import a class from a package for use in a program.

A demo: RectangleDemo.java Start in main Construct a new RectangleDemo object, which inherits from javax.swing.JPanel Create 4 Rectangle objects Create a javax.swing.JFrame window frame and add the panel Draw the Rectangle objects in a paint method inherited from javax.swing.JPanel

Inheriting from Classes /** * RectangleDemo * Demo illustrating the creation... */ package gui; import javax.swing.JFrame;... import java.awt.Color; public class RectangleDemo extends JPanel {

From main to Constructor /** * main - the program starts here args */ public static void main(String[] args) { RectangleDemo me = new RectangleDemo(); } Create an instance of this class to get out of the static main context

Data and Constructor public class RectangleDemo extends JPanel { Rectangle rect1, rect2, rect3, rect4; /** Constructor */ public RectangleDemo() { rect1 = new Rectangle(5,10,30,40); rect2 = new Rectangle(30,30,67,10); rect3 = new Rectangle(76,12,12,12); rect4 = new Rectangle(100,100,200,200);... Use attributes to represent rectangles

paint – an inherited method public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setBackground(new Color(100, 100, 200)); g2.setColor(new Color(10,20,30)); g2.draw(rect1); g2.setColor(new Color(110,120,130)); g2.draw(rect2); g2.setColor(Color.YELLOW); g2.fill(rect3); g2.setColor(Color.BLACK); g2.fill(rect4); } Paint rectangles: java.awt.Graphics2D

The API Documentation API: Application Programming Interface Lists classes and methods in the Java library

The API Documentation of the Standard Java Library Figure 13: The API Documentation of the Standard Java Library

The API Documentation for the Rectangle Class Figure 14: The API Documentation of the Rectangle Class