JAVA FIRST CLASS ARRAYS (1)
JAVA FIRST CLASS ARRAYS (1)
PROGRAM-3
package org.program;
public class Program1 {
public static void main(String[] args) {
byte[] arr = new byte[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("byte "+arr[i]);
}
int[] arr1 = new int[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("int "+arr1[i]);
}
short[] arr2 = new short[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("short "+arr2[i]);
}
long[] arr3 = new long[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("long "+arr3[i]);
}
float[] arr4 = new float[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("float "+arr4[i]);
}
double[] arr5 = new double[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("double "+arr5[i]);
}
char[] arr6 = new char[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("char "+arr6[i]);
}
boolean[] arr7 = new boolean[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("boolean "+arr7[i]);
}
String[] arr8 = new String[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("string "+arr8[i]);
}
}
}
OUTPUT:
byte 0
byte 0
int 0
int 0
short 0
short 0
long 0
long 0
float 0.0
float 0.0
double 0.0
double 0.0
char
char
Boolean False
Boolean False
string null
string null
PROGRAM-4
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,2,3,4,5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
OUTPUT:
1
2
3
4
5
PROGRAM-5
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,2,3,4,5};
for (int i = 0; i < arr.length; i=i+2) {
System.out.println(arr[i]);
}
}
}
OUTPUT:
1
3
5
PROGRAM-6
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,2,3,4,5};
for (int i = 1; i < arr.length; i=i+2) {
System.out.println(arr[i]);
}
}
}
OUTPUT:
2
4
PROGRAM-7
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,1,2,3,4,5};
for (int i = 0; i < arr.length; i=i+1) {
if(arr[i]==1) {
System.out.println(arr[i]);
}
}
}
}
OUTPUT:
1
1
PROGRAM-8
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,1,2,3,4,5};
int count=0;
for (int i = 0; i < arr.length; i=i+1) {
if(arr[i]==1) {
count++;
}
}
System.out.println(count);
}
}
OUTPUT:
2
PROGRAM-9
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,1,2,3,4,5};
int max=0;
for (int i = 0; i < arr.length; i++) {
if(max<arr[i]) {
max=arr[i];
}
}
System.out.println(max);
}
}
OUTPUT:
5
PROGRAM-10
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {70,10,20,30,40,50};
int max=0;
int secMax=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>max) {
secMax=max;
max=arr[i];
}
else if (arr[i]>secMax) {
secMax=arr[i];
}
}
System.out.println(max);
System.out.println(secMax);
}
}
OUTPUT:
70
50
PROGRAM-11
WRAPPER CLASS is used
Primitive class
byte
short
int
long
float
double
char
boolean
byte -Byte
short -Short
int -Int
long -Long
float -Float
double -Double
char -Charecter
boolean –Boolean
PROGRAM-12
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {-70,-10,-20,-30,-40,-50};
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
}
}
OUTPUT:
-2147483648
2147483647
PROGRAM-13
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {-70,-10,-20,-30,-40,-50};
int max=Integer.MIN_VALUE;
int secMax=Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>max) {
secMax=max;
max=arr[i];
}
else if (arr[i]>secMax) {
secMax=arr[i];
}
}
System.out.println(max);
System.out.println(secMax);
}
}
OUTPUT:
-10
-20
PROGRAM-13
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "12345";
System.out.println(Integer.parseInt(s));
}
}
OUTPUT:
12345
PROGRAM-14
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "Preethi12345";
System.out.println(Integer.parseInt(s));
}
}
OUTPUT:
Exception in thread "main"
java.lang.NumberFormatException: For input
string: "Preethi12345"at
java.lang.NumberFormatException.forInputString(
Unknown Source)at
java.lang.Integer.parseInt(Unknown Source)at
java.lang.Integer.parseInt(Unknown Source)at
org.program.Program1.main(Program1.java:6)
PROGRAM-15
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "Preethi12345";
try {
int i=Integer.parseInt(s);
System.out.println("yes");
}
catch (NumberFormatException nfe){
System.out.println("no");
}
}
}
OUTPUT:
No
PROGRAM-16
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "12345";
try {
int i=Integer.parseInt(s);
System.out.println("yes");
}
catch (NumberFormatException nfe){
System.out.println("no");
}
}
}
OUTPUT:
Yes
PROGRAM-16
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,20,40,50,60,70};
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
System.out.println(arr[i]);
}
}
}
}
OUTPUT:
20
20
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,20,40,50,60,70};
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
System.out.println(i);
}
}
}
}
OUTPUT:
1
2
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,20,40,50,60,70};
int count =0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
count++;
}
}
System.out.println(count);
}
}
OUTPUT:
2
SELECTION SORTING
PROGRAM-17
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,30,40,50,60,70};
boolean keyispresent= false;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==25)
{
keyispresent=true;
System.out.println("yes");
break;
}
}
if(keyispresent==false)
{
System.out.println("no");
}
}
}
OUTPUT:
no
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,30,40,50,60,70};
boolean keyispresent= false;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
keyispresent=true;
System.out.println("yes");
break;
}
}
if(keyispresent==false)
{
System.out.println("no");
}
}
}
OUTPUT:
Yes
PRIME NUMBER
package org.program;
public class Program1 {
public static void main(String[] args) {
int num =13;
boolean keyispresent= true;
for (int i = 2; i < num; i++) {
if(num%i==0)
{
keyispresent=false;
System.out.println("no");
break;
}
}
if(keyispresent==true)
{
System.out.println("yes");
}
}
}
OUTPUT:
Yes
SCANNER Class
package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number:");
int a = sc.nextInt();
System.out.println("number is "+ a);
}
}
OUTPUT:
Enter the number:
1
number is 1
package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
int arr[]= new int[5];
for (int i = 0; i < arr.length; i++) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number:");
int a = sc.nextInt();
System.out.println("number is "+ a);
}
}
}
OUTPUT:
Enter the number:
1
number is 1
Enter the number:
2
number is 2
Enter the number:
3
number is 3
Enter the number:
4
number is 4
Enter the number:
5
number is 5
PROGRAM-18
package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner arrayLength= new Scanner(System.in);
System.out.println("Enter the length:");
int length = arrayLength.nextInt();
int arr[]= new int[length];
for (int i = 0; i < arr.length; i++) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number:");
int a = sc.nextInt();
System.out.println("number is "+ a);
}
}
}
OUTPUT:
Enter the length:
5
Enter the number:
1
number is 1
Enter the number:
2
number is 2
Enter the number:
3
number is 3
Enter the number:
4
number is 4
Enter the number:
5
number is 5
package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,30,40,50};
for (int i = 0; i < arr.length; i++) {
int temp=arr[0];
for (i = 0; i < arr.length-1; i++) {
arr[i]=arr[i+1];
}
arr[i]=temp;
for (i = 0; i < arr.length; i++) {
System.out.println("after changing of array
"+arr[i]);
}
}
}
}
OUTPUT:
after changing of array 20
after changing of array 30
after changing of array 40
after changing of array 50
after changing of array 10