Chapter 1 Basic Computing and your first program

Chapter 1 - Basic Computing and your first program

Overview n n n Computer Basics Programming Basics Java Basics and your first program.

Computer Basics

Basic Computers and terms n Computer is made up of two parts: – Hardware • Physical equipment • Monitor, keyboard, memory, hard disk, processor, mouse. • Usually not changeable. – Software • The programs/information you run on the hardware. • Operating Systems (Windows, Linux), applications (Word, Photoshop), and games. • Modifiable.

Hardware components n Monitor/Printer (Output) – Shows what is happening in the computer. n Keyboard/Mouse/Joystick (Input) – Lets you interact with the computer n CPU (Central Processing Unit) – Brains of the computer. – Receives input from the user, performs operations, and directs the output devices to display the results.

Hardware Storage n We use storage to keep programs on our computer. It comes in many types: – Main memory – Where most of the calculations are done, temporary, wiped out when the power is turned off. – Hard Disk – Where data is saved for more permanent storage (your files and directories are usually on your hard disk). – Removable media – Usually used for archiving information/backing up. Can consist of tapes, CDR’s, removable hard disks, ZIP disks, or floppies (floppies bad).

Hardware storage – How n n n Information is stored on our computers in “bits, ” or 1’s and 0’s. A “byte” is 8 bits, which used to be the length needed to store a single character (there were 256 characters possible). The bytes are stored in a big list in memory, or on the hard drive. When I need a piece of information (say I open a midterm paper), the computer looks up its address in the list, and retrieves those 1’s and 0’s for me. The computer then interprets these bits and displays my paper.

Software n Software consists of the programs that we run on the computer. – Operating systems – Applications – Drivers n When we combine programs with data or input, then we get some sort of results or output.

Software programming n n n Software today is usually written in high-level languages, or programming languages that are easily read by humans. C/C++, Java, Perl, VB… Computers don’t understand these languages, so programs have to be translated into lower-level languages for the computer. Machine-language, assembly… Depending on when this translation is done, it is called compiling or interpreting.

Compiling/Interpreting … x = x + 1; … Source Program Source Code Compiling Interpreting 00011101 00100101 10001001011 Object code Machine code

Compiling vs. Interpreting n Compiling – Done before program is run. – Translates the whole program at once. – Takes longer to translate the program(compile). – Finished program runs faster. – Need to recompile for every new platform. n Interpreting – Done while program is run. – Translates a single line at a time. – Takes no time at all for any compilation. – Finished program runs slower. – No need to recompile for any new platforms.

How Java does it. n Java uses a mixture of interpretation and compilation. – Use the Java compiler to turn our high-level program into byte-code. – To run the program, we then give the byte-code to the Java byte-code interpreter to run the code. – This allows us to run our program on any OS that a Java byte-code interpreter has been created for (currently Windows, Linux, and Solaris) without having to recompile.

Large Java programs n n When programs get big, they are often done in multiple pieces, each in a separate file. Each file is separately compiled into its own piece of Java byte-code. Before you can run these large programs, the individual byte-code files need to be linked. Luckily for us, this is done behind-the-scenes for us, so we don’t have to worry about it.

Programming Basics

How to go about creating a computer program… n Four main steps (in this class) – Find out what you need to do (gather requirements). – Figure out how you’ll do it (design). – Do it (implement). – Check to see if you’ve done it correctly (test).

Gather Requirements. n n n Read the problem carefully! Read it twice if you have to. Figure out what it really wants.

Design n Start off with an English description of what you want to do in general. Step by step, start adding more details to your description. Continue this until you can translate this description into Java code.

Design Example n I want to write a program that converts temperatures. n …a program that converts Fahrenheit to Celsius. …a program that n a. b. c. n gets a value in Fahrenheit converts that value to Celsius outputs the Celsius value to the screen. … b. Celsius = 5*(degrees. F – 32)/9

Implement n Translate your design into actual Java code.

Test n Test your written program to make sure that is does all that the problem originally asked you to do.

Object Oriented Programming and Java. n n Java is heavily object oriented. In fact, almost everything we end up doing is with objects. Objects are things that surround us: books, cars, flashlights, etc. Methods are the actions that objects can perform. Cars can accelerate or turn, flashlights can turn on and off, etc. A class is a category of objects. Toyota Camry and Honda Accord both belong to the class of cars and to the class of automobiles.

Object Oriented Programming (OOP) n Composed of three principles – Encapsulation- I don’t need to know how it works, just that it does. – Polymorphism- The same word means different things to different people. – Inheritance- You inherit characteristics from your ancestors. n We will cover these more at a later time.

Your first Java program.
![First. Program. java public class First. Program { public static void main(String[] args) { First. Program. java public class First. Program { public static void main(String[] args) {](http://slidetodoc.com/presentation_image_h2/9bfc36865bf1969ceb358fc41b0382da/image-24.jpg)
First. Program. java public class First. Program { public static void main(String[] args) { System. out. println("Hello out there. "); System. out. println("Want to talk some more? "); System. out. println("Answer y for yes or n for no. "); char answer. Letter; answer. Letter = Savitch. In. read. Line. Nonwhite. Char(); if (answer. Letter == 'y') System. out. println("Nice weather we are having. "); System. out. println("Good-bye. "); System. out. println("Press enter key to end program. "); String junk; junk = Savitch. In. read. Line(); } }
![Code explanation public class First. Program { public static void main(String[] args) { n Code explanation public class First. Program { public static void main(String[] args) { n](http://slidetodoc.com/presentation_image_h2/9bfc36865bf1969ceb358fc41b0382da/image-25.jpg)
Code explanation public class First. Program { public static void main(String[] args) { n n n Always have nearly the same first two lines in every program for now. You will only change the class name (First. Program). Since it says public class First. Program you must save this in a file called “First. Program. java”. The beginning of the program is indicated by the main method.

Code Explanation System. out. println("Hello out there. "); System. out. println("Want to talk some more? "); System. out. println("Answer y for yes or n for no. "); n n tells the computer to write the upcoming message out to the monitor. When the program is run, these three lines will appear on the screen: System. out. println Hello out there. Want to talk some more? Answer y for yes or n for no.

Code Explanation char answer. Letter; answer. Letter = Savitch. In. read. Line. Nonwhite. Char(); n n Makes answer. Letter a variable that can hold a single character. Second line waits until the user inputs a single character. When they do, it assigns (single equal sign) whatever the user pressed in the variable.

Code Explanation if (answer. Letter == 'y') System. out. println("Nice weather we are having. "); n n Checks to see if what is stored in answer. Letter (what the user entered), is equal (double equal sign) to the letter ‘y’. If they are equal (if the user entered ‘y’) then it prints out the message about the weather. Else the weather message is not printed at all.

Code Explanation System. out. println("Good-bye. "); System. out. println("Press enter key to end program. "); String junk; junk = Savitch. In. read. Line(); n n Prints out a few more lines and then waits for the user to press the enter key before ending the program. Depending on your computer, you may or may not need these last two lines. It prevents the computer from shutting the program down before the user gets a chance to see all of the output.

Code Explanation } } n The ending curly brackets tell the computer that the program has ended. For every opening curly bracket ({), you must have a closing curly bracket (}). If you don’t the computer will complain of an error.

Java rules of syntax n Java’s syntax is very picky. – Don’t forget the semicolons! They usually go at the end of every statement. – Java is case sensitive. “System” and “system” are not the same thing in Java.

End of Chapter 1 n Computer Basics Programming Basics A first Java program. n Any questions? n n
- Slides: 32