IFS410 Advanced Analysis and Design

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Some basic I/O.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
By Mr. Muhammad Pervez Akhtar
Review for Nested loops & Math class methods & User defined methods.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CompSci 230 S Programming Techniques
Topics Designing a Program Input, Processing, and Output
Expressions.
Chapter 2 Basic Computation
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Expressions.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Assignment and Arithmetic expressions
Data types, Expressions and assignment, Input from User
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Introduction to C++ Programming
Chapter 6 Methods: A Deeper Look
Chapter 2 Edited by JJ Shepherd
MSIS 655 Advanced Business Applications Programming
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
CS 200 Primitives and Expressions
Expressions and Assignment
C Programming Getting started Variables Basic C operators Conditionals
CS2011 Introduction to Programming I Elementary Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
CS Week 2 Jim Williams, PhD.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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