You are on page 1of 8

Al-Farabi University College

Introduction to Java Programming

Java refers to a number of computer software products and specifications from


Sun Microsystems (the Java™ technology) that together provide a system for
developing and deploying cross-platform applications. Java is used in a wide variety
of computing platforms spanning from embedded devices and mobile phones on the
low end to enterprise servers and super computers on the high end. Java is fairly
ubiquitous in mobile phones, Web servers and enterprise applications, and somewhat
less common in desktop applications.

Writing in the Java programming language is the primary way to produce code
that will be deployed as Java byte code, though there are compilers available for
other languages such as JavaScript, Python and Ruby, and a native Java scripting
language called Groovy. Java syntax borrows heavily from C and C++ but it
eliminates certain low-level constructs such as pointers and has a very simple
memory model where every object is allocated on the heap and all variables of object
types are references. Memory management is handled through integrated automatic
garbage collection performed by the Java Virtual Machine (JVM).

OOP – Object Oriented Programming

OOP is a particular style of programming which involves a particular way of


designing solutions to particular problems. Most modern programming languages,
including Java, support this paradigm. When speaking about OOP one has to
mention:

Page 1
Al-Farabi University College

Variables and Data Types


A variable is a place where the program stores data temporarily. As the name
implies the value stored in such a location can be changed while a program is
executing.

class Example1 {
public static void main(String args[])
{ int var1; // this declares a variable
int var2; // this declares another variable
var1 = 1024; // this assigns 1024 to var1
System.out.println("var1 contains " + var1);
var2 = var1 / 2;
System.out.print("var2 contains var1 / 2: ");
System.out.println(var2); }
}

Output// var2 contains var1 / 2: 512

Mathematical Operators
class Example2 {
public static void main(String args[])
{ int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0; S
ystem.out.println("Result and remainder of 10 / 3: " + iresult +
" " + irem);

Page 2
Al-Farabi University College

System.out.println("Result and remainder of 10.0 / 3.0: " + dresult


+ " " + drem); } }

Output// Resultand Remainder of 10/3: 3 1 Result and Remainder of


10.0/3.0: 3.3333333333333335 1

Logical Operators
class Example3 {
public static void main(String args[]) {
int n, d; n = 10; d = 2;
if(d != 0 && (n % d) == 0)
System.out.println(d + " is a factor of " + n);
d = 0; // now, set d to zero
// Since d is zero, the second operand is not evaluated.
if(d != 0 && (n % d) == 0)
System.out.println(d + " is a factor of " + n);
/* Now, try same thing without short-circuit operator. This will
cause a divide-by-zero error. */
if(d != 0 & (n % d) == 0)
System.out.println(d + " is a factor of " + n); } }

Output// 2 is a factor of 10

Blocks of Code
class Example4 { public static void main(String args[]) { double
i, j, d; i = 5;
j = 10;
if(i != 0) {
System.out.println("i does not equal zero");
d = j / i;
System.out.print("j / i is " + d);
} System.out.println(); } }
Output// i does not equal to zero

Page 3
Al-Farabi University College

j/i is 2

The Math Class


class Example5 { public static void main(String args[])
{ double x, y, z; x = 3; y = 4;
z = Math.sqrt(x*x + y*y);
System.out.println("Hypotenuse is " +z); } }
Output// Hypotenuse is 5.0

Using the Scanner Class


import java.util.Scanner;
public class ScannerInput {
public static void main(String[] args)
{ //... Initialize Scanner to read from console.
Scanner input = new Scanner(System.in);
System.out.print("Enter first number : ");
int a = input.nextInt();
System.out.print("Enter second number: ");
int b = input.nextInt();
System.out.print("Enter last number : ");
int c = input.nextInt();
System.out.println("Average is " + (a+b+c)/3); } }

Using Swing Components


This is probably the most exciting version, since the Swing package offers a
graphical user interface (GUI) which allows the user to perform input into a program
via the mouse, keyboard and other input devices.

Page 4
Al-Farabi University College

import javax.swing.*; // * means „all‟

public class SwingInput {

public static void main(String[] args) {

String temp; // Temporary storage for input.

temp = JOptionPane.showInputDialog(null, "First number");

int a = Integer.parseInt(temp); // String to int

temp = JOptionPane.showInputDialog(null, "Second number");

int b = Integer.parseInt(temp);

temp = JOptionPane.showInputDialog(null, "Third number");

int c = Integer.parseInt(temp);
JOptionPane.showMessageDialog(null, "Average is " + (a+b+c)/3); }
}

One has to note that the input is stored as a string, temp, and then parsed to integer
using the method parseInt( ). This time the method accepts a parameter, temp, and
returns an integer. When the above program is executed, a dialog box will appear on
screen with a field to accept input from user via keyboard
(JOptionPane.showInputDialog). This is repeated three times and finally the result
is again displayed in a dialog box (JOptionPane.showMessageDialog).

Page 5
Al-Farabi University College

if-else-if Ladder
class Ladder { public static void main(String args[]) {

int x;

for(x=0; x<6; x++)

{ if(x==1)

System.out.println("x is one");

else if(x==2)

System.out.println("x is two");

else if(x==3)

System.out.println("x is three");

else if(x==4)

System.out.println("x is four");

Else

System.out.println("x is not between 1 and 4"); } } }

Output//

x is not between 1 and 4

x is one

x is two

x is three

x is four

x is not between 1 and 4

switch Statement (case of)


class SwitchDemo {

public static void main(String args[])

{ int i;

for(i=0; i<10; i++)

Page 6
Al-Farabi University College

switch(i)

{ case 0: System.out.println("i is zero");

break;

case 1: System.out.println("i is one");

break;

case 2: System.out.println("i is two");

break;

case 3: System.out.println("i is three");

break;

case 4:

System.out.println("i is four");

break;

default:

System.out.println("i is five or more"); } } }

The While Loop


class Power {

public static void main(String args[]) {

int e; int result;

for(int i=0; i < 10; i++)

{ result = 1; e = i;

while(e > 0)

{ result *= 2; e--; }

System.out.println("2 to the power of " + i + " is " + result); } } }

Output//

2 to the power of 0 is 1
2 to the power of 1 is 2
2 to the power of 2 is 4
2 to the power of 3 is 8

Page 7
Al-Farabi University College

2 to the power … (up to „of 9 is 512‟)

Using Break to Terminate a Loop


class BreakDemo {
public static void main(String args[]) {
int num; num = 100;
for(int i=0; i < num; i++)
{ if(i*i >= num) break; // terminate loop if i*i >= 100
System.out.print(i + " ");
} System.out.println("Loop complete."); } }

Output// 0 1 2 3 4 5 6 7 8 9 Loop complete.

Use of Continue (complement of Break)


class ContDemo { public static void main(String args[])

{ int i;

for(i = 0; i<=100; i++)

{ if((i%2) != 0) continue;

System.out.println(i); } } }

Output//

100

Page 8

You might also like