Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 1: Java Introduction and Eclipse Kirk Scott.

Similar presentations


Presentation on theme: "Unit 1: Java Introduction and Eclipse Kirk Scott."— Presentation transcript:

1 Unit 1: Java Introduction and Eclipse Kirk Scott

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

3 1.1 Java

4 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.

5 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?

6 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.

7 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.

8 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.

9 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.

10 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”.

11 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.

12 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.

13 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.

14 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.

15 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.

16 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.

17 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.

18 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.

19 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.

20 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.

21 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.

22 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).

23 1.2 Object-Orientation

24 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.

25 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.

26 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.

27 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.

28 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.

29 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.

30 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.

31 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.

32 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.

33 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.

34 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.

35 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.

36 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

37 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.

38 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.

39 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.

40 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.

41 Vocabulary Paradigm. Computationally intensive. Classes, objects. Methods, instance variables. Encapsulation.

42 1.3 UML

43 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.

44 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.

45 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.

46 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.

47 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.

48 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.

49 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.

50 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:

51

52 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:

53

54 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.

55

56 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:

57

58 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:

59

60 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.

61 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.

62 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.

63 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.

64 Vocabulary UML. Attribute. Instance variable. Field. Operation. Method.

65 1.4 The Eclipse Development Environment

66 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.

67 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:

68 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.

69 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:

70

71 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

72 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

73 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.

74 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.

75 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.

76 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.

77

78 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.

79

80 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.

81

82 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"); }

83 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.

84 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.

85

86 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

87 Vocabulary Project. Capitalized. Case sensitive. Extension.

88 The End


Download ppt "Unit 1: Java Introduction and Eclipse Kirk Scott."

Similar presentations


Ads by Google