cs 5ja introduction to java who is this course for? anyone. except... if you already have experience...

28
CS 5JA Introduction to Java Who is this course for? Anyone. Except... If you already have experience programming in a high- level, object-oriented language, then this course will be too easy for you. If you don't know what those words mean, this is the course for you. However, If you don't know what a computer is, this is probably the wrong course for you. If you just want to make webpages, this is probably not the course for you either. The course is not how to use programs, but how to make programs.

Post on 20-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

CS 5JA Introduction to Java

Who is this course for?

Anyone.

Except... If you already have experience programming in a high-level, object-oriented language, then this course will be too easy for you.

If you don't know what those words mean, this is the course for you.

However, If you don't know what a computer is, this is probably the wrong course for you.

If you just want to make webpages, this is probably not the course for you either.

The course is not how to use programs, but how to make programs.

CS 5JA Introduction to Java

What book are we using?

Deitel & Deitel, “Small Java”, 6th edition

It covers Java 5.0 (also known as 1.5).

Should be cheaper than their larger book.

You can use the other Deitel & Deitel book as well, but make sure it’s an edition that covers either Java 5.0 or Java 6.0.

You’ll have to figure out differences in page numbers yourself...

CS 5JA Introduction to Java

Why Java?

Java is probably the most popular programming language in the world.

(Relatively) easy to use and extend.

Lots of great built in libraries for networking, user-interfaces, graphics, etc.

Very similar to C++ in syntax. If you learn Java, learning other languages will be easy.

Used for most large web-sites. Used to run many millions of cell-phones.

Open source.

Robust & Secure.

CS 5JA Introduction to Java

Low Level High Level

Lowest Level : Native Machine Code.

A series of 1s and 0s. Best representation for the CPU, but totally impossible for a human to program.

Low Level : Assembly Language

Write code using a small set of symbolic words which map very closely to the available instructions on the CPU. Super-fast, but still very cryptic and hard to debug. The code needs to translated (compiled) into 1s and 0s.

High Level: C/C++, C#, Java, etc

Easy to program. Write code that looks (somewhat) like English and use common mathematical notation. Much more compact: A single line in a high-level program will do the work of dozens of lines of a low-level program.

CS 5JA Introduction to Java

What does Object-oriented mean?

"Object-oriented" means that you can write little groups of code which mimic objects or processes in the real world.

Every program builds a certain model of the world where objects in that world interact.

Makes it easy to reuse code. You can create lots of different versions of that same object, but with different attributes.

If you think of an object as a Noun, you can use different Adjectives to describe it.

Object-oriented code helps you set-up a model of the world which is similar to the way humans think of it.

CS 5JA Introduction to Java

Example: building a program where you calculate how far a person can throw a ball.

In an object-oriented language you would create a Ball object. That Generic Ball object would describe all of the features which are general to all balls. What features are similar to all (or at least most) balls? They are all round; they all possess weight, color or pattern, bounciness.

Then you can describe sub-versions of the generic ball which contain more specific aspects of a type of ball, say a bowling ball. What about a bowling ball is different than a general ball? (the indentations where you hold the ball, a specific range of weights, usually kind of a swirly-pattern, certain material, doesn’t bounce. etc)

CS 5JA Introduction to Java

What does Object-oriented mean?

How about a billiard ball?

Has standard size, has a number on it. etc.

What about a football?

CS 5JA Introduction to Java

Instructions & Models

Helps to think of programming in these two ways:

As a set of extremely detailed instructions.

As a set of simplified models of certain objects or processes in the world.

They go hand-in-hand: All instructions imply a model of the world. All models of the world imply a set of objects that have attributes and behaviors (that have the potential to be instructed).

Instructions are the “technical” part of programming. Modeling is the conceptual or “creative” part of programming.

CS 5JA Introduction to Java

Instructions

An instruction is a command that tells the computer to do something.

A typical instruction is extremely low-level. What we consider simple operations might take thousands of low-level instructions. For example, drawing a rectangle on the screen requires an instruction for each pixel.

A programming language simplifies the creation of instructions. You are able to say “draw rectangle” instead of issuing a command for each pixel. That is, in Java you generally give “high-level” instructions.

CS 5JA Introduction to Java

Instructions

You can think of a programmer as an architect and the program itself as a blueprint.

You hand-off the blueprint to a team of engineers (the compiler), and they in turn give pieces of it to various contractors, who in turn give it to the construction workers.

In our draw rectangle example, you are the architect that creates a blueprint which specifies how to draw a rectangle.

The code gets compiled by the “engineers”, who know to pass the instructions to the part of the CPU that controls the screen. The “construction workers” light up each pixel in the rectangle.

CS 5JA Introduction to Java

Instructions

Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. -Einstein

It’s important to know that the computer is an imbecile, an idiot savant that is really, really good at performing complicated logical and mathematical operations quickly, but doesn’t know how to tie its shoes.

Computer is completely literal. Can not reason at all with ambiguous instructions.

Take a minute or two and think about how you would explain to a child how to tie a shoe.

CS 5JA Introduction to Java

Instructions

Some assumptions that you have to make:

That the child knows the name of various part so his body and how to move them. That a child can move without losing balance.

That a child can recognize objects in the world.

That a child knows the difference between left and right, and can reason about the top or center of an object.

However, you cannot make these assumption when programming! This is a bit frustrating... But you get used to it.

CS 5JA Introduction to Java

Model

“Everything should be made as simple as possible, but no simpler.” Albert Einstein

The second part of programming is that you are creating a simplified model of the world, or some system in the world, so that you can solve problems.

You are trying to extract the essence of the situation.

CS 5JA Introduction to Java

Model

“I have a map of the United States... Actual size. It says, ‘Scale: 1 mile = 1 mile.’ I spent last summer folding it.” Stephen Wright

The whole point of a map is that it doesn't include everything. It is a vastly simplified version of reality, which is exactly why it's useful.

To make a model of something means you have to leave most stuff out.

CS 5JA Introduction to Java

Model

In fact it is impossible to leave everything in.

Even when software models numbers (which is so basic it is built in to all programming languages) they can only have a certain precision.

The real number line is infinite.

Computers model decimals with “floating-point arithmetic”, which basically means that it has to truncate numbers with too many decimals.

CS 5JA Introduction to Java

Model

Example: say you are making a "cash register" program.

What parts or functions of the physical cash register are important to model?

What parts are irrelevant?

CS 5JA Introduction to Java

Model

You want to model the way in which a cash register totals up the price of groceries and how it makes change for you. You are modeling the basic functionality of the cash register. You don't care at all about how it looks, the chi-ching sound of it, the way the drawer pops-out, etc..

But it also depends on what you problem you are trying to solve. Maybe you are trying to create a simulation where you test for noise pollution in a grocery store... Or maybe you are making a simulation to test the safety of the drawer popping out. In those cases you don’t care at all about how it makes change!

Thus the modeling of a real-world object in software depends upon what you are trying to accomplish.

CS 5JA Introduction to Java

Model

Another example: Let's think about a map again.

How exactly is it a model of reality?

What does it leave out?

What important features does it leave in?

CS 5JA Introduction to Java

Some random map of southern California...

CS 5JA Introduction to Java

Model

no representation of height/depthcolors = countiesdots = citieswhite = waterbig words all over the place...

a geographic model-- of how different cities are located in relation to each other.

If i just had a map where Los Angeles was a dot, and nothing else, it wouldn't be very useful...

CS 5JA Introduction to Java

Model

If we have an object in our program called “Ball”, it needs to do something in relation to other objects.

We probably want to have an object called Person, and define the action where the person throws the ball.

If the program is a physical simulation, we might want to have a Wind object so we can model air resistance.

We probably want to have a Gravity object so that we can model the force of gravity.

We can get as complicated as we want, or as is necessary for a particular problem.

CS 5JA Introduction to Java

Models & Instructions

Models define a set of instructions. Using the map we provided, we can’t ask a traveler to find the highest mountain peak. The map does not model elevation. This instruction makes no sense with this map. On the other hand, we can ask all sorts of other questions: Which is the biggest city? What highway do I take from LA to Santa Barbara? Is there a river in Ventura County.

Similarly, instructions assume a model. If I tell you to find the shortest route from LA to Santa Barbara using a map, I am assuming that the map and its legend model the location of these cities in relation to each other.

CS 5JA Introduction to Java

A science and an art

Writing software is really an art as much as it is a science.

You have to be creative, flexible, able to think of things is different ways

One of the fun parts of programming involves creating metaphors of reality.

The other fun part is coming up with tricky ways to write instructions efficiently and “elegantly”.

CS 5JA Introduction to Java

How a to compile and run a program

1) Write a program in Java in a text file (or some type of editor). Save it to disk as "MyProgram.java"

2) Run a program called a compiler. The Java compiler is called "javac". ex: javac MyProgram.java. This saves your program as "bytecode" on to the disk. MyProgram.java becomes MyProgram.class. It is not readable by humans. Open it in your text editor and see what happens.

3) Load the program into the "Java Virtual Machine" and execute it. ex: java MyProgram (starts the JVM (if not already started), looks for MyProgram.class, loads it into the JVM, and starts the program).

CS 5JA Introduction to Java

Practical Steps

Need to install latest version of Java. Might only be able to go up to 1.5 on macintosh (1.6 should ship with “Leopard”).

http://java.sun.com/javase/downloads/index.jsp (windows and linux)

http://www.apple.com/downloads/macosx/apple/macosx_updates/j2se50release4intel.html (mac)

CS 5JA Introduction to Java

Practical Steps

Open up WordPad or another text editor. Write a program. Save it to disk. (as a .java file) Ex. HelloWorld.java

Open up the command terminal. On Windows (Window-R, type "cmd"). On Mac run the program "terminal".

Type "cd" and then the path to where you saved the program. Then compile the program. Ex. javac HelloWorld.java (makes a “class” file called HelloWorld.class)

If you don't get any errors at this point, you can run the program. Ex. java HelloWorld

CS 5JA Introduction to Java

HelloWorld.java

public class HelloWorld

{

public static void main(String[] args)

{

System.out.println(“Hello World!");

}

}

CS 5JA Introduction to Java

For next time...

Homework on website.

http://cs.ucsb.edu/~cs5ja