home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1846
Discussions: 188,398, Posts: 2,243,595, Members: 232,629
Old January 20th, 2002, 10:26 PM   Digg it!   #1 (permalink)
Ultimate Member
 
Tekk's Avatar
 
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.

Tekk is offline   Reply With Quote
Old January 20th, 2002, 10:59 PM     #2 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
Which language?

strangerstill is offline   Reply With Quote
Old January 21st, 2002, 12:04 AM     #3 (permalink)
Ultimate Member
 
Tekk's Avatar
 
Join Date: Oct 2001
Location: Pasadena, CA
Posts: 2,152
hehe..silly me....its Java

Tekk is offline   Reply With Quote
Old January 21st, 2002, 11:16 AM     #4 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
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;
		}
	}
}
?
strangerstill is offline   Reply With Quote
Old January 21st, 2002, 05:27 PM     #5 (permalink)
Ultimate Member
 
Tekk's Avatar
 
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
Tekk is offline   Reply With Quote
Old January 21st, 2002, 05:45 PM     #6 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
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?
strangerstill is offline   Reply With Quote
Old January 21st, 2002, 06:07 PM     #7 (permalink)
Ultimate Member
 
Tekk's Avatar
 
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.
Tekk is offline   Reply With Quote
Old January 21st, 2002, 06:57 PM     #8 (permalink)
Banned
 
qball's Avatar
 
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();
qball is offline   Reply With Quote
Old January 21st, 2002, 07:59 PM     #9 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
aha, starting to understand.
in the constructor just put
Code:
v = new Vector(5);
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...
strangerstill is offline   Reply With Quote
Old January 21st, 2002, 08:01 PM     #10 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
oh and to 'figure out the syntax for the constructor'.. there's one place to go for java reference and that's http://java.sun.com/j2se/1.4/docs/api/index.html
strangerstill is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2905)
Unarmed man on his stomach shot by .. (6)
Misery Loves Company... (2144)
New Build ( Finally ) (6)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (444)
Upgrading RAM (6)
Recent Discussions
Partition Magic 7.0 (Unallocate.. (17)
For cheap price and good qualit.. (1)
Left 4 Dead Small Freezes (3)
RCA 52Inch HDTV wont turn on (4)
wishin i could edit my aol prof.. (0)
Sporadic internet connectivity (2)
Assassins Creed PC Problems (40)
I think my PSU is dieing (1)
building a gaming computer, inp.. (0)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 03:33 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28