CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra...
-
Upload
debra-hart -
Category
Documents
-
view
220 -
download
0
Transcript of CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra...
![Page 1: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/1.jpg)
CPSC150
Spring 2007
Dr. L. Lambert
![Page 2: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/2.jpg)
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
![Page 3: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/3.jpg)
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
![Page 4: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/4.jpg)
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.edu2. click on mailing lists, and students and careers3. Subscribe using an email address that you read
regularly
![Page 5: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/5.jpg)
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)
![Page 6: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/6.jpg)
Next Steps
• Running Java Code• Reading Java Code• Writing Java Code
![Page 7: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/7.jpg)
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?
![Page 8: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/8.jpg)
Reading Source CodeA 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
![Page 9: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/9.jpg)
Parts of a Java program: Textbook 1.10
commentsreserved wordsmodifiers - public/private othersstatementsblocks { }classesmethodsmain method
![Page 10: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/10.jpg)
Parts of a Java program:
commentsreserved wordsmodifiers - public/private othersstatementsblocks { }classesmethodsmain method - not there. skip this
Find in Circle source code
![Page 11: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/11.jpg)
Writing a Programpublic 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; }}
![Page 12: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/12.jpg)
• Go to Mac lab or Use laptops
• Bring laptops to class
![Page 13: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/13.jpg)
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 }}
![Page 14: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/14.jpg)
Java program
import package; // comments and /* … */ and /** javadoc here */
public class Name{// instance variables or fields
// constructors. with no parameters is Default
// methods}
![Page 15: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/15.jpg)
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?
![Page 16: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/16.jpg)
Instance VariablesWhat does the following do?
public ATestClass ( ) {number = 3;//prints in terminal window
System.out.println("number is " + number); String mystring = "this string";name = "another string";name = new String("A Third String");System.out.println("name is " + name + "mystring is " + mystring + ");System.out.println(name + mystring);}
1. Add this constructor. Compile
2. Create an object. What happened? Why?
3. Inspect fields by right clicking, then choosing inspect
4. Change the names of the variables. What happens?
![Page 17: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/17.jpg)
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
![Page 18: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/18.jpg)
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
![Page 19: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/19.jpg)
• Back to lecture
![Page 20: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/20.jpg)
Java program
import package; // comments and /* … */ and /** javadoc here */
public class Name{// instance variables or fields
// constructors. with no parameters is Default
// methods}
![Page 21: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/21.jpg)
Class details: constructors• Initialize objects. Called when object is created• same name as class name• no return type• can be overloaded
public Circle(int x, int y, int size, String startColor) {
diameter = size;xPosition = x;yPosition = y;color = startColor;isVisible = false;
}
public Circle( ) {
diameter = 30;xPosition = 20;yPosition = 60;color = "blue";isVisible = false;
}
![Page 22: CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.](https://reader036.fdocuments.in/reader036/viewer/2022081603/56649e8f5503460f94b940bb/html5/thumbnails/22.jpg)
public Circle(int x, int y, int size, String startColor) {
diameter = size;xPosition = x;yPosition = y;color = startColor;isVisible = false;
}
public Circle( ) {
diameter = 30;xPosition = 20;yPosition = 60;color = "blue";isVisible = false;
}
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.
Circle c = new Circle( ); Circle d = new Circle(10, 20, 30, "green" );