120 likes | 281 Views
Agenda. Remarks about last homeworks Methods(functions) Class methods(static methods) Presentations topic homework. FlowChart for GradeSystem C onverter. The GradeSystemConverter algorithm: 1. Prompt the user for a number.
E N D
Agenda • Remarks about last homeworks • Methods(functions) • Class methods(static methods) • Presentations topic • homework
FlowChart for GradeSystemConverter The GradeSystemConverteralgorithm: 1. Prompt the user for a number. 2. Determine the letter grade that corresponds to the entered numeric grade and display the result. 3. Repeat steps 1 and 2 until the user chooses to quit.
FlowChart for GradeSystemConverter Start Get numeric grade from user Determine and display letter grade End
Procedural programming Gather ingredients: 1c flour, 1c butter, 1c sugar, 2 eggs Preheat oven to 350 degrees Beat eggs and butter Add flour and sugar Mix Bake 10 minutes
Methods • Procedural programming – step by step instructions; linear order of instructions • A function is a ``black box'' that we've locked part of our program into, it performs some well-defined task, which will be useful to other parts of the program. • Idea of reuse.
Writing methods • A method consists of a declaration and a body. • Method declaration includes: access level, return type, name, and parameters, if any. • The method body contains the statements that implement the method. <access level> <return type> <method names> (<parameter>){…}
Example public static void main(String[] args) Smiling face applicatin example public void printSmileyface() { }
Methods signature • access level: public, private • Return type: void, int, double, char..any variable types • Method names: defined by yourselves • Parameters: can have 0 or more than 1
Example • Transfer the two applications into functions: • Print a smiling face – no parameters • RectanglePerimeterapplication that calculates and displays the perimeter of a rectangle with width 4 and length 13 – can be modified by passing parameters of width and length. 3. Modify the GradeConversionSystem, write a function which takes a number grade and returns a letter grade
Methods(functions) • Has its own block of codes, enclosed by curley braces • Called from main() by its method names
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
Homework • Modify the program of printing a star triangle upside down using method. • Write a public method, return type void, parameters 2, one for length, one for symbol. • The method uses one for loop to loop to the length,print the symbol. • Call the method 6 times to produce the same drawing as last homework