Announcements Final Exam TBD public static void mainString
Announcements • Final Exam: TBD
public static void main(String [] args) { final int ASIZE = 5; int [] int. Array= new int[ASIZE]; for(int i = 0; i < ASIZE; i++) int. Array[i] = i; System. out. println( int. Array[0]); change. Zero(int. Array); System. out. println( int. Array[0]); } static void change. Zero(int [] passed. Array) { passed. Array[0] = 1000; }
Initializing Arrays char [] c. Array 3 = {'a', 'b', 'c'}; int [] i. Array = {1, 2, 3, 4}; double [] d. Array = {3. 4, 5. 67 e 4};
Two Dimensional Arrays char [][] c. Array = new char[10][20]; int [][] i. Array = new int[100][50]; c. Array[0][0] = ‘a’; c. Array[0][1] = ‘b’; c. Array[9][19] = ‘x’; i. Array[0][0] = 99; i. Array[1][5] = 135; i. Array[99][49] = 0;
Math Class • • • Contains Typical Math Methods In Package java. lang (i. e. , automatically imported) Access using Math. math_thing Math. PI; Math. E (log e = 1) double pow (double base, double exp) double sqrt(double number) int round(double number) int abs( int number) min(number 1, number 2) and max(number 1, number 2) double random(); //random number between 0. 0 and 1. 0
Tokenizing/Parsing • Taking Apart a String and Separating It into Smaller Strings (i. e. , “tokens”) • Usually, Tokens are Items Separated by Whitespace • Tokens Can Be Defined with Different Separators • Parsing Takes Tokens and Applies Some Manipulation of Those Tokens
String. Tokenizer Class import java. util. *; … String big. String = “This is a sentence. ”; String. Tokenizer st = new String. Tokenizer(big. String); while (st. has. More. Tokens()) { System. out. println(st. next. Token()); } // Displays This, a, and sentence. one // per line
Classes • A Class Is an Object Type • A Class Represents a “Thing” (e. g. , employee, wrench, list, store, shopping cart, etc. ) • Service Class – Class used by Other Programs • Programmers Define Classes with Data Members (or Fields) and Methods • Must Be Created in Class. Name. java File • Client Program – Program using a class
Access Modifiers • Used to Identify What May Use Methods • public – any method may use (Usually methods) • private – only methods in same class may use (usually data members)
Designing a Class • Decide How It Will Be Used • Decide on Interface (i. e. , public representation, public methods) • Decide on Implementation (i. e. , private data and data manipulation)
Example Class Employee first. Name last. Name salary
Constructor • Class Method of Same Name • Example: class Employee Method Employee() • Called When Variable (invoking object) Declared (instantiated) of Class Type • Initializes Instantiated Object • May Have Multiple Constructors Each with Different Parameters
class Employee // Employee. java { private String first. Name; private String last. Name; private double salary; public Employee() { // set attributes of invoking object first. Name = "No. First. Name"; last. Name = "No. Last. Name"; salary = 0. 0; }
Accessor Methods • Public Methods for Getting Attributes of Class
class Employee { private String first. Name; private String last. Name; private double salary; public Employee() { first. Name = "No. First. Name"; last. Name = "No. Last. Name"; salary = 0. 0; } public String Get. First. Name() { return first. Name; } public String Get. Last. Name() { return last. Name; } public double Get. Salary() { return salary; }
Client Program • A Program that Uses a Class Is a Client of that Class • Class Objects are Declared and Instantiated Using the new keyword. • new Class. Name(parameters) Calls Constructor for Class • Class Public Methods Called Using Dot (e. g. , variable. Get. First. Name(); ) • variable is the invoking object
class use. Employee //use. Employee. java { public static void main(String [ ] args) { Employee emp 1 = new Employee(); System. out. println("Name is" + emp 1. Get. First. Name() + " " + emp 1. Get. Last. Name()); } first. Name } emp 1 “No. First. Name” last. Name “No. Last. Name” salary 0. 0
Mutator Methods • Class Methods Used to Modify a Class Object are Called Mutator Methods • Allows Restricted Manipulation of Class Object Data
public boolean Set. Salary(double passed. Salary) { if (passed. Salary <= MAXSALARY) { salary = passed. Salary; return true; } else { return false; } }
class use. Employee { public static void main(String [ ] args) { Employee emp 1 = new Employee(); System. out. println("Name is" + emp 1. Get. First. Name() + " " + emp 1. Get. Last. Name()); if (emp 1. Set. Salary(55000)) { System. out. println("Salary set to 55000"); } else { System. out. println("Salary not set"); } } }
Static Variables and Methods • static means “in class” methods and variables • static variable: one per class (not one per object) • static method: no invoking object (invoke with class. Name. method()) • Example: Math class methods
Static Variable Example public class My. Class { … private final int STARTID = 1; //the following are one per object private int IDNum = -1; //the following are one per class private static int next. IDNum = STARTID; public My. Class() { … IDNum = next. IDNum; next. IDNum++; }
Static Method Example public class My. Class { … private final int STARTID = 1; //the following are one per object private int IDNum = -1; //the following are one per class private static int next. IDNum = STARTID; … public static int Get. Next. ID() { // Called by My. Class. Get. Next. ID() return next. IDNum; } }
Other Class Methods • boolean equals( obj. Type com. Obj) – compares invoking object with passed object – Returns true if both are “equal”, false if not • void display() – Displays attributes • String to. String() – Converts all attributes to String and returns String
- Slides: 24