Packages. RHS – SOC 2 Packages in Java When using NetBeans, you have (hopefully) observed that files are organised into packages What is the purpose of.

Slides:



Advertisements
Similar presentations
2-May-15 GUI Design. 2 HMI design There are entire college courses taught on HMI (Human-Machine Interface) design This is just a very brief presentation.
Advertisements

1 eclipse Tips. 2 What is eclipse? Eclipse is a popular IDE (Integrated Development Environment) that we will use to create, compile, execute, and test.
Introduction to Excel 2007 Part 2: Bar Graphs and Histograms February 5, 2008.
Java.  Java is an object-oriented programming language.  Java is important to us because Android programming uses Java.  However, Java is much more.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
1 Lecture 3: Elaboration and System Architecture.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
Using the EG Applet Bram van Heuveln. Contents General Comments –Some general comments regarding the use of the applet Work Area –Instructions for creating.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Basic HTML The Magic Of Web Pages. Create an HTML folder  Make a folder in your H drive and name it “HTML”. We will save EVERYTHING for this unit here.
Manage your mailbox V: Retrieve, back up, or share messages Use your stored messages Whether you’re using the Personal Folders method or the Archive method.
Java: Chapter 1 Computer Systems Computer Programming II Aug
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
1 eclipse Tips. 2 What is eclipse? Eclipse is a popular IDE (Integrated Development Environment) that we will use to create, compile, execute, and test.
From BlueJ to NetBeans SWC 2.semester.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
Introduction to Scratch!
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Java Data Types Data types, variable declaration, and initialization.
File Management Organizing files and folders. In this tutorial you will learn how to:  Create folders  Name files and folders  Organize your files.
Microsoft ® Outlook ® 2010 Training Mailbox management 1: Creating folders.
Why making notes is important, and some ways to approach note-taking.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Introduction to Derby. RHS – What is Derby…? Derby is a DBMS –Database –GUI for administration –GUI for data maintenance Best part; integrated.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Factory Patterns. RHS – SOC 2 Being less concrete One important OO principle is: ”Program to an interface, not an implementation” Interfaces reduces the.
Chapter 4: A Paradigm Program structure Connecting to the Java world Types Access modifiers Lifetime modifiers.
Creating a Custom Drawing Sheet Tutorial. Create a new standard drawing file from menu options Opening a Drawing Sheet.
Saving Work to Your School Server Click through this presentation at your own speed. Use it as a review or a guide while saving a project.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
There are ten this week! Don’t get too excited!.  small pieces of information that tell more about the main idea.
Getting Started with Word & Saving Guided Lesson.
File Management In this exercise the A drive is used as an example. If you are using another storage device, substitute that drive letter (D, E, F…) where.
Building Packages BCIS 3680 Enterprise Programming.
Creating a Java Application and Applet
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Lecture 10 Using Interface Builder to create Mac Applications.
Multiplication of Common Fractions © Math As A Second Language All Rights Reserved next #6 Taking the Fear out of Math 1 3 ×1 3 Applying.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Working with Packages BCIS 3680 Enterprise Programming.
Conventions for Web Designers.  DO NOT: ◦ Use capital letters or spaces in names ◦ Use punctuation or symbols  DO: ◦ Use names that are easily recalled.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
For Monday Read pages Do exercise 1.1 on page 46 (to hand in – may be done in groups of up to 4) Complete WebCT quiz.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
THE AREA OF A CIRCLE An introduction to approximating the area of a circle.
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
CIS 355 Innovative Education-- snaptutorial.com
Designing and Debugging Batch and Interactive COBOL Programs
Exceptions Handling the unexpected
Packages and Interfaces
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Constructors, GUI’s(Using Swing) and ActionListner
CMPE212 – Reminders Assignment 2 sample solution is posted.
CMPE212 – Reminders Assignment 2 due today, 7pm.
Presentation transcript:

Packages

RHS – SOC 2 Packages in Java When using NetBeans, you have (hopefully) observed that files are organised into packages What is the purpose of using packages…?

RHS – SOC 3 Packages in Java As soon as a Java project grows beyond a few classes, we should organise the classes into packages –Makes it easier for the programmer to locate and use a specific class –Avoids name clashes –Enables a certain level of access control

RHS – SOC 4 Packages in Java How do I create a package…? Choose a proper name for it (not quite trivial, see later) In NetBeans: –Highlight ”Source Packages” –Right-click –Choose New | Java Package

RHS – SOC 5 Packages in Java

RHS – SOC 6 Packages in Java How do I create a class (or interface) within a specific package…? You have (hopefully) already tried this In NetBeans: –Highlight the package in which to put the new class (or interface) –Right-click –Choose New | Java Class (or Interface)

RHS – SOC 7 Packages in Java

RHS – SOC 8 Packages in Java How do I move a class from one package to another package…? In NetBeans: –Use drag-and-drop –Drag the class to the new package, and release it there –This is formally considered to ”refactor” the class, so a dialog appears

RHS – SOC 9 Packages in Java Just click ”Refactor”

RHS – SOC 10 Packages in Java What happens ”under the hood”…? Actually, not very much… –When a new package is created, a new subfolder with that name is created in the project folder –When a class is created in a package, the package statement in the class is set to the name of that package

RHS – SOC 11 Packages in Java

RHS – SOC 12 Naming a package One purpose of placing classes in packages is to avoid name clashes A name clash occurs when two classes in the same package have the same name Compiler cannot distinguish the classes However, two classes with the same name – but placed in different packages – is perfectly legal

RHS – SOC 13 Naming a package // These two classes are unrelated… jane.Square jS = new jane.Square();... graphics.Square gS = new graphics.Square(); // Note that the fully qualified names of // the classes are used

RHS – SOC 14 Naming a package // This is legal.. import jane.Square; // This now means jane.Square Square jS = new Square();... graphics.Square gS = new graphics.Square();

RHS – SOC 15 Naming a package // This is NOT legal.. import jane.Square; import graphics.Square // cannot determine what Square means… Square jS = new Square();... Square gS = new Square();

RHS – SOC 16 Naming a package Using packages does not save us completely from name clashes What if two packages have the same name…? A package name should preferably be unique ”world-wide”

RHS – SOC 17 Naming a package How can we make package names unique…? Impossible…but we can try our best One convention is to name packages: companyName.packageName or (to avoid internal name clashes) companyName.regionName.packageName

RHS – SOC 18 Naming a package In student projects, name clashes it not really a problem in practice Only matters if someone else is going to use your classes Common practice to start a class name with a lowercase letter, to avoid confusion with classes themselves

RHS – SOC 19 Using a package A class can use a class in a different package in several ways: –Use fully qualified name –Import specific class –Import entire package

RHS – SOC 20 Using a package Fully qualified name When refering to the class, prefix the class name with the package name, like: graphics.Rectangle No need for import statements Fine if you only refer to classes outside your own package a few times

RHS – SOC 21 Using a package Import specific class Use an import statement, like: import graphics.Rectangle; You can then refer to the class just by the class name: Rectangle r = new Rectangle();

RHS – SOC 22 Using a package Import all classes in a package Use an import statement, like: import graphics.*; You can then refer to all classes in that package just by the class name

RHS – SOC 23 Using a package NB: Packages do not form a hierarchy! Suppose we have packages: graphics graphics.2d graphics.3d The statement import graphics.* will only import the classes in graphics, not the classes in the other libraries

RHS – SOC 24 Organising packages The big question is now: How should we actually organise the classes into packages…? More specifically; along which ”dimensions” should we create packages?

RHS – SOC 25 Organising packages In any system architec- tures, we have two main ”dimensions” –Functional layers (GUI, business logic, data) –Services (isolated pieces of functionality)

RHS – SOC 26 Data Layer Logic Layer GUI Layer Balance Service Withdraw Service Deposit Service Organising packages Deposit GUI Deposit Logic Deposit Data Withdraw GUI Withdraw Logic Withdraw Data Balance GUI Balance Logic Balance Data

RHS – SOC 27 Organising packages There is no ”silver bullet” to how to organise packages Use common sense! Try to avoid packages with just one or two classes Also try to avoid packages with too many (>10 ?) classes

RHS – SOC 28 Exercises Download the three NetBeans projects listed below (found on the website, under Classes – week 44) –AbstractFactoryExample –CarRentalDB –CommandExample For each project, consider what would be the most relevant way of reorganising the classes Try to reorganise the classes in each project, by defining new packages, and moving the classes to packages Take a look inside the classes – what has changed? Also take a look inside the folders for the projects – what has changed?