January 20th, 2002, 10:26 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
| Adding the contents of a vector....
If you have a vector and its members are 10,4,4,2 how would you make it so a method getSum() would add them. |
| |
January 20th, 2002, 10:59 PM
|
#2 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
| |
| |
January 21st, 2002, 12:04 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
|
hehe..silly me....its Java |
| |
January 21st, 2002, 11:16 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
hmm, thought it probably was, didn't want to end up with a totally unrelated answer
so, anyway, something like Code: public class MathVector extends Vector {
double getSum() {
if isEmpty()
return 0;
else {
double sum = 0;
for (int i = 0; i < size(); i++)
sum += get(i);
return sum;
}
}
} ? |
| |
January 21st, 2002, 05:27 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
|
Cool  Thanks....but now Im having problems figure out how to setup the constructor for my class. Here's what I have basically.... Code: import java.io.*;
import java.util.*;
/////////////////////////////////////////////////
class Operator
{
private Vector v;
//----------------------------------------------
public Operator()
{
}
//----------------------------------------------
public static void testOperator()
{
v.addElement(new Integer(10));
v.addElement(new Integer(4));
v.addElement(new Integer(0));
v.addElement(new Integer(4));
v.addElement(new Integer(2));
System.out.println("The min value is " + getMin());
System.out.println("The max value is " + getMax());
System.out.println("The sum is " + getSum());
printPercentageOfSum();
System.out.println("The sum of the square is " + getSumOfTheSquares());
System.out.println("The standard deviation is " + getStandardDeviation());
}
//----------------------------------------------
} // End of Operator
////////////////////////////////////////////////
class Hw03
{
//----------------------------------------------
public static void main(String[] args)
{
Operator.testOperator();
}
//------------------------------------------------
} // End of Hw03 I only put in the testOperator method and constructor for the Operator class. What happens is that I am forced to put static in all the methods because testOperator is using non-static methods. I know its because of my constructor setup but I've fiddled and fiddled and can't figure out how I would set it up. Any ideas??? Try to point me in the right direction..hehe..thanks |
| |
January 21st, 2002, 05:45 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Hmm...so getMin() etc. are methods on the Operator object? Well, those need to be non-static because they reference the Vector which has to be non-static...
so testOperator should be non-static as well because it calls non-static methods on its parent Operator object.
(I think - I'm not too good at java)
Does this make any sense? |
| |
January 21st, 2002, 06:07 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
|
Well whats holding me up is that I need to figure out how to create a new Vector in the constructor. See...v has never really been created yet I call it in v.addElement(....) and that is why Im getting the null pointer exception error. I need to figure out the syntax for the constructor so that I can create v in the testOperator() method. But I can't figure out the syntax for it.
Im used just doing things like Code: Name a = new Name("firstName", "lastName"); That calls the constructor of the name class and creates a new name "a". But I need to do basically the same type of concept for this assignment but its the damn syntax I need. How can I create a new vector V using the constructor?
Also, the testOperator() method needs to be static because it is being called from the Hw03 class. Without the static it would not be present there. |
| |
January 21st, 2002, 06:57 PM
|
#8 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
|
Try:
v = new Vector();
But that t'ain't gonna help you. Constructors are invoked when a class is instantiated, not when a class is loaded. A class is 'loaded' when a static class method is invoked.
In fact you cannot access your:
private Vector v;
in any static methods as v is an instance variable. The compiler should pick this up...
Without fully understanding class and instance vars and methods, keep everything static, thus:
static Vector v = new Vector(); |
| |
January 21st, 2002, 07:59 PM
|
#9 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
aha, starting to understand.
in the constructor just put that will create a Vector with an initial capacity of 5,
then keep everything as non-static, so you need to instantiate your Operator: Code: public static void main(String[] args)
{
Operator o = new Operator()
o.testOperator();
} should have spotted that... |
| |
January 21st, 2002, 08:01 PM
|
#10 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
| |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |