Prog4IntLecture2Java

Download Report

Transcript Prog4IntLecture2Java

Programming for Interactivity
Professor Bill Tomlinson
Tuesday & Wednesday
6:00-7:50pm
Fall 2005
Computer Program
• A way to get a computer to do something
Programming Language
• A way of writing computer programs that is
(potentially) comprehensible by people.
Interpreted vs. Compiled
• Interpreted – one line at a time.
• Compiled:
Person->
Source Code ->
Machine Code ->
Computer
Source code
• Human-readable text that can be used to
make a computer operate in a certain way.
Commenting
• A way to get the computer to ignore
certain parts of a computer program.
• Useful for:
– Identifying the function of certain lines of code
– Temporarily removing parts of programs
– Leaving notes for yourself and others
Object Oriented Programming
• Wikipedia – “the idea that a computer
program is composed of a collection of
individual units, or objects, as opposed to
a traditional view in which a program is a
list of instructions to the computer.”
Java
• A compiled language
• An object oriented language
• Derived largely from C/C++
Java Source Code
/**
* Written on Sep 26, 2005 by Bill Tomlinson
*/
public class MinimalPrintOutProgram {
public static void main(String[] args) {
//Note: this program is often called "Hello World."
System.out.println("Hello there.");
}
}
Curly Braces
• Demarcate different sections of code.
• Nested braces
{
Section 1
{
Section 1a
}
{
Section 1b
}
}
Parentheses
• Different from curly braces
• Enclose arguments (we’ll get to arguments
in a moment)
Semicolons
• There’s one at the end of every statement.
Line Spacing
• Irrelevant to the computer.
• Useful for keeping track of what is going
on in complex code.
Classes
• These are the objects in “Object oriented
programming”.
Fields
• Similar to nouns in the English language.
• They store data.
• Some important data types:
– int – a number with no decimal places
– float – a number with some decimal places
– double – a number with lots of decimal places
– String – a group of letters (and numbers)
• Give examples
Methods
• Like verbs in English
• Methods are actions that objects can
perform
• They manipulate fields and interact with
other objects.
Arguments
• Methods sometimes include arguments,
that is, fields which are “passed in” to the
method to allow it to act on them.
Types and Names
• Creates a reference to a chunk of memory
of a certain type. May or may not put
information into that memory space.
• int age;
• String name = “Bill”;
Arrays
• A way of grouping things
String [] names = new String[3];
names[0] = “bill”;
Booleans
• true or false
• boolean myNameIsBill = true;
Loops
Lets the program do something over and
over again (which computers are very
good at).
for (int i = 0; i < 5; i ++)
{
System.out.println(“Round number “ + i);
}
Conditionals
if (age>18 && liveInUS)
{
vote();
}
System.out.println();
• A way to print something out.
• Useful for debugging.
Three Kinds of Commenting
//
/* */
/**
*
*
*/
//
//This line is now commented out
// I can also leave myself notes like this.
/* */
/*
This kind of comment is useful for
commenting out long blocks of code.
*/
/**
/**
*
*This is yet a third kind of comment.
*It is used to describe the content that
*is coming next.
*
*
*/
Capitalization
• By convention:
•
•
•
•
•
ClassesAreCapitalizedLikeThis
methodsAreCapitalizedLikeThis
someFieldsAreCapitalizedLikeThis
OTHER_FIELDS_LIKE_THIS
(We’ll get to that distinction later.)
Field and Method Names
• Should be descriptive
• Long is okay. There are tools in Eclipse
that make it easier. For now, even with
notepad and javac, make sure to use
descriptive names.
Questions about any of the letters?
/**
* Written on Sep 26, 2005 by Bill Tomlinson
*/
public class MinimalPrintOutProgram {
public static void main(String[] args) {
//Note: this program is often called "Hello World."
System.out.println("Hello there.");
}
}
A Longer Example
• (From your first assignment.)
Writing Code
• You may not (and probably won’t)
understand everything about the code
you’re working with.
Notepad, Javac and Java
• Walk through it together.
Applets
• Web based applications.
• Require one or more java class files and
an html file.
Debugging
• Coding is an iterative process.
• Debugging is the bulk of the work,
especially at first.
• Three Kinds of Bugs
– Compile time
– Run time
– Misbehavior
Compile-Time Bugs
• Javac will not convert the .java files into
.class files.
• Need to fix the code before you can even
run it.
Run-Time Bugs
• Javac will converty the .java files into class
files, but the code crashes somewhere.
• May crash before the code “works” at all,
or it may crash after running for a while.
Misbehavior
• Code compiles and runs but…
• It doesn’t do what you want.
• Part of the challenge is often figuring out
why it’s doing what it’s doing.
Assignment 1
• Make a graphical computer program.
• You may use BasicGraphicsSystem.java,
which I have provided, or you may use this
as an opportunity to go learn JOGL or
some other graphics system.
If There Is Time Left
• Start working on Assignment 1, and I will
help out as needed.