12 It
12 It
12 It
Class: XII
Topic: Unit-3:
Subject: Information Technology (802) Fundamentals of Java Programming.
Java is a very popular high level programming language and has been used widely to create various types of
computer applications such as database applications, desktop applications, Web based applications, mobile
applications, and games among others. A Java compiler instead of translating Java code to machine language
code, translates it into Java Bytecode (a highly optimized set of instructions). When the bytecode (also
called a Java class file) is to be run on a computer, a Java interpreter, called the Java Virtual Machine
(JVM), translates the bytecode into machine code and then executes it.
The advantage of such an approach is that once a programmer has compiled a Java program into bytecode, it
can be run on any platform (say Windows, Linux, or Mac) as long as it has a JVM running on it. This makes
Java programs platform independent and highly portable.
1. Variable names can begin with either an alphabetic character, an underscore (_), or a dollar sign ($).
However, convention is to begin a variable name with a letter. They can consist of only alphabets, digits, and
underscore.
2. Variable names must be one word. Spaces are not allowed in variable names. Underscores are allowed.
3. There are some reserved words in Java that cannot be used as variable names, for example - int.
4. Java is a case-sensitive language.
5. It is good practice to make variable names meaningful. The name should indicate the use of that variable.
6. You can define multiple variables of the same type in one statement by separating each with a comma.
String Variables: we want variables to store textual data, for example, the name of a student.
To store more than one character, we use the String class in Java. Eg. String first_name = "Mayank";
Control Flow: Is a sequence of instructions. Java executes the instructions in sequential order, that is, one
after the other.
Selection Structures
In real life, you often select your actions based on whether a condition is true or false. For example, if it is
raining outside, you carry an umbrella, otherwise not.
The if Else Statement : The if statement in Java lets us execute a block of code depending upon whether an
expression evaluates to true or false.
Repetition Structures
The ability of a computer to perform the same set of actions again and again is called looping. The sequence
of statements that is repeated again and again is called the body of the loop.
The test conditions that determine whether a loop is entered or exited is constructed using relational and
logical operators. A single pass through the loop is called an iteration.
For example, a loop that repeats the execution of the body three times goes through three iterations.
Java provides three statements – the while statement, the do while statement, and the for statement for
looping.
The for Statement
The for loop is the most widely used Java loop construct. The structure of the Java for statement is as below:
for (counter=initial_value; test_condition;change counter)
{
statements
}
While Statement
The while statement evaluates the test before executing the body of a loop. A while loop is an entry
controlled loop. The structure of the Java while statement is as shown:
The While Statement
while (expression)
{
statements
}
Do While Statement
The do while statement evaluates the test after executing the body of a loop.
A do-while loop is an exit control loop.
The structure of the Java do while statement is as shown:
do
{
statements
} while (expression);
ARRAY
An array is a collection of similar types of data. Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value. To declare an array, define the variable type
with square brackets [ ]:
Types of Array in java
There are two types of array.
Single Dimensional Array
Multidimensional Array
Single Dimensional Array in Java
Syntax to Declare an Array in Java
1. dataType[] arr; (or)
2. dataType arr[];
Instantiation of an Array in Java
1. arrayRefVar=new datatype[size];
For example, if we want to store the names of 100 people then we can create an array of the string type that
can store 100 names.
String[] array = new String[100];
Here, the above array cannot store more than 100 names. The number of values in a Java array is always
fixed.
For example:
double[] data;
Here, data is an array that can hold values of type double.
Output: Volvo
Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Output: Opel
Array Length
To find out how many elements an array has, use the length property:
Example:
public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
}
Output: 4
Example:
public static void main(String args[]) {
int a[]=new int[5];
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
To create a two-dimensional array, add each array within its own set of curly braces:
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers is now an array with two arrays as its elements.
To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the
element inside that array.
Example:
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x);
}
Output: 7
Example:
//Java Program to illustrate the use of multidimensional array
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
Example:
To store the marks of a student 5 students:
// TODO add your handling code here:
double[]marks = {346, 144, 103, 256.5, 387.5};
double total_marks = 400;
System.out.println("\tClass Report");
System.out.println("--------------------------------------");
System.out.println("RollNo\tMarks\tPercentage\tResult");
System.out.println("--------------------------------------");
for (int i = 0; i <marks.length; i++) {
double percentage = (marks[i]/total_marks)*100;
String result;
if (percentage >= 40)
result = "Passed";
else
result = "Failed";
System.out.print((i+1)+"\t");
System.out.print(marks[i]+"\t");
System.out.print(percentage+"\t\t");
System.out.println(result);
}
Example:
WAP to sort an array of Strings in alphabetic order:
public static void main(String args[]) {
String[] names = {"Shruti", "Kunal", "Gungun","Avani","Ravi","Tripti","Purva","Aditya","Chandan"};
System.out.println("Names Array before Sorting:");
for (int i = 0; i<names.length; i++)
System.out.print(names[i] + ",");
System.out.println();
Arrays.sort(names);
System.out.println("Names after Sorting:");
for (int i=0; i < names.length; i++)
System.out.print(names[i] + ",");
System.out.println();
Create a Method
A method must be declared within a class. It is defined with the name of the method, followed by
parentheses (). Java provides some pre-defined methods, such as System.out.println()
To call a method in Java, write the method's name followed by two parentheses () and a semicolon;
In the following example, myMethod() is used to print a text (the action), when it is called:
Example:
static void myMethod() {
System.out.println("I got FULL MARKS IN IT!");
}
public static void main(String[] args) {
myMethod();
}