WordPress.com



Unit 3 Review Notes3.1 Java MethodsMethods allow us to break down our code into reusable parts, avoid using repeated code, and simplify our code.Method - A method is a way to teach the computer a new commandDefine a Method - Defining a method means to teach the computer a new command and explain what it should do when receiving that command.Call a Method - Calling a method actually gives the command, so the computer will run the code for that method.3.2 ParametersParameters are inputs into methodsParameters allow us to generalize our methods so that they can solve more than one specific instance of a problem, and instead can solve different versions of the same problem (for example, add 10 to any given number).Parameter - A variable passed into a method from outside the method.Method Body - The part of the method that contains the commandsQuestions to think about: What is the () for in all the commands we see? System.out.println();What is the DRY principle?Example:Private void plusTwo(x){int x = x+2;System.out.println(x);}3.3 Return ValuesThink of a method like assigning a task. A return value is giving you the completed work. This allows you to write methods that do some work for you and send back the result. Parameters are like inputs into the function, and the return value is output.Return Statement – Exits a method and returns a valueReturn Value - The value returned from a methodReturn Type - A method’s return type is the type of value returned from that methodMethod Signature - A method’s method signature is the name of the method and the parameter listCall Site - A method’s call site is the point in the code where the method is called.Example:public void run(){ int input = readInt("Enter a number"); int twice = double(input); for(i = 0; i < twice; i++) { println("hello"); } } public void double(x){ System.out.println(2 * x);}The variable twice won’t hold any value because double just prints it out, and it’s lost! * Solution: instead of printing out the output from a method, we RETURN the output from the method. Returning a value sends the value back to the point in the program that called the method. It’s like giving some input into a box, and the box spits out a value back at you as output, and we can store that value in a variable.Same as above but with return statement:public void double(x){return 2 * x;}Return statements can also be used to exit a method, similar to the way a break statement is used to exit a while loop. 3.4 Javadocs and Commenting Comments are an important part of programming style. Programs are usually written by teams of people and maintained for many years. Others need to be able to read your code and understand what is going on.Javadocs are a standard, agreed upon way of commenting Java code. There is a tool, also called Javadoc, that is able to read Javadoc comments and create clean, clear documentation for your Java programs. Javadoc refers to both the tool and the style of commenting Java code.Javadoc - A specific standard for commenting Java programs. Javadoc is a format to follow when commenting in order to clearly explain your code.3.5 String MethodsStrings come prepackaged with several methods that can be used to help us do cool things. It is important to know how to find and read documentation about how to use the String methods. These methods get used on Strings in order to get information about them and manipulate them to form new Strings.Example:boolean sameStrings = str1.equals(str2);This is how we compare whether String str1 is equal to a different String str2 This is one example of calling a method on a String object. We are calling the equals method on the String str1 and passing in str2 as a parameter. The equals method will then return a boolean value saying whether or not the String passed in is equal.This is one of many String methods. In general, we can call a method on a String as follows:stringName.methodName(parameters);Class - A class is a template, or a blueprint, from which Java objects are created. All Java programs start with a class.String - String is a Java type that represents a string of characters (text)Char - char is a Java type that represents a single character (a single letter)Object - An object is a single instance of a Java class. An object has both state and behavior.Primitive Type - Primitive types are the basic, simple data types that are inherent to Java (int, double, char, and boolean)Java Documentation - Documentation showing the syntax and examples for how to use the various features of Java.Substring - A substring is a smaller sequence of characters in a larger String.Immutable - Unable to change. Strings in Java are immutable, meaning you can’t change it once you make it. If you take a substring of a String, or concatenate something to a String, the result is a brand new String, rather than a modification of the original.3.6 Strings and CharactersSimilarities and differences between the String class and the char primitive type exist. Strings are simply sequences of chars. All char values have a corresponding int value. chars are actually stored as numbers (ASCII). For example, 65 corresponds to ‘A’, 66 corresponds to ‘B’, and so on. The Character class provides several useful methods that allow us to manipulate and get information about char values.This lesson also covers special characters like tabs, quotes, and new lines are and how to store and print these special characters using escape sequences like ‘\n’Remember that each character in string has an index value. This index value starts at 0.Escape Sequences - Escape sequences are characters with special meanings. (\n, \t)char - char is a Java type that represents a single character (a single letter)Static Method - A method called on the Class, rather than on a specific object of the Class3.7 Exceptions (Types of Errors)All programs have bugs at some point in the development process. Bugs are ok! They show us exactly where the problems are in our code and give us helpful information so that we can fix these problems.When there is a bug in your program, Java will provide helpful information about where the bug is and what kind of bug is occurring by throwing an Exception. There are different kinds of exceptions. When we try running our programs and see exceptions, it’s important to know how to use that information to debug our programs!Bug - A bug is a problem in your pile time errors – An error in the actual Java code. The code will not compile into an executable program, because there are errors in the text of the code. Run time errors – program will run but it will crashCompiler - A tool that compiles your Java code, it takes the Java code you’ve written and turns it into an executable program.Runtime Error - An error that happens while the program is running. Even if the code is written with the proper syntax, there are things that can go wrong while the program is running.Exceptions – give information about what kind of error occurred Arithmetic Exception – thrown from within the divide method (i.e. divide by 0)IndexOutOfBoundsException – something went wrong trying to access an index that was out of boundsIllegal Argument Exception – program passes an argument that is inappropriate or doesn’t make sense3.8 String Processing (Challenges) You have learned about writing our own methods that take in parameters and return return values. You have learned about the relationship between Strings and characters. You have learned about using the methods of the String class and the Character class. You have learned about looping through the characters of a String using a for loop. Put it all together to write some methods that perform some advanced manipulation of Strings.Use Pseudocode!!Write in Scratchpad!!3.9 Quiz Know your definitions (method, parameter, String, return value, etc).Know the method signature, how to differentiate between a String method, Boolean method, a method that returns no value, etc.Know the various types of exceptions (errors) that can be thrown.Know arithmetic calculations in Java (casting, order of operations, modulus).You will be asked to predict the output of various methods, to spot the bug, and to choose the correctly written method for a desired output. Some questions will ask you to use string methods. You may use the Java documentation during the quiz to help you answer these questions. Make sure you know how to read Java documentations for String and character methods. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download