Question

In: Computer Science

PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {...

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] + " " ) ;
          }
     }

Solutions

Expert Solution

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.


Related Solutions

write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
in java Write a program that will print the following output 1 1 2 1 1...
in java Write a program that will print the following output 1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 18 8 4 2 1
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT