In: Computer Science
Language: Java
Create a TryIt.java program with a main method. You are going to
use this
program to demonstrate some things about how Java works. Make your
methods
here private, because they are not intended to be called from
outside the program.
For each test below, make sure that you print to the console:
1) What is being tested
2) The desired output
3) The actual output
Question to be answered: Should you use == or the String method
equals
to determine if two Strings are the same?
A. In main, create three Strings using the following pattern:
String varName = “string literal”;
Two of the Strings should be identical.
1) test to see if == works for identical and non-identical String
literals.
2
2) test to see if equals works for identical and non-identical
String literals
B. Also in main, create three Strings using the following
pattern:
String varName = new String(“phrase”);
Use the same phrases you used in part 1. Just create the Strings
differently.
Two of the Strings should be identical.
1) test to see if == works for identical and non-identical String
literals.
2) test to see if equals works for identical and non-identical
String literals
Question to be answered: If you pass an array ( or any other
object), and
you change the values of the elements (or the instance variables)
in the
method it was passed to, are the values changed back in the
calling
method?
C. Create a static method static void testTempVars(). Create this
local
variable in the method.
int [] myArray = {3, 4, 5};
Print out the original values using a for loop that makes i go from
0 to 2.
The print statement inside the loop will be:
System.out.println(myArray[i]);
Be sure to label the output as “original values”. Call the
testTempVars
method from main.
D. Create another method static void arrayPassing(int []theArray)
that
takes an integer array as a parameter. Add 1 to each element of the
array
inside the arrayPassing method using a for loop. Print out the
array. Be
sure to label the output as “arrayPassing values”.
E. In testTempVars after the print of the original values, call the
arrayPassing
method and pass in myArray. Also, at the end of testTempVars, print
out all
values in the array again. Be sure to label the output as “values
of original
array after arrayPassing executes”.
Question to be answered: If you pass a primitive, and you change
the
value of the primitive in the method it was passed to, is the value
changed
back in the calling method?
F. In testTempVars add an int variable and initialize it to 10.
Print the
variable with an appropriate label indicating this is the original
value.
3
G. Create another static void method called primitivePassing that
takes an int
parameter. In that method, subtract 5 from the parameter and then
print its
value with the label “primitivePassing value”.
H. In testTempVars after the print of the int variable, call
the
primitivePassing method and pass in the int variable. After the
method
call, print the value of the int variable again. Be sure to label
the output as
“values of original array after primitivePassing executes”.
Theory:
String:
String is a collections of charaters. It is a data type in Java which allow variables to store multiple characters.
String Comparison:
For compare two strings we use following methods:
1. using '==' operator:
It compares references not values thats why when we create same values strings using constructor and campare using '==' operator then it return false because both strings reffer different memory location.
2. equals() method:
In this method it compare actual values of strings.
Solution:
Code:
TryIt.java
import java.io.*;
import java.util.*;
class TryIt
{
private static void testTempVars()
{
//creating variable var and
initialize to 10
int var=10;
//creating Array
int [] myArray = {3, 4, 5};
System.out.println("C. Original
values");
//printing array
for(int i=0;i<3;i++)
{
System.out.println(myArray[i]);
}
//calling arrayPassing() and pass
myArray
arrayPassing(myArray);
//original Array
System.out.println("\nE. values of
original array after arrayPassing executes");
//printing array
for(int i=0;i<3;i++)
{
System.out.println(myArray[i]);
}
System.out.println("\nF. original
value of var variable="+var);
//call primitivePassing() function
and pass var variable
primitivePassing(var);
//print original value of
variable
System.out.println("\nH. values of
original variable after primitivePassing executes="+var);
}
static void arrayPassing(int []theArray)
{
//add 1 in each element of
array
for(int
i=0;i<theArray.length;i++)
{
theArray[i]=theArray[i]+1;
}
System.out.println("\n"+"D.
arrayPassing values");
//printing array
for(int i=0;i<3;i++)
{
System.out.println(theArray[i]);
}
}
//for substract 5 from the variable
static void primitivePassing(int var)
{
var=var-5;
System.out.println("\nG.
primitivePassing value="+var);
}
public static void main(String[] args) {
//create string using direct
assignments
String str1="South Africa";
String str2="America";
String str3="South Africa";
//compare strings with '=='
System.out.println("A.1 Testing
strings "+str1+" and "+str2+" by == :");
System.out.println(str1==str2);
System.out.println(" Testing
strings "+str1+" and "+str3+" by == :");
System.out.println(str1==str3);
//compare strings with 'equals()'
method
System.out.println("A.2 Testing
strings "+str1+" and "+str2+" by equals :");
System.out.println(str1.equals(str2));
System.out.println(" Testing
strings "+str1+" and "+str3+" by equals :");
System.out.println(str1.equals(str3)+"\n");
//create strings using string
constructor
String var1 = new String("South
Africa");
String var2 = new
String("America");
String var3 = new String("South
Africa");
//compare strings with '=='
System.out.println("B.1 Testing
strings "+var1+" and "+var2+" by == :");
System.out.println(var1==var2);
System.out.println(" Testing
strings "+var1+" and "+var3+" by == :");
System.out.println(var1==var3);
//compare strings with 'equals()'
method
System.out.println("B.2 Testing
strings "+var1+" and "+var2+" by equals :");
System.out.println(var1.equals(var2));
System.out.println(" Testing
strings "+var1+" and "+var3+" by equals :");
System.out.println(var1.equals(var3)+"\n");
//calling testTempVars()
function
testTempVars();
}
}
Output: