0

To be upfront and honest, this IS part of a homework assignment. I am NOT looking to cheat at all. On the contrary, I have completed about 60% of the assignment and now i'm stuck because I do not understand what is being asked of me in the specification for one of the methods I have to write/use.

Background: The assignment involves writing a program with 2 classes, one is main and the other is called VectorADT. The VectorADT class (to be brief) is just supposed to have two instance variable arrays (which are 'vectors' within the scope of this assignment) as well as some instance methods for manipulating both arrays (a few static methods exist as well).

My issue: One method I have to write is supposed to add up both vectors (arrays in this case) by adding the corresponding slots of the arrays. Both arrays are assumed to be of the same size! I managed to go all of this, and afterwards I am asked to return a VectorADT containing the sum of the two given VectorADT parameters (v1 + v2). What is meant by return a VectorADT? Isn't that the name of the class? And in this case, the type of the objects I passed into this add method? I physically do not understand what my return statement is supposed to be in the add method, and what I am supposed to assign the return to (in my main method).

The specification for the method: public static VectorADT add(VectorADT v1, VectorADT v2) Generates and returns the sum of two given VectorADTs. Note that vector addition is defined by adding the corresponding elements of each vector to get the corresponding element of the sum vector.

Parameters: v1 - the first VectorADT v2 - the second VectorADT

Preconditions: The VectorADT objects referred to by v1 and v2 have been instantiated and both have the same size.

Returns: A VectorADT containing the sum of the two given VectorADT parameters (v1 + v2).

Throws: IllegalArgument- Indicates that either v1 or v2 is null. InvalidSizeException- Indicates that v1 and v2 are of different sizes.

Code I have written:

class VectorOperations
{
    public static void main(String[] args)
    {
        //blue and yellow are used as an example here.
        int [] blue = new int [12]; 
        int [] yellow = new int [12];

        //initializes vector array using constructor
        VectorADT one = new VectorADT(blue);  

        //initializes vector array using constructor
        VectorADT two = new VectorADT(yello);

        //what am i supposed assign my return to?
        something????? = VectorADT.add(one, two);
    }
}

public class VectorADT
{
    private int [] vector;
    public VectorADT(int [] intArray)
    {
    //constructor that initializes instance variable vector.
    //vector ends up being the same size as the array in the 
    //constructors parameter. All slots initialized to zero.
    }
    public static VectorADT add(VectorADT one, VectorADT two) 
    {               //I used one and two instead of v1 and v2

       //some if statements and try-catch blocks for exceptions i need
       //if no exceptions thrown...
       int [] sum = new int [one.vector.length];  //one and two are same length
       for(int i = 0; i < one.vector.length; i++)
       {
           sum[i] = one.vector[i] + two.vector[i];
       }
       return //Totally confused here  :(
    }

    //other methods similar to VectorADT add() also exist...
}

Any help or guidance would be VERY appreciated. Thank you

1
  • return new VectorADT(sum) There you go, that's what the instructions say :) Jul 14, 2013 at 19:30

5 Answers 5

1

The disconnect in understanding seems to be from the fact that methods do not return classes, per se, but they can return an instance of a class (an object).

public static VectorADT add(VectorADT one, VectorADT two) 

This method signature says that this method accepts two objects of type VectorADT, and returns some object of also type VectorADT. Presumably, this object returned will be some new object that you create at some point in the method for the purpose of returning it to whatever calls your method.

VectorADT myVectorADT = new myVectorADT(..) //define a new object
//Do some stuff to make myVectorADT have the proper values, for example summing vector one and two's elements
return myVectorADT;  //return it!

First make sure to check that each provided object is not null

if (one == null || two == null) //throw something

Then simply, in your method, create a new vector, populating it with the elements of vector 1 + vector 2, then return it as shown above.

Here's an example that might help, it's self contained

class A {
    public int value;
    public A(int k) {  //Constructor
        value = k;
    }
}

Now let's defined a method like yours above

public static A addMethod (A one, A two) { //Method returns class type A, accepts two variables of type A
    A returnObject = new A(one.value + two.value);
    return returnObject;
}

This is a simple example, but honestly it's pretty much the same as what you have. The above method can now be called like follows:

A firstObject = new A(5);
A secondObject = new A(3);
A newObject = addMethod(firstObject, secondObject);

System.out.println(newObject.value); //prints 8
1
  • Your response helped too. Thanks :)
    – Mark
    Jul 14, 2013 at 20:57
0

You should return a new VectorADT object.

When you're asked to return a VectorADT containing the sum of the two given VectorADT parameters (v1 + v2), you're actually being asked to return an object of the VectorADT class, a new instance, not the class itself.

In your case, you would make the add method like this:

public static VectorADT add(VectorADT one, VectorADT two) 
{
   int [] sum = new int [one.vector.length];
   for(int i = 0; i < one.vector.length; i++)
   {
       sum[i] = one.vector[i] + two.vector[i];
   }
   return new VectorADT(sum);
}

and at the main method:

VectorADT sum = VectorADT.add(one, two);

where "sum" is the name you want for the variable holding the VectorADT object.

When you put new VectorADT(sum), you're calling the constructor, namely, public VectorADT(int [] intArray). As you can see, it receives an int[] and, as you said, put it in the instance variable vector. You're passing to it the int[] sum you made to receive the sum of both vectors (sum[i] = one.vector[i] + two.vector[i];)

As you put that as the return of the method, it's returning that new VectorADT object you just made with the sum of both parameters of the method wherever you call it. So, as you're calling it at main, and the method will return a new VectorADT, you put VectorADT as the type of the variable you declared (VectorADT sum = VectorADT.add(one, two);).

Hope it's clear now.

2
  • Yes, i understand now. Still trying to knock this object oriented programing aspect of java into my brain. Thanks for your help.
    – Mark
    Jul 14, 2013 at 20:56
  • Hahaha, no prob. If you're not familiar with object oriented programming, it might take some time until you get used to the concept. But you will get it eventually, believe me. And good luck.
    – Doodad
    Jul 14, 2013 at 21:00
0

The point is that you need to build a new instance of VectorADT.

Create it using:

VectorATD vec=new VectorADT(sum);

You can now call methods upon it to modify it if needed.

Then,

return vec;
0

It seems that you're supposed to return a new instance of VectorADT whose vector is the sum array you've computed.

It's simply return new VectorADT(sum);

This is a common operation in vector math. The sum of two vectors is a third vector whose components are the sum of the two corresponding components from the addend vectors.

0
public class AddVector {
     public static void main(String[] args)
        {
            //blue and yellow are used as an example here.
            int [] blue = {1,4,3,2,}; 
            int [] yellow = {3,5,6,4};
            //initializes vector array using constructor
            VectorADT one = new VectorADT(blue);  
            //initializes vector array using constructor
            VectorADT two = new VectorADT(yellow);
            //what am i supposed assign my return to?
            int sum = VectorADT.add(one, two);
            System.out.println("Sum of vector is : "+sum);
        }    
}

class VectorADT
{
    private int [] vector;
    public VectorADT(int [] intArray)
    {
        vector = intArray;
    }
    public static int add(VectorADT one, VectorADT two) 
    {               
       int total = 0;
       if(one.vector.length == two.vector.length){
           for(int i = 0; i < one.vector.length; i++)
           {
               total += one.vector[i] + two.vector[i];
           }
           return total;
       }else{
           return 0;
       }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.