In: Computer Science
PROGRAM SIMULATION. Understand the given JAVA program and write the output.
1. public class Places
{
public static void main(String args[])
{
String place[]=new String[4];
place[0]="Salmaniya";
place[1]="Salmabad";
place[2]="Isa Town";
place[3] = “Manama”
System.out.println(place[3]);
System.out.println(place[1].toLowerCase());
System.out.println(place[2].substring(4,6);
System.out.println(place[3].charAt(4));
System.out.println(place[1].equals(place[2]));
}
}
b. public class ChangeIt
{
public void doIt( int[] z )
{
z[0] = 0;
}
}
public class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt(myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
Answer 1.
Step 1
Source Code
public class Places
{
public static void main(String args[])
{
String place[]=new String[4];
place[0]="Salmaniya";
place[1]="Salmabad";
place[2]="Isa Town";
place[3] = “Manama”;
System.out.println(place[3]); //it just prints the value of a places array at index 3 and output is "Manama"
System.out.println(place[1].toLowerCase()); //it converts the place at index 1 of array to lowercase and prints it and output is "salmabad"
System.out.println(place[2].substring(4,6)); //it prints substring from 4 to 6 indexes excluding 6 of place at index 2 of array and output is "To"
System.out.println(place[3].charAt(4)); //it prints the character at index 4 of the place at index 3 of array and output is "m"
System.out.println(place[1].equals(place[2])); //it prints true if places at index1 and places at index 2 are same and output is "false"
}
}
Step 2
Output
Step 3
Source code of the Program: save the below code as Places.java
//Places.java
public class Places{
public static void main(String args[]){
String place[]=new String[4];
place[0]="Salmaniya";
place[1]="Salmabad";
place[2]="Isa Town";
//add semicolon at the last
place[3] ="Manama";
//display the place[3] string
Manama
System.out.println(place[3]);
//converts the all characters of place[1] string into lower
case
System.out.println(place[1].toLowerCase());
//add closing parenthesis at the
end
//display the characters from 4th indices to 5th indices
from the string place[2]
System.out.println(place[2].substring(4,6));
//display the character at the index of 4th of the string
place[3]
System.out.println(place[3].charAt(4));
//display true if the string place[1]is equal
to the string place[2]
System.out.println(place[1].equals(place[2]));
}
}
Run the Places.java and it will display the below output.
Answer b.
Step 1
The second java program uses two classes but it will show an error because the method in the ChangeIt class is not static so declare the method as static because inside the static method we can call only static members. The java program changes the 0th index element of the array using the doIt method of the ChangeIt class and then display the changed array elements in the other class TestIt through call the function.
Step 2
Source code of the Second Program: save both java files under the same project and run the TestIt.java class because it contains the main function.
//TestIt.java
public class TestIt {
//start of main function
public static void main ( String[] args ) {
int[] myArray = {1, 2, 3, 4, 5} ;
//calling the function doIt of ChageIt
class
ChangeIt.doIt(myArray );
//displaying the array element after changing
its 0th element using the doIt method
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}//end of main function
}
//ChangeIt.java
public class ChangeIt {
public static void doIt( int[] z )
{
//it is changing the array 0th index element to
0
z[0] = 0;
}
}
Run the TestIt.java and it will display the below output
Thank you.