Transcript CPSC150

CPSC150
Spring 2007
Dr. L. Lambert
CPSC150 Overview
• Syllabus
• Use Textbook, ask questions, extra
thorough, I will post sections covered
• All information and assignments posted on
CPSC150 web page:
– On web now, first homework
• For anything you don’t see on the web
page, send me email
• All grades posted on WebCT
Success in this Class
•
•
•
•
•
•
•
•
Collaborative
Come prepared to work in class
Take notes; pay attention; ask questions
Never procrastinate
Don’t assume you know/can program easily
Come see me/Java expert early and often
Ask questions
Consult with (possibly non-expert) peers, but be
careful. >50% of your grade is exams. Make
sure you are doing your own work before
you take the exam
Success in the Department
•
•
Join ACM, IEEE, SWE
Study with others.
– Right now, find others and exchange:
major, contact information, one thing
about you
• Sign up for department info.
1. open browser, go to www.pcs.cnu.edu
2. click on mailing lists, and students and careers
3. Subscribe using an email address that you read
regularly
Using Java and BlueJ
•
•
•
•
We will use BlueJ for program development
BlueJ runs on the Java virtual machine
BlueJ is IDE – lots of others (e.g., Eclipse)
BlueJ is free and available for Mac, Windows,
UNIX
• Download BlueJ for your home machines for
development: www.bluej.org
• (download Java 1.5 first):
http://java.sun.com/j2se/1.5.0/download.jsp
(Download SDK, NOT JRE)
Next Steps
• Running Java Code
• Reading Java Code
• Writing Java Code
Running a program:
BlueJ Demo: Shapes
•
•
•
•
•
Classes - everything in BlueJ is a class
Object Bench - holds objects in BlueJ
Objects
constructors - create an object
Calling methods
– makeVisible, changeSize, moveHorizontal
• Parameters: what info does method need?
Reading Source Code
A Java program
import package; // if any
// comments and /* … */ and /** javadoc here */
public class Name
{
// instance variables or fields
// visible to all methods in class
// constructors. with no parameters is Default
// methods
}
Look at: Circle source code
Parts of a Java program:
Textbook 1.10
comments
reserved words
modifiers - public/private others
statements
blocks { }
classes
methods
main method
Parts of a Java program:
Find in Circle source code
comments
reserved words
modifiers - public/private others
statements
blocks { }
classes
methods
main method - not there. skip this
Writing a Program
public class Sphere // in your code, put blank lines and {s on lines by
themselves
{ private double radius; // instance var should always be private
// other types: double, int, char, boolean, String (note capital), others
public Sphere( ) { // constructor is named same as class name.
radius = 1.0; }
public Sphere(double myRadius) { // second constructor
radius = myRadius; }
// double is type that is returned
public double calculateVolume( ) {
double volume; // local variable. no private/public
// = is assignment. this calculation is not quit right. why?
volume = 4 / 3 * 3.14149 * radius * radius * radius;
return volume; }
}
• Go to Mac lab or Use laptops
• Bring laptops to class
Write, Compile and Test
Listing Figure 1.2, page 22
/** This program displays a message */
import javax.swing.JOptionPane; // case matters
public class Message // begin classes with capital
{
// no variables in this program
/** Constructor is named the same thing as class.
In textbook, code is in main, and call to
JOptionPane has another parameter. either is ok */
public Message( )
{
JOptionPane.showMessageDialog(null, "Print your message here");
// or System.out.println("Print your message here"); on terminal
}
}
Java program
import package;
// comments and /* … */ and /** javadoc here */
public class Name
{
// instance variables or fields
// constructors. with no parameters is Default
// methods
}
Which are ok?
public class ATestClass {
/* A */ private int number;
/* B. */ public int number;
/* C. */ public Int number;
/* D. */ private String name;
/* E. */ String name;
// constructor on next page
} // keep the valid ones
Create a class
and type in the
following. Which
are ok?
Compile. See
what compiles.
Why do the
others not
compile? Which
compile, but are
not ok?
Instance Variables
What does the following do?
public ATestClass ( ) {
number = 3;
//prints in terminal window
1. Add this
constructor.
Compile
System.out.println("number is " + number); 2. Create an object.
What happened?
String mystring = "this string";
Why?
name = "another string";
3. Inspect fields by
name = new String("A Third String");
right clicking,
System.out.println("name is " + name +
then choosing
"mystring is " + mystring + ");
inspect
System.out.println(name + mystring);
4. Change the
}
names of the
variables. What
happens?
Class details:
fields/instance variables
• private type name;
– always private
– name is lowercase (Classes are caps)
• Visible to all methods in class
– local variables visible only to that method
• Can be primitive type or Object type
Primitive vs Objects
• private int myField; // primitive
– char, boolean, double, int, a few others
• private Square wall; // Object type
– user and library defined
– look at Picture and inspect
– primitive has data; object has pointer
1. Open Picture in Examples. Compile. Create a Picture
object
2. Inspect wall field/instance variable
3. Add an integer field
4. Inspect it
• Back to lecture
Java program
import package;
// comments and /* … */ and /** javadoc here */
public class Name
{
// instance variables or fields
// constructors. with no parameters is Default
// methods
}
Class details: constructors
•
•
•
•
Initialize objects. Called when object is created
same name as class name
no return type
can be overloaded
public Circle( )
{
diameter = 30;
xPosition = 20;
yPosition = 60;
color = "blue";
isVisible = false;
}
public Circle(int x, int y, int size, String startColor)
{
diameter = size;
xPosition = x;
yPosition = y;
color = startColor;
isVisible = false;
}
public Circle( )
public Circle(int x, int y, int size, String startColor)
{
{
diameter = 30;
diameter = size;
xPosition = 20;
xPosition = x;
yPosition = 60;
yPosition = y;
color = "blue";
color = startColor;
isVisible = false;
isVisible = false;
}
}
Circle c = new Circle( );
Circle d = new Circle(10, 20, 30, "green" );
1. On the board, write a Circle constructor with a color
parameter. call: new Circle("yellow");
2. Write a variable declaration for Circle e. Create
the circle using your new constructor.