unit 1: java introduction and eclipse

88
Unit 1: Java Introduction and Eclipse Kirk Scott

Upload: haig

Post on 25-Feb-2016

48 views

Category:

Documents


1 download

DESCRIPTION

Unit 1: Java Introduction and Eclipse. Kirk Scott. 1.1 Java 1.2 Object-Orientation 1.3 UML 1.4 The Eclipse Development Environment. 1.1 Java. Java is an object-oriented programming language. It is important to us because Android programming uses Java. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Unit 1:  Java Introduction and Eclipse

Unit 1: Java Introduction and Eclipse

Kirk Scott

Page 2: Unit 1:  Java Introduction and Eclipse

• 1.1 Java• 1.2 Object-Orientation• 1.3 UML• 1.4 The Eclipse Development Environment

Page 3: Unit 1:  Java Introduction and Eclipse

1.1 Java

Page 4: Unit 1:  Java Introduction and Eclipse

• Java is an object-oriented programming language.

• It is important to us because Android programming uses Java.

• However, Java is much more than just the language for Android.

Page 5: Unit 1:  Java Introduction and Eclipse

• What are some of Java’s characteristics that distinguish it from other programming languages?

• What features does it have that might make it useful in situations where other languages would not be as suitable?

Page 6: Unit 1:  Java Introduction and Eclipse

• From a technical point of view, Java has one fundamental characteristic which all other important characteristics stem from.

• Java is a language which is interpreted on a simulated hardware architecture. It is possible to design hardware that runs Java code directly, but in common computing environments such as Windows and Unix, installation of Java on a machine means that the simulation has been installed.

Page 7: Unit 1:  Java Introduction and Eclipse

• When programs run, they run on the simulation. For other languages such as C or C++, compilation results in an executable target file which consists of machine language for the architecture for which the compiler was designed.

• For Java, the process is still referred to as compilation, but it is not literal compilation. When Java source code is “compiled” the result is “byte code”, which is code which runs on the simulated Java machine.

Page 8: Unit 1:  Java Introduction and Eclipse

• In such a design, the programming language is merely a layer in a system’s software architecture and it is not dependent on the underlying hardware architecture of the machine.

• As a result, in theory at least, Java is potentially a “universal” programming language.

• Versions of Java exist for various hardware architectures. These implementations differ internally.

Page 9: Unit 1:  Java Introduction and Eclipse

• However, the intent is for the Java code generated for these different environments to be exactly the same.

• Thus, Java programs should be freely portable between them.

• If a program is written in one environment, no changes in the source code should be necessary in order for it to be compiled or, if already compiled, to be directly run in a different environment.

Page 10: Unit 1:  Java Introduction and Eclipse

• The statement made above is the ideal, which is encapsulated in the phrase “Write once, run everywhere”.

• This applies to desktop type computers. • Even in this case, the reality is that subtle

differences in implementations of the Java machine lead to problems which have been encapsulated in the phrase “Write once, debug everywhere”.

Page 11: Unit 1:  Java Introduction and Eclipse

• For us, it’s important to realize that Java as used in Android does differ from Android as used in other environments.

• The basic syntax is the same, but the overall structure of Android apps is different from the structure of apps in a desktop environment.

• An Android app will only run on an Android device. It will not run elsewhere.

Page 12: Unit 1:  Java Introduction and Eclipse

• As with all system and software design decisions, the nature of Java involves trade-offs.

• One of the driving forces behind Java was originally the World Wide Web.

• If a “universal” programming language exists, then programs can be downloaded and run on users’ machines regardless of the hardware involved.

• Now Java has been adapted to accomplish something similar in the cell phone environment.

Page 13: Unit 1:  Java Introduction and Eclipse

• The big trade-off in using Java is that Java programs are considerably slower than genuine compiled programs which run directly on the hardware of a given machine.

• Interpreting Java code on a simulated machine may take in the range of ten times as long.

• As hardware has become faster, and as the World Wide Web and cell phones have become the preferred means of distributing programs, this trade-off of speed for portability has been well worth it.

Page 14: Unit 1:  Java Introduction and Eclipse

• Another one of the design decisions in Java was driven by this desire to download runnable programs.

• Some languages, such as C and C++, give the programmer the ability to write code which directly accesses memory addresses in a machine’s hardware.

• This is done by means of pointers. • This is not possible in Java. • It may seem odd that a modern, advanced language does not

allow this. • It is possible to write code for linked structures in Java, but

not code which relies on memory addresses.

Page 15: Unit 1:  Java Introduction and Eclipse

• Programmers know that programs which directly access memory may do harmful things to a system, whether intentionally or unintentionally.

• For this reason, direct memory access is undesirable in Java.

• When users download programs, they want to be sure that these programs cannot do destructive things to their systems which result from direct memory access.

• Thus, by design, Java supports both portability and safety of programs.

Page 16: Unit 1:  Java Introduction and Eclipse

• The goal of this and the following units is to give an introduction to programming in the Java language and then progress to programming Android apps.

• Java is object-oriented. • It is designed to support event-driven programming.

• In other words, it can be used to create programs

with graphical user interfaces which support point-and-click actions.

Page 17: Unit 1:  Java Introduction and Eclipse

• Java has basic syntactic features found in structured programming languages, namely sequential execution of blocks of code, conditional execution (if statements), and iterative execution (loops).

• These syntactical elements will be covered in following units, as well as descriptions of the object-oriented concepts which support more advanced programming.

Page 18: Unit 1:  Java Introduction and Eclipse

• Experts may argue about the relative technical merits of Java.

• It must be noted at the outset that it has certain disadvantages for the learner.

• Java was not designed for teaching or learning purposes. • It was designed for building complex applications. • In addition to many advanced features within the

language itself, it comes with libraries of complete code components which an experienced programmer can use instead of writing components from scratch.

Page 19: Unit 1:  Java Introduction and Eclipse

• Neither the complete syntax nor the libraries can be learned in a single course.

• Much of what is learned at the outset may not seem particularly useful.

• However, it is necessary to cover the foundations of object-orientation before it becomes possible to write more interesting Java applications or Android apps.

Page 20: Unit 1:  Java Introduction and Eclipse

Section Summary

• The Java language is interpreted on a simulated machine architecture.

• This makes Java source and target code highly portable.

• This makes it possible to download Java code from the World Wide Web, although Java programs may not run especially quickly.

• In order to make downloaded programs safe, Java programs do not support direct memory access.

Page 21: Unit 1:  Java Introduction and Eclipse

• Java is an object-oriented language containing the features needed in order to write graphical applications with point-and-click interfaces.

• Java was designed as a production language, not a teaching language. It contains many features and comes with many libraries.

• It is possible to do things in Java that cannot be done, or can only be done with difficulty in other languages.

Page 22: Unit 1:  Java Introduction and Eclipse

Vocabulary

• Interpreted languages, compiled languages.• Source code, target code.• Pointers, direct memory access.• Object-orientation, software applications.• Event-driven programming.• Point-and-click, graphical user interfaces.• Sequential execution.• Conditional execution (if statements).• Iterative execution (loops).

Page 23: Unit 1:  Java Introduction and Eclipse

1.2 Object-Orientation

Page 24: Unit 1:  Java Introduction and Eclipse

• Programming languages and paradigms have evolved progressively over time.

• Some of the things that have driven change include: • New hardware, which has the capability of

supporting new programming paradigms; • new problems to be solved, which require new

programming capabilities; • and limitations in existing systems which are exposed

when large or complex projects are developed.

Page 25: Unit 1:  Java Introduction and Eclipse

• Object-oriented programming is one of the more recent programming paradigms.

• Its development and characteristics reflect these three points. • It is a computationally intensive paradigm which could not

have been supported by primitive machines; • the programming of graphical user interfaces is one area

where its use is apparent; • and because it has solved at least some of the problems

associated with structured programming, its most immediate predecessor, it has largely supplanted it for the development of complex software systems.

Page 26: Unit 1:  Java Introduction and Eclipse

• Java is an object-oriented language. • There are many object-oriented languages,

and each implements the paradigm in its own way.

• It is not the intent of these notes to give a full explanation of object-orientation or all aspects of object-orientation in Java.

Page 27: Unit 1:  Java Introduction and Eclipse

• However, it is desirable to start with some general ideas regarding object-orientation, building a vocabulary for the basic concepts involved.

• A fuller understanding will arise as the implementation of these concepts in Java is examined, and as more advanced concepts and their implementation are added to the basics.

Page 28: Unit 1:  Java Introduction and Eclipse

• Two fundamental terms are involved in object-orientation, and will be repeated over and over again.

• The terms are: • Class and object. • A class is a model for objects. • You could also say that a class is a template, or pattern for

objects. • To be more concrete, a class is a piece of computer code

which gives the complete specifications for objects. • The class definition is part of a program’s source code.

Page 29: Unit 1:  Java Introduction and Eclipse

• During the course of a program run it is possible to make a call which causes an object to be created according to the specifications of a given class.

• An object can be referred to as an instance of this class.

• A program may use more than one class, and more than one instance of any of these classes may be created during the course of a program run.

• The work of the program consists of manipulating these objects.

Page 30: Unit 1:  Java Introduction and Eclipse

• Two additional concepts are involved in object-orientation.

• Computer programs include sequences of instructions which belong together and can be executed to do some specific thing.

• In object-oriented programming, these are referred to as methods.

• Methods are part of a class definition.

Page 31: Unit 1:  Java Introduction and Eclipse

• Computer programs also include declarations of data, such as numbers, sequences of characters, etc., which are to be manipulated.

• In their simplest form, these are variables. • Variables are symbolic names which can be used to refer to

stored quantities or values in a program. • Different kinds of variables can be used in different places in

a program. • The variables associated with classes and objects are called

instance variables, and like methods, they are part of a class definition.

Page 32: Unit 1:  Java Introduction and Eclipse

• Although both appear in the class definition, methods and instance variables are treated quite differently in the object-oriented paradigm.

• In a program there may be more than one instance of more than one class.

• The following distinction between methods and instance variables applies:

• There is no need at run time for each instance of a class to get its own copy of method code.

Page 33: Unit 1:  Java Introduction and Eclipse

• Methods are defined in the class and are shared by all objects of that class that are in existence in a program at a given time.

• Thus, the methods for all objects of a class are exactly the same.

• However, when objects of a class are created, they get their own copies of the instance variables.

• Each object of a class gets the same set of variables, but different objects can have different values for the variables.

Page 34: Unit 1:  Java Introduction and Eclipse

• So, what’s the relationship between methods and instance variables?

• A fundamental aspect of the object-oriented paradigm is referred to as encapsulation.

• Each object has its own copies of the instance variables. • The values of these variables can be accessed or changed

during the course of a program run in only one way: • By calling a method which has the purpose of revealing or

changing the value of the instance variable.

Page 35: Unit 1:  Java Introduction and Eclipse

• This enforces a strong discipline on programmers. • It is not possible at random points in program code to

simply change instance variable values in a way that may seem convenient at the moment.

• Changes can only be made within the framework of encapsulation.

• Undisciplined changing of data values was considered one of the weaknesses of structured programming.

• This is corrected in object-oriented programming.

Page 36: Unit 1:  Java Introduction and Eclipse

Figure 1. Instance variables and methods.

• Here is a simple graphical representation of an object, showing the instance variables it contains and the methods it uses.

Methods

Instance variables

Page 37: Unit 1:  Java Introduction and Eclipse

• A handy way of remembering the meaning of the figure is by thinking of the object as a fried egg.

• Once the object is created, a program only has access to the yolk through the white.

• It is important to understand that the programmer making use of the object needs to know what methods are available for manipulating it.

• However, it is not necessary to know how the method code works.

Page 38: Unit 1:  Java Introduction and Eclipse

• Although the existence of some instance variables may be clear, the programmer does not need to know how they are implemented, and does not need to know all of the instance variables in order to make use of the object.

• In this sense, the object can be though of as a black box. • The programmer can do certain things to it, with certain

results expected, but how these results are accomplished is hidden.

• The methods provide an interface to the instance variables and their values contained inside the object.

Page 39: Unit 1:  Java Introduction and Eclipse

Section Summary

• Object-orientation relies on advanced hardware capabilities, solves problems that could not be solved with earlier programming paradigms, and addresses some of the shortcomings of previous paradigms.

• Java implements the object-oriented paradigm, and basic object-oriented terms and ideas are helpful in explaining Java.

• Classes and objects are two of the underlying concepts of object-orientation.

Page 40: Unit 1:  Java Introduction and Eclipse

• Methods and variables are two additional key concepts of object-orientation.

• When objects are created from a class definition, they share the code for methods, but they each get their own copies of the instance variables.

• Objects encapsulate their instance variables. The values of the variables can only be accessed or changed if there is a method in the class that permits it.

• A programmer using an object only has access through the methods, and does not need to know how the methods work or all of the instance variables there might be.

Page 41: Unit 1:  Java Introduction and Eclipse

Vocabulary

• Paradigm.• Computationally intensive.• Classes, objects.• Methods, instance variables.• Encapsulation.

Page 42: Unit 1:  Java Introduction and Eclipse

1.3 UML

Page 43: Unit 1:  Java Introduction and Eclipse

• Depending on the source, the acronym UML is said to stand for “unified modeling language” or “universal modeling language”.

• In any case, it is a set of conventions that can be used to diagram object-oriented software.

• It is possible to pictorially represent things like classes and their methods and instance variables;

• objects of those classes; relationships between classes; • and so on.

Page 44: Unit 1:  Java Introduction and Eclipse

• These notes will not give complete coverage of UML.

• However, at various points in the presentation of Java it may be helpful to represent an idea or relationship visually.

• In those cases, the corresponding UML notation will be introduced and used for that purpose.

Page 45: Unit 1:  Java Introduction and Eclipse

• UML has a vocabulary as well as a set of visual symbols which are used to represent object-oriented components.

• The terms “class” and “object” have already been used in these notes.

• These terms are used to mean the same thing in UML.

Page 46: Unit 1:  Java Introduction and Eclipse

• The terms “instance variable” and “method” have already been introduced.

• The corresponding terms in UML are “attribute” and “operation”.

• There is more than one object-oriented language. • UML is neutral. • An object-oriented program in any of these

languages could be described using UML terminology.

Page 47: Unit 1:  Java Introduction and Eclipse

• Since this book only covers Java, the terms “instance variable” and “method” will continue to be used.

• It should be noted that in the official Java documentation, the term “field” is used for “instance variable”.

• Since this is the term chosen by the language designers, in some sense it may be more technically correct.

• However, the term “field” may be confusing because it has other uses in various programming environments. “Instance variable” will be used instead because it is more descriptive.

Page 48: Unit 1:  Java Introduction and Eclipse

• In order to illustrate UML, it is necessary to have an example.

• It is possible to introduce such an example and talk about object-oriented ideas without even knowing any Java.

• The example which will be continued throughout the notes is based on the following idea:

• It is possible to store seeds in a cup.

Page 49: Unit 1:  Java Introduction and Eclipse

• This may seem a little impractical. • You typically think of a cup as containing a liquid,

and it’s not clear what kinds of interesting programs you might write involving cups and seeds.

• Any introductory example of a class would tend to be impractical anyway.

• The cup and seed example is given because it is a component of a game program which is developed later on in the notes.

Page 50: Unit 1:  Java Introduction and Eclipse

• Classes and objects are represented using rectangles in UML.

• Relationships are represented using labels, lines, arrowheads, and other symbols.

• In its simplest form, a class can be represented by a rectangle containing the name of the class.

• The name is capitalized and given in bold face. • For example, this would represent the Cup class:

Page 51: Unit 1:  Java Introduction and Eclipse

Cup

Page 52: Unit 1:  Java Introduction and Eclipse

• An object of a given class would have a name of its own.

• Say an instance is known as “my coffee mug”. • It is also represented by a rectangle. • The name is not capitalized. • It is followed by a colon and the name of the class it

is an instance of. • All of this is underlined. • For example:

Page 53: Unit 1:  Java Introduction and Eclipse

my coffee mug : Cup

Page 54: Unit 1:  Java Introduction and Eclipse

• The relationship between the class and the object can be graphically shown with an arrow.

• It turns out that in UML different kinds of relationships are shown with different kinds of arrows, so the form of the arrow is important.

• This relationship is shown with a dashed arrow with an open, or feathered arrowhead.

• The arrow points from the object to the class.

Page 55: Unit 1:  Java Introduction and Eclipse

Cup

my coffee mug : Cup

Page 56: Unit 1:  Java Introduction and Eclipse

• More detail about a class can be given by including its attributes and operations in the diagram.

• In general, such a class diagram takes this form:

Page 57: Unit 1:  Java Introduction and Eclipse

+operations()-attributes

Cup

Page 58: Unit 1:  Java Introduction and Eclipse

• To be specific, let the Cup class have one instance variable, named seedCount.

• Let the class have two methods, setSeedCount() and getSeedCount().

• Let the seedCount be an integer, and when a new instance of the class is created, let its seedCount be initialized to the value 0.

• The setSeedCount() method assigns a value to the seedCount of an object.

• The getSeedCount() method returns the value currently stored in the seedCount variable, whatever that may be.

• This information is shown in this more complete form of the class diagram:

Page 59: Unit 1:  Java Introduction and Eclipse

+setSeedCount()+getSeedCount() : int

-seedCount : int = 0

Cup

Page 60: Unit 1:  Java Introduction and Eclipse

• It is also possible to give even more detail, including information about the parameter of the setSeedCount() method, ranges of valid values, or other information, for example.

• The diagrams are used to document a software system, a program, or its components.

• The level of detail included should be gauged to be helpful to the intended reader of the diagram, whether a designer, a programmer, or a program user.

Page 61: Unit 1:  Java Introduction and Eclipse

Section Summary

• UML is used for diagramming object-oriented programs.

• UML has a vocabulary for the components of classes.

• Java has its own, parallel vocabulary for the components of classes.

• The cup and seed example is used for the purposes of illustration.

Page 62: Unit 1:  Java Introduction and Eclipse

• In UML classes and objects are represented by rectangles and relationships are represented by arrows and other symbols and labels.

• A simple class diagram is a rectangle containing the capitalized name of the class in bold face.

• A simple object diagram is a rectangle containing the name of the object followed by a colon and the name of its class, all in bold face.

Page 63: Unit 1:  Java Introduction and Eclipse

• The relationship between an object and its class can be shown by a dashed arrow with an open arrowhead from the object to the class.

• A more complete class diagram includes the class’s attributes and operations, possibly along with more detailed information.

• The level of detail in diagrams can vary.

Page 64: Unit 1:  Java Introduction and Eclipse

Vocabulary

• UML.• Attribute.• Instance variable.• Field.• Operation.• Method.

Page 65: Unit 1:  Java Introduction and Eclipse

1.4 The Eclipse Development Environment

Page 66: Unit 1:  Java Introduction and Eclipse

• These notes are based on the assumption that you will have available to you a development machine that already has the Eclipse environment installed on it.

• More details on installing the environment are given later, assuming that you might want to set this up at home.

• For the time being you just need to be familiar with several things in order to begin using Eclipse.

Page 67: Unit 1:  Java Introduction and Eclipse

• When you start Eclipse it will prompt you regarding the workspace that you want to use.

• This is where your programs will be stored. Unless directed otherwise, just accept the default.

• In simple terms, when Eclipse is up and running, the screen will be divided into 4 areas:

Page 68: Unit 1:  Java Introduction and Eclipse

• Across the top: This is the menu (no surprises). There are many, many menu options. Initially we will just be interested in a few.

• Down the left: This is the Explorer. In effect, this shows the contents of your workspace. In other words, it shows folders containing the programs you’ve written.

• In the center: This is the editor. This is where you can see and type in the contents of programs.

• Below the editor: There are several tabbed items here. The tab that will be of initial interest is the Console. This is where program output will be shown.

Page 69: Unit 1:  Java Introduction and Eclipse

• The process of loading and running a program will be described shortly.

• In order to illustrate the areas mentioned above, a screenshot of the first program open in the editor, after having run, is shown below:

Page 70: Unit 1:  Java Introduction and Eclipse
Page 71: Unit 1:  Java Introduction and Eclipse

• 1. In Eclipse, each program you write will be saved as a project in a folder of its own.

• 2. At the top of every program there will be a line of code of this form:

• • public class Name_of_program

Page 72: Unit 1:  Java Introduction and Eclipse

• The name of the file that the program is saved in has to agree exactly with the name that appears in this line of code within the program.

• The first letter of the name is capitalized within the program, and the first letter of the name of the file also has to be capitalized.

• In other words, in order to be valid, a program that started with the line of code given above would have to be saved with this name:

• • Name_of_program

Page 73: Unit 1:  Java Introduction and Eclipse

• 3. Java is completely case sensitive. • Microsoft Windows is not fully case sensitive. • If you are accustomed to working in Windows

and naming files there, this can lead to problems.

• You can largely avoid problems if you only work with your program code through Eclipse.

Page 74: Unit 1:  Java Introduction and Eclipse

• 4. There is one last bit of information that might be of interest.

• If you were curious you could find the workspace folder on your machine and find its /src subfolder.

• This is where your Java files are stored. • If you used Windows to look at their properties,

you should find that they are in fact Java files.

Page 75: Unit 1:  Java Introduction and Eclipse

• Depending on how your system is set up, you would find that the file names actually have an extension, .java.

• Because the Eclipse environment is taking care of the details, in practice you shouldn't have to worry about these facts.

• They are just presented for your information.

Page 76: Unit 1:  Java Introduction and Eclipse

• Now the process of using Eclipse to load and run your first example program will be explained.

• This is the process that resulted in the screenshot shown above.

• 1. Having started Eclipse and accepted the workspace take the following menu option:

• File, New, Java Project. • The resulting dialog is shown below. • Enter a Project Name value of MyFirstJavaProject and

accept the default values for all of the other fields as given.

Page 77: Unit 1:  Java Introduction and Eclipse
Page 78: Unit 1:  Java Introduction and Eclipse

• After filling in the Project Name, the Finish button will become active.

• After you click it, the MyFirstJavaProject folder should appear in the Explorer on the left hand side of the Eclipse interface.

• 2. Having successfully created the project, take the following menu option:

• File, New, Class. • The resulting New Java Class dialog is shown below. • Enter a Name for the class, for example, FirstProg. Accept

the default values for all of the other fields as given.

Page 79: Unit 1:  Java Introduction and Eclipse
Page 80: Unit 1:  Java Introduction and Eclipse

• After filling in the class Name, the Finish button will become active.

• After you click it, the FirstProg file will appear under the MyFirstJavaProject folder in the Explorer and the editor screen of Eclipse will show the empty outline of the FirstProg class, as shown in the screenshot below.

Page 81: Unit 1:  Java Introduction and Eclipse
Page 82: Unit 1:  Java Introduction and Eclipse

• 3. On the Web site for these notes in the Unit 1 folder there is a file named FirstProg.java.

• If you open it using the browser, this is what you’ll see: • • public class FirstProg• {• public static void main(String[] args)• {• System.println("Hello World");• }• }

Page 83: Unit 1:  Java Introduction and Eclipse

• Copy this and paste it into the Eclipse editor, replacing the blank template for the FirstProg class.

• If this was successful, there should be no red marks on any line in the code.

• Eclipse automatically compiles any code placed in the editor, so the lack of red marks means that the code compiled successfully and is ready to run.

Page 84: Unit 1:  Java Introduction and Eclipse

• 4. Having come this far, take the following menu option:

• Run, Run. • If there are no runtime problems, the program

will generate output, namely the message “Hello World”, and this will be shown at the bottom of the Eclipse interface.

• The screenshot of this successful sequence of events is repeated below.

Page 85: Unit 1:  Java Introduction and Eclipse
Page 86: Unit 1:  Java Introduction and Eclipse

Section Summary

• Java program files have to match the class name within them.

• Java is fully case sensitive.• The Eclipse environment automates the

creation of projects and program files, allowing programs to be compiled and run

Page 87: Unit 1:  Java Introduction and Eclipse

Vocabulary

• Project.• Capitalized.• Case sensitive.• Extension.

Page 88: Unit 1:  Java Introduction and Eclipse

The End