CSE 121 Lesson 6: Methods and Parameters Overview

cse 121 lesson 6 methods parameters n.w
1 / 11
Embed
Share

Explore the concepts of methods and parameters in Java programming through practical examples and visual aids. Get ready for Programming Assignment 1 and Quiz 0 while mastering nested loops, random number generation, and creating custom methods. Engage with interactive sessions and prepare for in-depth learning in Autumn 2024.

  • Java Programming
  • Methods
  • Parameters
  • Nested Loops
  • Random Numbers

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSE 121 Lesson 6: Methods & Parameters Matt Wang & Brett Wortzman Autumn 2024 Abby Afifah Ailsa Alice Aliyan Arohan Chlo Christopher Dalton Derek Elizabeth Ethan TAs: Hanna Hannah Heather Hibbah Janvi Jasmine Judy Julia Kelsey Lucas Luke Mahima Maitreyi Maria Merav Minh Neha Ronald Ruslana Sahej Sam Samrutha Sushma Vivian sli.do #cse121 Today s playlist: 121 24au lecture tunes Yijia Zachary Lesson 5 - Autumn 2024

  2. Announcements & Reminders Resubmission Cycle 0 (R0) due tomorrow, October 17 Programming Assignment 1 (P1) out today, due October 22 note: a big jump from C1. Start early! Quiz 0 next week in section (Thursday, October 24) Topics: everything up to and including today s lecture More review in upcoming sections & lectures Post-section work required for extra resub: 12 -> 10 Lesson 6 - Autumn 2024

  3. Last Time 1 Nested for loops Syntax & conventions: ( i , Applications: doing the same thing for multiple iterations j , k) for (int outerLoop = 1; outerLoop <= 5; outerLoop++) { System.out.println("outer loop iteration #" + outerLoop); for (int innerLoop = 1; innerLoop <= 7; innerLoop++) { System.out.println(" inner loop iteration #" + innerLoop); } System.out.println(outerLoop); } Lesson 6 - Autumn 2024

  4. Last Time 2 Random A Random object generates pseudo-random numbers nextInt(max) returns random int value [0, max) i.e. between 0 andmax-1 rand.nextInt(6) + 1 Lesson 6 - Autumn 2024

  5. (PCM) Methods Writing our own methodsallow us to define our own statements / commands in Java! Naming conventions for methods are the same as variables: camelCased public static void myMethod() { /*** Your code here * * / } Lesson 6 - Autumn 2024

  6. Poll in with your answer! What is the output of this program? public class HelloGoodbye { public static void main(String[] args) { welcome(); hello(); goodbye(); } sli.do #cse121 public static void hello() { System.out.print("Hello! "); glad(); } Welcome! Glad you re here. Hello! Glad you re here. Goodbye! A. C. Welcome! Hello! Goodbye! public static void goodbye() { System.out.println("Goodbye!"); } Welcome! Glad you're here. Hello! Glad you're here. Goodbye! Welcome! Hello! Goodbye! B. D. public static void welcome() { System.out.print("Welcome! "); glad(); } public static void glad() { System.out.println("Glad you're here."); } } Lesson 6 - Autumn 2024

  7. (PCM) Parameters Definition: A value passed to a method by its caller public static void myMethod(String musicalAct) { System.out.print(musicalAct + " is the best!"); ... } Calling a method with a parameter myMethod("Rush"); // Rush is the best! Lesson 6 - Autumn 2024

  8. (PCM) Scope Definition: The part of a program where a variable exists (and can thus be referenced/modified/used). From its declaration to the end ofthe { } braces (kind of) Ex: a variable declared in a for loop only exists in thatloop! for (int outerLoop = 1; outerLoop <= 5; outerLoop++) { System.out.println("outer loop iteration #" + outerLoop); for (int innerLoop = 1; innerLoop <= 3; innerLoop++) { System.out.println(" } System.out.println(outerLoop); } outerloop s scope innerloop s scope inner loop iteration #" + innerLoop); Lesson 6 - Autumn 2024

  9. (PCM) Scope Definition: The part of a program where a variable exists (and can thus be referenced/modified/used). From its declaration to the end ofthe { } braces (kind of) Ex: a variable declared in a method exists only in thatmethod! public static void example() { System.out.println("hello"); int x = 3; for (int i = 1; i <= 10; i++) { System.out.print(x); } } x's scope i's scope Lesson 6 - Autumn 2024

  10. Method Comments! Now that we know how to write methods, we have a new form of documentation (using comments) to write. Each method you write (except for main) should be accompanied by a short comment that describes what it does. // Randomly generates an addition problem where the // operands are in the range 1-10 (inclusive), and prints the result // rounded to two decimal places. public static void addTwoRandomNumbers() { Random randy = new Random(); int num1 = randy.nextInt(10) + 1; int num2 = randy.nextInt(10) + 1; int sum = num1 + num2; ... } Lesson 6 - Autumn 2024

  11. Class Constants A fixed value visible to the whole program (the entire class). Value can be set only at declaration; cannot be reassigned (so the value is constant) public static final type NAME_OF_CONSTANT = expression; Lesson 6 - Autumn 2024

Related


More Related Content