archive-edu.com » EDU » H » HWS.EDU

Total: 727

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Javanotes 7.0, Section 1.1 -- The Fetch and Execute Cycle: Machine Language
    in detail The CPU executes a program that is stored as a sequence of machine language instructions in main memory It does this by repeatedly reading or fetching an instruction from memory and then carrying out or executing that instruction This process fetch an instruction execute it fetch another instruction execute it and so on forever is called the fetch and execute cycle With one exception which will be covered in the next section this is all that the CPU ever does The details of the fetch and execute cycle are not terribly important but there are a few basic things you should know The CPU contains a few internal registers which are small memory units capable of holding a single number or machine language instruction The CPU uses one of these registers the program counter or PC to keep track of where it is in the program it is executing The PC simply stores the memory address of the next instruction that the CPU should execute At the beginning of each fetch and execute cycle the CPU checks the PC to see which instruction it should fetch During the course of the fetch and execute cycle the number in the PC is updated to indicate the instruction that is to be executed in the next cycle Usually but not always this is just the instruction that sequentially follows the current instruction in the program A computer executes machine language programs mechanically that is without understanding them or thinking about them simply because of the way it is physically put together This is not an easy concept A computer is a machine built of millions of tiny switches called transistors which have the property that they can be wired together in such a way that an output from one switch

    Original URL path: http://math.hws.edu/javanotes/c1/s1.html (2016-02-07)
    Open archived version from archive


  • Javanotes 7.0, Section 1.2 -- Asynchronous Events: Polling Loops and Interrupts
    pressed processing it and then returning to the task it was performing before you pressed the key Again you should understand that this is a purely mechanical process A device signals an interrupt simply by turning on a wire The CPU is built so that when that wire is turned on the CPU saves enough information about what it is currently doing so that it can return to the same state later This information consists of the contents of important internal registers such as the program counter Then the CPU jumps to some predetermined memory location and begins executing the instructions stored there Those instructions make up an interrupt handler that does the processing necessary to respond to the interrupt This interrupt handler is part of the device driver software for the device that signaled the interrupt At the end of the interrupt handler is an instruction that tells the CPU to jump back to what it was doing it does that by restoring its previously saved state Interrupts allow the CPU to deal with asynchronous events In the regular fetch and execute cycle things happen in a predetermined order everything that happens is synchronized with everything else Interrupts make it possible for the CPU to deal efficiently with events that happen asynchronously that is at unpredictable times As another example of how interrupts are used consider what happens when the CPU needs to access data that is stored on a hard disk The CPU can access data directly only if it is in main memory Data on the disk has to be copied into memory before it can be accessed Unfortunately on the scale of speed at which the CPU operates the disk drive is extremely slow When the CPU needs data from the disk it sends a signal to the disk drive telling it to locate the data and get it ready This signal is sent synchronously under the control of a regular program Then instead of just waiting the long and unpredictable amount of time that the disk drive will take to do this the CPU goes on with some other task When the disk drive has the data ready it sends an interrupt signal to the CPU The interrupt handler can then read the requested data Now you might have noticed that all this only makes sense if the CPU actually has several tasks to perform If it has nothing better to do it might as well spend its time polling for input or waiting for disk drive operations to complete All modern computers use multitasking to perform several tasks at once Some computers can be used by several people at once Since the CPU is so fast it can quickly switch its attention from one user to another devoting a fraction of a second to each user in turn This application of multitasking is called timesharing But a modern personal computer with just a single user also uses multitasking For example the user might be

    Original URL path: http://math.hws.edu/javanotes/c1/s2.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Section 1.3 -- The Java Virtual Machine
    into machine language but it is a machine language for a computer that doesn t really exist This so called virtual computer is known as the Java Virtual Machine or JVM The machine language for the Java Virtual Machine is called Java bytecode There is no reason why Java bytecode couldn t be used as the machine language of a real computer rather than a virtual computer But in fact the use of a virtual machine makes possible one of the main selling points of Java the fact that it can actually be used on any computer All that the computer needs is an interpreter for Java bytecode Such an interpreter simulates the JVM in the same way that a C64 emulator simulates a Commodore 64 computer The term JVM is also used for the Java bytecode interpreter program that does the simulation so we say that a computer needs a JVM in order to run Java programs Technically it would be more correct to say that the interpreter implements the JVM than to say that it is a JVM Of course a different Java bytecode interpreter is needed for each type of computer but once a computer has a Java bytecode interpreter it can run any Java bytecode program and the same program can be run on any computer that has such an interpreter This is one of the essential features of Java the same compiled program can be run on many different types of computers Why you might wonder use the intermediate Java bytecode at all Why not just distribute the original Java program and let each person compile it into the machine language of whatever computer they want to run it on There are several reasons First of all a compiler has to understand Java a complex high level language The compiler is itself a complex program A Java bytecode interpreter on the other hand is a relatively small simple program This makes it easy to write a bytecode interpreter for a new type of computer once that is done that computer can run any compiled Java program It would be much harder to write a Java compiler for the same computer Furthermore some Java programs are meant to be downloaded over a network This leads to obvious security concerns you don t want to download and run a program that will damage your computer or your files The bytecode interpreter acts as a buffer between you and the program you download You are really running the interpreter which runs the downloaded program indirectly The interpreter can protect you from potentially dangerous actions on the part of that program When Java was still a new language it was criticized for being slow Since Java bytecode was executed by an interpreter it seemed that Java bytecode programs could never run as quickly as programs compiled into native machine language that is the actual machine language of the computer on which the program is running However this problem has

    Original URL path: http://math.hws.edu/javanotes/c1/s3.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Section 1.4 -- Fundamental Building Blocks of Programs
    have commands for getting data into and out of variables and for doing computations with data For example the following assignment statement which might appear in a Java program tells the computer to take the number stored in the variable named principal multiply that number by 0 07 and then store the result in the variable named interest interest principal 0 07 There are also input commands for getting data from the user or from files on the computer s disks and there are output commands for sending data in the other direction These basic commands for moving data from place to place and for performing computations are the building blocks for all programs These building blocks are combined into complex programs using control structures and subroutines A program is a sequence of instructions In the ordinary flow of control the computer executes the instructions in the sequence in which they occur in the program one after the other However this is obviously very limited the computer would soon run out of instructions to execute Control structures are special instructions that can change the flow of control There are two basic types of control structure loops which allow a sequence of instructions to be repeated over and over and branches which allow the computer to decide between two or more different courses of action by testing conditions that occur as the program is running For example it might be that if the value of the variable principal is greater than 10000 then the interest should be computed by multiplying the principal by 0 05 if not then the interest should be computed by multiplying the principal by 0 04 A program needs some way of expressing this type of decision In Java it could be expressed using the following if statement if principal 10000 interest principal 0 05 else interest principal 0 04 Don t worry about the details for now Just remember that the computer can test a condition and decide what to do next on the basis of that test Loops are used when the same task has to be performed more than once For example if you want to print out a mailing label for each name on a mailing list you might say Get the first name and address and print the label get the second name and address and print the label get the third name and address and print the label But this quickly becomes ridiculous and might not work at all if you don t know in advance how many names there are What you would like to say is something like While there are more names to process get the next name and address and print the label A loop can be used in a program to express such repetition Large programs are so complex that it would be almost impossible to write them if there were not some way to break them up into manageable chunks Subroutines provide one way to

    Original URL path: http://math.hws.edu/javanotes/c1/s4.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Section 1.5 -- Objects and Object-oriented Programming
    some data along with some subroutines for manipulating that data For example a mailing list module might contain a list of names and addresses along with a subroutine for adding a new name a subroutine for printing mailing labels and so forth In such modules the data itself is often hidden inside the module a program that uses the module can then manipulate the data only indirectly by calling the subroutines provided by the module This protects the data since it can only be manipulated in known well defined ways And it makes it easier for programs to use the module since they don t have to worry about the details of how the data is represented Information about the representation of the data is hidden Modules that could support this kind of information hiding became common in programming languages in the early 1980s Since then a more advanced form of the same idea has more or less taken over software engineering This latest approach is called object oriented programming often abbreviated as OOP The central concept of object oriented programming is the object which is a kind of module containing data and subroutines The point of view in OOP is that an object is a kind of self sufficient entity that has an internal state the data it contains and that can respond to messages calls to its subroutines A mailing list object for example has a state consisting of a list of names and addresses If you send it a message telling it to add a name it will respond by modifying its state to reflect the change If you send it a message telling it to print itself it will respond by printing out its list of names and addresses The OOP approach to software engineering is to start by identifying the objects involved in a problem and the messages that those objects should respond to The program that results is a collection of objects each with its own data and its own set of responsibilities The objects interact by sending messages to each other There is not much top down in the large scale design of such a program and people used to more traditional programs can have a hard time getting used to OOP However people who use OOP would claim that object oriented programs tend to be better models of the way the world itself works and that they are therefore easier to write easier to understand and more likely to be correct You should think of objects as knowing how to respond to certain messages Different objects might respond to the same message in different ways For example a print message would produce very different results depending on the object it is sent to This property of objects that different objects can respond to the same message in different ways is called polymorphism It is common for objects to bear a kind of family resemblance to one another Objects that contain the same

    Original URL path: http://math.hws.edu/javanotes/c1/s5.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Section 1.7 -- The Internet and Beyond
    your computer sends a message to a domain name server to find out the corresponding IP address Then your computer uses the IP address rather than the domain name to communicate with the other computer The Internet provides a number of services to the computers connected to it and of course to the users of those computers These services use TCP IP to send various types of data over the net Among the most popular services are instant messaging file sharing electronic mail and the World Wide Web Each service has its own protocols which are used to control transmission of data over the network Each service also has some sort of user interface which allows the user to view send and receive data through the service For example the email service uses a protocol known as SMTP Simple Mail Transfer Protocol to transfer email messages from one computer to another Other protocols such as POP and IMAP are used to fetch messages from an email account so that the recipient can read them A person who uses email however doesn t need to understand or even know about these protocols Instead they are used behind the scenes by computer programs to send and receive email messages These programs provide the user with an easy to use user interface to the underlying network protocols The World Wide Web is perhaps the most exciting of network services The World Wide Web allows you to request pages of information that are stored on computers all over the Internet A Web page can contain links to other pages on the same computer from which it was obtained or to other computers anywhere in the world A computer that stores such pages of information is called a web server The user interface to the Web is the type of program known as a web browser Common web browsers include Internet Explorer Firefox Chrome and Safari You use a Web browser to request a page of information The browser sends a request for that page to the computer on which the page is stored and when a response is received from that computer the web browser displays it to you in a neatly formatted form A web browser is just a user interface to the Web Behind the scenes the web browser uses a protocol called HTTP HyperText Transfer Protocol to send each page request and to receive the response from the web server Now just what you might be thinking does all this have to do with Java In fact Java is intimately associated with the Internet and the World Wide Web When Java was first introduced one of its big attractions was the ability to write applets An applet is a small program that is transmitted over the Internet and that runs on a web page Applets make it possible for a web page to perform complex tasks and have complex interactions with the user Alas applets have suffered from a variety of

    Original URL path: http://math.hws.edu/javanotes/c1/s7.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Quiz on Chapter 1
    What is the difference between a compiler and an interpreter Question 4 Explain the difference between high level languages and machine language Question 5 If you have the source code for a Java program and you want to run that program you will need both a compiler and an interpreter What does the Java compiler do and what does the Java interpreter do Question 6 What is a subroutine Question

    Original URL path: http://math.hws.edu/javanotes/c1/quiz.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Section 2.5 -- Details of Expressions
    if the value of x is 6 the statement y x will change the value of x to 7 but it will change the value of y to 6 since the value assigned to y is the old value of x On the other hand the value of x is defined to be the new value of x after the 1 is added So if x is 6 then the statement y x changes the values of both x and y to 7 The decrement operator works in a similar way Note in particular that the statement x x does not change the value of x This is because the value that is being assigned to x is the old value of x the one that it had before the statement was executed The net result is that x is incremented but then immediately changed back to its previous value You also need to remember that x is not the same as x 1 The expression x changes the value of x the expression x 1 does not This can be confusing and I have seen many bugs in student programs resulting from the confusion My advice is Don t be confused Use and only as stand alone statements not as expressions I will follow this advice in almost all examples in these notes 2 5 3 Relational Operators Java has boolean variables and boolean valued expressions that can be used to express conditions that can be either true or false One way to form a boolean valued expression is to compare two values using a relational operator Relational operators are used to test whether two values are equal whether one value is greater than another and so forth The relational operators in Java are and The meanings of these operators are A B Is A equal to B A B Is A not equal to B A B Is A less than B A B Is A greater than B A B Is A less than or equal to B A B Is A greater than or equal to B These operators can be used to compare values of any of the numeric types They can also be used to compare values of type char For characters and are defined according the numeric Unicode values of the characters This might not always be what you want It is not the same as alphabetical order because all the upper case letters come before all the lower case letters When using boolean expressions you should remember that as far as the computer is concerned there is nothing special about boolean values In the next chapter you will see how to use them in loop and branch statements But you can also assign boolean valued expressions to boolean variables just as you can assign numeric values to numeric variables And functions can return boolean values By the way the operators and can be used to compare boolean values too This is occasionally useful For example can you figure out what this does boolean sameSign sameSign x 0 y 0 One thing that you cannot do with the relational operators and is to use them to compare values of type String You can legally use and to compare Strings but because of peculiarities in the way objects behave they might not give the results you want The operator checks whether two objects are stored in the same memory location rather than whether they contain the same value Occasionally for some objects you do want to make such a check but rarely for strings I ll get back to this in a later chapter Instead you should use the subroutines equals equalsIgnoreCase and compareTo which were described in Subsection 2 3 3 to compare two Strings Another place where and don t work as you would expect is with Double NaN the constant that represents an undefined value of type double The values of x Double NaN and x Double NaN are both defined to be false in all cases whether or not x is Double NaN To test whether a real value x is the undefined value Double NaN use the boolean valued function Double isNaN x 2 5 4 Boolean Operators In English complicated conditions can be formed using the words and or and not For example If there is a test and you did not study for it And or and not are boolean operators and they exist in Java as well as in English In Java the boolean operator and is represented by The operator is used to combine two boolean values The result is also a boolean value The result is true if both of the combined values are true and the result is false if either of the combined values is false For example x 0 y 0 is true if and only if both x is equal to 0 and y is equal to 0 The boolean operator or is represented by That s supposed to be two of the vertical line characters The expression A B is true if either A is true or B is true or if both are true A B is false only if both A and B are false The operators and are said to be short circuited versions of the boolean operators This means that the second operand of or is not necessarily evaluated Consider the test x 0 y x 1 Suppose that the value of x is in fact zero In that case the division y x is undefined mathematically However the computer will never perform the division since when the computer evaluates x 0 it finds that the result is false and so it knows that x 0 anything has to be false Therefore it doesn t bother to evaluate the second operand The evaluation has been short circuited and the division by zero is avoided This may seem like a technicality and it is But at

    Original URL path: http://math.hws.edu/javanotes/c2/s5.html (2016-02-07)
    Open archived version from archive