1 / 81

Unit 2: Java Introduction to Programming

Unit 2: Java Introduction to Programming. Kirk Scott. 2.1 Initial Example 2.2 Information on Program Appearance and Printing 2.3 The Java API Documentation 2.4 Creating and Using Objects 2.5 Errors. 2.1 Initial Example.

yuki
Download Presentation

Unit 2: Java Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 2: Java Introduction to Programming Kirk Scott

  2. 2.1 Initial Example 2.2 Information on Program Appearance and Printing 2.3 The Java API Documentation 2.4 Creating and Using Objects 2.5 Errors

  3. 2.1 Initial Example

  4. The goal of this section is to give an initial explanation of the first example program, given at the end of the previous set of notes. Here is the program: • public class FirstProg • { • public static void main(String[] args) • { • System.out.println("Hello World"); • } • }

  5. What follows is a step by step description of the syntax of the program along with some explanation of how and why the code is written in this way. • At this early stage it is most important to become familiar with the syntax, so that you can make use of it when beginning to write your own programs.

  6. The explanations are given because most people don’t want to try and learn something in a vacuum. • However, if the explanations seem cryptic or incomplete, don’t worry. • More detail will be given in future units.

  7. 1. The first line of code shows the syntax for the declaration of a class: • public class FirstProg • All user written programs are classes. • The class is public, which means it is freely available for general use. • The name of the class is “FirstProg”.

  8. 2. The second line of code is quite easy to explain, and it goes with the last line of code. • { • All class definitions are enclosed in a matched set of braces, {}.

  9. 3. The third line of code introduces a method. • public static void main(String[] args) • The name of this method is “main()”. • A stand-alone program has to have a main() method. • When you ask the Java system to run the program, the system looks inside the program class and causes this method to run.

  10. The keyword “public” signifies that the method is freely available for the system to call. • The keyword “static” signifies that main() is a general purpose method, not one that is used on an object. • This will be explained in greater detail later. • The keyword “void” indicates that running this method does not cause any value to be returned.

  11. Every method name is followed by a matched pair of parentheses. • When methods are referred to in these notes, they will always be referred to by name plus parentheses, such as “main()”. • This makes it clear that the name “main” is the name of a method rather than the name of something else.

  12. The main() method has something in the parentheses. • These are parameters for command line arguments. • This program does not make use of them, but it is still necessary to include the declaration in the code. • The actual form of the declaration will be explained in a future set of notes.

  13. 4. The fourth and sixth lines of code are quite easy to explain. • All method definitions are enclosed in a matched set of braces, {}.

  14. 5. The fifth line of code does the real work of the program. • System.out.println("Hello World"); • Here the method println() is called. • This causes output to be sent to the Console. • In general a method is called “on” something, and that is symbolized by the words System and out and the use of dots. • The method is called with the parameter “Hello World”. • Calling methods is important and will be a major topic in the note files.

  15. If you were able to compile and run the program when it was given in Unit 1, you know what it does: • It prints the message “Hello World” in the Console. • The ability to accomplish this is the starting point for learning how to do other things in Java.

  16. 2.2 Information on Program Appearance and Printing

  17. Comments are explanatory notes that a programmer can include in a program. • They are not code and they do not affect how the program runs. • On any line where “//” appears, whatever follows this pair of symbols is treated as a comment. • Likewise, anything that appears between “/*” and “*/” will be treated as a comment.

  18. Here is a commented version of the first program: • /* This is my first program. */ • public class FirstProg • { • public static void main(String[] args) • { • // A method is called here. • System.out.println("Hello World"); • } • }

  19. When writing a program it is helpful to indent every set of matched braces so that it is clear where a block of code begins and ends, and whether or not there are nested sets of blocks. • Eclipse will do this for you automatically, and all examples given will be shown in this form.

  20. Here is a brief summary of some of the aspects of printing in Java when using println() and related methods. • 1. The method println() prints a line of output followed by a new line or carriage return. • You can also make use of the method print(), which prints a line without a carriage return. • The complete call would be of the form: • System.out.print("Some Parameter");

  21. 2. You can print arithmetic constants as well as strings of characters. • A call such as this would print out the numeric value 3: • System.out.println(3);

  22. Note that what would appear on the screen for the following call would be exactly the same, but that there is an important distinction in the meaning of the program code. • The example above prints a numeric value, while the example below prints a string containing the symbol “3”: • System.out.println("3");

  23. 3. You can print out more than one parameter at a time. • The symbol that accomplishes this is the “+” sign. • When printing strings, if you want blank spaces between things, you need to supply them explicitly. If you did this: • System.out.println("Hello" + "World"); • You would see this: • HelloWorld

  24. However, you could also do this: • System.out.println("Hello" + " World"); • Or this: • System.out.println("Hello" + " " + "World"); • And you would see: • Hello World

  25. 4. Using the “+” sign when printing numeric values has a different effect. • It does the arithmetic. • If you did this: • System.out.println(3 + 4); • You would see this: • 7

  26. 5. You can also combine a parameter in quotes with a numerical value without quotes using a “+” sign. • In this case the system automatically converts the numerical value to a string and does concatenation. • If you did this: • System.out.println("Hello" + 7); • You would see this: • Hello7

  27. The full explanation of the different ways in which the “+” sign works will be given later. • For the time being you should simply be able to use it correctly with strings and numbers.

  28. 6. Although the dot has a special syntactic meaning in various places in Java code, in places where simple parameters in the form of numbers are allowed, such as when printing, it has its customary meaning—that of a decimal point. • Thus, if you did this: • System.out.println(3.4); • You would see this value: • 3.4

  29. 7. In Java printing, the backslash “\” is used as the escape sequence. • This means that any symbol immediately following the backslash is simply treated as a printable character. • That character is not treated as having any syntactic meaning. • This allows you to print characters that would otherwise be ambiguous or syntactically incorrect in a printing parameter.

  30. If you wanted to print a double quote, you could do this: • System.out.println("\""); • If you wanted to print the backslash itself, you could do this: • System.out.println("\\");

  31. The backslash can also be used to insert special printing instructions into a printed string. \n represents a new line. • Thus, if you did this: • System.out.println("Hello\nWorld"); • You would see this: • Hello • World

  32. 2.3 The Java API Documentation

  33. What follows are the contents of the Java Application Programming Interface (API) Documentation for the system supplied class Point. • The complete online Java API documentation can easily be found through a Web search. • This is a comprehensive reference for questions about programming in Java. • An explanation of the excerpt from the documentation is given following it.

  34. java.awtClass Point • java.lang.Object | +--java.awt.geom.Point2D | +--java.awt.PointAll Implemented Interfaces: • Cloneable, Serializable

  35. public class Point • extends Point2D • implements Serializable • A point representing a location in (x, y) coordinate space, specified in integer precision. • Since: • JDK1.0 • See Also: • Serialized Form

  36. The information given at the top of the documentation concerns the naming of the class and where it is located. • System supplied classes are arranged in packages. • If you scan through the information at the top you will eventually find this: • java.awt.Point. • “awt” stands for “abstract windowing toolkit”. • This is the name of a package in Java which includes classes related to doing graphical things.

  37. Point is one of those classes. If a program uses a system supplied class, a line like this is put at the top of the code: • import java.awt.Point; • The idea is that this will make the class available for use in the program. • This will be done in the example programs. • It is possible to import all classes in a package at once. • If you chose to do this, you would use the * as a wildcard: • import java.awt.*;

  38. The next segment of interest in the documentation is entitled “Field Summary”. • In the documentation, what are referred to in these notes as instance variables are referred to as fields. • In other words, you discover from this documentation that an object created from the Point class will have two instance variables, an x coordinate and a y coordinate. • These instance variables are given the type “int”, which signifies that these coordinates can take on integer values.

  39. The next segment in the documentation is entitled “Constructor Summary”. • Constructors are special pieces of code used for creating instances of classes. • Constructors have a form reminiscent of methods—they have a name followed by a set of parentheses which may or may not contain parameters.

  40. Constructors are not methods. • Their name is the same as the class they belong to. • As you can see, a single class may have more than one constructor. • The system can tell them apart because they have different parameter lists. • In the documentation the types of the parameters are shown. • For the time being we will restrict our attention to examples with parameters of the type “int”.

  41. The last segment of the documentation is the “Method Summary”. • This gives all of the methods by name and all of their parameters by name, including their types. • There can be different methods with the same name. • The system tells them apart by their parameter lists. • It is only through these methods that a program can affect the instance variables, the x and y coordinates, of a Point object that has been created.

  42. As you can see, int and double are two different numeric types. • The meaning of types will be covered in the next unit. • For the time being, examples will be restricted to the int, or integer type. • This type can hold whole number values.

  43. 2.4 Creating and Using Objects

  44. Writing the code for classes of your own will come later. • At this time it is possible to understand and correctly write lines of code and small, complete programs that construct objects from system supplied classes and then make use of them.

  45. Assuming that the Point class has been imported into a program, the following two lines of code 1) Declare a name which can be used for a Point object, and 2) Construct an object and give it that name, using the “=” sign, or assignment to do so: • Point myPoint; • myPoint = new Point(10, 20);

More Related