1 / 12

Agenda

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.

hestia
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda • Remarks about last homeworks • Methods(functions) • Class methods(static methods) • Presentations topic • homework

  2. 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.

  3. FlowChart for GradeSystemConverter Start Get numeric grade from user Determine and display letter grade End

  4. 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

  5. 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.

  6. 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>){…}

  7. Example public static void main(String[] args) Smiling face applicatin example public void printSmileyface() { }

  8. 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

  9. 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

  10. Methods(functions) • Has its own block of codes, enclosed by curley braces • Called from main() by its method names

  11. 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

  12. 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

More Related