Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.

Slides:



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

Lab#1 (14/3/1431h) Introduction To java programming cs425
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
April 20023CSG11 Electronic Commerce Java (1) John Wordsworth Department of Computer Science The University of Reading Room 129,
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Java Programming Review (Part I) Enterprise Systems Programming.
Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Introduction to Java Programming CS 21a: Introduction to Computing I First Semester,
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Java Language and SW Dev’t
Introduction to Object-Oriented Programming
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Programming Languages and Paradigms Object-Oriented Programming.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
1 Programming Fundamentals AITI-GP. 2 Introduction to Programming.
IDEs Department of Information Systems and Computer Science Ateneo de Manila University.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Class Relationships and Object Interaction. 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Programming Languages and Paradigms Programming C++ Classes.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Java Translation Program Structure
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Class Relationships and Object Interaction CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
Introduction to Programming. The Programming Process Create/Edit Program Compile Program Execute Program Compile Errors?Run-Time Errors? Source Program.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Chapter 3 Implementing Classes
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Lecture 3 John Woodward.
Introduction to.
Chapter Three - Implementing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Applets.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Debugging Exercise 00 Try compiling the code samples.
Computer Programming-1 CSC 111
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Outline Morning Session Introduction to Java, OOP, and the BlueJ environment Creating classes for the Robots project Afternoon Session Graphics and Graphical User Interfaces in Java Demo: BombsAway application Creating your own game using the a game engine Demo: developing mobile apps within the Android platform Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 2

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 3 The Java Programming Language Java: an object-oriented programming language that is simple safe platform independent designed for the internet Many universities use Java as the introductory programming language for beginning programmers Ateneo adopted Java for CS 21a in 1997

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 4 Two types of Java programs: Applications general-purpose programs standalone executed through the operating system Applets programs meant for the WWW embedded in a Web page normally executed through a browser

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 5 Simple Java Application File: Hello.java // Hello World application public class Hello { public static void main( String args[] ) { System.out.println( “Hello world” ); }

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 6 The Programming Process Create/Edit Program Compile Program Execute Program Compile Errors?Run-Time Errors? Source Program Object Program

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 7 Creation, Compilation, and Execution Create Java program C:\> edit Hello.java Hello.java file is created Compile using javac (compiler) C:\> javac Hello.java Hello.class file is produced Execute using java (interpreter) C:\>java Hello requires a Hello.class file

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 8 Simple Java Applet File: HelloAgain.java import javax.swing.*; import java.awt.*; public class HelloAgain extends JApplet { public void paint( Graphics g ) { g.drawString( “Hello”, 50, 50 ); }

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 9 Executing Applets After compiling the java program: Embed an “applet tag” in an.html document that references the.class file Open the.html document using a browser or the appletviewer

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 10 Sample.html Document File: HA.html My Sample Applet

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 11 Java Program Structure Java Program (optional) import declarations class declaration Class class name should match its file name may extend an existing class (such as JApplet) contains method/function declarations

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 12 Object-Oriented Programming The traditional definition of a program is: a sequence of instructions to be executed on a computer In the Object-Oriented Programming (OOP) paradigm, a program that executes is a collection of interacting objects In this paradigm, the programs we specify what are in these objects and how these objects behave

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 13 So … What is an Object? A “thing” that has type, identity, state, and behavior type: belongs to a “class” of similar objects identity: is a distinct “instance” of a class of objects state / attributes: has a set of properties (aka fields) each field can have different values behavior: has “methods” (things that the object knows how to do) we say we “call” a method on the object

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 14 Examples of Objects state/attributes on (true or false) behavior switch on switch off check if on state/attributes # of liters of gas in tank total # of km run so far efficiency (km/liter) behavior drive load gas change efficiency check gas check odometer reading LightBulb Car BankAccount state/attributes balance behavior deposit withdraw check balance Note each object is an “instance” of that “type” of object each instance has its own values for its attributes e.g., different accounts can have different balances

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 15 BankAccount example (A Preview) BankAccount state/attributes balance behavior get balance deposit withdraw BankAccount double balance double getBalance() void deposit( double amount ) … and more public class BankAccount { private double balance = 0; public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } BankAccount.java

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 16 Class Definition in Java BankAccount type (or class) state/attributes (fields) behavior (methods) may have input parameters in parenthesis may have output (or “return”) type has “body” with code public class BankAccount { private double balance = 0; public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } BankAccount.java “double” means a floating Point number like

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 17 A Class with a Constructor public class BankAccount { private double balance; public BankAccount() { balance = 0; } public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } BankAccount.java Constructor: special method that handles initialization For now, view constructors as an alternative to initializing fields as they are declared Later: more advanced uses for constructors

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 18 A Class and Its Instances A single class can have multiple instances Each instance is a separate object Each instance can have different values for its fields The definition of methods is the same for all instances of the same type Thus, there is only one class definition Written as the.java file for that class

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 19 Lab Exercise: Try it in BlueJ Open the project (ObjectExamples) that contains a BankAccount class (BankAccount.java) Create BankAccount objects (instances of the class) Right-click on the class Carry out operations on the BankAccount objects (invoke the deposit and getBalance methods) Right-click on the instances

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 20 Lab Exercise, continued Instantiate and use a BankAccount object within in a Java application How? Create a UseObjects class (UseObjects.java) “New Class” on left pane of BlueJ environment Inside the UseObjects class type the following code…

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 21 Lab Exercise, continued public static void main( String args[] ) { BankAccount b = new BankAccount(); b.deposit( ); b.withdraw( ); System.out.println( b.getBalance() ); b.deposit( ); System.out.println( b.getBalance() ); } Compile and execute UseObjects.java The values and should be printed out

Lab Exercise, continued Close the current BlueJ project Create a Robots project Create a Robot class moving along a two dimensional maze Attributes: x and y coordinates (int type) Methods: moveLeft, moveRight, moveUp, moveDown Test the class by creating instances within BlueJ Test the class by creating another class that uses the Robot class—call it RobotTest Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1: Intro Java Slide 22

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved L3: Intro to OOP Slide 23 Summary In Java, we write programs for objects These programs are called classes A class consists of fields and methods to specify the state and behavior for its objects Once a class has been defined, objects of that class can be created (instantiated) Methods are invoked on an object, and may cause the state of the object to change