Agenda • • • • • • Remarks about last homeworks Class methods(static methods) Class signature Arrays Presentations topic homework Static methods • BPJ lesson 20; Barron’s chapter 2 • Static methods are sometimes called class methods. • The difference between static and no-static methods: we are accessing them at the class level rather than the object level. • Math class vs Random class Math.sqrt(4.0); Random.nextInput(); => wrong public static void main(String[] args) { testMethod2.calculateRectanglePerimeter( 4, 13); //testMethod2.printSmiley(); testMethod2 tM = new testMethod2(); tM.printSmiley(); } Practice • Create a class Aclass with two methods, one with static keyword, and one without. This class doesn’t need to have main(). • Create another class Bclass with a main() to call the methods in Aclass. Try to see the differences about static and non-static methods calling. Arrays • An array is a structure that can store many of the same kind of data together at once. • An array has a fixed length and can contain only as many data items as its length allows: • Array of size 4 1 2 3 4 Philip Costa Minna Justin Friends Arrays • An Array elements: can be any data type • Each element has an index value, with 0 being the index of the first item, 1 the index of the second item, and so on. • Declaration: String[] friends = new String[5]; //5 friends int[] intArray = new int[20]; char[] charArray = new char[40]; Arrays • Array Declaration <type>[] <name>; //declare array <name> = new <type>[<num>]; //allocate space for elements • Array declaration and initialization String[] friends = {"Kermit", "Lucille", "Sammy", "Roxy"}; • Practice array declaration and initialization with every primitive types. • Example: int[] intArray = {10, 20, 30}; • Do the exercise on Lesson 18 exercise 1 - 6 Array index 1 2 3 4 Philip Costa Minna Justin Friends Array index starts from 0, assume that we want to create a Friends array like above: String[] Friends = new String[4]; Friends[0] = “Philip”; Friends[1] = “Costa”; Friends[2] = “Minna”; Friends[3] = “Justin”; Array Accessing example doulbe[] sgt = new double[20]; for(int i = 0; i <sgt.length; i++) sgt[i] = (double) i; for(int i = 0; i <sgt.length; i++) System.out.println("sgt element " +sgt[i]); Class signature <access _ level> class <name> { <variables> <constructors> <methods> } Classes • A class is written in a separate file and consists of a declaration and a body. • The class declaration includes the access level, the keyword class, and the class name. • The class body contains variables, constructors, and methods. • Constructors are used to initialize variables in a class. Variables and methods are called the members of a class Homework Create a StudentRoster application that prompts the user for the number of students in the class and then prompts the user for each student’s name and stores the names in an array. After all the names have been entered, the application should display the title ”Student Roster” and then list the names in the array.