OOP-Lecture 3 2013 Input in Java: JOptionPane is a easy way to do dialog boxes, messages or inputs. You have to import it at the beginning of the program: import javax.swing.JOptionPane; //A sample program. Sample.java import javax.swing.JOptionPane; // imports JOptionPane class public class Sample { public static void main ( String args[] ) { JOptionPane.showMessageDialog ( null, "This is a sample program" ); System.exit ( 0 ); //stops the program } } There are different kinds of icons for a dialog box, just replace the last argument: JOptionPane.PLAIN_MESSAGE // this is a plain message JOptionPane.INFORMATION_MESSAGE // this is a info message JOptionPane.ERROR_MESSAGE // this is a error message JOptionPane.WARNING_MESSAGE // this is a warning message You can also use JOptionPane for dialog boxes for input, and assign them to variables, just put: name= JOptionPane.showInputDialog ( "put your message here" ); Example import javax.swing.JOptionPane; //import class JOptionPane public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum; 1 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 //read in the first number from user as a string firstNumber = JOptionPane.showInputDialog ( "Enter first integer" ); //read in the second number from user as a string secondNumber = JOptionPane.showInputDialog ( "Enter second interger"); //convert numbers from type String to type int number1 = Integer.parseInt ( firstNumber); number2 = Integer.parseInt ( secondNumber); //add the numbers sum = number1 + number2; //display the results JOptionPane.showMessageDialog ( null, "The sum is " + sum,"Results",JOptionPane.PLAIN_MESSAGE); System.exit ( 0 ); //ends the program } } Java basic operator: We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators Logical Operators Assignment Operators 1- The Arithmetic Operators: Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators: Assume integer variable A holds 10 and variable B holds 20 2 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 2- The Relational Operators: There are following relational operators supported by Java language: Assume variable A holds 10 and variable B holds 20, then: 3 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 3- The Bitwise Operators: Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows: a = 0011 1100 b = 0000 1101 Assume integer variable A holds 60 and variable B holds 13, then: 4- The Logical Operators: The following table lists the logical operators: Assume Boolean variables A holds true and variable B holds false, then: 4 Lecturer: Hawraa Sh. OOP-Lecture 3 5 2013 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 5- The Assignment Operators: There are following assignment operators supported by Java language: Java Decision Making: There are two types of decision making statements in Java. They are: if statements switch statements 6 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 The if Statement: An if statement consists of a Boolean expression followed by one or more statements. Syntax: The syntax of an if statement is: The if...else Statement: An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax: The syntax of an if...else is: 7 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 The if...else if...else Statement: Syntax: The syntax of an if...else is: Nested if...else Statement: It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement. Syntax: 8 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 The switch Statement: A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax: The syntax of enhanced for loop is: publicclassTest{ publicstaticvoid main(String args[]){ switch(grade) { case'A': System.out.println("Excellent!"); break; case'B': case'C': System.out.println("Well done"); break; case'D': System.out.println("You passed"); case'F': System.out.println("Better try again"); break; default: System.out.println("Invalid grade"); } System.out.println("Your grade is "+ grade);}} 9 Lecturer: Hawraa Sh. OOP-Lecture 3 2013 Java numbers Number Methods: SN 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 10 Methods with Description xxxValue() Converts the value of this Number object to the xxx data type and returned it. compareTo() Compares this Number object to the argument. equals() Determines whether this number object is equal to the argument. valueOf() Returns an Integer object holding the value of the specified primitive. toString() Returns a String object representing the value of specified int or Integer. parseInt() This method is used to get the primitive data type of a certain String. abs() Returns the absolute value of the argument. ceil() Returns the smallest integer that is greater than or equal to the argument. Returned as a double. floor() Returns the largest integer that is less than or equal to the argument. Returned as a double. rint() Returns the integer that is closest in value to the argument. Returned as a double. round() Returns the closest long or int, as indicated by the method's return type, to the argument. min() Returns the smaller of the two arguments. max() Returns the larger of the two arguments. exp() Returns the base of the natural logarithms, e, to the power of the argument. log() Returns the natural logarithm of the argument. pow() Returns the value of the first argument raised to the power of the second argument. sqrt() Returns the square root of the argument. sin() Returns the sine of the specified double value. cos() Returns the cosine of the specified double value. tan() Returns the tangent of the specified double value. asin() Returns the arcsine of the specified double value. acos() Returns the arccosine of the specified double value. atan() Returns the arctangent of the specified double value. atan2() Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta. toDegrees() Converts the argument to degrees toRadians() Converts the argument to radians. random() Returns a random number Lecturer: Hawraa Sh.