The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.

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

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
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.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 1 These slides for CSE 110 Sections are based in part on the textbook-authors’ slides, which are copyright by the authors. The authors state that.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Object-oriented analysis (OOA) techniques are used to (1) study existing objects to see if they can be reused or adapted for new uses, and (2) define new.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
Chapter 3  Implementing Classes 1 Chapter 3 Implementing Classes.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Three: Implementing Classes.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Chapter 3 – Implementing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CHAPTER 1 INTRODUCTION. CHAPTER GOALS To understand the activity of programming To understand the activity of programming To learn about the architecture.
Fall 2006Slides adapted from Java Concepts companion slides1 Introduction Advanced Programming ICOM 4015 Lecture 1 Reading: Java Concepts Chapter 1.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
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.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Java Programming Week 1: Java Fundamental Revision (Text book: ch1, ch2, ch3, ch4)
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Fall 2006Slides adapted from Java Concepts companion slides1 Implementing Classes Advanced Programming ICOM 4015 Lecture 3 Reading: Java Concepts Chapter.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Chapter 1 Introduction. Chapter Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
CHAPTER 1 INTRODUCTION. CHAPTER GOALS To understand the activity of programming To learn about the architecture of computers To learn about machine code.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
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
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
Lecture 3 John Woodward.
Chapter 3 – Implementing Classes
Chapter Three: Implementing Classes
Implementing Classes Yonglei Tao.
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Chapter Three - Implementing Classes
Chapter 3 Implementing Classes
Java Intro.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed for the internet (applets and java scripts )

Becoming Familiar with your Computer Login Locate the Java compiler - javac Understand files and folders Write a simple program (later) Save your work

An Integrated Development Environment

File Hello.java source code 1 public class Hello 2{ 3public static void main(String[] args) // implies // application class 4{ 5// display a greeting in the console window 6System.out.println("Hello, World!"); 7} 8}

A simple program  public class ClassName  public static void main(String[] args)  // comment  Method call object.methodName(parameters)  System class  System.out object  println method

Syntax : Method Call object.methodName(parameters ) –Example: System.out.println("Hello”); Purpose: To invoke a method of an object and supply any additional parameters

Compiling and Running  Type program into text editor  Save  Open command shell  Compile into byte codes javac Hello.java  Execute byte codes java Hello

From Source Code to Running Program

The Edit-Compile-Test Loop

Objects and Classes  Object: entity that you can manipulate in your programs (by invoking methods)  Each instant object belongs to a class  Object Class: Set of objects with the same behavior – each object of the class is called an instance object

Rectangle Class Construct a rectangle: an instance of the Rectangle class new Rectangle(5, 10, 20, 30) new Rectangle() Use the constructed object System.out.println(new Rectangle(5, 10, 20, 30)); prints java.awt.Rectangle[x=5,y=10,w idth=20,height=30]

Rectangle Shapes

A Rectangle Object

Syntax : Object Construction new ClassName(parameters) –Example: new Rectangle(5, 10, 20, 30) new Car("BMW 540ti", 2004) –Purpose: To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object.

Object Variables  Declare and optionally initialize: Rectangle cerealBox = new Rectangle(5, 10, 20, 30); Rectangle crispyCrunchy;  Apply methods: cerealBox.translate(15, 25);  Share objects: r = cerealBox;

Uninitialized and Initialized Variables Uninitialized Initialized

Two Object Variables Referring to the Same Object

Syntax : Variable Definition TypeName variableName; TypeName variableName = expression; –Example: Rectangle cerealBox; String name ="Dave"; –Purpose: To define a new variable of a particular type and optionally supply an initial value

Syntax : 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 Simple Class public class Greeter { public String sayHello() { String message ="Hello,World!"; return message; } }

Method Definition  access specifier (such as public)  return type (such as String or void)  method name (such as sayHello)  list of parameters (empty for sayHello)  method body in { }

Method Parameters public class Rectangle {... public void translate(int x, int y) { method body }... }

Syntax : Method Implementation public class ClassName {... accessSpecifier returnType methodName(parameterType parameterName,...) { method body }... } …Continue

…Continue –Example: public class Greeter { public String sayHello() { String message ="Hello,World!"; return message; } } –Purpose: To define the behavior of a method A method definition specifies the method name, parameters, and the statements for carrying out the method's actions

Testing a Class  Test class: a class with a main method that contains statements to test another class.  Typically carries out the following steps: »Construct one or more objects of the class that is being tested. Invoke one or more methods. »Print out one or more results

A Test Class for the Greeter Class public class GreeterTest { public static void main(String [] args)) { Greeter worldGreeter = new Greeter(); System.out.println(worldGreeter.sayHello() ); }

Building a Test Program 1. Make a new subfolder for your program. 2.Make two files, one for each class. 3.Compile both files. 4.Run the test program.

Testing with the SDK Tools mkdir greeter cd greeter edit Greeter.java edit GreeterTest.java javac Greeter.java javac GreeterTest.java java GreeterTest

Instance Fields public class Greeter {... private String name; }  access specifier (such as private)  type of variable (such as String)  name of variable (such as name)

Accessing Instance Fields  The sayHello method of the Greeter class can access the private instance field: public String sayHello() { String message = "Hello, " + name + "!"; return message; }

 Other methods cannot: public class GreeterTest { public static void main(String[] args) {... System.out.println(daveGreeter. name); // ERROR } }  Encapsulation = Hiding data and providing access through methods

Syntax : Instance Field Declaration accessSpecifier class ClassName {... accessSpecifier fieldType fieldName;... }

–Example: public class Greeter {... private String name;... } –Purpose: To define a field that is present in every object of a class

Constructors  A constructor initializes the instance variables  Constructor name = class name –public class Greeter() { public Greeter(String aName) { name = aName; }... }  Invoked in new expression new Greeter("Dave")

Syntax : Constructor Implementation accessSpecifier class ClassName {... accessSpecifier ClassName(parameterType parameterName...) { constructor implementation }... }

–Example: public class Greeter {... public Greeter(String aName) { name = aName; }... } –Purpose: To define the behavior of a constructor, which is used to initialize the instance fields of newly created objects

File Greeter.java 1 public class Greeter 2 { 3 public Greeter(String aName) 4 { 5 name = aName; 6 } 7 8 public String sayHello() 9 {

10 String message = "Hello, " + name + "!"; 11 return message; 12 } private String name; 15 }

File GreeterTest.java 1 public class GreeterTest 2 { 3 public static void main(String[] args) 4 { 5 Greeter worldGreeter = new Greeter("World"); 6System.out.println(worldGreeter.sayHe llo()); 7

8 Greeter daveGreeter = new Greeter("Dave"); 9System.out.println(daveGreeter.s ayHello()); 10 } 11 }

Designing the Public Interface Behavior of bank account:  deposit money  withdraw money  get balance Methods of BankAccount class:  deposit  withdraw  getBalance

BankAccount Public Interface public BankAccount() public BankAccount(double initialBalance) public void deposit(double amount) public void withdraw(double amount) public double getBalance()

Using the Public Interface  Transfer balance –double amt = 500; momsSavings.withdraw(amt); harrysChecking.deposit(amt);  Add interest –double rate = 5; // 5% double amt = acct.getBalance() * rate / 100; acct.deposit(amt);

Commenting the Public Interface /** Withdraws money from the bank the amount to withdraw */ public void withdraw(double amount) { implementation filled in later }

/** Gets the current balance of the bank the current balance */ public double getBalance() { implementation filled in later }

Class Comment /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount {... }

Javadoc Method Summary

Javadoc Method Detail

BankAccount Class Implementation Determine instance variables to hold object state private double balance Implement methods and constructors

File BankAccount.java 1 /** 2 A bank account has a balance that can be changed by 3 deposits and withdrawals. 4 */ 5 public class BankAccount 6 {

7 /** 8 Constructs a bank account with a zero balance 9 */ 10 public BankAccount() 11 { 12 balance = 0; 13 } 14

15 /** 16 Constructs a bank account with a given balance initialBalance the initial balance 18 */ 19 public BankAccount(double initialBalance) 20 { 21 balance = initialBalance; 22 }

23 24 /** 25 Deposits money into the bank account. amount the amount to deposit 27 */ 28 public void deposit(double amount) 29 { 30 double newBalance = balance + amount; 31 balance = newBalance; 32 } 33

34 /** 35 Withdraws money from the bank account. amount the amount to withdraw 37 */ 38 public void withdraw(double amount) 39 { 40 double newBalance = balance - amount; 41 balance = newBalance; 42 } 43

44 /** 45 Gets the current balance of the bank account. the current balance 47 */ 48 public double getBalance() 49 { 50 return balance; 51 } private double balance; 54 }

File BankAccountTest.java 1 /** 2 A class to test the BankAccount class. 3 */ 4 public class BankAccountTest 5 { 6 /** 7 Tests the methods of the BankAccount class. args not used 9 */

10 public static void main(String[] args) 11 { 12 BankAccount harrysChecking = new BankAccount(); 13 harrysChecking.deposit(2000) 14 harrysChecking.withdraw(500) 15 System.out.println(harrysChecking.getBalance()); 16 } 17 }

Variable Types  Instance fields (balance in BankAccount)  Local variables (newBalance in deposit method)  Parameter variables (amount in deposit method)