OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon Java Translation • The Java compiler translates Java source code into a special representation called bytecode • Java bytecode is not the machine language for any traditional CPU • Another software tool, called an interpreter, translates bytecode into machine language and executes it • Therefore the Java compiler is not tied to any particular machine • Java is considered to be architecture-neutral Java Translation Java source code Java compiler Java bytecode Bytecode interpreter Bytecode compiler Machine code 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: //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" ); 13 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon 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; //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 } } 14 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon 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- The Relational Operators: There are following relational operators supported by Java language: Assume variable A holds 10 and variable B holds 20, then: 15 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon 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: 16 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon 4- The Logical Operators: The following table lists the logical operators: Assume Boolean variables A holds true and variable B holds false, then: 17 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon 5- The Assignment Operators: There are following assignment operators supported by Java language: 18 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon Java Decision Making: There are two types of decision making statements in Java. They are: if statements switch statements The if Statement: An if statement consists of a Boolean expression followed by one or more statements. Syntax: The if...else Statement: An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax: 19 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon The if...else if...else Statement: Syntax: 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: 20 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon 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: Public class Test{ Public static void 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); } 21 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon } 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 22 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. OOP with Java Programming Dr. Ahmed Al-Salih 25 26 27 2rd class – Department of Programming College of IT- University of Babylon toDegrees( ) Converts the argument to degrees toRadians( ) Converts the argument to radians. random() Returns a random number Java Loop Controls: You can use one of the following three loops: while Loop do...while Loop for Loop 1- The while Loop: A while loop is a control structure that allows you to repeat a task a certain number of times. Syntax: The syntax of a while loop is: while(Boolean_expression) { //Statements } Example: Public class Test{ Public static void main(String args[]){ int x =10; while( x <20){ System.out.print("value of x : "+ x ); x++; System.out.print("\n"); } } } 2- The do...while Loop: A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. Syntax: The syntax of a do...while loop is: do { //Statements }while(Boolean_expression); 23 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon Example: Public class Test{ Public static void main(String args[]){ int x =10; do{ System.out.print("value of x : "+ x ); x++; System.out.print("\n"); }while( x <20); } } 3- The for Loop: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when you know how many times a task is to be repeated. Syntax: The syntax of a for loop is: for(initialization;Boolean_expression; update) { //Statements } Example: Public class Test{ Public static void main(String args[]){ for(int x =10; x <20; x = x+1){ System.out.print("value of x : "+ x ); System.out.print("\n"); } } } The break Keyword: The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement. Syntax: The syntax of a break is a single statement inside any loop: break; 24 OOP with Java Programming Dr. Ahmed Al-Salih 2rd class – Department of Programming College of IT- University of Babylon Example: Public class Test{ public static void main(String args[]){ for(int x =10; x <50; x = x+10){ if( x ==30){ break; } System.out.print( x ); System.out.print("\n"); } } } The output: 10 20 The continue Keyword: The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes flow of control to immediately jump to the update statement. In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression. Syntax: The syntax of a continue is a single statement inside any loop: continue; Example: Public class Test{ Public static void main(String args[]){ for(int x =10; x <50; x = x+10){ if( x ==30){ continue; } System.out.print( x ); System.out.print("\n"); } } } The output: 10 20 40 50 25