agenda
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Agenda
• Remarks about last homeworks• Methods(functions)• Class methods(static methods)• Presentations topic• homework
FlowChart for GradeSystemConverter
The GradeSystemConverter algorithm: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
Get numeric grade from user
Start
End
Determine and display letter grade
Procedural programming1. Gather ingredients: 1c flour, 1c
butter, 1c sugar, 2 eggs2. Preheat oven to 350 degrees3. Beat eggs and butter4. Add flour and sugar5. Mix6. 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.
• 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>){…}
Writing methods
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:
1. Print a smiling face – no parameters2. RectanglePerimeter application that
calculates and displays the perimeter of a rectangle with width 4 and length 13 –
can be modified by passing parameters ofwidth and length.
3. Modify the GradeConversionSystem, writea 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 classMath.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