Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements Program 2 is due tomorrow by noon Lab 4 was due today

Similar presentations


Presentation on theme: "Announcements Program 2 is due tomorrow by noon Lab 4 was due today"— Presentation transcript:

1 Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Midterm coming up Lab 5 and Program 3 will be assigned Friday (Bring laptops Friday)

2 Questions? Classes, Instance Variables, Methods

3 Today in COMP 110 More Classes, Instance Vars, & Methods visibility
accessors/mutators parameters interface versus implementation

4 Defining the Class Student
Class name public class Student { public String name; public int year; public double GPA; public String major; // ... public String getMajor() { return major; } public void increaseYear() { year++; // … Data (instance variables) Methods

5 Local/Instance variables
Declared in a class Can be used anywhere in the class that declares the variable, including inside the class’ methods Local variables Declared in a method Can only be used inside the method that declares the variable

6 Simple Example year and name are instance variables
public class Student { public String name; public int year; // ... public void printInfo() { String info = name + ": " + year; System.out.println(info); } public void increaseYear() { year++; public void decreaseYear() { year--; year and name are instance variables can be used in any method in this class info is a local variable declared inside method printInfo() can only be used inside method printInfo()

7 Simple Example public class Student { public String name; public int year; // ... public void printInfo() { String info = name + “: ” + year; System.out.println(info); } public void increaseYear() { year++; info = “My info string”; // ERROR!!! public void decreaseYear() { year--; The compiler will not recognize the variable info inside of method increaseYear()

8 More about Local Variables
public static void main(String[] args) { Student jack = new Student(); jack.name = “Jack Smith”; jack.major = “Computer Science”; String info = “Hello there!”; System.out.println(info); System.out.println(jack.name + “ is majoring in ” + jack.major); Student sam = new Student(); sam.name = “Samantha Smart”; sam.major = “Biology”; System.out.println(sam.name + “ is majoring in ” + sam.major); } Variable info in main method not affected by variable info in printInfo method in class Student

9 Named Constants in Classes
We can declare named constants as instance variables public class Circle { public double radius; //normal instance variable public final double PI = ; //named constant //… }

10 Methods with Parameters

11 Methods with Parameters
Parameters are used to hold the value that you pass to the method Parameters can be used as (local) variables inside the method public int square(int number) { return number * number; } Parameters go inside parentheses of method header

12 Calling a Method with Parameters
public class Student { public String name; public int year; // ... public void setName(String studentName) { name = studentName; } public void setClassYear(int classYear) { year = classYear;

13 Calling a Method with Parameters
public static void main(String[] args) { Student jack = new Student(); jack.setName(“Jack Smith”); jack.setClassYear(3); } Arguments

14 Methods with Multiple Parameters
Multiple parameters separated by commas public double getTotal(double price, double tax) { return price + price * tax; }

15 Method Parameters and Arguments
Order, type, and number of arguments must match parameters specified in method heading public String getMessage(int number, char c) { return number + ":" + character; } object.getMessage(7, 'c'); //ok object.getMessage(7.5, 'c'); //error object.getMessage(7, 6); //error = ???

16 Automatic Type Casting
public class SalesComputer { public double getTotal(double price, double tax) { return price + price * tax; } // ... SalesComputer sc = new SalesComputer(); double total = sc.getTotal(“19.99”, Color.RED); double total = sc.getTotal(19.99); double total = sc.getTotal(19.99, 0.065); int price = 50; total = sc.getTotal(price, 0.065); Automatic typecasting

17 Call-by-Value Parameters in Java are passed by value
The value of a variable is passed, not the variable itself Variables passed to a method can never be changed public class Example { public void setVariable(int a) { a = 7; } public static void main(String[] args) { Example example = new Example(); int a = 5; //a == 5 example.setVariable(a); //does not change the value of a

18 The Keyword this Within a class definition, this is a name for the receiving object Frequently omitted, but understood to be there public class Student { public int year; public void increaseYear() { this.year++; //use of this not necessary in this case } 18

19 The Keyword this this indicates the receiving object
(The object the method was called on) public static void main(String[] args) { Student jack = new Student(); jack.increaseYear(); Student sam = new Student(); sam.increaseYear(); } public class Student() { public int year; public void increaseYear() { this.year++; }

20 Calling Methods within Methods
It’s possible to call methods within other methods If calling a method of the same class, no need to specify receiving object public void method1() { method2(); //no object needed, current object assumed }

21 The Keyword this Why don’t we need to specify an object when calling between methods of the same class? Use of this keyword is implied public void method1() { method2(); } public void method1() { this.method2(); } ===

22 Calling Methods within Methods
public class Example { public void method1() { System.out.println(“method1!"); method2(); //no object needed, current object assumed } public void method2() { System.out.println(“method2!"); public static void main(String[] args) { Example example = new Example(); //create object of class Example example.method1(); System.out.println("Finished!");

23 Public vs Private public void increaseYear() public int year; public: there is no restriction on how you can use the method or instance variable

24 Public vs Private private double gpa; private int year; private: can not directly use the method or instance variable’s name outside the class

25 Example public class Student { public int year; private String major; } Student jack = new Student(); jack.year = 1; jack.major = “Computer Science”; OK, year is public Error!!! major is private

26 More about Private Hides instance variables and methods inside the class/object Invisible to external users of the class Users cannot access private class members directly Information hiding

27 Private Instance Variables
Instance variables should be declared private Force users of the class to access instance variables only through methods Gives you control of how programmers use your class Why is this important?

28 Example: Rectangle public class Rectangle { public int width; public int height; public int area; public void setDimensions( int newWidth, int newHeight) { width = newWidth; height = newHeight; area = width * height; } public int getArea() { return area; Rectangle box = new Rectangle(); //set dimensions and calculate area box.setDimensions(10, 5); System.out.println(box.getArea()); // Output: 50 (correct) //access instance variable directly box.width = 6; // Output: 50 (wrong answer!)

29 Accessors and Mutators
How do you access private instance variables? Accessor methods (a.k.a. get methods, getters) Allow you to look at data in private instance variables Mutator methods (a.k.a. set methods, setters) Allow you to change data in private instance variables

30 Example: Student Mutators Accessors
public class Student { private String name; private int age; public void setName(String studentName) { name = studentName; } public void setAge(int studentAge) { age = studentAge; public String getName() { return name; public int getAge() { return age; Mutators Accessors

31 Private Methods Why make methods private? Encapsulation
Helper methods that will only be used from inside a class should be private External users have no need to call these methods Encapsulation

32 Encapsulation Hiding details of a class that are not necessary to understand how objects of the class are used Two parts Interface Implementation

33 Class Interface A class interface tells programmers all they need to know to use the class in a program A class interface includes Headings for public methods public Return_Type Method_Name(Parameters) Defined constants public static final Return_Type Var_Name = Constant; Comments for using public methods /* Precondition: Postcondition: */

34 Implementation The implementation of a class consists of the private elements of the class definition Private instance variables and constants private Type Var_Name; Private methods private Return_Type Method_Name() Bodies of public methods

35 Encapsulation Implementation should not affect behavior described by interface Two classes can have the same behavior but different implementations

36 Two Implementations of Rectangle
public class Rectangle { private int width; private int height; private int area; public void setDimensions( int newWidth, int newHeight) { width = newWidth; height = newHeight; area = width * height; } public int getArea() { return area; public class Rectangle { private int width; private int height; public void setDimensions( int newWidth, int newHeight) { width = newWidth; height = newHeight; } public int getArea() { return width * height;

37 Encapsulation Interface: Programmer Who Uses the Class:
Comments Headings of public methods Public named constants Implementation: Private instance variables Private constants Private methods Bodies of public methods Programmer Who Uses the Class: Create objects Call methods

38 Guidelines Comments before class definition
Class header Declare instance variables as private Use get & set methods to access instance variables Pre and post comments before methods Declare helper methods as private /**/ for interface comments and // for implementation comments

39 Calculator Class Demo

40 Friday Recitation Bring Laptops (fully charged)


Download ppt "Announcements Program 2 is due tomorrow by noon Lab 4 was due today"

Similar presentations


Ads by Google