Implement a method named totals that takes a two dimensional integer array named
ID: 3812428 • Letter: I
Question
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
Java Program :
public class Sample1 {
public static int[] total(int arr[][]) {
int ans[] = new int[arr[0].length];
for (int i = 0; i < arr[0].length; i++) {
for (int j = 0; j < arr.length; j++) {
ans[i] = ans[i] + arr[j][i];
}
}
return ans;
}
public static void main(String[] args) {
int arr[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
System.out.println(Arrays.toString(total(arr)));
}
}
OUTPUT :
[6, 8, 10, 12]
Java Program :
import java.io.*;
import java.util.*;
class Geometric {
int side;
char top;
String name;
public Geometric(int side, char top, String name) {
this.side = side;
this.top = top;
this.name = name;
}
public boolean equals(Geometric geo) {
if((this.side==geo.side)&&(this.top==geo.top)&&(this.name.equals(geo.name))){
return true;
}
return false;
}
}
public class Sample1 {
public static void main(String[] args) {
Geometric g1 = new Geometric(5,'A',"circle");
Geometric g2 = new Geometric(5,'A',"circle");
System.out.println(g1.equals(g2));
Geometric g3 = new Geometric(3,'A',"circle");
System.out.println(g1.equals(g3));
}
}
OUTPUT :
true
false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.