Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
ITM 200 Tip sheet, JAVA
Declaring Variables
int a;
double average;
String s;
char b;
//
//
//
//
Declare “a” to be an integer variable
Declare “average” to be a double variable
Declare “s” to be a String variable
Declare “b” to be a character variable
Assigning value to the variable
a = 5;
average = 6.8;
s = “Hello”
b = 'A';
// Assign 5 to a;
// Assign 6.8 to average;
// Assign “Hello” to s;
// Assign 'A' to a;
Declaring and Initializing in One Step
Int a = 5;
Double average = 6.8;
String s = “Hello”;
Char b = „A‟;
Shortcut Assignment Operators
Operator
+=
-=
*=
/=
%=
Example
Equivalent
i += 8
f -= 8.0
i *= 8
i /= 8
i %= 8
i=i+8
f = f - 8.0
i=i*8
i=i/8
i=i%8
Programming Errors
Syntax Errors - Detected by the compiler:
public class SyntaxErrors {
public static void main(String[] args) {
x = 30;
// x is unnown
System.out.println(i + 4); }
}
Runtime Errors - Causes the program to abort:
public class RuntimeErrors {
public static void main(String[] args) {
int i = 1 / 0; }
// Cannot divide by 0
}
Logic Errors - Produces incorrect result
Prepared by Pavel Chubarev
ITM 200 Tip sheet, JAVA
Working with String
To print a message:
System.out.println(“Print your message”);
String output = JOptionPane.showMessageDialog(null, “Print your message“);
To input data:
String input = JOptionPane.showInputDialog(“enter a number”);
Converting Strings to Integers and Doubles:
int num = Integer.parseInt(s); // Where s is a String such as “123”
double num =Double.parseDouble(s); // Where s is a String such as “123.55”
String index:
All symbols in a String have their own index that always begins at 0:
M
0
Y
1
2
N
3
A
4
M
5
E
6
7
I
8
S
9
10
A
11
D
12
R
13
I
14
A
15
N
16
N
17
A
18
!
19
To find an Index of specific symbol in the String, for example “A” use indexOf function:
int location1 = stringName.indexOf(“A”); // location1 = 4
Or more complex one, if there is more than one symbol “A” in the string, then start from index
of the first symbol “A” to find out the index of the second symbol “A” and so on:
int location2 = stringName.indexOf(“A”, location1 + 1);
To find a String length use method length():
String input = “my name is Adrianna!”;
int size = input.length(); // (returns 20)
To return a part of the string use substring method. For example to get only the name from
the string above:
String Name = stringName.substring(11,19); // The last index 19 will not be included in
the new string Name. The substring(a,b) will return a substring from a to (b-1). So the result will
be: String Name = “ADRIANNA”;
Splitting the String:
When the input includes more than one parameter, it is possible to split input and assign to
different string variables. For example:
String input = “FirstName,SecondName”; In order to split input in to two variables FirstName
and SecondName find the index of “,” that separates to inputs:
int location = input.indexOf(“,”);
Then use index of “,” to split string:
String firstName = input.substring(0, location);
String lastName = input.substring(location + 1);
Prepared by Pavel Chubarev
ITM 200 Tip sheet, JAVA
Loops
While loop: Executes a group of statements as long as a condition is true
int counter = 0;
while (counter <= 10)
{
System.out.println (counter);
Counter ++;
}
Do while loop: Similar to the while loop it executes a group of statements as long as a condition
is true, except that its body statements will always execute the first time, regardless of whether
the condition is true or false because the condition is located at the end of the loop
int counter = 0;
do
{
System.out.println (counter);
Counter ++;
} while (counter <= 10)
For loop: Executes a group of statements repeatedly until a given test fails. The loop declares a
loop counter variable that is used in the test, update, and body of the loop
Syntax: for (<initialization> ; <test> ; <update>)
Example:
for (int i = 0; i < 10; i++) // This loop will run 10 times and then quit
{
System.out.println(i); // i will change from 0 to 9
}
Switch Statement: Shorthand for multiple if statements. Instead of using many if else
statements, the switch statement can be used for convenience
Example:
int day = 6;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
case 4: System.out.println("Thursday"); break;
case 5: System.out.println("Friday"); break;
case 6: System.out.println("Saturday"); break;
case 7: System.out.println("Sunday"); break;
default: System.out.println("Invalid day"); break; // In the situation, when none of the cases
was right it will print “Invalid day”
}
Prepared by Pavel Chubarev
ITM 200 Tip sheet, JAVA
// In this case the output will be “Saturday”
Arrays
Array is a data structure that represents a collection of the same types of data.
int[] numbers;
// declares an array of integers
numbers = new int[10];
// allocates memory for 10 integers
numbers [0] = 100; // initialize first element
numbers [1] = 200; // initialize second element
numbers [2] = 300; // etc.
numbers [3] = 400;
numbers [4] = 500;
numbers [5] = 600;
numbers [6] = 700;
numbers [7] = 800;
numbers [8] = 900;
numbers [9] = 1000;
Declaring, creating, initializing in one step: double[] numbers = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the following statements:
double[] myList = new double[4];
myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
An example of inputting data in to an Array:
public static void main (String [] args) {
int list []; //Allocating space for an Array any size
list = new int [10]; // Specifying size
String s;
for (int i = 0 ; i < 10 ; i++) {
s = JOptionPane.showInputDialog("Please enter an integer value:");
list [i] = Integer.parseInt(s); Assigning the data to the Array
}
for (int i=0; i<10; i++) {
System.out.println(list[i]); // Printing all the data. Has to be done in the loop
}
}
Prepared by Pavel Chubarev