230 likes | 237 Views
This announcement covers important information such as the deadline for Homework W1, regrade policies, the availability of consultants, and the topics covered in Lecture 4, including the Class String, printing output, accessing modifiers, and constructors.
E N D
Announcements • Homework W1 is due today • Regrades: Ok, but we will regrade the entire assignment. Must be w/in 72 hours of when returned • Consultants start on Sunday. Alan is often in the lab as well. Lecture 4
Today’s Topics • Review • The Class String • Printing output: System.out.print, System.out.println, and System.out.flush() • Accessing modifiers public and private • Functions versus procedures, void • Constructors Lecture 4
Review • Classes are conceptually similar to types, like int, char, double. • Classes are programmer-defined • Objects are instances of classes. Objects of a particular class type get all the fields and methods associated with that class. Lecture 4
Review example Coordinate c1; Coordinate d1; c1 = new Coordinate(); d1 = new Coordinate(); c1.setX(3); d1.setX(4); d1.setY(2); c1.setY(d1.sumSquares()); d1.setY(c1.y); Lecture 4
Today’s example // An instance of Employee contains a person's name, // salary, and year hired. It has a constructor and // methods for raising the salary, printing the data, and // retrieving the person's name and the year hired. public class Employee { public String name; // The person's name publicdouble pay; // The person's yearly salary publicint hireDate; // The year hired Lecture 4
// Set the name to n publicvoid setName(String n) {name= n;} // Set the salary to s publicvoid setSalary(double s) {pay= s;} // Set the year to y publicvoid setHireDate(int y) {hireDate= y;} // Yield the year the person was hired publicint hireYear() {return hireDate;} Lecture 4
// Raise the pay by p percent publicvoid raiseSalary(double p) {pay= pay * (1 + p/100.0);} // Yield the person's name public String getName() {return name;} // Yield a String containing person’s data public String toString() { String s= name; s= s + " " + pay + " " + hireDate; return s;} } Lecture 4
Class String • An instance of class string is a “string” of characters: abcde, oo145^^!2l, . . . • Declare asString d; • Assigned as: d = new String(“L. Millett”) // just like other classesord = “L. Millett” // only because strings are common • Lots of methods available in the String class Lecture 4
Some String Methods • charAt(int index) // return char at index • endsWith(String suffix) // return true if . . . • toLowerCase() // return string w/no caps • length() // return length of the string String str1 = “Strings are cool.”; int n; if (str1.endsWith(“ool.”)) System.out.println(“Yes”); n = str1.length(); String str2 = str1.toLowerCase(str1); Lecture 4
Catenating Strings • Use the infix operator ‘+’ -- here it means something different than addition: • d + “xyz” + “ ” + “qed”evaluates to“Lyn Millettxyz qed” • Note that you need to explicitly put in spaces “ ” where you want them Lecture 4
millett // s1 millett // s2 String equality • Suppose: • if (s1 == s2)returns false! • Use if (s1.equals(s2)) instead • equals is another method of class String Lecture 4
Printing output • To print string s in the output window, use System.out.print(s) or System.out.println(s); • The latter prints an “end of line” after s • The following are equivalent:(1) System.out.print(s1); System.out.println(s2);(2) System.out.println(s1 + s2); Lecture 4
Flush • // Flush the output buffer// Make sure everything is sent to the screen • System.out.flush(); • Use this when you want to be sure the output is seen immediately • println automatically flushes, print does not • See pp.86-87 of text. Lecture 4
Now, back to our example • Accessing fields of an Employee objectEmployee c;c= new Employee();c.name= “G”;c.pay= 1000000;c.hireDate= 1998;c.pay= 220000; • Anyone see any problems here? Logically? Lecture 4
Security Issue • Allowing access to the pay field means that any program that can reference c can change pay. • This could be bad . . . • Changing a ‘sensitive’ field like pay should be limited to methods of the class • How to do this in Java? Lecture 4
Public/Private • // Anyone can access salarypublic int salary; • // Only methods w/in Employee can // reference payprivate int pay; • The following is now illegal:Employee c;c = new Employee();c.pay = 1000000; Lecture 4
So, how to change pay? • Now to change pay it’s necessary to call the method setSalary or raiseSalary • If the methods were declared private, then this would not be possible except from within the Class Lecture 4
Procedures vs. Functions • Method raiseSalary has prefix voidpublic void raiseSalary(double p); • void indicates that it does not return a result -- a procedure • Method getName has prefix Stringpublic String getName(); • A non-void prefix indicates that a result is returned -- a function Lecture 4
A Function • // Yield the person’s namepublic String getName() { return Name; } • Recall: Executing return <expression> terminates execution of the method (function) and returns the value of <expression> to the place of the call • String s = e1.getName(); • Think of functions in math: f(g(h(x))) • Can do similar things in Java Lecture 4
Constructors • A regular method except: • its name is the same as the name of the Class • it has no type class prefix of void • Use to initialize fields//Constructor: name n, year d hired, salary spublic Employee(String n, double s, int d) {name= n; pay= s; hireDate= d; } • c = new Employee(“millett”, 50000, 1999) Suuuuuuuure... Lecture 4
Another class (p. 142 in text) Class Rectangle { int length, width; public Rectangle (int side1, int side2) { length = side1; width = side2; } public int area () { return (length * width); } Lecture 4
Use of class Rectangle int userLength = Integer.parseInt(stdin.readLine()); int userWidth = Integer.parseInt(stdin.readLine()); Rectangle r1; r1 = new Rectangle(userLength, userWidth); System.out.println(“The area of your rectangle is: ”); System.out.println(r1.area()); Rectangle r2 = new Rectangle(5, 10); if (r2.area() > r1.area()) { System.out.println(“Your rectangle is smaller.”); } Lecture 4
What Have We Learned? • How to define a class -- fields, methods • Definition of a class variable • Creation of a new instance of a class • Referencing and assigning fields • Assigning class instance to class variable to create an alias • Class methods • Class constructors Lecture 4