ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Slides:



Advertisements
Similar presentations
ITEC 320 Lecture 12 Records. Intro to records Review Look_ahead(char,EOL); –What is this? –What does it allow us to do?
Advertisements

ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Java Programming (Chapter 1). Java Programming Classes, Types, and Objects.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
ITEC 320 Lecture 5 Scope Exceptions. Scope / Exceptions Review Functions Procedures.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?
Introduction to programming in the Java programming language.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Mixing integer and floating point numbers in an arithmetic operation.
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
CSC 142 Computer Science II Zhen Jiang West Chester University
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
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.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Methods.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
TK1924 Program Design & Problem Solving Session 2011/2012
Functions + Overloading + Scope
Building Java Programs
CSC240 Computer Science III
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Building Java Programs
Building Java Programs
Subprograms Functions.
Chapter 8 Lecture 8-1: Classes and Objects
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CSE 142 Lecture Notes Defining New Types of Objects
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Developing Java Applications with NetBeans
Building Java Programs
Building Java Programs
Building Java Programs
Developing Java Applications with NetBeans
Dr. R Z Khan Handout-3 Classes
Building Java Programs
reading: 8.6 self-check: #18, exercises: #9, 14
Methods/Functions.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

ITEC 320 Lecture 16 Packages (1)

Review Questions? –HW –Exam Nested records –Benefits –Downsides

Packages(1) Objectives Packages –OO in Ada

Packages(1) Definitions Client –Uses the class and its methods –Routine or package that uses the types and routines defined in package P Examples –With ada.text_io; use ada.text_io; Which one is Java? Which one is Ada?

Packages(1) Ada “Classes” Records store information Procedures work with records / other data Put the two together and you have the Ada version of classes Contain –Procedures –Types

Packages(1) Java class Pair { int x, y; int distanceToOrigin() // How far to origin { return Math.abs(x) + Math.abs(y); } } // Client for class Pair class PairClient { public static void main(String[] args) { Pair p; p = new Pair(); p.x = 1; p.y = 2; Sop(p.distanceToOrigin()); }

Packages(1) Ada 2 pieces –Record for a pair –Function to calculate the distance to the origin File structure –Specification –Body

Packages(1) Specificatio n Tells the compiler what the package does, but not how to do it Store in a.ads file package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;

Packages(1) Body Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;

Packages(1) Ada / Java Java classes (1 file) Ada method (2 files) What are the benefits / downsides of each approach? How does Java sort of provide the same idea that Ada does?

Packages(1) Rules The specification must be followed to the letter All functions / procedures must be implemented All return / parameter types must match

Packages(1) Use Without the use statement, you have to fully-qualify the package name with pairpkg; procedure pairclient is p: pairpkg.Pair; begin p.x := 1; p.y := 2; put(pairpkg.distanceToOrigin(p)); end pairclient;

Packages(1) Specificatio n Tells the compiler what the package does, but not how to do it Store in a.ads file package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;

Packages(1) Body Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;

Packages(1) Compilation Rule of thumb –Keep it all in the same directory –Some way of pointing where to look for specifications / bodies Compiler does the work for you –Automatically pulls in packages thanks to the with statement Take a look at the files produced and think about why they are there…

Packages(1) Privacy Java has private variables Why ? What do they provide? How are they used?

Packages(1) Ada package PairPkg is -- Public section of package type Pair is private; -- Only type name is public function newPair(x, y: Integer) return Pair; function distanceToOrigin(p: Pair) return Integer; -- Private section of package private -- Code below is NOT visible to client type Pair is record -- Type implementation is private x, y: Integer; end record end PairPkg; What can you do with the

Packages(1) Package package body PairPkg is function newPair(x, y: Integer) return Pair is temp: Pair := (x, y); begin return temp; end newPair; function distanceToOrigin(p: Pair) return Integer is begin return p.x + p.y; end distanceToOrigin; end PairPkg; What does this look like?

Packages(1) Client with ada.integer_text_io; use ada.integer_text_io; with pairpkg; use pairpkg; procedure pairclient is p: Pair; begin p := newPair(1, 2); put(distanceToOrigin(p)); p.x := 3; -- Syntax error end pairclient;

Packages(1) Specificatio n Tells the compiler what the package does, but not how to do it Store in a.ads file package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;

Packages(1) Body Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;

Packages(1) Summary Packages in Ada –Specifications –Body