Review Classes Standard methods to String for converting












- Slides: 12
Review: Classes – Standard methods to. String() - for converting to a String for printing. . . public String to. String() { return "(" + x + ", " + y + ")"; } equals() - for comparing if fields are the same public boolean equals(Object obj) { if (obj instanceof Point) { Point point 2 = (Point) obj; return x == point 2. x && y == point 2. y; } else { return false; } } the keyword this – the implicit parameter public void set. Location(int x, int y) { this. x = x; this. y = y; }
Activity Design a class called Time to be used in an appointment book to hold the times of appointments for a day. What fields should we have? What constructors? What methods?
Review : Working With Files Create a File object: File f = new File("example. txt"); Open the file for reading with a Scanner object: Scanner input = new Scanner(f); For now, just throw the File. Not. Found. Exception to whoever called you. . method(. . . ) throws File. Not. Found. Exception { } . . . Scanner input = new Scanner(f); . . .
Review : Input Cursor A Scanner views all input as a stream of characters. The current position is called the input cursor. 10 20 30n 40 50nn 60n ^ input. next() -> 10 10 20 30n 40 50nn 60n ^ input. next() -> 20 10 20 30n 40 50nn 60n ^ input. next() -> 30 10 20 30n 40 50nn 60n ^ Calling next() is called "consuming input".
Review: File Processing It is extremely common to organize file by lines. Use line based processing. Read the file line by line, and then do further reprocessing of lines if necessary. Can eliminate throws clause from the method by catching the exception. We will not be using that on the final. The try/catch statement is used for handling exceptions. try { File. Not. Found. Exception <statements>; } catch (File. Not. Found. Exception e) { <statements>; }
• We create a Print. Stream() on a File() to write to it. To write to a Print. Stream we use print(), println(), and printf() just like with System. out. • File f = new File(filename); Print. Stream output = new Print. Stream(f); output. println("stuff for the file"); Output. println(“Whatever”);
Review: Arrays array: stores many values of the same type element: one value in an array index: desired element from 0 to a. length – 1 int[] a = new int[10]; index 0 1 2 3 4 5 6 7 8 9 value 0 0 0 0 0 Arrays are automatically initialized to the "zero" value. Array elements are selected with a[index] and can be used exactly the same as regular variables. a[0] += a[i] + a[i+1] + 7; Special syntax for initializing arrays with preset values. int[] primes = {2, 3, 5, 7, 11, 13, 17}; For-each loop for processing each value in an array for (int num : a) { sum += num; }
Review: Arrays are objects and are stored by reference. They can be passed as parameters into methods and returned as the result of a method. // return the maximum value in an array public static int max(int[] a) { int max = a[0]; for (int val : a) { if (val > max) { max = val; } } return max; } Since they are passed be reference, any modifications to an array inside a method are seen by the caller. Multi-dimensional arrays: int[][][] tic. Tac. Toe 3 D = new int[4][4][4];
Review: Classes and Objects A class is a template for creating objects. Write each class in its own. java file of the same name. Classes/objects have fields, constructors, and methods. Point. java public class Point { int x, y; public Point(int xval, int yval) { x = xval; y = yval; } public void translate(int dx, int dy) { x += dx; y += dy; }. . .
Review Instance methods are called with: <object>. <method>(<args) Class methods have an added static in their declaration and are called with: <class>. <method>(<args>) Class methods can not use any instance fields because they aren't invoked by specifying which instance to use. You can control the visibility (encapsulation) of fields and methods with: public – everyone can see it private – only visible within this class
Review: Classes – Standard methods to. String() - for converting to a String for printing. . . public String to. String() { return "(" + x + ", " + y + ")"; } equals() - for comparing if fields are the same } public boolean equals(Object obj) { return x == point 2. x && y == point 2. y; the keyword this – the implicit parameter public void set. Location(int x, int y) { this. x = x; this. y = y; }
Activity Design a class called Time to be used in an appointment book to hold the times of appointments for a day. What fields should we have? What constructors? What methods?