Announcements Lab 5 due Wednesday at noon
Questions? Classes, Instance Vars, & Methods visibility accessors/mutators parameters interface versus implementation
Today in COMP 110 Review Some common mistakes (recent) Programming Demo Objects & References
Review Started with simple arithmetic Needed choice Needed repetition variables and expressions Needed choice Perform different actions based on different inputs if statements switch statements Needed repetition Can do known number of iterations without loops Just copy and paste code (Triangle) For variable number of iterations: while, do-while, for loops
Review Duplicating code is hazardous prone to mistakes, tedious to change (Sandwiches) consolidate replicated code into a method may want the method to take in a value (parameter) may want the method to return a value (return type) Duplicating data is also hazardous finite amount must be known at compile-time run out of variable names! code becomes too long and not readable Use classes instead
Programming Constructs Variables/Expressions Conditionals Loops Methods Classes (also enumerations) More to come...
If-Statement Example What is the output? int x = 7; int y = 5; if(y > x) x = x + y; System.out.println("y > x"); System.out.println("y <= x"); Output y > x y <= x
If-Statement Remember to include curly braces for if-statement bodies that include multiple statements int x = 7; int y = 5; if(y > x) { x = x + y; System.out.println("y > x"); } System.out.println("y <= x");
Local Variables What is the output of greet()? Output hello public class Example { private String str = “hello”; public void foo() { String str = “goodbye”; } public void greet() { foo(); System.out.println(str); Output hello
Local Variables What is the output of greet()? Output goodbye public class Example { private String str = “hello”; public void foo() { str = “goodbye”; } public void greet() { foo(); System.out.println(str); Output goodbye 10
Accessors & Mutators public class Example { private double data; //private, can not be accessed directly //from OUTSIDE the class public double getData() { //this is an accessor return data; //this method is the ONLY way to access } //“data” public void setData(double newData) { //this is a mutator data = newData; //this method is the ONLY way to } //change “data” }
Accessors & Mutators public class ExampleTest { public static void main(String[] args) { Example e = new Example(); e.data = 6.5; //not allowed, data is private e.setData(6.5); //ok, only way to set data double d = e.getData(); //ok, only way to get data }
Programming Demo Grading Program Functionality Two quizzes – 10 points each A midterm and final – 100 points each Final Exam – 50% Midterm – 25% Quizzes – 25% Functionality Read in a students score and display record
Programming Demo Approach Instance variable for each score Method to read input Method to display student record Including total score and final grade(A-F) Use two helper methods One to calculate total score Other to get letter grade
Programming Demo Programming
Variables of a Primitive Type Variables of primitive type hold a value int a = 6; double d = 6.55; boolean b = a > d; We can say The value of a is 6 The value of d is 6.55 The value of b is false
Variables of a Class Type Classes can have multiple data members public class Student { public String name; public int year; public double GPA; public String major; //… } What is the “value” of a variable of a class type? Student jack = new Student();
Variables of a Class Type The value of a variable of a class type is a memory address The address of the object it refers to The address to this other location is called a reference to the object Class types are also called reference types
Example: Books public class Book { private String name; private int page; public void setPage(int page) { this.page = page; } public void setName(String name) { this.name = name;
Example: Books Assume we have a class named Book vs. Book jacksBook = new Book(); Book samsBook = new Book(); vs. Book samsBook = jacksBook; //samsBook now refers to the same object as jacksBook
Objects in Memory Memory jacksBook samsBook 2078 1056 2078 ? 2078 ? ? Book jacksBook; Book samsBook; jacksBook = new Book(); jacksBook.setName("Java"); samsBook = new Book(); samsBook.setName("Java"); jacksBook.setPage(137); samsBook.setPage(253); samsBook = jacksBook; samsBook.setPage(509); jacksBook is now on p. 509! jacksBook samsBook 2078 1056 2078 ? 2078 ? ? 2078 ? 1056 2078 Java ? 137 ? ? Java Java ? Java 253 137 Java 253 509
Remember Variables of a class type contain memory addresses NOT objects themselves
== Operator on Objects The == operator checks whether the values of two variables are the same The value of class variable is a memory address When using the == operator to compare two objects, you are checking whether they have the same address in memory
== vs. equals() for Strings Explained String is a class type What happens when you have String s1 = new String("Hello"); String s2 = new String("Hello"); boolean strEqual = (s1 == s2); strEqual is false! Why? s1 and s2 store different addresses!
== vs. equals() for Strings explained What happens when you have String s1 = new String("Hello"); String s2 = new String("Hello"); boolean strEqual = (s1.equals(s2)); strEqual is true! Why? String’s .equals() method checks if all the characters in the two Strings are the same
Defining an equals Method Every class has a default .equals() method if it is not explicitly written Does not necessarily do what you want You decide what it means for two objects of a specific class type to be considered equal Perhaps books are equal if the names and page numbers are equal Perhaps only if the names are equal Put this logic inside .equals() method
Writing the .equals() Method public class Book { private String name; private int page; //… public boolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); }
Parameters of a Primitive Type public void increaseNum(int num) { num++; } public void foo() { int x = 5; increaseNum(x); System.out.println(x); What is the output? 5
Parameters of a Class Type public void changeBook(Book book) { book = new Book("Biology"); } public void foo() { Book jacksBook = new Book("Java"); changeBook(jacksBook); System.out.println(jacksBook.getName()); What is the output? Java
Parameters of a Class Type public void changeBook(Book book) { book.setName("Biology"); } public void foo() { Book jacksBook = new Book("Java"); changeBook(jacksBook); System.out.println(jacksBook.getName()); What is the output? Biology 30
Wednesday Constructors