Unformatted text preview:

Alice in Action with JavaObjectivesJava’s Math classSlide 4Slide 5The String TypeThe String Type (continued)Slide 8Slide 9Slide 10The boolean TypeSlide 12Slide 13The boolean Type (continued)Slide 15MethodsIntroductory Example: The Hokey Pokey SongSlide 18Slide 19Slide 20Methods (continued)Method DesignExercise for MethodsNon-void vs. void MethodsEinstein’s FormulaSlide 26Non-void Method exerciseHow to startMethod TesterMethod LibrariesProblem Description: Ballooning a BedroomProgram DesignProgram Design (continued)Slide 34Slide 35Slide 36Program ImplementationSlide 38Slide 39Slide 40Unit TestingSlide 42Test-Driven DevelopmentInstance MethodsBox ObjectsSlide 46Slide 47Slide 48Box Objects (continued)Slide 50Slide 51Slide 52Sphere ObjectsSlide 54Slide 55Slide 56The BalloonPrank Program Using ObjectsSlide 58Slide 59Classes, Methods, and DesignSlide 61Keywords, Identifiers, and ScopeSummarySummary (continued)Alice in Action with JavaChapter 9MethodsAlice in Action with Java 2Objectives•Use Math methods•Use string methods•Understand boolean type•Build your own Java methods•Define parameters and pass arguments to them•Distinguish between class and instance methods•Build a method libraryAlice in Action with Java 3Java’s Math class•Provides a set of math functions•Part of Java.lang, so you don’t have to import•Static methods (no need to create a Math guy)•Math methods most often take double arguments•Math class constants: Math.E and Math.PI•Example: compute volume of a sphere given radius–Formula: volume = 4/3 x PI x radius3–Implementation: double volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3.0);Alice in Action with Java 4Math ClassAlice in Action with Java 5Math ClassAlice in Action with Java 6The String Type•Used to store a sequence of characters•Example: String lastName = "Cat";•Different handles may refer to one String object•String literal: 0 or more characters between “ and ” –Escape sequences can also be used in String literals•String handle can be set to null or empty stringAlice in Action with Java 7The String Type (continued)•Instance method: message sent to an object•Class method: message sent to a class–Indicated by the word static•Java API lists a rich set of String operations•Example of instance method sent to String object –char lastInit = lastName.charAt(0); •Example of class method sent to String class–String PI_STR = String.valueOf(Math.PI);Alice in Action with Java 8The String Type (continued)Alice in Action with Java 9The String Type (continued)Alice in Action with Java 10The String Type (continued)•Concatenation–Joins String values using the + operator –At least one operand must be a String type•An illustration of concatenation–String word = "good"; word = word + "bye"; –Second statement refers to a new object–Garbage collector disposes of the de-referenced object•+=: the concatenation-assignment shortcut– Example: word += "bye";Alice in Action with Java 11The boolean Type•Holds one of two values: true (1) or false (0)•boolean (logical) expressions–Control flow of execution through a program•Relational operators (<, >, <=, >=, ==, !=)–Compare two operands and return a boolean value–May also be used to build simple logical expressions•Example of a simple boolean expression–boolean seniorStatus = age >= 65;–Produces true if age >= 65; otherwise falseAlice in Action with Java 12The boolean Type (continued)Alice in Action with Java 13The boolean Type (continued)Alice in Action with Java 14The boolean Type (continued)•Logical operators (&&, ||, and !)–Used to build compound logical expressions•Example of a compound logical expression–boolean liqWater; // declare boolean variable liqWater = 0.0 < wTemp && wTemp < 100.0;–wTemp must be > 0 and < 100 to produce true •Truth table–Relates combinations of operands to each operator–Shows value produced by each logical operationAlice in Action with Java 15The boolean Type (continued)Alice in Action with Java 16Methods•How to perform a method–Send a message to an object or class•Building a method in Alice–Click the create new method button–Drag statements into the method •Focus of Chapter 9–Learning how to build methods in Java•You have been creating main methods, and might have created other methods also in the last homeworkAlice in Action with Java 17Introductory Example: The Hokey Pokey Song•Problem: write a Java program to display song lyrics•Brute force approach–One String object stores the song lyrics–One action displays those lyrics–Implement program using one println()message –Issue: program is about 60 lines long (excessive)•A better approach takes advantage of song structure–Each verse only differs by the body part that is moved–Implement program with a single method to print verse–printVerse()takes one argument for the bodyPartAlice in Action with Java 18Introductory Example: The Hokey Pokey Song (continued)Alice in Action with Java 19Introductory Example: The Hokey Pokey Song (continued)Alice in Action with Java 20Introductory Example: The Hokey Pokey Song (continued)Alice in Action with Java 21Methods (continued)•Analyzing the first line of printVerse()–public: allows another class access to the method –static: indicates that the message is a class method–void: indicates that the method does not return a value –printVerse: the method’s name–(): contains parameters, such as String bodyPart–{: indicates the beginning of the method statements•Simplified pattern for a Java method[AccessMode] [static] ReturnType MethodName (Params) {Statements}Alice in Action with Java 22Method Design•Procedure for developing a method–Figure out inputs and outputs (story) of the whole problem. (Test data here can help)–Figure out what repeats or is complex enough to splice out - > These are your methods•Maybe flow chart the main routine•Figure out the inputs and outputs for each method•List test data for the inputs and outputs•Determine Locals: variables and constants declared in a methodExercise for MethodsYour task: Print a story that says: I am a lonely cat, and I really like talking to you.I am a sad cat, and I really like talking to you.I am a mad cat, and I really like talking to you.You came home!I am a happy cat, and I really like talking to you.-----Remember to figure out what your methods will be and what your main program flow will be.


View Full Document

ADELPHI CSC 160 - Methods

Documents in this Course
Load more
Download Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Methods 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?