• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

How to calculate a vector value and compare?

Hi
There is a vector c, in vector c, it has 10 parameters, all of them have different values.
e.e. M=1*2, N=4*2, O=6*10, P=5*12, Q=9*3...etc

How can calculate the vector c value?
Does it just 1*2 + 4*2 + 6*10 + 5*12 + 9*3?

If there are 2 vectors: b and c

then how to compare them? compare M(b) and M(c), N(b) and N(c)....
or compare vectors b and c after their values are summarized?
0
techques
Asked:
techques
  • 13
  • 11
1 Solution
 
KuldeepchaturvediCommented:
in Java Vectors are collection of objects.. so given that a vector no not calculated just adding those objects or anything like that..
It really depends what you want to accomplish and if vector are really right implementation for you.
0
 
techquesAuthor Commented:
Dear Sir

The case is that:

I need to compare the parameters values of 2 vector objects.
vector ObjA - age=2*15, gender=3*1, job=1*20, sport=4*6, color=2*12
vector ObjB - age=2*8, gender=3*12, job=1*15, sport=4*13, color=2*7

1) How to set the age, gender, job, sport, color values to vector objects?
2) How to get them?
3) How to compare the age, gender, job, sport, color values of vector ObjA and ObjB?


0
 
KuldeepchaturvediCommented:
I do not think that a vector is a  very good fit to your above requirement.

you are much better off creating your own object and setting all these properties inside the object, work with your object from there..

A vector is collection of objects..
in your above example you are actually trying to store properties in a vector..

not a very good implementation
for example a Vector can store a String lets say..

Vector V = new Vector();
V.add("age");
so now it will store the string age.... how we will associate the age value ( i.e. 2*15) inside the same vector?
I hope the explanation is clear to you.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
techquesAuthor Commented:
Dear Sir

How can I use Hashtable or HashMap to compare the parameters values of 2 objects.
ObjA - age=2*15, gender=3*1, job=1*20, sport=4*6, color=2*12
ObjB - age=2*8, gender=3*12, job=1*15, sport=4*13, color=2*7

1) How to set the age, gender, job, sport, color values to objects?
2) How to get them?
3) How to compare the age, gender, job, sport, color values of ObjA and ObjB?
4) If I use Hashtable or HashMap, is it still need to create object and setting all these properties inside the object, work with object from there?
0
 
KuldeepchaturvediCommented:
Well the Hashmap code will look something like below..

Hashmap h1 = new Hashmap();

h1.add("age","2*15");
// here I am assuming that 2*15 is a string & not a calculation if they are numbers then you need to "" take out..
h1.add("gender","3*1");
h1.add("job","1*20");
//Add all properties similarly
..
..
Hashmap h2 = new Hashmap();
h2.add("age","2*8");
h2.add("gender","3*12");
// add all properties

now if you want to compare these values.. it shd be as simple as

if(h1.get("age").equals(h2.get("age")))

same logic will go for all other properties..

As I have said in other comment, it is much better to create an object of your own..

like
public class Person
{
String age,gender,job,sport,color="";

public Person( String a, String b, String c, String d, String e)
{
this.age =a;
this.gender=b;
this.job=c;
this.sport=d;
this.color=e;
}
//make all setters & getters as well

//
Make a method
boolean compare(Person a)
{
if(this.age .equals(a.age)&&this.gender.equals(a.gender)&&this.job.equals(a.job)&&this.sport.equals(a.sport)&&this.color.equals(a.color))
return true
else
return false;
}
}
}


Now you can use this object in following way
Person a = new Person("2.15","3.1"..rest of your values)
Person b = new Person("2.8",...rest of your values).

then
you can simply use
if(a.compare(b))...
0
 
techquesAuthor Commented:
Dear Sir

1) make all setters & getters as well
could you take age as example and write code of it?

2) If I build a class Person, how can I add it to Hashmap?
Hashmap h1 = new Hashmap();
h1.add("age","2*15");

Person a = new Person("2.15","3.1"..rest of your values)
a.setage(2,15);

Do you mean that if I use class Person, then I do not need to use Hashmap?

So, Vector is in fact a class like Person to set and get values?

Thanks for advice


0
 
KuldeepchaturvediCommented:
>>Do you mean that if I use class Person, then I do not need to use Hashmap?

>>So, Vector is in fact a class like Person to set and get values?

Correct on both statements, Basically in Java all the things are classes ( well almost all of them)..
Vector is a class already provided by java, & so is hashmap, String, Request.. etc etc etc..

& for getter & setters..( Again I have assumed you are dealing with strings, they can be of any other type as well

//Getter
public String getAge()
{
return this.age;
}
//Setter
public void setAge(String age)
{
this.age=age;
}
0
 
techquesAuthor Commented:
If I need to sum 2 (or more) objects' age values and sort the sum values, do I use hashmap better then class Person?

 

0
 
KuldeepchaturvediCommented:
Still the objects will work..
something like below..

sum = objecta.getAge()+objectb.getAge();
0
 
techquesAuthor Commented:
Hi

I created a class Member with all set and get values.

I suppose there are 8 members, I want to randomly select one of them.

Member randomValue = (Member)(Math.random() * 100); //connot convert from double to object
Member[] x, y;
x = new Member[8];
y = new Member[8];
for(int i = 0; i<8;i++) {
x[i] = genRandom(8);
y[i] = genRandom(8);
}
 
public Member genRandom(int Data) {
Member randomValue = (Member)(Math.random() * 100); //connot convert from double to object
return randomValue;
}

Open in new window

0
 
KuldeepchaturvediCommented:
>>I suppose there are 8 members, I want to randomly select one of them.

Member randomValue = (Member)(Math.random() * 100); //connot convert from double to object

How you can convert a number to your object?????

Math.random() will return A number.... say "5".. now how a "5" can be converted to your object??

you *MAY* want fifth object out of 8 ( in which case 5 was the index of your object array & not the object itself..!!)..


Also Are you initializing your objects anywhere?

In the code above, you just made an array of 8 objects and then trying to do this random thing..

All you will get is null objects... ( once you declared the objects, you need to create them and fill their values i.e. age, color etc etc etc..)
0
 
techquesAuthor Commented:
oh I see.

How about the following code?

Is it correct?
public Member genRandom(int Data) {
int randomMember = (int)Math.abs((Math.random() * Data));
Member[] M = new Member[Data];
return M[randomMember];
}

Open in new window

0
 
KuldeepchaturvediCommented:
that shd work, As long as your Members are initialized.. otherwise this code will work but later on it will throw a null pointer exception
0
 
techquesAuthor Commented:
I have problem at initializing the object.

The set sex, age needs to use sql to get from DB
My logic is that:
1. get how many member in table
2. new Member object
3. select their id
4. assign id to object
e.g. member object[0] has the member id data
5.  initialize object[i] by passing the id parameter to initializememberdata(int y) method;

But, it throws exception there.

I think I wrote the wrong code in Member class.

I add more points to this complicated question.
main.java
 
int d = getnumberofmember();  //e.g. d=10
Member[] x = new Member[d];  
MySQLConnector db1 = new MySQLConnector();
Connection con1 = db1.Getcon();
Statement s1 = con1.createStatement();
String sql1 = "select id from member";	
ResultSet rs1 = s1.executeQuery(sql1);
for(int i = 0; i<d; i++) {
if (rs1.next()) {
int y = rs1.getInt("id"); //member id, e.g. 5
x[i].initializememberdata(y); //THROW exception
x[i] = cent.genRandom(d);
}
rs1.close();
s1.close();
con1.close();                 
}
 
Member.java
 
public class Member{
private int ID;
private int sex;
private int age;  	 
	  
public void setid(int newValue)
{
ID=newValue;
}
public int getid()
{
return ID;
}
	  
public void setsex(int newValue)
{
sex=newValue;
}
public int getsex()
{
return sex;
}
	  	  
public void setage(int newValue)
{
age=newValue;
}
public int getage()
{
return age;
}
 
public void initializememberdata(int ID) throws MemberException
{
try {
MySQLConnector db = new MySQLConnector();
Connection con = db.Getcon();
Statement s = con.createStatement();
String sql = "select sex, age from member where id='" + ID + "'"; 
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
Member member = new Member(); //do i need it?
member.setid(myID);
member.setsex(rs.getInt("sex")); 		    	
member.setage(rs.getInt("age")); 		    	
}
rs.close();
s.close();
con.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
}   
 
}

Open in new window

0
 
KuldeepchaturvediCommented:
>> this needs to be rewritten
if (rs.next()) {
Member member = new Member(); //do i need it?
member.setid(myID);
member.setsex(rs.getInt("sex"));                        
member.setage(rs.getInt("age"));                        
}

shd be

if (rs.next()) {
this.setid(myID);
this.setsex(rs.getInt("sex"));                        
this.setage(rs.getInt("age"));                        
}
0
 
techquesAuthor Commented:
Dear Sir

It still throws nullpoinerexception at line x[i].initializememberdata(y); but i already new Member object and y can get id value. What's wrong with my code?

Member[] x = new Member[d];  
for(int i = 0; i<d; i++) {
if (rs1.next()) {
int y = rs1.getInt("id"); //can get member id
x[i].initializememberdata(y);  //throws nullpointerexception
0
 
techquesAuthor Commented:
I test the code like this:

Member[] x = new Member[d];  
x[0].initializememberdata(10);

It does not step into for loop, but it also throws nullpointerexception.  
0
 
KuldeepchaturvediCommented:
Member[] x = new Member[d];  
x[0].initializememberdata(10);
needs to be

x[0] = new Member().initializememberdata(10);

or

Member[] x = new Member[d];  
for(int i = 0; i<d; i++) {
if (rs1.next()) {
int y = rs1.getInt("id"); //can get member id
x[i]=new Member().initializememberdata(y);
0
 
techquesAuthor Commented:
Since public void initializememberdata(int ID) is void type, it has compile error:

cannot convert from void to Member

0
 
KuldeepchaturvediCommented:
oops missed that
x[0] = new Member();
x[0].initializememberdata(10);

or

Member[] x = new Member[d];  
for(int i = 0; i<d; i++) {
if (rs1.next()) {
int y = rs1.getInt("id"); //can get member id
x[i]=new Member();
x[i].initializememberdata(y);
0
 
techquesAuthor Commented:
It can initialize the member object now. However, when it get random member object from member, it became null again. How can I assign random initialized member to x[i] ?  

x[i]=new Member();
x[i].initializememberdata(y);
x[i] = cent.genRandom(d);
public Member genRandom(int Data) {
int randomMember = (int)Math.abs((Math.random() * Data));
Member[] M = new Member[Data];
return M[randomMember];
}
0
 
KuldeepchaturvediCommented:
public Member genRandom(int Data) {
int randomMember = (int)Math.abs((Math.random() * Data));
Member[] M = new Member[Data];
return M[randomMember];
}

needs to be

public Member genRandom(int Data) {
int randomMember = (int)Math.abs((Math.random() * Data));
Member[randomMember] M = new Member();
return M[randomMember];
}

that shd do it..
as I said again.. if not all the members are initialized.. your random number piece wont work right..
0
 
techquesAuthor Commented:
Thanks for help and guideline, I have to add more points to this question.
0
 
techquesAuthor Commented:
Excellent guideline and detail illustration with example.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now