d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Question About Array Comparison
12Next
Add Reply New Topic New Poll
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Nov 6 2014 05:53pm
Ok so we have to make a class that can be called by the main method class to compare various items. I have all my methods working fine except this one example that was given to us and I'm having a hard time understanding the logic behind it. Below I'll post the class that we had to make and all the methods in it and then I'll post the main method with the example problem that I'm having trouble with.

The class that I created:
Code
import java.util.Arrays;

public class Stat {

private double[] data;


public Stat(double[] data)
{
this.data = new double[data.length];
for(int i=0; i<data.length; i++)
{
this.data[i] = data[i];
}
}

public Stat()
{
data = new double[] {0.0};
}

public double[] getData()
{
return data;
}

public void setData(double[] d)
{
data = d;
}

public boolean equals(Stat s)
{
return Arrays.toString(data).equals(s.toString());
}

public String toString()
{
return Arrays.toString(data);
}

public double min()
{
double min=data[0];
for(int i=0; i < data.length; i++)
{
if(data[i] < min)
min = data[i];
}
return min;
}

public double max()
{
double max = data[0];
for(int i=0; i < data.length; i++)
{
if(data[i] > max)
max = data[i];
}
return max;
}

public double average()
{
double sum=0;
for(int i=0; i <data.length; i++)
{
sum += data[i];
}
return sum / data.length;
}

public double mode()
{
double maxValue;
double[] b = new double[data.length];
maxValue = b[0];
for(int i=0; i<data.length; i++)
{
int count=0;
for(int j=0; j<data.length; j++)
{
if (data[j] == data[i])
{
count++;
}
b[i] = count;
}
}

if(data.length > 1)
{
for(int k=0; k < data.length; k++)
{
if(b[k] > maxValue)
{
maxValue = data[(int) b[k]];
}
else if(b[k] == maxValue)
{
maxValue = Double.NaN;
}
}
}
else
maxValue = data[0];

return maxValue;
}
}


The Main method class with example:
Code
public class Example_Main {

public static void main(String[] args) {
double[] data1 = {10.0, 20.0, 30.0};
Stat stat1 = new Stat();
stat1.setData(data1);
Stat stat2 = new Stat(data1);
double[] data2 = stat1.getData();

System.out.println("The arrays are identical: " + (data1 == data2));
System.out.println("stat2 equals stat1: " + stat2.equals(stat1));
System.out.println("stat1 equals stat2: " + stat1.equals(stat2));
}
}


So the output according to the example is suppose to be false, true, true. I keep getting all true. For the first system print line I guess it's suppose to be false because it should be comparing memory location and not actual values, is that correct? If so, then what am I doing wrong in my code? Thanks!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2014 06:02pm
first, have you read the documentation for Arrays.toString(double[])? if not, do so now.
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(double[])

Next, stat1 and stat2 have the same data.

Code
stat1.setData(data1);
Stat stat2 = new Stat(data1);


what do you expect to see when Arrays.toString is called on two double arrays that have the same data?

run this code:
double[] array1 = {10.0, 20.0, 30.0};
double[] array2 = {10.0, 20.0, 30.0};

System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));

what do you see?

if you're confused about what String's equals(...) method does, here's the documentation:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)

At what point are you confused?

This post was edited by carteblanche on Nov 6 2014 06:05pm
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Nov 6 2014 06:08pm
My confusion lies with this part of the example code:

Code
System.out.println("The arrays are identical: " + (data1 == data2));


No methods are being called and the answer should return false according to the key, but instead I get true. This part of the code isn't calling any of my methods so that's why I'm confused about it. I'm fine with the last two lines of the code because they should be true true. The first part is what's confusing me. The only reason why I think it should be false is because it should be comparing the memory location of the arrays but if that's true then I not sure what's wrong with my code because I don't call any methods in that line of code.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2014 06:13pm
Quote (ice060788 @ Nov 6 2014 07:08pm)
My confusion lies with this part of the example code:

Code
System.out.println("The arrays are identical: " + (data1 == data2));


No methods are being called and the answer should return false according to the key, but instead I get true. This part of the code isn't calling any of my methods so that's why I'm confused about it. I'm fine with the last two lines of the code because they should be true true. The first part is what's confusing me. The only reason why I think it should be false is because it should be comparing the memory location of the arrays but if that's true then I not sure what's wrong with my code because I don't call any methods in that line of code.


oh, my mistake. i thought you were confused about the bottom two.

ok.

for clarity, these are the two lines of code we're interested in:
Code
stat1.setData(data1);
double[] data2 = stat1.getData();


and here's what setData does:
Code
public void setData(double[] d)
{
data = d;
}
public double[] getData()
{
return data;
}


So you're essentially doing this:

Code
double[] data = data1; // from setData
double[] data2 = data; // from getData


if we ignore the middle man, you're doing this:
Code
double[] data2 = data1;

Can you see that data1 and data2 are the exact same object?

This post was edited by carteblanche on Nov 6 2014 06:16pm
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Nov 6 2014 06:16pm
That's what I thought, that they are both the same but the answer that we should get is false for that line of code stating that they are not the same, that's why I'm confused by that.

Here is the link to the examples that we are suppose to do. It's number 4 at the bottom, if you wanted to see what I'm referring to.
http://cobweb.cs.uga.edu/~cs1301/files/Lab10.pdf

This post was edited by ice060788 on Nov 6 2014 06:18pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2014 06:21pm
Quote (ice060788 @ Nov 6 2014 07:16pm)
That's what I thought, that they are both the same but the answer that we should get is false for that line of code stating that they are not the same, that's why I'm confused by that.

Here is the link to the examples that we are suppose to do. It's number 4 at the bottom, if you wanted to see what I'm referring to.
http://cobweb.cs.uga.edu/~cs1301/files/Lab10.pdf


i have no interest in reading your assignment. i'm just explaining what your code does. if there's a problem interpreting the assignment or if the prof screwed up the assignment, that's between you and the prof/ta. im not gonna let an assignment get in the way of learning programming.

one last point of advice:

Code
Stat stat2 = new Stat(data1);
double[] data2 = stat1.getData();


that code looks super fishy to me. i suspect what you really wanted to do was call stat2.getData() instead of stat1.getData()

This post was edited by carteblanche on Nov 6 2014 06:25pm
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Nov 6 2014 06:25pm
I just thought that it might be a typo or something because we aren't suppose to modify the example main methods but use them to verify our code. So I'm guessing there was possibly a typo with the answer key they supplied to us because I don't see how the first line of code can be false with how it is written
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 6 2014 06:51pm
I am going to attempt to provide a very overly-simplified table description of what the JVM is doing with each variable for each line of code you have to illustrate why you are getting true when you do your reference equality on data1 and data2

Couple notes: I understand that instance variable values are normally stored on the heap. There are times where the JVM will choose to store them on the stack, so for the sake of simplicity I have represented the array length as a value stored on the stack. I also didnt go into what the JVM stores for an objects value on the heap.

Just follow the allocations. @X = Address on Heap.

Code
double[] data1 = {10.0, 20.0, 30.0};

Stack:
Name Type Value
data1 double[] @0
data1.length int 3

Heap:
Address Value
@0 10.0
@1 20.0
@2 30.0

Stat stat1 = new Stat();

Stack:
Name Type Value
data1 double[] @0
data1.length int 3
stat1 Stat @3
stat1.data double[] @4
stat1.data.length int 1



Heap:
Address Value
@0 10.0
@1 20.0
@2 30.0
@3 ...
@4 0.0

stat1.setData(data1);

Stack:
Name Type Value
data1 double[] @0
data1.length int 3
stat1 Stat @3
stat1.data double[] @0
stat1.data.length int 3

Heap:
Address Value
@0 10.0
@1 20.0
@2 30.0
@3 ...
@4 0.0

Stat stat2 = new Stat(data1);

Stack:
Name Type Value
data1 double[] @0
data1.length int 3
stat1 Stat @3
stat1.data double[] @0
stat1.data.length int 3
stat2 Stat @5
stat2.data double[] @6
stat2.data.length int 3

Heap:
Address Value
@0 10.0
@1 20.0
@2 30.0
@3 ...
@4 0.0
@5 ...
@6 10.0
@7 20.0
@8 30.0

double[] data2 = stat1.getData();

Stack:
Name Type Value
data1 double[] @0
data1.length int 3
stat1 Stat @3
stat1.data double[] @0
stat1.data.length int 3
stat2 Stat @5
stat2.data double[] @6
stat2.data.length int 3
data2 double[] @0
data2.length int 3

Heap:
Address Value
@0 10.0
@1 20.0
@2 30.0
@3 ...
@4 0.0
@5 ...
@6 10.0
@7 20.0
@8 30.0

System.out.println("The arrays are identical: " + (data1 == data2));

data1 = @0
data2 = @0
true
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Nov 6 2014 07:01pm
Wow very informative! This actually helped me a lot. Thanks Minkomonster! So you also agree that the answer should be true and not false like the answer key provided?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 6 2014 07:04pm
Quote (ice060788 @ Nov 6 2014 08:01pm)
Wow very informative! This actually helped me a lot. Thanks Minkomonster! So you also agree that the answer should be true and not false like the answer key provided?


Absolutely.

However, I feel this line is wrong:

double[] data2 = stat1.getData();

I feel like that should be

double[] data2 = stat2.getData();

Which would make more sense, and give false for data1=data2 while true for the others. It also better shows what I feel the assignment is aiming for.

This post was edited by Minkomonster on Nov 6 2014 07:06pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll