Introduction to Object Oriented Programming in Java

1.Class- Class is the blue print or boiler plate of our project. It consists of attributes(Colour, names and model) and non static methods(functions like turnOn( ); etc) of objects. Microphone Class //Microphone class public class CreatingClass { //Atrributes String name = "sony" ; String colour = "Blue" ; int model = 123 ; //Methods public void turnOn (){ System . out . println ( "Turned on" ); } public void adjustVol (){ System . out . println ( "Volume adjusted" ); } public void turnoff (){ System . out . println ( "Turned off" ); } public void description (){ System . out . println ( this .name + " is my brand. " + "My colour is " + this .colour); } } Main class public class CreatingClassMain { public static void main ( String [] args ) { CreatingClass Microphone1 =new CreatingClass (); Microphone1. turnoff ();

Java Array Problems

Java Program to Find Largest Number in an array
public class A_MaxNumInArray {
public static void main(String[] args) {
int[] array={4,-3,7,100,89,45,99};
//Find the largest number in this array.
//Logic-
// 1. We consider the 1st number highest(int max).
// 2. store values of array in j after each ilteration.
// 3. if j>max then max=j.

int max=Integer.MIN_VALUE;
for(int i=0; i<array.length;i++){
int j=array[i];
if (j>max){
max=j;
}
}
System.out.println("The max number is: "+max);
}
}
Java Program to Find Smallest Number in an array
public class A_MinNumInArray {
public static void main(String[] args) {
int[] array={100,39,-90,45,3,1,57};
//Find the minimum number from the array.
//Logic-
//1. let 1st number be minimum(int min).
//2. ilitarate through array and store in j.
//3. if j<min then min=j;
int min=Integer.MAX_VALUE;
for(int i=0; i<array.length; i++){
int j=array[i];
if(j<min){
min=j;
}
}

System.out.println("Min number: "+min);
}

}

Java Program to Find 2nd Largest Number in an array

public class B_SecondLargestInArray {
public static void main(String[] args) {
int[] array={-3,20,20,15,-7,23,23};
//Find the 2nd largest number in array.
//Logic-
//1.Find the first largest number
//2. 2nd largest number is the number in the array only less than 1st largest.
int firstLarge=Integer.MIN_VALUE;
for(int i=0; i<array.length; i++){
int j=array[i];
if (j>firstLarge){
firstLarge=j;
}
}
System.out.println("1st largest: "+firstLarge);
int secLarge=Integer.MIN_VALUE;
for (int k = 0; k < array.length; k++) {
int l=array[k];
if(l>secLarge && l<firstLarge){
secLarge=l;
}
}
System.out.println("2nd largest: "+secLarge);
}
}

Java Program to Find 2nd Smallest Number in an array

public class B_SecondSmallestInArray {
public static void main(String[] args) {
int[] array = {-3, 20, 20, 15, -7, 23, 23};
//Find the 2nd smallest number from the array.
int firstSmall = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
int j = array[i];
if (j < firstSmall) {
firstSmall = j;
}

}
System.out.println(firstSmall);
int secSmall=Integer.MAX_VALUE;
for (int k = 0; k< array.length; k++) {
int l=array[k];
if(l<secSmall && l>firstSmall){
secSmall=l;
}
}
System.out.println(secSmall);

}
}

Java Program to Find 3rd Largest Number in an array

public class C_ThirdLargeArray {
public static void main(String[] args) {
int[] array={100,-90,4,25,-15,90,230,-20,79,55,89,-19};
//Find the third largest number
int firstLarge=Integer.MIN_VALUE;
for(int i=0; i< array.length; i++){
int j=array[i];
if (j>firstLarge){
firstLarge=j;
}
}
System.out.println(firstLarge);
int secLarge=Integer.MIN_VALUE;
for (int k=0; k< array.length; k++){
int l=array[k];
if(l>secLarge && l<firstLarge){
secLarge=l;
}
}
System.out.println(secLarge);
int thirdLarge=Integer.MIN_VALUE;
for (int m = 0; m < array.length ; m++) {
int n=array[m];
if(n>thirdLarge && n<secLarge)
thirdLarge=n;

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

Java Program to Find 3rd Smallest Number in an array

public class C_ThirdSmallArray {
public static void main(String[] args) {
int[] array = {100, -90, 4, 25, -15, 90, 230, -20, 79, 55, 89, -19};
int firstSmall = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
int j = array[i];
if (j < firstSmall) {
firstSmall = j;
}
}
System.out.println(firstSmall);
int secSmall = Integer.MAX_VALUE;
for (int k = 0; k < array.length; k++) {
int l = array[k];
if (l < secSmall && l > firstSmall) {
secSmall=l;
}
}
System.out.println(secSmall);
int thirdSmall=Integer.MAX_VALUE;
for (int m = 0; m < array.length ; m++) {
int n=array[m];
if(n<thirdSmall && n>secSmall){
thirdSmall=n;
}
}
System.out.println(thirdSmall);
}
}

Java Program to find the sum of all elements in array

public class Z_Test {
public static void main(String[] args) {
int[] array = {-90, 100, 90,-100,45,50,-45};
//Find Sum of all the elements.
int sum = 0;


for (int i = 0; i < array.length; i++) {
int j = array[i];
sum=j+sum;

}
System.out.println(sum);

}
}
Java Program to print odd and even numbers in array
public class Z_Test {
public static void main(String[] args) {
int[] array={5,20,12,9,3,15,90,81,74,30};
//Print odd & even numbers

System.out.println("The even numbers are: ");
for(int i=0;i<array.length;i++){
int j=array[i];
if(j%2==0){
System.out.println(j);
}
}
System.out.println("The odd numbers are: ");
for (int k = 0; k < array.length ; k++) {
int l=array[k];
if(l%2 !=0){
System.out.println(l);
}

}
}

}
Java Program to copy all elements of one array to another
public class Z_Test {
public static void main(String[] args) {

int[] array1 = {5, 9, 10, 16, 25, -37};
//Program to copy all elements of one array into another array
int[] array2 = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
int j = array1[i];
array2[i] = j;

}
for (int k = 0; k < array2.length; k++) {
System.out.println(array2[k]);

}

}}
Java Program to left rotate elements in arrays.
public class Z_Test {
public static void main(String[] args) {
int[] array={5,4,9,0,7,2,15};
//make an arrayB such that
//int[] arrayB={4,9,8,7,2,15,5};
int[] arrayB=new int[array.length];
for(int i=1; i<array.length;i++){
System.out.println(array[i]);
int j=array[i];
arrayB[i-1]=j;
}
arrayB[array.length-1]=array[0];
System.out.println("The required left rotate: ");
for (int j = 0; j < arrayB.length; j++) {
System.out.println(arrayB[j]);
}
}
}
Java Program toright rotate elements in arrays.
public class Z_Test {
public static void main(String[] args) {
int[] array={5,2,9,7,8,4,3,1};
//Java Program to right rotate the elements.
int[] arrayB=new int [array.length];
for (int i = 0; i < array.length-1; i++) {
int j=array[i];
arrayB[i+1]=j;
}
arrayB[0]=array[array.length-1];
for (int k = 0; k < arrayB.length; k++) {
System.out.println(arrayB[k]);
}}}
Java Program to reverse the elements of an arrays.
public class Z_Test {
public static void main(String[] args) {
int[] array={5,-4,9,5,3,2,1};
//Make another array such that arrayB={1,2,3,5,9,-4,5};
int[] arrayB=new int[array.length];

for (int i = 0; i < array.length ; i++) {
int j=array[i];
arrayB[arrayB.length-1-i]=j;

}
for (int j = 0; j <arrayB.length ; j++) {
System.out.println(arrayB[j]);
}
}
}
Java Program to print elements of array in even position
public class Z_Test {
public static void main(String[] args) {
int[] array={5,2,9,7,8,4,3,1};
//Java Program to print the elements of an array present on even position.
for (int i = 1; i < array.length ; i=i+2) {
System.out.println(array[i]);
}
}


}
Java program to print elements of object in odd position
public class Z_Test {
public static void main(String[] args) {
int[] array={5,2,9,7,8,4,3,1};
//Java Program to print the elements of an array present on odd position.
for (int i = 0; i < array.length; i=i+2) {
System.out.println(array[i]);
}
}
}



To Be Continued....

Comments

Post a Comment

Popular posts from this blog

Introduction to Object Oriented Programming in Java