Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Similar presentations


Presentation on theme: "COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014."— Presentation transcript:

1 COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014

2 COMP 110: Spring Announcements Lab 3 & 4 as well as Program 2 are graded

3 COMP 110: Spring 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 youve submitted correctly Follow the instructions for checking your jar file Check the name of the file blackboard has received

4 COMP 110: Spring Lab 3

5 COMP 110: Spring Today in COMP 110 A few random things Objects & References

6 COMP 110: Spring 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

7 COMP 110: Spring 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");

8 COMP 110: Spring 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

9 COMP 110: Spring 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

10 COMP 110: Spring 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 }

11 COMP 110: Spring 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 data double d = e.getData(); //ok, only way to get data }

12 COMP 110: Spring DecimalFormat import java.text.DecimalFormat; DecimalFormat df = new DecimalFormat("0.00"); double number = ; System.out.println(df.format(number)); Output: "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

13 COMP 110: Spring DecimalFormat DecimalFormats format method returns a StringBuffer, not a String, but you can still print out a StringBuffer See Appendix 4 for more details 13

14 COMP 110: Spring 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

15 COMP 110: Spring 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

16 COMP 110: Spring Programming Demo Programming

17 COMP 110: Spring Objects & References Section 5.3 in text

18 COMP 110: Spring 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

19 COMP 110: Spring 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; public int year; public double GPA; public String major; //… }

20 COMP 110: Spring 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

21 COMP 110: Spring 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; }

22 COMP 110: Spring 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

23 COMP 110: Spring 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 ? Java 137 Java 253 Java 137 Java 253 Java ? ???? ?

24 COMP 110: Spring Remember Variables of a class type contain memory addresses NOT objects themselves

25 COMP 110: Spring == 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

26 COMP 110: Spring == vs. equals() for Strings Explained String is a class type What 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!

27 COMP 110: Spring == 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? Strings.equals() method checks if all the characters in the two Strings are the same

28 COMP 110: Spring 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

29 COMP 110: Spring 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); }

30 COMP 110: Spring 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

31 COMP 110: Spring 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

32 COMP 110: Spring 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


Download ppt "COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014."

Similar presentations


Ads by Google