Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

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?
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

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.
Avatar of techques
techques

ASKER

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?


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.
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?
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))...
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


>>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;
}
If I need to sum 2 (or more) objects' age values and sort the sum values, do I use hashmap better then class Person?

 

Still the objects will work..
something like below..

sum = objecta.getAge()+objectb.getAge();
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

>>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..)
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

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
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

>> 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"));                        
}
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
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.  
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);
Since public void initializememberdata(int ID) is void type, it has compile error:

cannot convert from void to Member

ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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];
}
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..
Thanks for help and guideline, I have to add more points to this question.
Excellent guideline and detail illustration with example.