Download 09F-NoteSet12 - Lyle School of Engineering

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSE 1341 Honors
Professor Mark Fontenot
Southern Methodist University
Note Set 12
Classes and Objects - 1
• Object-Oriented Programming
– software development methodology
– development is based on looking at the problem
from the perspective of what objects are
interacting in the system.
– i.e. University Registration System
•
•
•
•
Students
Professors
Classrooms
Courses with multiple class sections
What have we been doing so far?
• Procedural programming
– Thinking about the different sub-procedures of
how to solve a problem.
– Data and procedures are separate and unrelated
• except for the fact that procedures operate on the data
– e.g. Average grades for students:
• Procedural: getGrades, averageGrades, dispalyAverage
Classes and Objects - 2
• An Object is a programming construct that
• has state (contains data)
– i.e. every student has an id and name, but values
may be different for each student
• and has behavior (contains methods)
– interface allows us to access and manipulate the
data in a controlled environment.
– i.e. Functionality to retrieve the students name
from the object or update the students GPA.
Some Object Semantics
• We communicate with an object by sending
messages to the object.
• In Java, we send messages by calling methods
on the object.
Scanner s = new Scanner (System.in);
s.nextInt();
s.nextDouble();
Creating a scanner object
Sending message to scanner
obj to tell it to read the next
integer from the keyboard.
Sending message to scanner
obj to tell it to read the next
double from the keyboard.
Primitive vs. Reference Variables
• Primitive Variable – a variable with a primitive
data type
– primitive data types are part of the core Java
language
– short, int, long, char, byte, boolean, double, float.
• Reference Variable – a variable with a nonprimitive data type
– EVERYTHING ELSE!
– String, Scanner, TouchSensor
Primitive Variables
• Can be declare with out “creating” using the
keyword new.
• We access the variable directly
– Doesn’t have an interface of methods through
which to access functionality and data.
– Programmer can use the relational operators to
compare
int a = 10;
int b = 20;
if (a < b) {
//…
}
Reference Variables
• Used to refer to objects
• Must use the new operator to create an
object of a particular type
– new Scanner(System.in);
– new returns a reference (memory location) that is
stored in the reference variable
– Scanner s = new Scanner (System.in);
returns a memory location
that is then stored in s
The Java API
• API = Application Programming Interface
• Contains about 3000+ classes that are already
implemented and tested
• You can use this so as to not re-invent the wheel.
– Scanner is a perfect example
– Scanner class contains the code to create a scanner
object that can allow you to read from a data source.
– String is another example
• We’ll explore this in much more depth
throughout the semester.
Classes vs. Objects
• A class is a blueprint for an object.
– It tells the JVM how to construct an object when
the programmer asks for one
Class Student
Student
Objects of Type Student
Mark
12345678
4.0
Name:String
id:String
gpa:float
void getName()
void PrintGPA
Sam
11223344
3.8
Joe
22334455
3.7
The String Class
• You can have multiple Strings in your program
Each created based on same String
blueprint
String fName = “Mark”;
String lName = “Fontenot;
String school = “Southern Methodist University”;
• Can call any method from the String class on
any of the String objects.
int len = fName.length();
System.out.println(lName.charAt(2));
if (school.equals(“TCU”))
System.out.println(“Boo!”);
How do you know what methods are available??
• Look at the API documentation…
– http://java.sun.com/j2se/1.5.0/docs/api/index.html
– Lists all of the methods that you can call on a java String
object
– There is an API doc page for every class that is part of the
Java API.
BREAKOUT 1