Create a class named ArrayMethods. No attributes. No Constructor. Implement the
ID: 3810463 • Letter: C
Question
Create a class named ArrayMethods. No attributes. No Constructor.
Implement the following methods which all take an integer array named list as a parameter:
13. Implement a method that will accept a String parameter and return the number of pairs (2 identical adjacent
letters) that are in the string. The string “balloon” has 2 pair. The string “abcccde” has only 1 pair. The string
“abcdddde” has 2 pair.
2.
Implement a method named totals that takes a two dimensional integer array named array as a parameter
and returns a one dimensional array where each element is the sum of the columns of the input array.
10.Given the following attributes of the class named Geometric, implement the equals method that accepts a Geometric parameter named geo and returns true if the two objects are identical. They are identical if each attribute is the same in both objects.
public class Geometric{
int side;
char top;
String name;
public boolean equals ( )
{
}
}
Explanation / Answer
HI, I have implemented first 2 questions.
Please repost others in separate post.
Please let me know in case fo any issue in first 2.
public class ArrayMethods {
public static int countIdenticalPair(String str){
if(str == null || str.trim().length() <= 1)
return 0;
int count = 0;
for(int i=0; i<str.length()-1; i+=2)
if(str.charAt(i) == str.charAt(i+1))
count++;
return count;
}
public static int[] columnSum(int[][] arr){
int colSumArr[] = new int[arr[0].length];
int k = 0;
for(int i=0; i<arr[0].length; i++){
int sum = 0;
for(int j=0; j<arr.length; j++)
sum = sum + arr[j][i];
colSumArr[k++] = sum;
}
return colSumArr;
}
public static void main(String[] args) {
System.out.println(countIdenticalPair("balloon"));
System.out.println(countIdenticalPair("abcccde"));
int [][] arr = {
{1,2,3},
{2,3,4}
};
int colSum[] = columnSum(arr);
System.out.println(Arrays.toString(colSum));
}
}
/*
Sample run:
2
1
[3, 5, 7]
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.