Thursday June 8, 2000


Quiz 3


1. What is the binary equivalent of the decimal number 40?

2. What is the octal equivalent of the binary number 11011?

3. Is Java bytecode platform dependant?

4. What are the three types of errors that can occur in programming?




































Today we will conclude our discussion of the number systems. To be more precision regarding definitions, recall that the smallest unit that the CPU can understand is the bit.

A bit is either a 0 or 1. Bit is an abbreviation for binary digit. All data that gets passed to the CPU will be encoded with bits.

A collection of 8 bits is called a byte. Recalling our discussion of yesterday, when we have n binary digits we can represent 2n distinct states. So with one byte we can represent 256 distinct states.

One way that a byte can be utilized is in the encoding of characters. Since the CPU can only understand 0s and 1s, we need some kind of encoding scheme in order for the CPU to process strings and similar data. One popular encoding scheme is called ASCII (American Standard Code for Information Interchange). ASCII uses the 256 distinct states represented by one byte in order to encode all possible character data.

Another natural problem that must be dealt with is that of positive and negative numbers. How can we use bits and bytes to represent both of these?

One way to solve this problem is to use one of our bits as a sign bit. Then the other bits could be used to represent the magnitude or size of the number.

For example, suppose we have a machine that represents all integers with 1 byte. If we use one of these bits as a sign bit, then the other 7 could be used to represent the magnitude of the number. So with one byte of data we could represent numbers that range from

-27 = -128 to 27 -1 = 127


Why is the upper limit = 127 and not 128? The reason is because we can classify numbers as being either negative or non-negative. Notice that I didn't say negative and positive. Why? Because 0 is non-negative, but not positive. So we have to use of the non-negative numbers to represent 0.

Software Development Process

As stated before there is a sequence of steps we go through when we create a Java program.

The first step is to use some editor to generate source code. Source code will consist of Java commands. These are commands that we will be able to understand.

After the source code is written, it is passed to the compiler, javac. javac produces Java bytecode. This is code that we are not able to read. However, the CPU cannot understand it either. If our source file is called Example1.java, then if there are no compile-time errors, a file called Example1.class will be produced.

Since the CPU can't understand Java bytecode, we need a program will be translate the bytecode into something that it can understand. As we have seen before this program is called an interpreter. The interpreter for java is called java. The interpreter translates the Java bytecode into machine executable code and sends it to the CPU for processing.

Question: Which programs are machine dependent? That is, which of these programs will only execute on the machine on which it is created?

The answer is that, necessarily, the interpreter must be machine dependent. Normally a program that is executable on a specific machine cannot be ported to another machine. For example, if you create a .exe file on a Windows platform and then take that program to a Unix or Mac, it will not run. So each platform must have its own interpreter.

However, the Java bytecode is independent of the platform on which it is created. This flexibility makes Java a very powerful language. Because bytecode can be ported to many different platforms, Java is a very natural language to use when writing programs that you wish to make available over the Internet.

  • Example1.java

    As we have discussed earlier, there are three major classes of problems that we may encounter when trying to generate a program: compile-time, run-time, and logic. Notice that compile time errors can only occur during the first phase. That is, once a .class file is generated compile-time errors will not occur. However, during the execution of the program, that is when the interpreter creates the machine executable code and sends it to the CPU both run-time errors and logic errors can occur. It might also be safe to replace the can with a will. You will find in programming that is not very often that there is not some small mistake unless the program is trivial.

    Object-Oriented Programming

    Recall that Java is an Object-Oriented Programming Language. The Object-Oriented paradigm was introduced early in the last decade. Before it was introduced, most programming was done in a sequential fashion.

    In Object-Oriented design we deal with entities that are called objects. These objects can have certain properties and methods associated with them. For example, if we have an object called Human, then a method associated with the object Human would be things like talk, walk, etc. Anything that is of the type Human would have these methods associated with them.

    Throughout this semester we will look at very basic objects. In CIS 121 you will write that programs that implement the Object-Oriented design in more detail.

    Problem Solving

    Recall that this course is called Problem Solving and Programming Concepts. There is a common misconception that this is a Java programming course. The intention of this course is not to teach Java in and of itself. The intent is to teach basic problem solving concepts and to allow you to use Java to demonstrate that you understand these concepts. Therefor it is necessary that we do discuss specifics of Java so that you will be able to write programs. But keep in mind that what you should get out of this course is a fundamental understanding of how to take a problem that is given to you and to find a solution.

    George Polya studied the concepts of problem solving. What he discovered was that we can break down the process into the following steps.
    1. Understand the problem - This is an essential step because if you don't understand what you are being asked to solve, you are unlikely to be able to solve it.

    2. Design or Devise a plan - A plan is called an algorithm. It is a finite sequence of logical steps that terminate to produce a desired outcome.

    3. Implement the Plan

    4. Evaluate the Outcome

    In implementing these steps we can list the steps that occur in the life of software.

    Software Life Cycle

    BIRTH
    1. Requirements specification. Here we must understand the problem and decide what the desired outcome should be.

    2. Analysis - consider the requirements specifications and raise and questions about Input/Output issues, etc.

    3. Design - we come up with an algorithm. An algorithm should not contain code that is specific to the language with which we are dealing. It should simply be an abstract solution of the problem. For example, one step in algorithm might state: load an integer from a file, but not discuss any specific Java code used to open a file.

    4. Implementation - after we have come up with a good plan, then we put it into action. We write specific code to implement our plan. Sometimes it is tempting to skip the design phase, but very often this will cause you to be unsuccessful in solving the problem.

    5. Test/Debug - more than likely when we are writing a program that is not trivial, there will be errors. The purpose of testing our program is to try to find errors. However, through testing alone we cannot prove that our program will always work correctly. We simply use testing to try to discover any problems and try to correct them. Removing the problems we find is called debugging. Where did this term originate?

    6. Maintenance - once we have successfully written a program to solve a problem, we must maintain it, that is, while a client is using the program they may discover problems with it or decide that would like something new added to it.

    7. Ugrading - when new errors are discovered or new requirements are requested we must modify our original solution
    DEATH

    Coding standards When writing programs there are a certain set of standards that we are encouraged to follow. Some of these standards include:
    1. Meaningful identifiers
    2. Identation
    This is not a requirement of the compiler. The compiler doesn't care about indentation or what identifiers are called. For example,
    public class Example1 { public static void main(String[] args) {Sytem.out.println("Hello World");
       }
    }
    
    The compiler would not have any problem understanding this code.

    However, we as programmers would.

    The reason we insist on standards in programming is to make maintenance easier. It can be very costly to try to maintain code that was poorly written.

    Another important aspect of writing programs is documentation. Without documentation, maintenance is very difficult. Even if you are the one writing the program, you may write the code and a year later not remember how you did it.

    Basic programming constructs

    Sequential Execution

    Conditional Execution

    Repeated Execution