CSCE145 F2026 05 Methods
Overview
JJ Shepherd explains how Java methods organize functional code into reusable actions, covering method declarations, access scope, return types, parameters, calls, and the call stack. A worked unit-converter program applies these ideas through input validation, six inches/feet/centimeters conversions, and separate methods for prompting and displaying results.
Key takeaways
- A Java method declaration specifies where it can be called, what value it returns, its name, the information it accepts, and the code it executes.
- Use parameters to pass values into methods and a non-void return type to send computed information back; for example, inchesToFeet(double inches) returns inches divided by 12.
- Java methods called from main in this example require constructing an instance with new, then calling the method through that object.
- The call stack is last-in, first-out: nested methods finish in reverse order and return control to their caller.
- The converter's isValidUnit method centralizes case-insensitive checks for inches, centimeters, and feet, avoiding repeated validation logic in the main loop.
- Splitting the converter into methods for prompts, validation, conversions, and results makes each action easier to locate and debug.
Chapters
- Methods organize growing programs so code can be reused, extended, and debugged more easily.
- Java code is structured in projects, classes, and methods; functional statements such as loops and conditionals belong inside method bodies.
- The system calls main as the program's entry point; programmers can define additional methods for distinct actions.
- A method declaration combines scope, return type, identifier, parameter list, and a body enclosed in braces.
- public methods can be called inside or outside their class; private methods can only be called within their class.
- A return type such as String or double requires a matching return statement; void indicates that the method returns no value.
- Method names follow variable identifier rules and use lowercase starts with camelCase for multiword names, such as getGreetingString.
- Parameters pass information into a method and are declared with both a type and an identifier, such as double inches.
- Multiple parameters are separated by commas; the inches-to-centimeters example returns inches multiplied by 2.54.
- The body holds functional code, including local variables, branching statements, and loops; method parameters and locals are limited to their method.
- Methods can return computed values, as in a boolean comparison that checks whether integer A is greater than integer B.
- Calling or invoking a method transfers execution to its body, then resumes at the call site when the method completes.
- A method called within its defining class uses its identifier and arguments; calls across classes require public access and an instance.
- An instance is constructed with new, as in Scanner keyboard = new Scanner(System.in), then its methods are called with dot notation.
- The main method creates a GreetingsProgram object before calling its printGreetings method.
- The main method begins on the call stack; each invoked method is pushed on top and removed when it completes.
- Because the stack is last-in, first-out, nested calls such as start → printOne → printTwo → printThree finish in reverse call order.
- Changing the order of method calls changes console output, while each completed method returns execution to the statement that invoked it.
- The MeasureConverter example starts by constructing an object in main and calling its public void start method.
- The start method creates a Scanner for System.in and calls printGreetings to display a welcome message.
- Public static final String constants represent inches, feet, and centimeters; printOptions lists the available unit pairs inside a while loop.
- The loop reads two unit names with Scanner.nextLine and stores them in unit1 and unit2.
- isValidUnit(String input) returns a boolean by comparing input case-insensitively against inches, centimeters, and feet.
- The converter checks both unit names before proceeding; invalid input displays an error and continue restarts the loop.
- printInput(unit1, unit2) prompts for the amount, which is read as a double with nextDouble; nextLine clears the remaining newline.
- Separate double-returning methods handle inches↔feet, inches↔centimeters, and feet↔centimeters; examples include inches divided by 12 and inches multiplied by 2.54.
- Conditional branches select a conversion method based on unit1 and unit2; matching units use the original value as the result.
- printResults receives both unit names and the computed double, keeping output formatting separate from conversion logic.
- The program asks whether to continue or quit, compares the response case-insensitively, and prints goodbye after the loop ends.
- JJ Shepherd tests inches to centimeters with 1, feet to centimeters with 2, and an invalid inches-to-miles request that triggers validation.
Transcript excerpt
0:00 Hey everyone, how it goes and welcome back to computer science 145. Today we're going to discuss methods. We are now going to start discussing how to organize code better. We've started writing programs where the size of the programs, the number of the statements are becoming increasingly larger and larger and more complex. The need to start organizing code becomes absolutely necessary. We would like to reuse parts of the code. That way, we don't have to use as many statements. We also want to be able to organize code better so we can quickly find bugs and errors. And finally, we would like to add or extend functionality of programs that we have already written. All of this
0:47 requires that we organize code in a better way. Everything that we've learned so far doesn't change. basic computation, branching statements, loops, and even the basic data structure called the array. None of that changes. However, what is going to change is how we write it and where we start organizing this code. Now, as described in a previous lecture, Java structures its code first in projects, then inside of classes, then following methods. Today we begin discussing methods. Now methods are where we write functional code and some of that involves where we declare and use variables such as local temporary or method variables which are
1:34 basically the same idea. This also goes true for branching statements and loops. Essentially everything that we've learned before we understand that that functional code is placed inside of a method. Now, whenever we use a method, this is referred to as invoking or calling a method. And so far, the only method that we've used is the main method. As described before, the main method is the entry point of the software. It is
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.