Download presentation
Presentation is loading. Please wait.
1
Announcements Lab 5 due Wednesday at noon
2
Questions? Classes, Instance Vars, & Methods visibility
accessors/mutators parameters interface versus implementation
3
Today in COMP 110 Review Some common mistakes (recent)
Programming Demo Objects & References
4
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
5
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
6
Programming Constructs
Variables/Expressions Conditionals Loops Methods Classes (also enumerations) More to come...
7
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
8
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");
9
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
10
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
11
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” }
12
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 }
13
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
14
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
15
Programming Demo Programming
16
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
17
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();
18
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
19
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;
20
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
21
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
22
Remember Variables of a class type contain memory addresses
NOT objects themselves
23
== 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
24
== 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!
25
== 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
26
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
27
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); }
28
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
29
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
30
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
31
Wednesday Constructors
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.