1 / 23

IDS 201 Introduction to Business Programming (Fall 201 4 )

IDS 201 Introduction to Business Programming (Fall 201 4 ). Week2. Output. Non-formatted output System.out.print (); System.out.print (3); System.out.print (‘a’); System.out.print (“ abc ”); System.out.println (); System.out.println (3); System.out.printl (‘a’);

pancho
Download Presentation

IDS 201 Introduction to Business Programming (Fall 201 4 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IDS 201Introduction to Business Programming (Fall 2014) Week2

  2. Output • Non-formatted output • System.out.print(); • System.out.print(3); System.out.print(‘a’); System.out.print(“abc”); • System.out.println(); • System.out.println(3); System.out.printl(‘a’); • System.out.println(3+5); System.out.println(“abc”+“def”); • Formatted output (for alignment) • System.out.printf(parameter1, parameter2); • It takes two or more parameters. The first parameter is aformat specifierthat specifies the format of the output. The remaining parameters specify the values that are to be output. • Parameter 1 is a string. – surrounded by double quotes.

  3. Format Specifier • Every format specifier begins with a percent sign (%) and ends with a letter, possibly with some extra formatting information in between. • Most frequently used specifiers • Integer (%d), real number (%f), string (%s), … • %12d: output an integer with a minimum number of spaces 12. • Right-justified: add blank spaces in front to bring up to 12 if the integer is fewer than 12 spaces, print it out otherwise.

  4. Format Specifier • %x.yf: in a general floating-point format • x: total number of characters to output. • y: number of characters after the decimal point. • %x.ye: in an exponential format • x: total number of characters to output. • y: number of characters after the decimal point • %x.yg: in floating-point form for small values and in exponential form for large values. • y: total number of digits, including both before and after the decimal point.

  5. Format Specifier • Separate big numbers into groups by using comma. • The comma should come at the beginning of the format specifier, before the field width. • int x = 1000000; • System.out.printf(“%,d”,x); //1,000,000 • double y = 1123.4567; • System.out.printf(“%,12.3f”, y); // 1,123.457 (3 spaces in front) • Total length is 12, including comma, dot.

  6. Input (Scanner) • Add the following line before “public class <class name>” import java.util.Scanner; • Declare a Scanner object variable Scanners= newScanner( System.in); • Many methods defined by Scanner class are used to get input from users or keyboards. • s.nextInt() reads one value of type int from the user and returns it. • nextLong(), nextDouble(), nextBoolean(), nextLine(),…

  7. Input (Scanner) • nextLong() // long integer • nextDouble() // floating number • nextBoolean() // boolean value • next() // String • nextLine() // String String x = “”; Scanner sc = new Scanner(System.in); x = sc.next(); or x = sc.nextLine(); System.out.println(x); If you input: James studies at UIC, next() will only take James, while nextLine() will take the entire line. next() only gets up to the next space while nextLine() gets up to the \n break

  8. Scanner Example import java.util.Scanner; public class ScannerExample{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); //declare a Scanner class object System.out.println("please enter your name"); String name = sc.nextLine(); // get a string from user System.out.println(“Hello " + name); System.out.println("please enter an integer"); intnum = sc.nextInt(); // get an integer from user System.out.println(”the integer you entered is " + num); } }

  9. Code Comments • Use comments to debug codes. • Use comments to provide overview or summaries of chunks of codes. • All comments will be ignored when you compile and run your programs.

  10. Code Comments • Three types of comments in Java • /* any text */: the compiler ignores everything from /* to */. • Usually comment methods, functions: the functionality of the method. • // any text : the compiler ignores everything from // to the end of the line. • Usually comment single statement: the meaning of the variable or the statement. • Commenting one line. • /** any text */: this is a documentation comment (doc comment for short). JDK javadoctool uses doc comment when preparing automatically generated documentation. • Usually comment the entire class, including author, date, what methods/interfaces, etc. • They can be used as the input of javadocto generate HTML based java documentation.

  11. Code Comments Examples /** * It is implemented by @author: alice; * Last modified on @date: 2013-08-01; * It has one method: add */ public class HelloWorldApp { /* add two integers and return the sum * input parameters: two integers */ public int add(int x, int y){ return x+y; // return the sum of two integers. } public static void main(String[] args) { System.out.println("Hello World!");//display the string. } }

  12. Code Comments DoNOT nest comments

  13. Expression • An expression is a combination of literals, operators, variable names, and parentheses used to calculate a value. • Parentheses must be matched. • Variable names must be valid identifiers. • Multiplication sign (*) can NOT be ignored.

  14. Correct or Not Correct? 25 2 ( a – b ) A - b / c + D ( (x + y) / z) / (a - b) 25 – value % m -sum + partial ( (m - n) + (w - x - z) / (p % q) 201ids - num

  15. Operators • Two-expression/operand operators • Arithmetic operators • Relational operators • Boolean operators (except not) • One-expression/operand operators • Increment • decrement • Multi-expression/operand operators • Conditional operator

  16. Arithmetic Operators • Addition: + • Subtraction: - • Multiplication: * • Division: / • Reminder: % • Integer division (c = a/b) • aand b are integers, c will be an integer; • E.g., 7/4  1 instead of 1.75 • As long as a, b has at least one real, then c will be a real. • E.g., 7/4.0  1.75

  17. Reminder (%) • Positive case • 13%5 = 3 • Reminder with negative integers • Perform the operation as if both operands are positive; • If the left operand is negative, then make the result negative; • If the left operand is positive, the make the result positive; • Ignore the sign of the right operand in all cases.

  18. Relational Operators The return type of expressions using relational operators is boolean.

  19. Relational Operators • They can be used to compare any numeric values. • For characters, compare their Unicode values (A: 65, a: 97, …). • Therefore, ‘A’ < ‘a’ • ==, != can also be used to compare two boolean values. booleansameSign; sameSign= ((x > 0) == (y > 0)); • ==, !=: better NOT use for String objects. • String is a Object type, using their specific methods.

  20. Boolean Operators • && : and • The value is true if both values are true. The value is false if either value is false. • || : or • The value is true if either value is true. The value is false if both values are false. • ! : not • Will reverse the value.

  21. Boolean Operators • && • || • !

  22. Short Circuited Version • Operand 1 && Operand 2 • If operand 1 is false, operand 2 doesn’t have to be evaluated. int X = 0; int Y = 1; int Z = (X != 0) && (Y/X); No errors The division by zero is avoided, because X != 0 is false. But, boolean Z = Y/X; error!!!

  23. Short Circuited Version • Operand 1 || Operand 2 • If operand 1 is true, operand 2 doesn’t have to be evaluated. int X = 0; int Y = 1; boolean Z = (X == 0) || (Y/X); No errors The division by zero is avoided, because X == 0 is true. But, boolean Z = Y/X; error!!!

More Related