COMP 110 Introduction to Programming Tyler Johnson Feb
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11: 00 AM-12: 15 PM Sitterson 014
Announcements Lab 3 & 4 as well as Program 2 are graded 2 COMP 110: Spring 2009
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 file Check the name of the file blackboard has received 3 COMP 110: Spring 2009
Lab 3 4 COMP 110: Spring 2009
Today in COMP 110 A few random things Objects & References 5 COMP 110: Spring 2009
If-Statement Example What is the output? int x = 7; int y = 5; if(y > x) x = x + y; System. out. println("y > x"); Output y>x y <= x System. out. println("y <= x"); 6 COMP 110: Spring 2009
If-Statement Remember to include curly braces for ifstatement 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"); 7 COMP 110: Spring 2009
Local Variables What is the output of greet()? public class Example { } 8 private String str = “hello”; Output public void foo() { String str = “goodbye”; } hello public void greet() { foo(); System. out. println(str); } COMP 110: Spring 2009
Local Variables What is the output of greet()? public class Example { } 9 private String str = “hello”; Output public void foo() { str = “goodbye”; } goodbye public void greet() { foo(); System. out. println(str); } COMP 110: Spring 2009 9
Accessors & Mutators public class Example { private double data; //private, can not be accessed directly //from OUTSIDE the class public double get. Data() { //this is an accessor return data; //this method is the ONLY way to access } //“data” public void set. Data(double new. Data) { //this is a mutator data = new. Data; //this method is the ONLY way to } //change “data” } 10 COMP 110: Spring 2009
Accessors & Mutators public static void main(String[] args) { Example e = new Example(); e. data = 6. 5; //not allowed, data is private e. set. Data(6. 5); //ok, only way to set data double d = e. get. Data(); //ok, only way to get data } 11 COMP 110: Spring 2009
Decimal. Format import java. text. Decimal. Format; Decimal. Format df = new Decimal. Format("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 200912
Decimal. Format’s format method returns a String. Buffer, not a String, but you can still print out a String. Buffer See Appendix 4 for more details 13 COMP 110: Spring 200913
Programming Demo Grading Program 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 COMP 110: Spring 2009
Programming Demo Approach Instance variable for each score Method to read input Method 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 15 COMP 110: Spring 2009
Programming Demo Programming 16 COMP 110: Spring 2009
Objects & References Section 5. 3 in text 17 COMP 110: Spring 2009
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 18 COMP 110: Spring 2009
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 members public class Student { public //… String name; int year; double GPA; String major; } 19 COMP 110: Spring 2009
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 20 COMP 110: Spring 2009
Example: Books public class Book { private String name; private int page; public void set. Page(int page) { this. page = page; } public void set. Name(String name) { this. name = name; } } 21 COMP 110: Spring 2009
Example: Books Assume we have a class named Book jacks. Book = new Book(); Book sams. Book = new Book(); //each object refers to a different book vs. Book jacks. Book = new Book(); Book sams. Book = jacks. Book; //sams. Book refers to the same object as jacks. Book 22 COMP 110: Spring 2009
Objects in Memory jacks. Book ? 2078 sams. Book ? 1056 2078 Book jacks. Book; Book sams. Book; jacks. Book = new Book(); jacks. Book. set. Name("Java"); sams. Book = new Book(); sams. Book. set. Name("Java"); ? 1056 ? Java ? 253 jacks. Book. set. Page(137); sams. Book. set. Page(253); ? 2078 ? Java ? 137 509 sams. Book = jacks. Book; sams. Book. set. Page(509); jacks. Book is now on p. 509! 23 COMP 110: Spring 2009
Remember Variables of a class type contain memory addresses NOT objects themselves 24 COMP 110: Spring 2009
== 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 25 COMP 110: Spring 2009
== vs. equals() for Strings Explained String is a class type What is the result of String s 1 = new String("Hello"); String s 2 = new String("Hello"); boolean str. Equal = (s 1 == s 2); str. Equal is false! Why? s 1 and s 2 store different addresses! 26 COMP 110: Spring 2009
== vs. equals() for Strings explained What is the results of String s 1 = new String("Hello"); String s 2 = new String("Hello"); boolean str. Equal = (s 1. equals(s 2)); str. Equal is true! Why? String’s. equals() method checks if all the characters in the two Strings are the same 27 COMP 110: Spring 2009
Defining an equals Method Every class has a default. equals() method Returns whether two objects of the class are “equal” in some sense Does 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 equal Perhaps only if the names are equal Put this logic inside. equals() method 28 COMP 110: Spring 2009
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); } } 29 COMP 110: Spring 2009
Parameters of a Primitive Type public void increase. Num(int num) { num++; } public void foo() { int x = 5; increase. Num(x); System. out. println(x); } What is the output? 5 30 COMP 110: Spring 2009
Parameters of a Class Type public void change. Book(Book book) { book = new Book("Biology"); } public void foo() { Book jacks. Book = new Book("Java"); change. Book(jacks. Book); System. out. println(jacks. Book. get. Name()); } What is the output? Java 31 COMP 110: Spring 2009
Parameters of a Class Type public void change. Book(Book book) { book. set. Name("Biology"); } public void foo() { Book jacks. Book = new Book("Java"); change. Book(jacks. Book); System. out. println(jacks. Book. get. Name()); } What is the output? Biology 32 COMP 110: Spring 200932
Wednesday Constructors 33 COMP 110: Spring 2009
- Slides: 33