Transcript Slide 1

APCS-A: Java Intro
Introduction to Java and BlueJ
September 28
week4
1
The Java Programming
language
• Section 1.4 of your book has a history of Java as
•
•
a programming language
Developed in the early 90s by James Gosling at
Sun Microsystems
The Java Platform has three major groups:
 Java 2 Platform, Standard Edition (J2SE)
• We focus on this one
 Java 2 Platform, Enterprise Edition (J2EE)
 Java 2 Platform, Micro Edition (J2ME)
week4
2
A simple Java Program
// HelloWorld.java
// Author: Ms.K
// Demonstrates a simple java program
public class HelloWorld{
public void sayHello(){
System.out.println(“Hello”);
}
/* This is an example of a multi-line comment
in the book it talks about the main method:
public static void main(String [ ] args)
we won’t be programming main until later */
}
week4
3
How Java works
public class myProgram{
Compiler
(javac)
sayHello();
}
Program in the Java
language
Checks for errors,
makes sure your
code will run
Output (code)
Java bytecode
.class file
Hello
Class!
week4
Java Virtual Machine (JVM)
Translates the bytecode to something the underlying
platform (computer) understands, and runs your program
4
BlueJ
week4
5
How BlueJ works
• BlueJ is a freely available program
 An interactive development environment (IDE)
designed to help students learn to program
 Makes it easy to sit down, write, and compile a
program
• we’ll see later that it’s not always easy in other environments
where you have to deal with classpaths, file systems, DOS
commands, etc
 Has visualization of class structure (very helpful for
beginning and advanced programmers)
 Can directly create objects and then interact with their
methods
week4
6
Programming Languages
• Languages can be:
 High level or low level
 Platform dependent or independent
 Procedural, functional, logic, or object-oriented
week4
7
Java is…
• A high-level,
• Object-oriented,
• Platform independent
Programming Language
• What does that mean?
week4
8
Java as a Language
• High-level – More like a real language, can be read by a
•
human and understood, made more for ease of use for
people than for the computer
Object-oriented – think about problems from the things
(objects) that are in the problem and what those objects
can do (methods)
 This allows you to be able to:
• design code in a natural way
• easily add new capabilities without retesting all the code
• Platform independent – code that you write on one
computer can be transported and run on another
computer without re-compiling
week4
9
Lab 1:
Introduction to Java and BlueJ
•
•
•
•
•
Open BlueJ (and make shortcut on desktop)
Make new project called “Eyes”
Add class from file (“JavaEyes.java”)
Compile it
Run it
 Call Methods on it
• Look at the code (open editor)
week4
10
Homework
• Installing Java and BlueJ at home (handout)
week4
11
APCS-A: Java
Classes, Objects and Methods in
Java
September 29, 2005
week4
12
OO Programming as Modeling
• The parts of the model are objects in the
problem domain
• What are the properties of objects?
 Objects can be organized into categories
 Objects do things
 Objects know things
week4
13
Classes
• (Just like we discussed with Alice…)
• A class describes (abstractly) a category of objects
 Think of a class as a blueprint for an object – it describes the boundaries
of what an object can know or can do
• How would we model/program a dog?
week4
14
Creating a Dog class in Java
public class Dog {
// This is a comment, used to describe code
This is the start of a one-line comment
}
CODE GOES HERE: what the Dog knows and what it does
Curly brace to end the class
week4
15
Object/Instance
• A specific description, an instance of a class
 A specific dog that we are modeling
• An object knows things and does things
 Also called attributes and behaviors
• Things an object knows are variables
 Data that is unique for an object of a given type
• Things an object does are methods
• Variables and methods are defined as part of the
class
 Variable values are set only for a specific instance
week4
16
Dog Class
public class Dog{
String breed;
String name;
int size;
These are variables.
Variables have a
data type and a name
public void bark(){ }
}
week4
This is a method.
Right now it doesn’t
do anything
A dog object would have individual values
for each of these variables
17
My Dog Instance
Breed: Bulldog
Name: Bully
Size: 24
Color: Purple
This dog can bark (invoke the method)
week4
18
What’s a Method?
• Things an object does
void bark() {
System.out.println(“Ruff! Ruff”);
}
This is a statement; this tells the computer
what you want to do in this method
week4
19
What’s a Statement?
• A line of code that gives a specific instruction
• Must end with a semicolon
int x; /* a declaration is a type of statement */
x = 5; // an assignment statement
System.out.println(“Ruff! Ruff”); /* a print
statement */
week4
20
What if you want to teach your
dog to bark differently?
• Make the dog say “Grrruffff”, “Bow-wow”,
or “Ruff Ruff”
 Could change the Dog program each time and
recompile like we did yesterday
 Or we could have the method take a
parameter (a String)
• {{ Explore this together in BlueJ }}
week4
21
Starting in BlueJ
BlueJ has a template that
gives you some starting code
(because I forgot to delete it)
It automatically creates:
•An instance variable
•A constructor
•An example method, with
example javadoc comments
week4
22
Lewis & Loftus: Chapter One
• Read/Skim it at your leisure - it is an overview of a lot of the ideas
that we will cover (or a basic intro to some of the things that we may
not have time to really discuss much)
 1.1 Computer Processing (overview, binary, analog/digital)
 1.2 Hardware Components (architecture, I/O devices, memory, cpu)
 1.3 Networks (connections, local-area & wide-area networks, Internet,
World Wide Web, urls)
 1.4 Java Programming Language (Sample program, comments,
identifiers & reserved words, white space)
 1.5 Program Development (language levels, editors, compilers &
interpreters, development environments, syntax & semantics, errors)
 1.6 Object-Oriented Programming (Problem-Solving, OO Software
Principles)
week4
23
Lab 2
• Code Experiment
• Messing up on purpose and understanding
error messages
week4
24