CSE 110: Introduction to Computer Science JAVA HELP ---------- This document provides information regarding the following topics. 1. Variables 2. Arithmetic Expressions. 3. Boolean Expressions. 4. if statement. 5. while statement. 6. Creating, compiling and running java programs. 1. Variables: ------------- In Java, variables are used to store data. Variables have type, name, and value. Variable names begin with a character, such as x, D, Y, z. Other examples are xy1, abc2, Count, N, sum, Sum, product etc. These are all variable names. Different variable types are int, char, double. A variable type tells you what kind of data can be stored in that variable. For example: int x; // This means variable x can store numbers such as 2, 10, -5. char y; // This means variable y can store single characters 'a', 'A'. double z; // This means variable z can store real numbers such as 10.45, 3.13411. The above are declarations for variables x, y and z. Important points: 1. Note that a variable has to be declared before being used. 2. The values assigned to a variable correspond to its type. Statements below represent assignment of values to a variable. x = 100; // x is an integer variable. y = 'A'; // y is a character variable. abc = 10.45; // abc is of type double (real numbers). 3. Both variable declaration and assignment of values can be done in same statement. For example, int x; x = 100; is same as int x = 100; 4. A variable is declared ONLY ONCE. int x; // Declaration for x. x = 100; // Initialization. x = x + 12; // Using x in an assignment statement. 3. A variable can be assigned a value from the keyboard input. This means a variable gets whatever value you type in at the prompt. For example, int x = Console.readInt("Enter a number:"); // One statement. is exactly same as: int x; x = Console.readInt("Enter a number:"); // Two statements. 2. Arithmetic Expressions -------------------------- Arithmetic expressions use arithmetic operators such as +, -, /, *, and %. The % operator is the remainder or modulo operator. Arithmetic expressions are used to assign arithmetic values to variables. The following code describes the use of different arithmetic expressions. int x, y, z; // Three integer variables declared at the same time. x = 10; y = 12; z = y / x; // z is assigned the value of y divided by x. // Here z will have value 1. z = x + y; // z is assigned the value of x+y // Here z will have value 22. z = y % x // z is assigned the value of remainder when y // is divided by x. Here z will have value 2. 3. Boolean Expressions ----------------------- Boolean expressions are expressions which are either true or false. The different boolean operators are < (less than), > (greater than), == (equal to), >= (greater or equal to), <= (less or equal), != (not equal to). Example: int x = 10; int y = 4; int z = 5; (x < 10) // This expression checks if x is less than 10. (y > 1) // This expression checks if y is greater than 1. ((x - y) == (z + 1)); // This expression checks // if (x - y) equals (z + 1). A boolean expression can also be a combination of other boolean expressions. Two or more boolean expressions can be connected using && (logical AND) and || (logical OR) operators. The && operator represents logical AND. The expression is true only if both boolean expressions are true. The || operator represents logical OR. This expression would be true if any one of the associated expres- sions is true. Example: int x = 10; int y = 4; int z = 5; (x <= 10) && (y > 1) // This expression checks if x is less // than 10 AND y is greater than 1. // This expression is TRUE. (x*y == 41) || (z == 5) // This expression checks if x*y is equal // to 40 OR if z is equal to 5. // This expression is FALSE 4. if statement ---------------- The `if' statement in java is used to execute a group of statements only if some condition is true. The if statement has the following format. if some-condition is true then do the then-part. else do the else-part. Java syntax for if statement is given below. if (Boolean Expression or condition) { // Then-part // A group of statements } else { // Else-part // A group of statements } It is possible to have if statement without the else part. The then part is executed only if the Boolean expression is true. In such case, the syntax is: if (Boolean Expression or condition) { // Then-part. // A group of statements } Example 4.1: int a = 10; int b = 2; if ( a % b == 0 ) { System.out.println(a + " is divisible by "+ b); } else { System.out.println(a + " is not divisible by " + b); } Example 4.2: int a = Console.readInt("Enter any number:"); if(a % 2 == 0) // This has no else part. { System.out.println(a + " is an even number "); } if(a > 100) { System.out.println(a + " is greater than 100"); } 5. while statement ------------------ The `while' statement in java is used to execute some code in a repeated manner until some condition becomes false. The while state- ment has the following format. while (some condition is true) do a group of statements Java syntax for while is: while (Boolean expression) { // A group of statements } Example 5.1: This while-loop prints numbers from 0 to n, where n is input to the java program. int i = 0; n = Console.readInt("Enter a positive number: "); while (i < n) { System.out.println(" "+i); i = i + 1; } Example 5.2: This code prints even numbers from 20 to 0. int i = 20; while (i >= 0 ) { System.out.println(" "+i); i = i - 1; } Example 5.3: This one prints the square of all numbers, where the square is less than 1000. int i = 2; int n ; n = i * i; // n is equal to i * i while (n < 1000) { System.out.println(" "+i); i = i + 1; n = i * i ; } 6. Creating, compiling and running java programs. ------------------------------------------------- In this section we put together an entire java program. The structure of the java program is given below: public class ClassName { public static void main (String args[]) { // Your code for method main. } } Notes: 1. Every open `{' has to be closed by `}' . Otherwise you will get compile time errors in your program. 2. This program has to be stored in a file called, ClassName.java 3. Java is case sensitive. Upper case and lower case are different. 4. To compile the above program, type javac ClassName.java 5. To run the above program, type java ClassName Example: // This program computes prints the square of the first 100 numbers. public class MyFirstProg { public static void main( String args[] ) { int i; int sqr; i = 0; while (i < 100) { sqr = i * i; System.out.println(" " + sqr); } } } Store the program in a file called MyFirstProg.java. Compile: javac MyFirstProg.java Run: java MyFirstProg Example: // This program reads in a number and checks if it is between // 100 and 200. If the number is greater than 200, program sets // it to 200. If the number is less than 100, it sets it to 100. public class MySecondProg { public static void main (String args[]) { int i = Console.readInt("Enter a number between 100 and 200: "); if ( i > 200 ) { i = 200; System.out.println(i + " is greater than 200, setting it to 200."); } if( i < 100 ) { i = 100; System.out.println(i + " is less than 100, setting it to 100."); } System.out.println("The new value of number is: " + i); } } Save it in the file MySecondProg.java Compile: javac MySecondProg.java Run: java MySecondProg