0% found this document useful (0 votes)
91 views9 pages

JAVA3RDEX

The fourth problem reads an unknown number of strings from the user, stores them in an array, and sorts the array in ascending alphabetical order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views9 pages

JAVA3RDEX

The fourth problem reads an unknown number of strings from the user, stores them in an array, and sorts the array in ascending alphabetical order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Ex.

No :
3

Name
: NITHIN ZAHIRUDDIN S.A
Reg. No : 18MIS1106
Programme : M.Tech (SE)
Semester : Fall 2019-20
Course : Programming in Java Lab
Code : SWE1007
Faculty : prof.RAJ KUMAR
Slot : L43+44

1. Create a Java program called ArrayProgram for the following: - declare and create an array newNumbers which
can contain 12 int numbers - assign the following values into newNumbers 10, 6, 88, 91, 25, 77, 14, 23, 4, 235,
66, 81. - find and display the length of the array. - display the value stored in the 5th element of the array. -
change the value in the 5th element to 35. Display again. - calculate and display the average of the values
stored in the array (use the length attribute). - Sort the newNumbers in ascending order. - Search a number
from newNumbers.

Code:

import java.io.*;
class ArrayProgram

public static void main(String args[])throws Exception

DataInputStream d=new DataInputStream(System.in);

int newNumbers[]={10,6,88,91,25,77,14,23,4,235,66,81}; //using 1D ARRAY

int arraylength=newNumbers.length; //using the function name.length to print length

System.out.println("Fifth element is"+newNumbers[4]); //to print the 5 th subset of array

newNumbers[4]=35; //change of numbers in the array set[4]

System.out.println("Rewritten element is"+newNumbers[4]); //printing the changed array number

int i,j,total=0,temp;

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

total=total+newNumbers[i];

int avg=total/arraylength; //to print the average of the array

System.out.println("Average is"+avg);

for(i=0;i<arraylength-1;i++)

for(j=0;j<=arraylength-i-1;j++)

if(newNumbers[j]>newNumbers[j+1])

temp=newNumbers[j];

newNumbers[j]=newNumbers[j+1];

newNumbers[j+1]=temp;

}
}

System.out.println("sorted array is");

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

System.out.println(newNumbers[i]);

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

if(newNumbers[i]==77)

int position;

position=i-1;

System.out.println("position is"+position);

Output:
2. A mathematician wants to develop an automated program to check the matrix is symmetric or not. He/She
approaches you for the help. Being a programmer, through a java program to fulfill his/her requirement. Example:
AT = A 1 2 3 2 8 4 3 4 9 Note: Read the inputs through keyboard.

Code:

import java.io.*; //package

class Symmetry //creating class

public static void main(String args[])throws Exception

int a[][]=new int[3][3]; //two dimensional array usuage

DataInputStream d=new DataInputStream(System.in);

int count=0,i,j;

System.out.println("Enter the array"); //getting input from the user

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

{ //2D two for loops for iteration

for(j=0;j<3;j++)

a[i][j]=Integer.parseInt(d.readLine()); //conversion

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

{ //since 2D two for loops are used

for(j=0;j<3;j++)

if(a[i][j]!=a[j][i]) //keeps checking for same values as in 00,01=10,11=11,12=21......

count=1; //if both the values are not equal then it goes inside the loop and becomes 1

}
}

if(count==0) //if the values are equal while checking using boolean numbers then it prints symmetric

System.out.println("Its symmetric");

else

System.out.println("Its not symmetric"); //print or not symmetric

Output:

3. Develop a String program to find the index position of the following character marked in aeromark: S= “All the
best to you”

Code:

import java.io.*;

class index

public static void main(String args[])

DataInputStream d=new DataInputStream(System.in);

String S="All the best to you";

int pos=S.indexOf("t");
int pos1=S.indexOf("t",pos+1);

System.out.println(pos1);

Output:

4. Develop a Java program to read ‘n’ strings and sort the strings in ascending order. Note: Read the inputs through
keyboard.

Code:

import java.util.Scanner;

public class sortstring

{
public static void main(String[] args)

int n;

String temp;

Scanner s = new Scanner(System.in);

System.out.print("Enter number of strings:");

n = s.nextInt();

String a[] = new String[n];

Scanner s1 = new Scanner(System.in);

System.out.println("Enter strings:");

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

a[i] = s1.nextLine();

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

for (int j = i + 1; j < n; j++)

if (a[i].compareTo(a[j])>0)

temp = a[i];

a[i] = a[j];

a[j] = temp;

System.out.print("Sorted Order:");

for (int i = 0; i < n - 1; i++)


{

System.out.print(a[i] + ",");

System.out.print(a[n - 1]);

sir when I retried the same problem online at home ,i was not able to get the ouput. But in class I got the output
sir.i'm unable to find the error in the program.

You might also like