Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSIS 655 Advanced Business Applications Programming

Similar presentations


Presentation on theme: "MSIS 655 Advanced Business Applications Programming"— Presentation transcript:

1 MSIS 655 Advanced Business Applications Programming
Week 2 Intro to Java Application This week, a few review of application will be presented. The first assignment is reviewed first, and new concepts in the java is presented. Some operations and methods are briefly presented. 12/3/2018 2.1

2 Assignment 1: Addition Addition.java 12/3/2018
import javax.swing.JOptionPane; public class Addition { public static void main( String args[] ) { String firstNumber; // first string entered by user String secondNumber; // second string entered by user int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2 firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); secondNumber = JOptionPane.showInputDialog( "Enter second integer" ); number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); sum = number1 + number2; JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // terminate application } // end method main } // end class Addition 12/3/2018

3 A few tips for Java programming
Comments Every program should begin with a comment that explains the purpose of the program, the author and the date and time the program was last modified. Blank line Makes program more readable Blank lines, spaces, and tabs are white-space characters Ignored by compiler Use blank lines and space characters to enhance program readability. Java Application: Applications begin executing at main() Parentheses indicate main is a method (Ch. 3 and 6) Java applications contain one or more methods Exactly one method must be called main() Each line within this method (ex. String firstNumber;) is called a statement. Statements must end with semicolon ; 12/3/2018

4 Java Identifier Java identifier (name for classes, variables, or methods) Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Examples: Welcome1, $value, _value, button7 7button is invalid Java is case sensitive (capitalization matters) a1 and A1 are different By convention, always begin a class name’s identifier with a capital letter and start each subsequent word in the identifier with a capital letter. Java programmers know that such identifiers normally represent Java classes, so naming your classes in this manner makes your programs more readable. Java is case sensitive. Not using the proper uppercase and lowercase letters for an identifier normally causes a compilation error. It is an error for a public class to have a file name that is not identical to the class name (plus the .java extension) in terms of both spelling and capitalization. It is an error not to end a file name with the .java extension for a file containing a class declaration. If that extension is missing, the Java compiler will not be able to compile the class declaration. 12/3/2018

5 Modifying the first program: Another Adding Integers
Fig 2.7 (p. 47) Addition Program using class Scanner Contrast with the assignment 1. 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.” 12/3/2018

6 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). Some labs are not upgraded to 5.0 (- -;) This edition of 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. If you have downloaded the J2SE 5.0 for your PC, the program should work as it is. 12/3/2018

7 import import declarations
Used by compiler to identify and locate classes used in Java programs All import declarations must appear before the first class declaration in the file. Tells compiler to load class Scanner from java.util package All import declarations must appear before the first class declaration in the file. Placing an import declaration inside a class declaration’s body or after a class declaration is a syntax error. Forgetting to include an import declaration for a class used in your program typically results in a compilation error containing a message such as “cannot resolve symbol.” When this occurs, check that you provided the proper import declarations and that the names in the import declarations are spelled correctly, including proper use of uppercase and lowercase letters. import java.util.Scanner; // program uses class Scanner 12/3/2018

8 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. 12/3/2018

9 Method “print()” print(argument) – print argument on a line and stop at the end of argument println(argument) – print argument on a line and go to the beginning of next line printf(argument with %_, reference) – formatted printing %d: digit = short, int, long %f: floating point = float, double %s: String = String 12/3/2018

10 Escape Caracters Escape characters Newline characters (\n)
Backslash ( \ ) Indicates special characters be output Newline characters (\n) Interpreted as “special characters” by methods System.out.print and System.out.println, etc. Indicates cursor should be at the beginning of the next line 12/3/2018

11 Fig. 2.5 | Some common escape sequences.
12/3/2018

12 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. Assignment statement “sum = number1 + number2” Calculates sum of number1 and number2 (right hand side) Uses assignment operator = to assign result to variable sum Read as: sum gets the value of number1 + number2 number1 and number2 are operands 12/3/2018

13 if statement Condition if statement
Expression can be either true or false if statement Simple version in this section, more detail later If a condition is true, then the body of the if statement executed Control always resumes after the if statement Conditions in if statements can be formed using equality or relational operators (next slide) 12/3/2018

14 Equality and Relational Operators
!= not equal > < >= <= Confusing the equality operator, ==, with the assignment operator, =, can cause a logic error or a syntax error. The equality operator should be read as “is equal to,” and the assignment operator should be read as “gets” or “gets the value of.” To avoid confusion, some people read the equality operator as “double equals” or “equals equals.” It is a syntax error if the operators ==, !=, >= and <= contain spaces between their symbols, as in = =, ! =, > = and < =, respectively. 12/3/2018

15 12/3/2018 Line 6: begins class Comparison declaration
Line 12: declares Scanner variable input and assigns it a Scanner that inputs data from the standard input Lines 14-15: declare int variables Lines 17-18: prompt the user to enter the first integer and input the value Lines 20-21: prompt the user to enter the second integer and input the value if statement to test for equality using (==) If variables equal (condition true) Line 24 executes If variables not equal, statement skipped No semicolon at the end of if statement Empty statement No task is performed 12/3/2018

16 Lines 26-27, 29-30, 32-33, and 38-39 Compare number1 and number2 with the operators !=, <, >, <= and >=, respectively 12/3/2018

17 Lab activities (Week 2) Coding Assignment 2
Exercises (pp ) 2:24, 2:25, 2:27, 2:30, 2:31 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. 12/3/2018


Download ppt "MSIS 655 Advanced Business Applications Programming"

Similar presentations


Ads by Google