32
Chapter 1- Basic Computing and your first program

Chapter 1- Basic Computing and your first program

Embed Size (px)

Citation preview

Page 1: Chapter 1- Basic Computing and your first program

Chapter 1- Basic Computing and your first program

Page 2: Chapter 1- Basic Computing and your first program

Overview

Computer Basics Programming Basics Java Basics and your first program.

Page 3: Chapter 1- Basic Computing and your first program

Computer Basics

Page 4: Chapter 1- Basic Computing and your first program

Basic Computers and terms 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.

Page 5: Chapter 1- Basic Computing and your first program

Hardware components

Monitor/Printer (Output)– Shows what is happening in the computer.

Keyboard/Mouse/Joystick (Input)– Lets you interact with the computer

CPU (Central Processing Unit)– Brains of the computer. – Receives input from the user, performs operations,

and directs the output devices to display the results.

Page 6: Chapter 1- Basic Computing and your first program

Hardware Storage

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, CD-R’s, removable hard disks, ZIP disks, or floppies (floppies bad).

Page 7: Chapter 1- Basic Computing and your first program

Hardware storage – How

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.

Page 8: Chapter 1- Basic Computing and your first program

Software

Software consists of the programs that we run on the computer.– Operating systems– Applications– Drivers

When we combine programs with data or input, then we get some sort of results or output.

Page 9: Chapter 1- Basic Computing and your first program

Software programming

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.

Page 10: Chapter 1- Basic Computing and your first program

Compiling/Interpreting

x = x + 1;

00011101001001011000100101001011

Compiling

Source ProgramSource Code

Object codeMachine code

Interpreting

Page 11: Chapter 1- Basic Computing and your first program

Compiling vs. Interpreting

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.

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.

Page 12: Chapter 1- Basic Computing and your first program

How Java does it.

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.

Page 13: Chapter 1- Basic Computing and your first program

Large Java programs

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.

Page 14: Chapter 1- Basic Computing and your first program

Programming Basics

Page 15: Chapter 1- Basic Computing and your first program

How to go about creating a computer program…

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).

Page 16: Chapter 1- Basic Computing and your first program

Gather Requirements.

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

Page 17: Chapter 1- Basic Computing and your first program

Design

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.

Page 18: Chapter 1- Basic Computing and your first program

Design Example

I want to write a program that converts temperatures.

…a program that converts Fahrenheit to Celsius. …a program that

a. gets a value in Fahrenheit

b. converts that value to Celsius

c. outputs the Celsius value to the screen.

… b. Celsius = 5*(degreesF – 32)/9

Page 19: Chapter 1- Basic Computing and your first program

Implement

Translate your design into actual Java code.

Page 20: Chapter 1- Basic Computing and your first program

Test

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

Page 21: Chapter 1- Basic Computing and your first program

Object Oriented Programming and Java.

Java is heavily object oriented. In fact, almost everything we end up doing is with objects.

Objects are the 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.

Page 22: Chapter 1- Basic Computing and your first program

Object Oriented Programming (OOP)

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. We will cover these more at a later time.

Page 23: Chapter 1- Basic Computing and your first program

Your first Java program.

Page 24: Chapter 1- Basic Computing and your first program

FirstProgram.java

public class FirstProgram{ 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 answerLetter; answerLetter = SavitchIn.readLineNonwhiteChar(); if (answerLetter == '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 = SavitchIn.readLine(); }}

Page 25: Chapter 1- Basic Computing and your first program

Code explanation

Always have nearly the same first two lines in every program for now. You will only change the class name (FirstProgram).

Since it says public class FirstProgram you must save this in a file called “FirstProgram.java” .

The beginning of the program is indicated by the main method.

public class FirstProgram{ public static void main(String[] args) {

Page 26: Chapter 1- Basic Computing and your first program

Code Explanation

System.out.println 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.");System.out.println("Want to talk some more?");System.out.println("Answer y for yes or n for no.");

Hello out there.Want to talk some more?Answer y for yes or n for no.

Page 27: Chapter 1- Basic Computing and your first program

Code Explanation

Makes answerLetter 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.

char answerLetter;answerLetter = SavitchIn.readLineNonwhiteChar();

Page 28: Chapter 1- Basic Computing and your first program

Code Explanation

Checks to see if what is stored in answerLetter (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.

if (answerLetter == 'y') System.out.println("Nice weather we are having.");

Page 29: Chapter 1- Basic Computing and your first program

Code Explanation

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.

System.out.println("Good-bye.");System.out.println("Press enter key to end program.");String junk;junk = SavitchIn.readLine();

Page 30: Chapter 1- Basic Computing and your first program

Code Explanation

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.

}}

Page 31: Chapter 1- Basic Computing and your first program

Java rules of syntax

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.

Page 32: Chapter 1- Basic Computing and your first program

End of Chapter 1

Computer Basics Programming Basics A first Java program.

Any questions?