Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 Object-Oriented Programming and Java Part 1.

Similar presentations


Presentation on theme: "CSC 212 Object-Oriented Programming and Java Part 1."— Presentation transcript:

1 CSC 212 Object-Oriented Programming and Java Part 1

2 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.

3 A Quick Overview of Java Java is modern object-oriented language  Programs are written via Java classes  Classes define the types for instance variables (“objects”) Classes contain 2 groups of members:  Descriptions of data (fields)  Algorithms for manipulating data (methods)

4 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)

5 Object Terminology What 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.

6 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.

7 Classes and Objects Classes specify data and behavior of objects. Fields describe what the class is. Methods describe what the class does.

8 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.

9 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 results  Also have visibility attributes

10 Student Class public class Student { // declare the fields // define the constructors // define the methods }

11 Student Class What data defines a student?

12 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

13 Constructors for Student Constructors 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++; }

14 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++; }

15 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

16 Java classes can have many methods. Methods describe what a class does or what can be done to the class  Should be the starting point of defining a class  E.g. Should start by asking: What does this data do? What is the effect of this data? Methods

17 Methods for Student Class What methods would this class define?

18 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 }

19 void setId(String newId) { studentID = newId; } Set and Get Methods Provide set and get methods for each field  Controlling access to fields  Limits errors and problems (or amount of searching when debugging)  Common design pattern public String getId() { return(studentID); }

20 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)?

21 Why use get & set methods? What are the advantages of using get & set methods?

22 Access Protection All classes, methods, and fields can be:  public - Accessible by anyone from anywhere  package [default] - Accessed only by members in the same package Methods and fields can also be:  private - Only class instances can access it  protected - Accessible by instances of the same class, subclasses & package members

23 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

24 Access Protection Benefits Enforce constraints on an object's state Provide simpler client interface  Abstraction: Make available only what people must know  Encapsulation: Separate interface from implementation

25 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

26 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".

27 Java Variables and Values Every variable has a type  Primitive boolean char byte, short, int, long float, double  Class Defined by the programmer

28 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"!

29 Formal and Actual Parameters Student 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 (could be 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

30 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

31 Daily Quiz Do problem R-1.9 from the book (p. 52)  For this quiz, no documentation is required


Download ppt "CSC 212 Object-Oriented Programming and Java Part 1."

Similar presentations


Ads by Google