460 likes | 720 Views
Programming and Problem Solving Using Java. Bobby D. Gerardo. Kunsan National University Summer 2004. Instructor. Bobby D. Gerardo Office: Distributed Systems Laboratry School of Electronic and Information Eng. Kunsan National University Email: bgerardo@kunsan.ac.kr
E N D
Programming and Problem Solving Using Java Bobby D. Gerardo Kunsan National University Summer 2004
Instructor Bobby D. Gerardo Office: Distributed Systems Laboratry School of Electronic and Information Eng. Kunsan National University Email: bgerardo@kunsan.ac.kr URL: http:/clik.to/gerardo phone: 469-4696
Lecture Materials • Main reference: Problem Solving with Java by Koffman. Addison-Wesley, Inc.2002. • Handouts: Copies of the lecture notes and additional reading materials will be available from my website. http://clik.to/gerardo
Training Schedule • The class shall be every Monday, Wednesday and Friday. • 3 hours per session, this is 2 hours in the morning with 10 minutes break and 1 hour in the afternoon. • The training shall be for a total of 4 weeks for a total of 36 hours.
Methods of Learning • First Hour: Lecture on concepts and principles • Second Hour: Demonstrations, problem solving, exercises and case studies. • Third Hour: Review, solution of case studies, assignment of projects. Note: After the session, students shall prepare a journal (report) to be sent to my email: bgerardo@kunsan.ac.kr. Your report shall be checked and be submitted to the office.
Lessons 1. Introduction to Computers, Problem Solving, and Programming. Overview of Computers. Computer Components. Computer Software. Processing a High-Level Language Program. The Software Development Method. Professional Ethics for Computer Programming. 2. Using Primitive Data Types and Using Classes. Primitive Data Types. Processing Numeric Data. Introduction to Methods. The String Class. Input/Output with Class JOptionPane and Method println(). Problem Solving in Action. Anatomy of a Java Program. Numerical Computations with Class Math. Common Errors and Debugging.
Lessons 3. Object-Oriented Design and Writing Worker Classes. A First Worker Class: Class FoodItem. A Worker Class that Manipulates String Objects. A Worker Class that Processes Integers. Review of Methods. Simplifying a Solution Using Multiple Classes. Formatting Output and Class KeyIn (Optional). Applets, AWT, and the Graphics Class (Optional). Common Programming Errors. 4. Control Structures: Decisions and Loops. Control Structures. boolean Expressions. The if Statement. Decision Steps in Algorithms. Multiple-Alternative Decisions: Nested if and switch. Counting Loops, while and for statements. State-Controlled Loops. Putting It All Together. Debugging and Testing Programs with Decisions and Loops. Common Programming Errors.
Lessons 5. Arrays and Vectors. 6. Class Hierarchies, Inheritance, and Interfaces. 7. Graphical User Interfaces (GUIs). 8. Exceptions, Streams, and Files. 9. Recursion. 10. Linked Data Structures.
Programming • Program- a set of coded instructions that enables a machine, especially a computer, to perform a desired sequence of operations. • Programming - the writing of a computer program
Algorithms • An algorithm is a step-by-step procedure for performing some task in a finite amount of time. • Examples: • sorting • searching
Algorithm Analysis • Algorithm analysis is the process of determining the amount of resources the algorithm will require. • time • space • Can be used as a tool to compare algorithms that perform the same task.
Problem Solving • Problem Solving - Problem solving consists of setting up the objectives of a problem-solving act, planning a sequence of steps to achieve that objective, execute the actions in each of the steps as planned, and evaluate what so been so far achieved against the objective. means learning by analyzing and solving problems. • Problem Solving Software - problem solving software uses idea generation techniques. It manages your ideas, concepts, the creative problem-solving process and enhances your natural creative abilities to solve a problem.
What is Java? • The Java programming language lets you write powerful, enterprise-worthy programs that run in the browser, from the desktop, on a server, or on a consumer device. Java programs are run on -- interpreted by -- another program called the Java Virtual Machine (Java VM). Rather than running directly on the native operating system, the program is interpreted by the Java VM for the native operating system. • This means that any computer system with the Java VM installed can run a Java program regardless of the computer system on which the application was originally developed.
Who invented Java? • Java is a new computer programming language developed by Sun Microsystems. Java has a good chance to be the first really successful new computer language in several decades. Advanced programmers like it because it has a clean, well-designed definition. Business likes it because it dominates an important new application, Web programming. • Java has several important features: • A Java program runs exactly the same way on all computers. Most other languages allow small differences in interpretation of the standards. • It is not just the source that is portable. A Java program is a stream of bytes that can be run on any machine. An interpreter program is built into Web browsers, though it can run separately. Java programs can be distributed through the Web to any client computer. • Java applets are safe. The interpreter program does not allow Java code loaded from the network to access local disk files, other machines on the local network, or local databases. The code can display information on the screen and communicate back to the server from which it was loaded.
Who invented Java? (cont) • A group at Sun reluctantly invented Java when they decided that existing computer languages could not solve the problem of distributing applications over the network. C++ inherited many unsafe practices from the old C language. Basic was too static and constrained to support the development of large applications and libraries. May 23, 1995 . • Today, every major vendor supports Java. Netscape incorporates Java support in every version of its Browser and Server products. Oracle will support Java on the Client, the Web Server, and the Database Server. IBM looks to Java to solve the problems caused by its heterogeneous product line. • Microsoft distributes Java with Internet Explorer, with its Visual J++ development environment, and with a free Java SDK that can be downloaded from www.microsoft.com. This version of Java support installs directly into the Windows directory and becomes an extension of the operating system. Microsoft has also added features to the Java execution support (to the "virtual machine") so that Java programs can act as OLE clients or servers. This makes Java a full-fledged Windows development language, but it enrages Sun because programmers can now develop Java programs that depend on OLE and are no longer portable to all systems.
Chapter 1Introduction to computers, Problem Solving, and Programming
1.1. Overview of computers • Mainframe and Minicomputers– The first electronic computer was designed in the late 1930s by Dr. John Atanasoff at Iowa State University. • Sharing computer resources– In the old days a time sharing technique was used for computer usage. • Microcomputers– Altair and Apple computer companies manufactured in 1970s
1.2. Computer Components • The computer system consist of two major components. • Hardware • Software • Memory– main and secondary memory • Bytes and Bits • Byte is smallest addressable unit of memory • Bit is smaller units of byte
1 bit Fig. 1.3. Relationship between a byte and a bit
1.2. Computer Components (cont) • Central processing unit • Coordinating all computer operations • Performing arithmetic and logical operations on data. • Input/Output Devices - these are used to communicate with computer. Allows us to enter data for computation and to observe the results of that computation.
Computer networks • Computer network tasks is to connect computers together in order to share resources. • Common Types • LAN • WAN
World Wide Web • World Wide Web or WWW was introduced in 1989 and is one of the most popular applications of the Internet. • Programming the Web– one of the reasons for the popularity of the Java is that Java is the first programming language to exploit the networked programming environment.
1.3. Computer Software • Operating System– collection of computer programs that control the interaction between the user and the computer hardware • Application Software– programs that assist user in accomplishing specific tasks. • Programming Languages– tools to develop new software. • Object Oriented Programming– can make it easier to reuse and adapt previously written software. i.e. C++ and Java.
1.4. Processing High level Language Program • Cross-Platform Transparency– WWW have encouraged the concept of cross-platform transparency. This means that an application program must run correctly regardless of what machine (platform) you use. • Java Virtual Machine, JVM– simulated computer that creates object code (byte code). See Fig. 1.8 and 1.9.
Applets and Applications • Applets– are programs that run as part of the WWW. JVM supports applets. It is stored on a remote site on the web and executed on the user’s local computers. • Java Applications– is not part of the web and runs like any other program that runs on a particular computer.
Local Computer Remote Computer Internet Connection Applet (Byte code) Applet (Byte code) JVM Fig. 1.10. Accessing an applet from a remote computer and running it.
1.5 Software Development Method • Specify the problem requirements • Analyze the problem and identify the classes that will be need. • design the classes to solve the problem. a. Locate the relevant classes in libraries. b. Modify existing classes if necessary. c. Design new classes where necessary • Implement the new and modified classes. • Test and verify the completed program. • Maintain and update the program.
We will not break tradition! Let’s start with the Hello World Example
Detailed Instructions for Your First Program The following instructions will help you write your first program. These instructions are for users of Microsoft Windows platforms, which include Windows 95/98/2000/ME/XP/NT 4.0. We start with a checklist of what you need to write your first program. Next, we cover the steps to creating an application, steps to creating an applet, and explanations of error messages you may encounter. 1. A Checklist 2. Creating Your First Application a. Create a Java Source File b. Compile the Source File c. Run the Program 3. Creating Your First Applet (separate doc)
1. A Checklist To write your first program, you need: • The JavaTM 2 Platform, Standard Edition. You can download the SDK now and consult the installation instructions . (Make sure you download the SDK, not the JRE.) • A text editor. In this example, we'll use NotePad, the simple editor included with the Windows platforms. To find NotePad, from the Start menu select Programs > Accessories > NotePad. You can easily adapt these instructions if you use a different text editor. • These two items are all you need to write your first Java program. Note: You may want to consider using an IDE to help you write your programs. Java 2 SDK, Standard Edition v. 1.4, is available bundled with an IDE, the Sun™ ONE Studio 4, Community Edition (formerly known as the ForteTM for JavaTM, Community Edition). You can download this Sun ONE Studio bundle from the 1.4 download page .
2. Creating Your First Application Your first program, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: • Create a source file. A source file contains text, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files. • Compile the source file into a bytecode file. The compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler converts these instructions into a bytecode file. • Run the program contained in the bytecode file. The Java interpreter installed on your computer implements the Java VM. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.
a. Create a Source File. 1. Start NotePad. In a new document, type in the following code: Be Careful When You Type Type all code, commands, and file names exactly as shown. The Java compiler and interpreter are case-sensitive, so you must capitalize consistently. HelloWorldApp ≠ helloworldapp
2. Save this code to a file. From the menu bar, select File > Save As. In the Save As dialog box: • Using the Save in drop-down menu, specify the folder (directory) where you'll save your file. In this example, the directory is java on the C drive. • In the File name text box, type "HelloWorldApp.java", including the double quotation marks. • From the Save as type drop-down menu, choose Text Document.
When you're finished, the dialog box should look like this: • Now click Save, and exit NotePad.
b. Compile the Source File. • From the Start menu, select the MS-DOS Prompt application (Windows 95/98) or Command Prompt application (Windows NT). When the application launches, it should look like this:
b. Compile … (cont) • Now you can compile. At the prompt, type the following command and press Enter: • javac HelloWorldApp.javaIf your prompt reappears without error messages, congratulations. You have successfully compiled your program.
The compiler has generated a Java bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated: Now that you have a .class file, you can run your program.
c. Run the Program. In the same directory, enter at the prompt: Now you should see: Congratulations! Your program works.
Error Explanation Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp If you receive this error, java cannot find your bytecode file, HelloWorldApp.class. One of the places java tries to find your bytecode file is your current directory. So, if your bytecode file is in C:\java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter: cd c:\javaThe prompt should change to C:\java>. If you enter dir at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again. If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try "clobbering" the classpath with the following command: set CLASSPATH=Now enter java HelloWorldApp again. If the program works now, you'll have to change your CLASSPATH variable.
Case Study Solution Format • Problem– specify the problem requirement • Analysis– analyze the problem an identify the classes that will be needed • Design– design the classes to solve the problem. Locate the relevant classes in libraries. Modify existing classes if necessary. Design new classes where necessary • Implementation– implement the new and modified classes. • Testing– test and verify the completed program.
Programming Project:(1) Name in Hello(2) Input/Output of First and Family Name