Question

In: Computer Science

Language: Java Create a TryIt.java program with a main method. You are going to use this...

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”.

Solutions

Expert Solution

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:


Related Solutions

Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Use Visual Basic Language In this assignment you will need to create a program that will...
Use Visual Basic Language In this assignment you will need to create a program that will have both a “for statement” and an “if statement”. Your program will read 2 numbers from the input screen and it will determine which is the larger of the 2 numbers. It will do this 10 times. It will also keep track of both the largest and smallest numbers throughout the entire 10 times through the loop. An example of the program would be...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
Java Language Add a recursive method to the program shown in the previous section that states...
Java Language Add a recursive method to the program shown in the previous section that states how many nodes does the stack have. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT