Presentation is loading. Please wait.

Presentation is loading. Please wait.

IFS410 Advanced Analysis and Design

Similar presentations


Presentation on theme: "IFS410 Advanced Analysis and Design"— Presentation transcript:

1 IFS410 Advanced Analysis and Design
Week 2 Methods: A Deeper Look This week, instead of going directly the concept of objects and classes, a few review of application will be presented. The first assignment is reviewed first, and new concepts in the java is presented. Some operations, variable types, and methods are briefly presented. These basic concepts are not directly related to the structure of the program itself, but necessary to perform some assignments. 2018/12/7 2.1

2 Assignment 1: Addition 2018/12/7
Fig 2.7 (p. 47) Addition Program using class Scanner Class Scanner was imported before the class declaration. new Scanner command invokes a new object “input.” Numbers were assigned to int variables without converting. System method “printf.” 2018/12/7

3 class Scanner (new to v. 5.0)
Imported to the package to be used with the program Scanner variable (input) is initialized, and used to read data typed by the user at the keyboard (without converting String to integer). The new edition of the book (6th ed.) is fully incorporated the J2SE 5.0 which includes this Scanner class and printf method of the class System. The labs should have been upgraded but wasn’t in time for this semester. If you have downloaded the J2SE 5.0 for your PC, the program should work as it is. 2018/12/7

4 objects System.in, System.out, System.err
Included in the package java.lang. Always imported by compiler. System.in: enables program to input bytes from keyboard. System.out: enables program to output data to screen System.err: enables program to output error message to screen Actually, these three objects are objects of stream. For detailed discussion about the stream, please refer to the chapter 14. These objects are instantiated for any applications executed to prepare for any input, output, or error messages. 2018/12/7

5 Arithmetic Operation Addition + Subtraction - Multiplication *
Division / Modulus % (r % s = r mod s) Addition to division should need no explanation. Modulus is sometimes called Remainder, which produce an integer value that is a remnant of division. That is if r is divided by s, and the integer value of the answer was t, then r-s*t is the remainder. e.g., 1 % 3 = 1, 2 % 3 = 2, 3 % 3 = 0, 4 % 3 = 1, … In the same way the operations are conducted in arithmetic, multiplication, division, and modulus are performed prior to addition or subtraction unless those are in parentheses. 2018/12/7

6 Equality and Relational Operators
!= not equal > < >= <= These operators are used mostly in the condition determination, i.e., logic controls such as IF statements. e.g., if ( r == 0 ) { System.out.println( “Radius cannot be zero.” ); } else if ( r < 0 ) { System.out.println( “Radius must be a positive number.” ); else System.out.println( “Diameter is “ + 2 * r ); 2018/12/7

7 Assignment Operators Variables are assigned a value based on the operators on the right-hand side. Variable = variable operator expression += -= *= /= %= += operator (and similar others) is used to express the original number is replaced with some incremental number. e.g., c += 3 is equivalent to c = c + 3 2018/12/7

8 Increment and Decrement Operators
Unary increment /decrement operator ++/-- ++a and a++ mean different For these statements, c=5; System.out.println( c ); System.out.println( c++); Results are: 5 6 System.out.println( ++c ); 2018/12/7

9 Example Look at the above code. Can you guess what will happen if this program is compiled and run? 2018/12/7

10 Primitive Data Types Java is strongly typed – all variables in a java program must have a type. Primitive types are portable across platform. boolean, byte, char, short, int, long, float, double See Appendix D (p. 1402) for the detail of these primitive types. 2018/12/7

11 Method Objects have behavior that is implemented by its methods.
Math, String, I/O operations, Error checking, Programmer defined methods Other objects can ask an object to do something by invoking its methods. Method declaration At minimum, a method declaration has a name and a return type indicating the data type of the value returned by the method: returnType methodName() { . . . } This method declaration is very basic. Methods have many other attributes such as arguments, access control, and so on. Passing Information into a Method Perhaps the most commonly used optional component of a method declaration are method parameters. Similar to functions in other programming languages, Java methods accept input from the caller through its parameters. Parameters provide information to the method from outside the scope of the method. The Method Body The method body is where all of the action of a method takes place; the method body contains all of the legal Java instructions that implement the method. 2018/12/7

12 Passing Information The set of arguments to any method is a comma-separated list of variable declarations where each variable declaration is a type/name pair: data-type name When you write your method, you declare the number and type of the arguments required by that method. You declare the type and name for each argument in the method signature. For example, the following is a method that computes the monthly payments for a home loan based on the amount of the loan, the interest rate, the length of the loan (the number of periods), and the future value of the loan (presumably the future value of the loan is zero because at the end of the loan, you've paid it off): double computePayment(double loanAmt, double rate, double futureValue, int numPeriods) { double I, partial1, denominator, answer; // declaring local variables I = rate / 100.0; partial1 = Math.pow((1 + I), (0.0 - numPeriods)); denominator = (1 - partial1) / I; answer = ((-1 * loanAmt) / denominator) - ((futureValue * partial1) / denominator); return answer; } This method takes four arguments: the loan amount, the interest rate, the future value and the number of periods. The first three are double-precision floating point numbers, and the fourth is an integer. 2018/12/7

13 Math Class Methods All Math methods are static Invoking a Math method
e.g., System.out.println( Math.sqrt( 900.0) ); Other than the methods in the Math class, Java has predefined operations (as some of them are explained earlier): Arithmetic Operators: +, -, *, /, %, () Equality and Relational Operators: ==, !=, <, >, <=, >= Conditional Operators: ? : Assignment Operators: =, +=, -=, *=, /=, %= Increment and Decrement Operators: c++, ++c, c--, --c Primitive Data types: Char, byte, short, int, long, float, double, boolean (Remember, String is not a primitive type.) 2018/12/7

14 Lab activities (Week 2) Coding Assignment 2
Creating an application using some of the techniques learned. if statement maybe necessary, but use the example. Use textbook Fig 2.7 as an example (if you have J2SE 5.0). This problem should be similar to the example just we reviewed. Check the examples in the book, you will be able to find necessary stuff. 2018/12/7


Download ppt "IFS410 Advanced Analysis and Design"

Similar presentations


Ads by Google