



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































algorithm A finite set of clearly stated executable steps that will eventually terminate producing a desired outcome. In programming courses we learn to write these algorithms in a programming language, but the intention is the same. American Standard Code for Information Interchange (ASCII) A code that specifies a mapping between characters and a corresponding byte to represent it. According to ASCII. This encoding is very widely used, but Java uses Unicode instead. Character Byte Decimal 'A' 01000001 65 'B' 01000010 66 etc. 'a' 01100001 97 'b' 01100010 98 etc. '1' 00110001 49 '2' 00110010 50 etc. ' ' 00100000 32 '(' 00101000 40 assignment statement This statement, in the form :
bit : Short for b inary dig it. A 0 or a 1. boolean One of the eight primitive data types supported by Java. We can set a variable of this type to hold either ‘true’ or ‘false’. Example: boolean finished = false; and then at some point later in our code we might under some situation use the assignment statement: finished = true; to set the control variable. We might use such a variable to control a selection structure: if (finished) { System.out.println(“Goodbye”); } byte : A collection of 8 bits viewed as an entity. A single byte can hold any one of 256 values, since 2^8 = 256 (i.e., 00000000, 00000001, etc. 11111111) class A class is a “kind of thing” (example Human). A specific instance of a class is called an object. By coding convention, in Java we spell class names with a capital letter at the start of each word. For example: String, Human, HelloWorld. compiler A program that reads source code files (“java” files) and produces object code files. The truth is that in Java the output of the Java compiler (called “javac”) is not in fact machine code instructions but rather is a file of “byte codes” (which are similar to machine code instructions found on most machines). These Java byte codes (the files produced are “class” files) are NOT specific to any given machine, thus they are not “directly executable” by any machine. At runtime we load another program called an “interpreter” that will then actually cause the Java byte codes to be executed on a given machine. The Java interpreter is called “java”. My Source code: X.java javac X.class java runs on a given machine. Fact: X.class code is platform independent!! A major virtue on Internet!
I/O : An acronym for Input/Output. Input: Keyboard, mouse, modem Output: Monitor, modem IDE (Integrated Development Environment) A working environment in which we can develop and debug programs. It integrates the editing, compiling and running phases. It also provides extensive online documentation regarding the language. IDE’s also support a visual development environment - we can see what the graphical aspects of our program will look like as we write it. JGrasp, Eclipse and Jbuilder are examples of an IDE. identifiers Identifiers are used to name things in Java. They can name constant values (e.g. PI), they are used to name methods(functions), they are used to name simple variables, they are used to name object references and they are used to name classes. In Java a valid identifier must begin with an alphabetic character, the $, or the ‘’ character and is then followed by one or more of the following: (a) the "" or ”$” character, (b) an alphabetic character, or (c) a digit. You may not have a space embedded in a name. Thus "myCost" is okay but "my Cost" is not okay. Java is case-sensitive meaning that if you type "myCost" and "MyCost" these are treated as different names! Capitalization conventions: (a) class names (each word in identifier starts with a capital letter). Ex: MyClass (b) variable and object names (each word begins with a cap, except the first). Ex. myVariable, myObject (c) constants (Spelled in all caps with an “_” between words). Ex: PI or MY_COST (d) method (function) names (each word begins with a cap except the first). Ex. myMethod(..) (e) Java keywords (all lower case). Ex: public, class, static, void, int, if, etc. int/byte/short/long Java primitive data types that are used to hold integers. Note, however, that the int data type is not capable of accurately imitating integers from mathematics. In Java ints have a limited range. On our system they are limited from -2^31 to +2^31 (since ints get 4 bytes – 32 bits). This limitation can lead to overflow problems. Byte gets 1 byte of storage, short gets 2 bytes, and long gets 4 bytes. integer division This is division done by the ‘/’ operator if both operands are of type ‘int’. The division is performed and the result is truncated to an integer value. E.g. 7 / 3 yields 2. interpreter A program which we load so that it can read a “program” in some form that cannot be directly executed on a given machine. The interpreter will make
this “program” run on the given machine by converting the instructions into something that can be run on the given machine. For obvious reasons code that is being “interpreted” will not run as fast as code which was compiled to directly executable machine code instructions. In Java the interpreter program is called “java”. That program reads “.class” files (Java byte codes) that were “compiled” by javac. java.lang A Java package that comes with the JDK. It is the ONLY Java package that is automatically imported into every program by the compiler. Any class defined within java.lang is available for use within our program without any need for an import statement. For example, java.lang defines the classes called System, String, Integer (and 7 other wrapper classed), and Math (Note: both begin with a capital letter because they are class names!) Other classes that are part of the JDK require us to explicitly use an import statement to bring in the package that includes the definition of the desired class. The JDK comes with a LOT of packages of utility classes that allow us to do powerful things easily, if we will invest enough effort to study and investigate the classes defined within those packages. There is an enormous amount of online help available about all of these packages, classes and the methods made available by those classes. Read!!! There is gold in that documentation. Java A high level object oriented programming language that was first developed in 1993 by a team from Sun Corporation (led by James Gosling). The language has become extremely popular because it offers a number of features that set it apart from other programming languages. It is said to be platform independent (meaning that the same Java byte codes can run on various machines with different operating systems). The language is also geared for Internet use as it offers different levels of “security” that make it a good choice for internet applications. java The name of the Java interpreter that comes with the JDK. This is a program. It is machine specific! Thus one would get one version of the Java interpreter to run on a Pentium-Windows machine, and one would get yet a different version of the interpreter to run on a Mac or on a Unix box. This program reads the (platform independent) Java byte codes (a “.class” file) and implements what they mean on the given machine/OS. X.class java runs on given machine Java application This is a Java program that runs on your machine as a stand alone program. Such Java classes must have a “main” method to be used as a program, where that main method looks like: public static void main(String[] args) { code… }
Pentium 4 systems are rated as 3 Gigahertz (3000 Mhz). By the way, the original IBM PC was 4.7 MHz! modulus (mod) operator This is an integer or float operator that takes one value and divides it by the second and returns the remainder (the quotient itself is not the issue). In Java the ‘%’ symbol is used for the modulus operator. For example: 4 % 7 yields 4, 12 % 7 yields 5; 30 % 7 yields 2; 21 % 7 yields 0. object An object is an instance of a class. Example: bob is an object of class Human (i.e. bob is a specific Human). object code The version of a program that can be executed on a machine. This version is not easily readable by humans. Object Oriented Programming (OOP) This is a style of programming that focuses on the creation of objects of different classes and the tight binding of objects to the code that performs the methods offered by a given class. We will see that in much of our code we will create objects and then ask those objects to perform some function (“method”) that they know how to do. In object oriented programming there is a heavy focus on data and we think of the data that makes up an object as being “active” (i.e. able to “perform” some function if asked to). Making a simple analogy: we create an object that we will reference by the name of ‘john’ of class HumanBeing. We then could ask john to perform any method that objects of the HumanBeing class know how to do: john.goBuy(car). Here we tell the object john to perform the method “goBuy” and we supply a parameter called car telling him what to buy. This style of programming can be contrasted to the widely used “imperative programming” where we focus on telling the computer a sequence of verbs to perform and data takes a secondary role. You shall see that OOP proves to be very flexible in many situations. order of operations In the evaluation of an expression some operators have higher precedence over others (in the absence of parentheses). In the case of Java for arithmetic operations, the order of operations says do operations in parentheses first, then multiplicative operations (left to right) and then additive operations. output device Any device that allows a computer to display or otherwise project any data that it wants to show. This includes monitors and printer. overflow A computer error that occurs as a consequence of limited storage space for numeric values. Computers due to this ‘finite space’ limitation can only represent integer (int) or real (float) numbers that are so big. Due to this
finite limit there does exist a largest and a smallest number that any numeric variable type can represent. If we go above that max or below that minimum (a large negative number) in a computation then have experienced an overflow error (this is a runtime problem). An example of this can be seen in the fact that ints use 4 bytes (32 bits) so they can only be used to hold numbers from about -2^31 to + 2^31 and if we keep adding one to a variable it will eventually exceed 2^31 (thus an overflow error). programming comments Lines of text added to a program to enlighten a reader. These may be used to identify the author, the date, the purpose of a program. They may also be used to shed some light on a line of code that may be tricky or to identify that a chunk of code that follows is to serve some stated purpose. To help the reader understand!! In Java all text on a given line that follows a ‘//’ is treated as a comment. One can also use ‘/’ .... ‘/’ to form multi-line comments. However, the later symbols are usually reserved for commenting out a piece of commented code as you can have a ‘//’ comment within a /.../ comment. Polya problem solving Polya, in 1957, proposed a "general problem solving methodology" that consisted of four steps. These steps prove to be very useful in approaching problems whose solution we plan on programming. His four steps were (a) recognize and fully understand the problem to be solved, (b) devise a good plan to solve the problem, (c) implement the plan, and (d) evaluate the implementation for its correctness and quality. A careful comparison of these four steps shows similarities to the phases of the software life cycle. program A sequence of instructions to be executed by a machine to solve a problem. The terms "source code" and "object code" are used to refer to two versions of the same program. The programmer writes the source code version in a programming language and then a compiler converts that to the object code version that is actually executable. In the Windows world such object code programs are designated by an EXE file suffix. Example: PROGRAM1.EXE. The Java compiler (javac) actually creates byte codes that must be interpreted (not machine code instructions). programming languages -- (High Level) A language created to allow humans to state their solutions to problems. These languages are usually somewhat "English-like" though they may have very strict rules about syntax. A programmer uses an editor to write a program in such a language, thus creating source code, and then submits that program to a compiler that then converts the program into a form that can be executed by the machine itself (or by a byte code interpreter in the case of Java). Examples of high level programming languages include: Pascal, BASIC, FORTRAN, COBOL, C, C++, Smalltalk, PL/I, Lisp, Ada, C# and Java. Hundreds of such languages exist.
contents each time the power is removed. Secondary memory could be implemented in a variety of technologies, but the most prevalent now is magnetic. Both hard disks and thumb-drives are examples of secondary memory. software life cycle (SLC) The steps that a program evolves through from its "birth" to its "death". Six phases are: (a) requirements specification (SLC - 1) A formal statement of the complete nature of the problem to be solved. This may involve understanding some existing manual solution from which a computer solution is to be devised. This document can act as a kind of "contract" between the customer and the software developer. (b) analysis (SLC -2) Study the specifications to fully understand what is to be input, what is to be done with it, and what is to be output. Consider different approaches that one might take to achieving the solution. Of the choices select the "best". This might be based on any one of a number of criteria. For example: (a) easiest/fastest to implement (b) the solutions that will ultimately run the fastest, or (c) the solution that provides the prettiest output. (c) design (SLC -3) Find and describe a solution to the problem. This process often involves a series of steps. One may state an overall solution in very general "abstract" terms and then proceed to refine the solution by then describing how each of those abstractly stated steps would in fact be achieved. This in turn is leading to a more detailed description of the steps involved. Thus through a series of steps we can go from the abstract solution to a very detailed solution. The abstract solution reflects our first level of understanding while the later detailed versions represent something that can more easily be coded as it is more detailed. This approach to design is called "stepwise refinement". Design (not coding) is the hard part! The efforts of design are documented by using some design notation. A variety of such notations exist, some of these include: flow charts, object modeling notations (e.g. UML – Unified Modeling Language), structure charts, and pseudo code. Which design notation you use depends on the style of programming you are using (e.g., object oriented versus procedural). Avoid the temptation to short change this phase of the software life cycle. (d) implementation (SLC -4) Convert the design for the solution into a high level programming language. Remove all of the syntax errors.
(e) testing and verification (SLC -5) Methodically construct good test cases to be applied to the software to "put it through its paces". This must be carefully done to assure that the code has been well tested. The answer is enough good tests not a lot of unstructured tests. Testing does not "prove" that a program is correct, it simply increases your confidence that it is correct. (f) maintenance (SLC -6) After a program has been delivered to the "customer" it may still need to be changed. These changes could include: fixing bugs that are found "late" (i.e. by the customer), adding new features to the program to meet new demands, or modifying the existing code to suit it to a changing environment. It surprises many people that often better than 60% of a program's life is spent in this phase of the software life cycle. This phase is made substantially easier if the earlier phases were well done! This translates to "it is a lot easier to maintain a well designed program than one that was kludged together". source code A sequence of instructions written in a high level programming language (e.g., Java) to solve a problem. This version is not executable, but is easily read and edited (e.g. your Name.java file). syntax error When the compiler detects that you have violated one of the syntax rules of the language you get an error message explaining the nature of your mistake. These mistakes are generally fairly easy to correct. (Examples: '";" expected here', or "Unknown identifier"). system clock All events in a computer system happen at discrete points in time. The system clock provides the timed signals (like a drum beat) that allow all system components to synchronize their activities. Unicode Unicode is a 16-bit (2 byte) encoding scheme used by Java to represent character data. Unicode was created to solve the problem with the older ASCII char encoding scheme. The ASCII system used only 1 byte per character, and as a consequence could only represent 2^8 (256) different character. With Unicode we can represent 2^16 (65,536) different characters – thus giving us a bit represent for a MUCH broader set of characters – allowing us now to have representations for some international characters that previously had no ASCII representation. Unicode is in a sense backward compatible with ASCII. For example, for each of the 256 characters that ASCII does represent, the Unicode version of that character is the 0-byte followed by the ASCII byte. For example, ‘A’ in ASCII is ‘0100 0001’ and is ‘0000 0000 0100 0001’ in Unicode.
boolean constants These are the values true and false. In Java they are language keywords. boolean (or logical) expressions A Boolean expression can be a Boolean constant, a Boolean variable, a relational expression or any of these connected by Boolean operators. boolean (or logical) operators These are operators that work on Boolean values and yield a Boolean value. The three most common Boolean operators are AND (&& in Java), OR (|| in Java) and NOT (! in Java). See Truth Tables for a description of the outcome of the application of these Boolean operators. boolean short-circuit evaluation In some cases, after we evaluate the first component of a Boolean expression we ‘know’ the outcome without having to even evaluate the second component. Skipping the evaluation of such a component is called Boolean short-circuited evaluation. For example in P && Q if P is FALSE then the result will be FALSE and we can skip evaluation Q, and for P || Q if P is TRUE the entire expression will be true so we could skip evaluation of Q. boolean type This is one of the 8 primitive data types supported by Java. 1 byte of storage used. break statement Each clause of a switch-statement usually ends with a “break;” statement. If one leaves this out then after the given code for that clause is executed, the execution then “falls into” executing the code for the next clause of the switch statement. Thus, you almost always want to use a break statement at the end of each switch clause!! However, as it comes last one need not use a break statement at the end of the default clause of the switch statement if you have one. code comments Java supports two forms of user comments within a program. One of these begins with “//” and includes all text to the end of that line. These are called one-line comments. An alternative used begins with “/” and goes until a “/” is encountered (potentially many lines). This latter form of comment is called a multi-line comment. Multi-line comments may include single line comments within them. The Java compiler (javac) ignores and removes ALL comments! We add source code comments to (a) document the file itself e.g. dates, names, authors, etc and (b) to explain what the code is doing. These are added only for other programmers, but their importance should not be overlooked! Hint: in code you wrote a LONG time ago the comments will be helpful to you to remind even you of what you were thinking when you wrote the code. One should avoid over-commenting code as it can in some cases actually impede the reading of the code. In general you should assume that your reader knows Java, but may not know what your algorithm is trying to do… so explain. class
A Java keyword used to mark the beginning of a class definition. A class definition consist of five types of components: (1) Class variables (primitive data types or objects) – uses ‘static’ in declaration (2) Instance variables (3) Constructors (A special kind of method – same name as class itself – used to create and initialize an object of the given class), (4) Class methods – uses static in the method header (5) Instance methods Class names themselves are by our convention, spelled with a capital letter at the beginning of each word. Example: class MyClass { // 1. Class Variables here – note use of “static”. static countOfMyClassObjects = 0; // inc 1 for each made. // 2. Instance variables here – note no “static”. int myCounter; // 3. Constructors. More than one is okay if “signatures” differ. MyClass() { // Default constructor – works with no parameters. Etc. } // end of Default constructor // 4. Class methods here – note use of “static” in header. public static void classMethod(…) { … code … } // 5. Instance methods here – note lack of “static”. public int getCount () { … code … } } // end of class MyClass _ class method/instance method_** Methods defined within a given class (excluding constructors) are either class methods or instance methods (we can spot the difference since all the class methods use the keyword ‘static’ In their headers). Instance methods are methods that we view as being executed by a given object of our class. This means that if our class is called MyClass and we create an instance of that class (called an object of that class), say myObject (note capitalization), then if we would use the line: myObject.someInstanceMethod(…) then the method is being “performed by” the designated object and the method is an instance method. It is as if the named object is the “actor” who executes the instance method. Hence inside of such a method if instance variables are used we are referring to instance variables of the “actor”. Class methods are made available for use but are not viewed as being executed by any particular object. In our code if we wanted to invoke such a class method we would use a line like: MyClass.classMethod(…); Note the difference in their invocations: In one case we ask a given object to perform the method (it is an instance method) in the other case we ask the class itself to perform the method (it is a class method). _ class variable/instance variable_** A class definition can include both class and instance variables. We can spot the difference since any class variable must use the keyword ‘static’ in its
constructor that works with a single int parameter). “myObject” will then reference that newly created object. By analogy, if we use the line: Human john = new Human(); then john is an object reference to an object of class Human and it refers to the actual Human that was just created. Try to be clear on the difference between the object itself and the name that we use to refer to the object. counting loop This is a loop that is controlled by a variable which we increment or decrement until a specific value is reached. It can be done by a while-loop, but is nicely handled by a for-loop. dangling-else problem This ‘problem’ arises when we have an if-then-else nested within an if-then. Consider: if (count < 100) if (count < 10) System.out.println(“Line 1”); else // indentation is fooling you here! System.out.println(“Line 2”); If count has a value of 150 what does the above code print? Answer: NOTHING! Despite the indentation used above, the else is “binding” to the second if-clause, NOT the first one! Java uses a rule that states that an ‘else’ in the language will bind to the most recent unclosed if. If we want a given else to bind to an ‘earlier’ if, then we must be sure that all previous ifs have been properly ‘closed’ (e.g., by having its own else or by being inside of {...}. For example we can “fix” the above code to have the else-clause bind to the first if-clause instead of the second by using “{}” as follows: if (count < 100) { if (count < 10) System.out.println(“Line 1”); } else // In this version indentation is now correct! System.out.println(“Line 2”); This version will print “Line 2” if count has a value of 150. data dictionary A table used to document the variables that we will use in our program. This table includes the identifier used, its type (and thus implicitly the number of bytes that will be reserved for that variable), and an “explanation” of the intended use of the variable. Clearly with meaningful identifiers this explanation just supplements the name selected. debugging The act of examining error messages and code output to locate the source of a coding related error. These “bugs” are normally classified as: “syntax” (caught at compile time), runtitme errors (flagged at runt time) or logic error that may result in a program running but producing incorrect output. The last of these three is the most difficult to spot and fix. Some IDE’s have built-in support to aid in the task of debugging.
decision structure (“selection”) A linguistic structure that allows us, based on a stated criteria to execute an appropriate block of code, skipping any inappropriate code. Examples of this include the if-then, the if-then-else, and the switch structures. forever loop We may in some circumstances be running a program that is supposed to, in essence run forever. Such a program might be one that does process control. It is not intended to be stopped (though on occasions it might be). In Java we can achieve such a loop by: while(true) statement; In this code the given statement (possibly a compound statement) would be executed ‘forever’. In some cases the loop itself might contain as exit-statement (i.e. a break or return that would in fact stop the execution) -- but from the criteria stated with the loop it appears to be a forever-loop. if-then A linguistic structure that will execute a given block of code only if a given criteria is met. if-then-else A linguistic structure that will execute one of two given blocks of code based on a given criteria being met or not. import A Java keyword used to indicate that a given prewritten class (or collection of classes) is to be brought into this file for use. We use this to bring in the libraries of Java code that come with the JDK, or for that matter to bring in classes of our own that we might have developed in prior projects. To bring in a single class such as the BufferedReader class, we identify the name of the package that holds the class and then the class name itself. Thus the line: import java.io.BufferedReader; would mean to bring in the class “BufferedReader” from the package called “java.io”. The java.io package actually holds a number of classes, thus we might end up importing several classes from that same package. Instead of having a separate import statement for each class we import from that package we can use a wildcard feature to write: import java.io.*; This means to import all the classes in the java.io package. The import statement(s) should be put in the file at the very top (or after a package statement if one is used). The package java.lang (and all of the classes it defines – such as String and System) is automatically brought into any program, but any other package must have its classes explicitly imported. infinite loop This is a situation where a loop body continues to execute over and over and the exit criteria for the loop is never satisfied. This occurrence of this problem is not detected by the system (since it cannot differentiate between an infinite loop and one that executes ‘lots’ of times). This problem usually represents a logic error on the programmers part (but conceivably might have been intentional – see forever-loop). Java special operators
type MyClass, but that it does not actually refer to any such object yet. In fact, it is probably not a bad idea for all object declarations to either be of the form: MyClass myObject = null; or MyClass my Object = new MyClass(…); We can also later in our program use null to check to see if a given object reference has been set to refer to an object yet or not. For example: if (myObject == null) { …code if myObject not set yet… } else { …code if myObject does refer to an object… } object reference In Java when we declare a variable to be one of the eight primitive data types the variable name itself refers to the storage cell in main memory that holds the given data item. When we declare a variable to be some kind of object, the variable is used to hold a 4-byte reference to an object of the given class and could in fact be null if we have not yet asked for an object of that class to be constructed. int simpleVariable = 35; String myString = new String(“Hello World”); +----+ +----+ | 35 | versus | *-------->”Hello World” (A String Object) +----+ +----+ that resides on the heap. simpleVariable myString 4-bytes for int 4 bytes to hold object reference. operator associativity Operator associativity is either “left” or “right”. This associativity tells us which operation to do first if we find two consecutive operators of the same precedence. For example if we have 5
is possible that that the loop body will be executed zero of more times. In Java the while-loop and the for-loop are both examples of a pre-test loop. prefix operator An operator that is placed before its operand. For example: ++i x = 5; y = ++x; // Now x holds 6 and y holds 6! Increment x before value returned postfix operator An operator that is placed after its operand. For example: i++ x = 5; y = x++; // Now x holds 6 and y holds 5! Increment x after value returned post-test loop These are looping structures (where some instructions may get executed more than once) where the “test” to determine if the loop-body instructions need to be executed again or not is made after the loop. As a result of such a test occurring after executing the loop body, we know that the loop body will be executed one of more times. In Java the do-while-loop is an example of a post-test loop. relational operators These are operators used to compare expressions of comparable types and yield a true if the relationship holds and a false otherwise. In Java the relational operators are: <, <=, >, >=, !=, and == (two = signs not 1. 1 = is the assignment operator). relational expression Expression1 relationalOperator Expression2 forms a relational expression that evaluates to a true or false depending on the value of the two expressions and the given relational operator. Example: (x+y) < 10*(z/x) is a relational expression. sentinel value A value used to ‘mark’ the end of an input stream or the end of some collection of input values. For example, read user input values until user type a ‘-1’. The -1 is being used as a sentinel value. Usually the sentinel value is not considered to be one of the input values to be processed but rather is only used as a marker. In some cases it might be possible that a known valid input data item will always be placed last, in which case the sentinel value might also be a valid data item. static A Java keyword used with either variables or methods to designate that the variable or method is a class variable or class method. Any variable or method that lacks this keyword is an instance variable or method. switch (or case) A linguistic structure that may execute one of several given blocks of code based on the value of a given expression. This may include a block of default code to be executed if the given expression does not match any of the specified values. Usually each clause of a switch statement ends with a break-statement! tracing execution