csc 212 object-oriented programming and java part 1
TRANSCRIPT
![Page 1: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/1.jpg)
CSC 212CSC 212
Object-Oriented Programming Object-Oriented Programming and Javaand Java
Part 1Part 1
![Page 2: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/2.jpg)
Announcements
Watch the CSC212 webpage for announcements
No lab section today --- go to Mass If you need additional Java help, speak up
and get extra help now. We’re going fast.The “Getting help” page contains links to
several on-line Java tutorials.
![Page 3: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/3.jpg)
A Quick Overview of Java
Java is modern object-oriented languagePrograms are written via Java classesClasses define the types for instance
variables (“objects”) Classes contain 2 groups of members:
Descriptions of data (fields)Algorithms for manipulating data (methods)
![Page 4: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/4.jpg)
Classes and Objects: First Look Class : a blueprint for an object Objects : defined by and created from classes (blueprints)
House Floor Plan
Houses w/same Floor Plan
Figure from Lewis and Loftus: Java Software Solutions, p.123
house158
new house158(asphalt, clapboards)
new house158(asphalt, stucco)
new house158(tile, stucco)
![Page 5: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/5.jpg)
Object TerminologyWhat Are Classes?A class defines a "type" for objects. A class definition includes all of the fields and methods common to all objects of that "type". All Java code is contained within a class. Software classes usually model classes of real-world objects you find in everyday life.
What Is an Object?An object is an instance of a class, like a variable is an instance of a primitive type.
What Are Fields?Fields define what data can be stored within each object of a class.
What Are Methods?Methods define the operations on objects of a class. Typically, methods modify the data stored in an objects’ fields.
![Page 6: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/6.jpg)
Object-Oriented Programming
Procedural programming: procedures and functions are active; data are passive.
Object-orientation: data - objects - are active; methods serve the data.Java supports primitive types: ints, floats, Strings,
arrays…BUT can also have Cars, Motorcycles, People and
any other type that's important to your problem.
![Page 7: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/7.jpg)
Classes and Objects
Classes specify data and behavior of objects.
Fields describe what the class is. Methods describe what the class does.
![Page 8: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/8.jpg)
Classes and Objects
Objects of the same class must have the same fields……but field values may differ.
Objects of the same class must have the same methods……but may behave differently because of
different values in the fields and arguments.
![Page 9: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/9.jpg)
Structure of a Class Definition
Class name with class modifiers Fields – typed with field modifiers Constructors – build class instances
Typically how to initialize the instance's data fields
Methods – manipulate data to get resultsAlso have visibility attributes
![Page 10: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/10.jpg)
Student Class
public class Student {
// declare the fields
// define the constructors
// define the methods
}
![Page 11: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/11.jpg)
Student Class
What data defines a student?
![Page 12: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/12.jpg)
The Student Variables
public class Student {
public String name, studentID;
protected int years_attended;
private float gpa, credits;
public static int total_enrollment;
// define the constructors
// define the methods
} // end of class definition
![Page 13: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/13.jpg)
Constructors for StudentConstructors are special methods which create instances. Typically initialize fields values. (They can do more–later)
public Student (String sname, long ssn) { name = sname; studentID = Long.toString(ssn); years_attended = 0; gpa = credits = 0; total_enrollment = total_enrollment++; }
![Page 14: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/14.jpg)
Additional Constructors Classes can have several constructors
Must differ in parameter lists (signatures).
public Student (String sname, String id) { name = sname; studentID = id; years_attended = 0; gpa = credits = 0; total_enrollment++; }
public Student () { name = “J Doe”; studentID = “none”; years_attended = 0; gpa = credits = 0; total_enrollment++; }
![Page 15: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/15.jpg)
The Student Class public class Student {
protected String name, studentID;
protected int years_attended;
private float gpa;
public static int total_enrollment;
public Student(String sname, long ssn)
{ …}
public Student(String sname, String id)
{ …}
public Student()
{ …}
// define the methods
} // end of class definition
![Page 16: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/16.jpg)
Java classes can have many methods. Methods describe what a class does or
what can be done to the classShould be the starting point of defining a classE.g. Should start by asking: What does this
data do? What is the effect of this data?
Methods
![Page 17: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/17.jpg)
Methods for Student Class
What methods would this class define?
![Page 18: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/18.jpg)
Update a Student's GPA
/** Add grades and update GPA*/void addClass(float creditHrs, float grade) { gpa = (gpa*credits+grade*creditHrs)
/ (credits+creditHrs); credits += creditHrs; // shorthand: credits = credits +creditHrs}
![Page 19: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/19.jpg)
void setId(String newId) { studentID = newId;}
Set and Get Methods
Provide set and get methods for each fieldControlling access to fieldsLimits errors and problems (or amount of
searching when debugging) Common design pattern
public String getId() { return(studentID);}
![Page 20: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/20.jpg)
Why Use Set Methods?
public void setId(long new_id){ if (new_id < 0) { studentID=Long.toString(0); System.out.println("Bad value, ID set to 0"); } else
studentID = Long.toString(new_id);}
Public String name, studentID
Two ways to set studentID:
s.set_id(150);
s.studentID = "150";
Which is better programming practice? (and why)?
![Page 21: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/21.jpg)
Why use get & set methods?
What are the advantages of using get & set methods?
![Page 22: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/22.jpg)
Access Protection
All classes, methods, and fields can be:public - Accessible by anyone from anywherepackage [default] - Accessed only by members in
the same package Methods and fields can also be:
private - Only class instances can access itprotected - Accessible by instances of the same
class, subclasses & package members
![Page 23: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/23.jpg)
Access Protection
Public elements enable access to the classes functionality.
Private (or protected) elements hide inner workings of the class.
Access protection is an important part of proper object-oriented progamming
![Page 24: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/24.jpg)
Access Protection Benefits
Enforce constraints on an object's state Provide simpler client interface
Abstraction:Make available only what people must
knowEncapsulation:
Separate interface from implementation
![Page 25: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/25.jpg)
Rules of Thumb
Classes are public Fields are private
Outside access only using “get” and “set” methods
Constructors are public Get and set methods (if any) are public Other methods on a case-by-case basis
![Page 26: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/26.jpg)
Instantiation
New instances are created (“instantiated”) using new:
Student r1 = new Student("Bob", 1050);
The variable storing the new object instance. The types must match.
Create a new instance of the Student class
A call to one of the constructors for the Student class.
This instance will have the name "Bob" and a studentID of "1050".
![Page 27: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/27.jpg)
Java Variables and Values
Every variable has a typePrimitive
boolean char byte, short, int, long float, double
Class Defined by the programmer
![Page 28: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/28.jpg)
Java Variables and Values
Variables contain:Values of primitive type
int x = 20; int y = x; y = y + 3; // No change to value in x
References to instances Student s1 = new Student("Bob", 150); Student s2 = s1; //s1 and s2 refer to SAME object s2.add_class(4.0,2.5); // GPA of s1 also updated String str = s1.getId(); // str == "150"!
![Page 29: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/29.jpg)
Formal and Actual ParametersStudent s1 = new Student("Bob", 150);
Public Student(String sname, long ssn) { name = sname; studentID = Long.toString(ssn); years_attended = 0; gpa = credits = 0;
total_enrollment; = total_enrollment++; }
In a constructor (or any method) call:
In class Student - the constructor for Student
Actual Values (couldbe variables, too)
Actual values transmitted to corresponding formal parameters. Alignment is done left to right, one to one. Actual and formal parameters must have matching types. Transmission is done by copying.
Formal parameters - values obtained from corresponding actual parameters
![Page 30: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/30.jpg)
Method main()
public class Student {…public static void main(String[] args) {
Student s1 = new Student("Bob", 1050); s1.add_class(4.0, 3.5); System.out.print(s1.get_id + "\t"); System.out.println(Float.toString(s1.gpa)); } }
Each program starts at special method called “main” Any class can have a main method Adding main to class can serve as debugging tool
![Page 31: CSC 212 Object-Oriented Programming and Java Part 1](https://reader034.vdocuments.site/reader034/viewer/2022052302/56649e915503460f94b96886/html5/thumbnails/31.jpg)
Daily Quiz
Do problem R-1.9 from the book (p. 52)For this quiz, no documentation is required