Coding Java

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Array Ask Doubts

Methods & Arrays [Java]

Lectures Doubts Downloads


Java Operators
Java operators fall into different categories:

Arithmetic Operator
Flowcharts

Flowcharts
Assignment | Score:0/0

Programming Fundamentals 1

Programming Fundamentals 1
Assignment | Score:266.66/400

Condition loop
Note

Programming Fundamentals 2

Programming Fundamentals 2
Assignment | Score:0/360 publicclassTest{

Test 1 publicstaticvoidmain(Stringargs[]){
Test | Score:80/160
System.out.println("IntegerArithmetic");
inti=1+1;
Array intn=i*3;
Note intm=n/4;
intp=mi;
Arrays intq=p;
Note System.out.println("i="+i);
System.out.println("n="+n);
System.out.println("m="+m);
Arrays - 1 System.out.println("p="+p);
System.out.println("q="+q);
Arrays - 1 //arithmeticusingdoubles
Assignment | Score:0/440 System.out.println("\nFloatingPointArithmetic");
doublea=1+1;
doubleb=a*3;
Arrays - 2 doublec=b/4;
doubled=ca;
doublee=d;
Arrays - 2
System.out.println("a="+a);
Assignment | Score:0/360
System.out.println("b="+b);
System.out.println("c="+c);
String System.out.println("d="+d);
Note System.out.println("e="+e);
}
}
Multi Dimension Array
Note
IntegerArithmetic
i=2
Strings and 2D Arrays n=6
m=1
p=1
Strings
q=1
Assignment | Score:0/320

FloatingPointArithmetic
Recursion a=2.0
b=6.0
c=1.5
d=0.5
e=0.5

Relational Operator

Lectures Doubts Downloads

Flowcharts
Flowcharts

Flowcharts
publicclassTest{
Assignment | Score:0/0

publicstaticvoidmain(Stringargs[]){
Programming Fundamentals 1 inta=10;
intb=20;
System.out.println("a==b="+(a==b));
Programming Fundamentals 1
System.out.println("a!=b="+(a!=b));
Assignment | Score:266.66/400
System.out.println("a>b="+(a>b));
System.out.println("a<b="+(a<b));
Condition loop System.out.println("b>=a="+(b>=a));
Note System.out.println("b<=a="+(b<=a));
}
}
Programming Fundamentals 2
Output:
Programming Fundamentals 2 a==b=false
Assignment | Score:0/360 a!=b=true
a>b=false
a<b=true
Test 1 b>=a=true
Test | Score:80/160 b<=a=false

Array
Note
Logical Operator
Arrays
Note

Arrays - 1

Arrays - 1
Assignment | Score:0/440

Arrays - 2

publicclassTest{
Arrays - 2
Assignment | Score:0/360
publicstaticvoidmain(Stringargs[]){
booleana=true;
String booleanb=false;
Note System.out.println("a&&b="+(a&&b));
System.out.println("a||b="+(a||b));
System.out.println("!(a&&b)="+!(a&&b));
Multi Dimension Array
}
Note
}
Output:
Strings and 2D Arrays
a&&b=false
a||b=true
Strings
!(a&&b)=true
Assignment | Score:0/320

Recursion
Bitwise operators

Lectures Doubts Downloads

Flowcharts

Flowcharts
Assignment | Score:0/0

Programming Fundamentals 1

Programming Fundamentals 1
Assignment | Score:266.66/400

Condition loop
Note

Programming Fundamentals 2
Programming Fundamentals 2
Assignment | Score:0/360

Test 1
Test | Score:80/160

Note:
Array
>>> is unsigned bit shift >> and << are signed bit shift
Note
Conditional Operator:
Arrays
Note
if(a>b){
max=a;
Arrays - 1 }
else{
max=b;
Arrays - 1
}
Assignment | Score:0/440

Arrays - 2

Setting a single variable to one of two states based on single condition is such a common use of if-else that a
Arrays - 2 shortcut has been devised for it, here is it.
Assignment | Score:0/360

String
max=(a>b)?a:b;
Note

Multi Dimension Array


Note
Precedence Rule
Strings and 2D Arrays Java has well-dened rules for specifying the order in which the operators in an expression are evaluated when
the expression has several operators.

Strings
Assignment | Score:0/320

Recursion

Lectures Doubts Downloads

Flowcharts

Flowcharts
Assignment | Score:0/0

Programming Fundamentals 1

publicclassPrecedence{
Programming Fundamentals 1
publicstaticvoidmain(String[]args){
Assignment | Score:266.66/400
System.out.println(3+3*2);
System.out.println(3*32);
Condition loop System.out.println(3*3/2);
Note System.out.println("");

System.out.println(1*1+1*1);
Programming Fundamentals 2
System.out.println(1+1/11);
System.out.println(3*3/2+2);
Programming Fundamentals 2 }
Assignment | Score:0/360 }

Output:
Test 1
9
Test | Score:80/160
7
4
Array
Note 2
1
6
Arrays
Note

Arrays - 1
Scope of Variables
Scope of Variables
Arrays - 1 The scope of a variable denes the section of the code in which the variable is visible. As a general rule,
Assignment | Score:0/440 variables that are dened within a block are not accessible outside that block. The lifetime of a variable refers
to how long the variable exists before it is destroyed.
Arrays - 2
These example will give your better understanding of scope of variable.

Arrays - 2
Assignment | Score:0/360
publicclassTest{
publicstaticvoidmain(Stringargs[]){
String {
Note intx=10;
System.out.println(x);
Multi Dimension Array
}
Note
System.out.println(x);//errorxnotvisiblehere
}

Strings and 2D Arrays }

classTest{
Strings
publicstaticvoidmain(Stringargs[]){
Assignment | Score:0/320
{
intx=5;
{
intx=1010;//errorduplicatelocalvariablexSystem.out.println(x);
}
}
}
}

classTest{
publicstaticvoidmain(Stringargs[])
{
for(intx=0;x<4;x++)
{
System.out.println(x);
}

System.out.println(x)//error
//xcannotberesolvedtoavariable
}
}

Lectures Doubts Downloads

Method:
What a method(or function) is ?
Flowcharts
A function is a block of statements, which is used to perform a specic task. Suppose you are building an
application in Java language and in one of your program, you need to perform a same task more than once. So
Flowcharts in such scenario you have two options
Assignment | Score:0/0

Use the same set of statements every time you want to perform the task
Programming Fundamentals 1
Create a function, which would do the task, and just call it every time you need to perform the same task.
Programming Fundamentals 1 Why we need a method?
Assignment | Score:266.66/400 Functions are used because of following reasons

Condition loop To improve the readability of code.


Note
Improves the reusability of the code, same function can be used in any program rather than writing the
Programming Fundamentals 2
same code from scratch.

Programming Fundamentals 2 Debugging of the code would be easier if you use functions, as errors are easy to be traced.
Assignment | Score:0/360
Reduces the size of the code, duplicate set of statements are replaced by function calls.
Test 1
Syntax of dening a method
Test | Score:80/160

Array modifierreturnTypenameOfMethod(ParameterList){
Note //methodbody
}

Arrays
Note

The syntax shown above includes:


Arrays - 1

modier: will study this later


Arrays - 1
Assignment | Score:0/440
returnType: method may return a value.
Arrays - 2
nameOfMethod: it is advised to have a meaningful name for the method so that it would be easy to understand
Arrays - 2
the purpose of method just by seeing its name.
Assignment | Score:0/360

Parameter List: it is the type, order, and number of parameters of a method, these are optional, method may
String
Note contain zero parameters.

Multi Dimension Array method body: method body denes what the method does with statements.
Note

Method Calling
Strings and 2D Arrays
Method Calling
For using a method, it should be called. When a program invokes a method, the program control gets
transferred to the called method. There are two ways in which a method is called:
Strings
Assignment | Score:0/320
when a method returns a value

when a method returns no value

when a method returns value:

publicclassTest{

publicstaticvoidmain(String[]args){
inta=11;
intb=6;
intc=minFunction(a,b);
System.out.println("MinimumValue="+c);
}

/**returnstheminimumoftwonumbers*/
Lectures Doubts Downloads
publicstaticintminFunction(intn1,intn2){
intmin;
if(n1>n2)
min=n2;
else
Flowcharts
min=n1;

Flowcharts returnmin;
Assignment | Score:0/0 }
}

Programming Fundamentals 1

Programming Fundamentals 1
Assignment | Score:266.66/400
when a method returns no value:
Condition loop
The void keyword allows us to create methods which do not return a value. Here, in the following example we're
Note
considering method fun() that is a void method which does not return any value.

Programming Fundamentals 2

publicclassExampleVoid{
Programming Fundamentals 2
Assignment | Score:0/360
publicstaticvoidmain(String[]args){
fun(255.7);
Test 1 }
Test | Score:80/160
publicstaticvoidfun(doublepoints){
if(points>=202.5){
Array
System.out.println("Rank:A1");
Note
}
elseif(points>=122.4){
Arrays System.out.println("Rank:A2");
Note }
else{
System.out.println("Rank:A3");
Arrays - 1 }
}
Arrays - 1 }
Assignment | Score:0/440
Output:

Arrays - 2 Rank:A1

Arrays - 2
Assignment | Score:0/360

String What is call by value ?


Note
The call by value method of passing arguments to a function copies the actual value of an argument into the
Multi Dimension Array formal parameter of the function. In this case, changes made to the parameter inside the method have no
Note effect on the argument. Java passes method arguments by value

Strings and 2D Arrays


publicclassCallByValue{
Strings
Assignment | Score:0/320 publicstaticvoidmain(String[]args){
intx=3;
System.out.println("Valueofxbeforecallingincrement()is"+x);
increment(x);
System.out.println("Valueofxaftercallingincrement()is"+x);
}

publicstaticvoidincrement(inta){
System.out.println("Valueofabeforeincrementingis"+a);
a=a+1;
System.out.println("Valueofaafterincrementingis"+a);

}
}
}

Output:

Valueofxbeforecallingincrement()is3
Valueofabeforeincrementingis3
Valueofaafterincrementingis4
Valueofxaftercallingincrement()is3

Lectures Doubts Downloads


Note:
There are no default parameters in java.

Example :
Flowcharts

Flowcharts
voidfunction(intnum=10){}//notallowed
Assignment | Score:0/0

Programming Fundamentals 1

Programming Fundamentals 1 Array


Assignment | Score:266.66/400 An Array is a collection of variables of the same type.

For instance an array of int is a collection of variables of type int. The variables in the array are ordered and
Condition loop
each have an index.
Note

Programming Fundamentals 2 Initializing Arrays:


Programming Fundamentals 2
Assignment | Score:0/360
int[]temps=newint[3];
temps[0]=78;//fillingoneelementatatime
Test 1
temps[1]=88;
Test | Score:80/160
temps[2]=53;

Array
Note

Arrays Initialize array to take user input:


Note

Arrays - 1
Scanners=newScanner(System.in);
int[]nums=newint[8];
Arrays - 1
for(intctr=0;ctr<8;ctr++){
Assignment | Score:0/440
nums[ctr]=s.nextInt();
}
Arrays - 2

Arrays - 2
Assignment | Score:0/360
Initialize at time of declaration:
String
Note

double[]temperature={13.5,18.4,19.6,21.4};
Multi Dimension Array
Note

Strings and 2D Arrays

Finding max value from array:


Strings
Assignment | Score:0/320

publicclassTestArray{

publicstaticvoidmain(String[]args){

double[]myList={1.9,2.9,3.4,3.5};

//Printallthearrayelements
for(inti=0;i<myList.length;i++){
System.out.println(myList[i]+"");
}

//Summingallelements
doubletotal=0;
for(inti=0;i<myList.length;i++){
total+=myList[i];
}
System.out.println("Totalis"+total);

//Findingthelargestelement
doublemax=myList[0];
for(inti=1;i<myList.length;i++){
Lectures Doubts Downloads if(myList[i]>max){
max=myList[i];
}
}
Flowcharts System.out.println("Maxis"+max);
Flowcharts System.out.println("Maxis"+max);
}
}
Flowcharts
Assignment | Score:0/0

Programming Fundamentals 1

Returning array from method


Programming Fundamentals 1
Assignment | Score:266.66/400

Condition loop classArrayUse{


Note publicstaticvoidmain(String[]args){
int[]A=numbers();
}
Programming Fundamentals 2

publicstaticint[]numbers(){
Programming Fundamentals 2 int[]A=newint[3];
Assignment | Score:0/360 A[0]=2;
A[1]=3;
A[2]=4;
Test 1
returnA;
Test | Score:80/160
}
}
Array
Note

Arrays
Note Passing array to method:
Arrays - 1

classTestarray{
Arrays - 1
staticvoidmin(intarr[]){
Assignment | Score:0/440
intmin=arr[0];
for(inti=1;i<arr.length;i++){
Arrays - 2 if(min>arr[i]){
min=arr[i];
}
Arrays - 2 System.out.println(min);
Assignment | Score:0/360 }
publicstaticvoidmain(Stringargs[]){
String inta[]={33,3,4,5};
Note min(a);
}
}
Multi Dimension Array
Note

Strings and 2D Arrays

Reverse an array
Strings
Assignment | Score:0/320

staticvoidreverse(intarr[]){
inttemp;
for(inti=0;i<arr.length/2;i++){
temp=arr[i];
arr[i]=arr[arr.length1i];
arr[arr.length1i]=temp;
}
}
publicstaticvoidmain(String[]args){
inta[]={1,2,3,4,5,6};
reverse(a);
for(inti:a){
System.out.print(i+"");
}
}
Output:
654321

Lectures Doubts Downloads Bubble Sort


Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they
are in wrong order.
Flowcharts

Flowcharts
Assignment | Score:0/0

Programming Fundamentals 1

Programming Fundamentals 1
Assignment | Score:266.66/400

Condition loop
Note

Programming Fundamentals 2
Programming Fundamentals 2
Assignment | Score:0/360

Test 1
Test | Score:80/160

Array
Note

Arrays
Note

Arrays - 1

Arrays - 1
Assignment | Score:0/440

Arrays - 2

Arrays - 2
Assignment | Score:0/360

String
Note

Multi Dimension Array


Note

Strings and 2D Arrays

Strings
Assignment | Score:0/320

Lectures Doubts Downloads

Flowcharts

Flowcharts
Assignment | Score:0/0

Programming Fundamentals 1

Programming Fundamentals 1
Assignment | Score:266.66/400

Condition loop
Note

Programming Fundamentals 2

Programming Fundamentals 2
Assignment | Score:0/360

Test 1
Test | Score:80/160

Array
Note

Arrays
Note

Arrays - 1
Arrays - 1
Assignment | Score:0/440

Arrays - 2

Arrays - 2
Assignment | Score:0/360

String
Note

Multi Dimension Array


Note

Strings and 2D Arrays

Strings
Assignment | Score:0/320

Lectures Doubts Downloads


publicstaticvoidBubbleSort(intarray[]){
inttemp=0;
for(inti=0;i<array.length1;i++){
intflag=1;
Flowcharts
for(intj=0;j<array.lengthi1;j++){
if(array[j]>array[j+1]){
Flowcharts temp=array[j];
Assignment | Score:0/0 array[j]=array[j+1];
array[j+1]=temp;
flag=0;
Programming Fundamentals 1
}
}
Programming Fundamentals 1 if(flag==1)
Assignment | Score:266.66/400 break;
}
}
Condition loop
Note

Programming Fundamentals 2
Selection Sort
Programming Fundamentals 2 The smallest element is selected from the unsorted array and swapped with the leftmost element, and that
Assignment | Score:0/360 element becomes a part of the sorted array. This process continues moving unsorted array boundary by one
element to the right.
Test 1
Test | Score:80/160

Array
Note

Arrays
Note

Arrays - 1

Arrays - 1
Assignment | Score:0/440

Arrays - 2

Arrays - 2
Assignment | Score:0/360

String
Note
publicstaticvoidsort(intarray[]){
intsmallest=1,temp=0;
Multi Dimension Array for(inti=0;i<array.length1;i++){
Note smallest=i;
for(intj=i+1;j<array.length;j++){
for(intj=i+1;j<array.length;j++){
Strings and 2D Arrays if(array[smallest]>array[j]){
smallest=j;
}
Strings }
Assignment | Score:0/320 temp=array[smallest];
array[smallest]=array[i];
array[i]=temp;
}
}

Insertion Sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted.
For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this
sorted sub-list, has to nd its appropriate place and then it has to be inserted there. Hence the name, insertion
sort.

The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the
same array).

Lectures Doubts Downloads

Flowcharts

Flowcharts
Assignment | Score:0/0

Programming Fundamentals 1

Programming Fundamentals 1
Assignment | Score:266.66/400

Condition loop
Note

Programming Fundamentals 2
publicstaticvoidsort(intarray[]){
inttemp=0,i=0,j=0;
Programming Fundamentals 2 for(i=1;i<array.length;i++){
Assignment | Score:0/360 temp=array[i];
for(j=i1;j>=0&&temp<array[j];j){
Test 1
array[j+1]=array[j];
Test | Score:80/160
}
array[j+1]=temp;
}
Array }
Note

Arrays
Note

Arrays - 1

Arrays - 1
Assignment | Score:0/440

Arrays - 2

Arrays - 2
Assignment | Score:0/360

String
Note

Multi Dimension Array


Note

Strings and 2D Arrays

Strings
Assignment | Score:0/320

You might also like