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

12
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

13
COMP 110: Spring 200913 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 200914 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 200915 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

18
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 say The value of a is 6 The value of d is 6.55 The value of b is false

19
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 members public class Student { public String name; public int year; public double GPA; public String major; //… }

20
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

22
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

25
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

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

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