Upload
maya-johnston
View
217
Download
0
Tags:
Embed Size (px)
Citation preview
COMP 110:Introduction to Programming
Tyler JohnsonFeb 23, 2009
MWF 11:00AM-12:15PMSitterson 014
COMP 110: Spring 20092
Announcements
Lab 3 & 4 as well as Program 2 are graded
COMP 110: Spring 20093
Submitting Assignments
There is a penalty of 1-day late (25%) for all resubmissions
You spend a lot of time on these assignments
Spend a few minutes making sure you’ve submitted correctly
Follow the instructions for checking your jar fileCheck the name of the file blackboard has received
COMP 110: Spring 20094
Lab 3
COMP 110: Spring 20095
Today in COMP 110
A few random things
Objects & References
COMP 110: Spring 20096
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");
y > x
y <= x
Output
COMP 110: Spring 20097
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");
COMP 110: Spring 20098
Local Variables
What is the output of greet()?
public class Example {
private String str = “hello”;
public void foo() { String str = “goodbye”; }
public void greet() { foo(); System.out.println(str); }}
hello
Output
COMP 110: Spring 20099
Local Variables
What is the output of greet()?
public class Example {
private String str = “hello”;
public void foo() { str = “goodbye”; }
public void greet() { foo(); System.out.println(str); }}
9
goodbye
Output
COMP 110: Spring 200910
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”
}
COMP 110: Spring 200911
Accessors & Mutators
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 datadouble d = e.getData(); //ok, only way to get data
}
COMP 110: Spring 200912
DecimalFormat
import java.text.DecimalFormat;DecimalFormat df = new DecimalFormat("0.00");double number = 12.3456;System.out.println(df.format(number));
Output: 12.35"0.00" is the pattern that the format method will use to format its output
Two digits after the decimal point, one digit before (but it will display all digits if more than one before)
Fractional portion will be rounded
12
COMP 110: Spring 200913
DecimalFormat
DecimalFormat’s format method returns a StringBuffer, not a String, but you can still print out a StringBuffer
See Appendix 4 for more details
13
COMP 110: Spring 200914
Programming Demo
Grading ProgramTwo quizzes – 10 points eachA Midterm and Final – 100 points eachFinal Exam – 50%Midterm – 25%Quizzes – 25%
FunctionalityRead in a students score and display record
COMP 110: Spring 200915
Programming Demo
ApproachInstance variable for each scoreMethod to read inputMethod to display student record• Including final score and final grade (A-F)• Use two helper methods
– One to calculate final score– Other to get final grade
COMP 110: Spring 200916
Programming Demo
Programming
COMP 110: Spring 200917
Objects & References
Section 5.3 in text
COMP 110: Spring 200918
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 sayThe value of a is 6The value of d is 6.55The value of b is false
COMP 110: Spring 200919
Variables of a Class Type
What is the “value” of a variable of a class type?
Student jack = new Student(); //what is the value of jack?
Classes can have multiple data memberspublic class Student {
public String name; public int year; public double GPA; public String major;
//…
}
COMP 110: Spring 200920
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
Student jack = new Student(); //jack holds the address of the newly created
//object of the Student class
The address to this other location is called a reference to the object
Class types are also called reference types
COMP 110: Spring 200921
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;
}}
COMP 110: Spring 200922
Example: Books
Assume we have a class named Book
Book jacksBook = new Book();Book samsBook = new Book();//each object refers to a different book
vs.Book jacksBook = new Book();Book samsBook = jacksBook; //samsBook refers to the same object as jacksBook
COMP 110: Spring 200923
Objects in Memory
jacksBook
samsBook
?
?
Memory 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!
??
??
??
Java?
Java?
Java?
Java?
Java137
Java253
Java137
Java253
Java509
2078
?
2078
1056
?
?
?
2078
1056
2078
2078
2078
COMP 110: Spring 200924
Remember
Variables of a class type contain memory addresses
NOT objects themselves
COMP 110: Spring 200925
== 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
COMP 110: Spring 200926
== vs. equals() for Strings Explained
String is a class typeWhat is the result of
String s1 = new String("Hello");String s2 = new String("Hello");boolean strEqual = (s1 == s2);
strEqual is false! Why?s1 and s2 store different addresses!
COMP 110: Spring 200927
== vs. equals() for Strings explained
What is the results of
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
COMP 110: Spring 200928
Defining an equals Method
Every class has a default .equals() methodReturns whether two objects of the class are “equal” in some senseDoes not necessarily do what you want
You decide what it means for two objects of a specific class type to be considered equal by writing your own .equals() method
Perhaps books are equal if the names and page numbers are equalPerhaps only if the names are equalPut this logic inside .equals() method
COMP 110: Spring 200929
Writing the .equals() Method
public class Book {
private String name; private int page;
//…
//two books are equal if their name and pages are equal public boolean equals(Book book) { return (this.name.equals(book.name) && this.page ==
book.page); }}
COMP 110: Spring 200930
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
COMP 110: Spring 200931
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
COMP 110: Spring 200932
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
32
COMP 110: Spring 200933
Wednesday
Constructors