Question

In: Computer Science

(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...

(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true.

Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element is populated incrementally beginning at 1 and ending at the size (inclusive). Example: if the parameter was 5, your method should return an int[] array containing values: {1,2,3,4,5}. Example 2: if the parameter was 9 your method should return an int[] array containing values: {1,2,3,4,5,6,7,8,9}. Hint - try to populate the array with 0 - size-1 first and then adjust :) *You may assume they will enter a positive number.

Main.java exists to test your code - write the methods described below in Assignment10.

public class Main
{
public static void main(String[] args) {

//You can test your code here.

//Sample testing code:

Assignment10 q = new Assignment10(); //creates an object of your class



/* //TEST PROBLEM 1 - Uncomment to use


  
String[] a = {"dog", "cat"};
String[] b = {"dog", "cat", "banana"};
  
System.out.println(q.compare(a,b)); //should print false - a & b do not contain same elements and are not the same length
  

*/


//Change values around to ensure your code works properly
  
  
/* //TEST PROBLEM 2 - Uncomment to use
  
  
//Continues using object q created in line 9. ***Testing code requires your additions.
  
int size = 3; //change number to thoroughly test.
  
int[] returnedArray = q.generateArray(size); //returns an array - stored in an array.
  
//TODO: write a simple for loop to print out each element of the array to test
  
//YOUR CODE HERE TO FINISH TESTING (TODO instructions above!).
  
//Note: System.out.println(returnedArray); should not be used - prints a memory address - why?


*/




   }
}

Solutions

Expert Solution

Code

import java.util.*;

  class Assignment10
 {
   public boolean compare(String a[],String b[])
   {
     if(a.length!=b.length) //if different length return false
     {
       return false;
     }
     for(int i=0;i<a.length;i++)
     {
       if(!a[i].equals(b[i])) //if any element mismatched return false
         return false;
     }
     return true; //return true if all equals
   }
   public int[] generateArray(int n)
   {
     int a[]=new int[n];
     for(int i=0;i<n;i++)
     {
       a[i]=i+1; //store in the array
     }
     return a;
   }
 }

 public class Main
 {
   public static void main(String args[])
   { 
     Assignment10 q = new Assignment10();
     String[] a = {"dog", "cat"};
     String[] b = {"dog", "cat", "banana"};
     System.out.println(q.compare(a,b));
     int size = 3; 
     int[] returnedArray = q.generateArray(size);
     for(int i=0;i<size;i++)
     {
       System.out.print(returnedArray[i]+" ");
     }
   }
 }

Terminal Work

.


Related Solutions

Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Create a method named stringOperations () that accepts a string variable. This method must perform the...
Create a method named stringOperations () that accepts a string variable. This method must perform the following operations:                Split the sentence into an array                Display each element in the array on a seprate line using “ForEach”                Print the first element in the array, using the array syntax                Tell the user where the second word starts                Display the first 3 characters of the second word In your man method, ask the user to enter a sentence...
Given two String arrays (named below), write the code that you would need in order to...
Given two String arrays (named below), write the code that you would need in order to copy the data from relatives into weddingInvitations in java. 1. relatives (which is loaded with data) 2. weddingInvitations (that is the same size as relatives)
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT