?
Solved

altering code to read a string, distance calculations

Posted on 2003-02-21
16
Medium Priority
?
190 Views
Last Modified: 2010-03-31
Hi,

i have code which calculates the distance between all possible sets of atom interactions, using x,y and z co-ordinates. This information is defined in the code. Howeve, i am having problems in expanding this so that this information is read directly from an input file
(same format), which can contain 100 atoms.

import java.util.StringTokenizer;

public class AtomDistanceTest {
public static void main(String[] args) {
new AtomDistanceTest();
}

public AtomDistanceTest() {
try {
Atom a = new Atom("ATOM 5 CA PHE 1 113.142 75.993 130.862", "A");
Atom b = new Atom("ATOM 25 CA ARG 2 113.940 72.288 131.419", "B");
Atom c = new Atom("ATOM 49 CA TYR 3 116.236 72.885 134.471", "C");

Atom.printDistance(a, b);
Atom.printDistance(a, c);
Atom.printDistance(b, c);
} catch (Exception ex) {
System.out.println (ex);
}
}

}

class Atom {
public double x;
public double y;
public double z;
public String name;

public Atom(String s, String name) throws IllegalArgumentException {
try {
this.name = name;
StringTokenizer t = new StringTokenizer (s, " ");
for (int j=0; j<5; j++) t.nextToken(); //skip first 5 tokens
this.x = new Double(t.nextToken()).doubleValue();
this.y = new Double(t.nextToken()).doubleValue();
this.z = new Double(t.nextToken()).doubleValue();
} catch (Exception ex) {
throw new IllegalArgumentException ("Illegal atom description");
}
}

public String toString() {
return "atom : " + name + "(x=" + x + " y=" + y + " z=" + z + ")";
}

public double distanceFrom (Atom other) {
return calculateDistance (x, y, z, other.x, other.y, other.z);
}

public static double calculateDistance (double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt(Math.sqrt(Math.pow(Math.abs(x1-x2),2)+Math.pow(Math.abs(y1-y2),2))+Math.pow(Math.abs(z1-z2),2));
}

public static void printDistance (Atom a, Atom b) {
System.out.println ("Distance between " + a.toString() + " and " + b.toString() + " is " + a.distanceFrom(b));
}
}

0
Comment
Question by:indie_campbell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +4
16 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 80 total points
ID: 7992634
 Use the following to open a file:

BufferedReader in = new BufferedReader(new FileReader("<the fiel you want to read>"));

  and the following to read the file line by line and create the Atom:

String line;
while ((line = in.readLine()) != null)
{
   Atom a = new Atom(line, <you read this data form file as well>);
}

  The rest should be more or less the same.
0
 

Author Comment

by:indie_campbell
ID: 7992699
i will give it a go
0
 

Author Comment

by:indie_campbell
ID: 7992723
i am little confused by the statement u have written Atom a....etc
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:indie_campbell
ID: 7992780
regarding that in the original code Atom a , which had defined co-ordinate information
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 7993521
Try this:-

public AtomDistanceTest() {
   try {
      LineNumberReader reader = new LineNumberReader(new FileReader("config.prop"));
      //read one line from this file.
      String line = reader.readLine();
      while(line!=null) {
          //create an atom object.
          Atom a = new Atom(line);
          line = readLine();
      }
   }catch(Exception ex) {
     System.out.println(ex);
   }
}
0
 

Author Comment

by:indie_campbell
ID: 7993959
i have tried this read in procedure, see below....having problems with the Atom a =new Atom(line)

import java.util.StringTokenizer;
import java.io.*;
import java.lang.*;

public class AtomDistanceTest1 {
     public static void main(String[] args) {
          new AtomDistanceTest1();
     }

     public AtomDistanceTest1() {
          try {
              LineNumberReader reader = new LineNumberReader(new FileReader("model2test.pdb"));
                    String line = reader.readLine();
                    while(line !=null) {
               Atom a = new Atom(line);
                        line = readLine();
          }} catch (Exception ex) {
               System.out.println (ex);
          }
     }

     }

class Atom {
     public double x;
     public double y;
     public double z;
     public String name;

     public Atom(String s, String name) throws IllegalArgumentException {
          try {
               this.name = name;
               StringTokenizer t = new StringTokenizer (s, " ");
               for (int j=0; j<5; j++) t.nextToken(); //skip first 5 tokens
               this.x = new Double(t.nextToken()).doubleValue();
               this.y = new Double(t.nextToken()).doubleValue();
               this.z = new Double(t.nextToken()).doubleValue();
          } catch (Exception ex) {
               throw new IllegalArgumentException ("Illegal atom description");
          }
     }

     public String toString() {
          return "atom : " + name + "(x=" + x + " y=" + y + " z=" + z + ")";
     }

     public double distanceFrom (Atom other) {
          return calculateDistance (x, y, z, other.x, other.y, other.z);
     }

     public static double calculateDistance (double x1, double y1, double z1, double x2, double y2, double z2) {
          return Math.sqrt(Math.sqrt(Math.pow(Math.abs(x1-x2),2)+Math.pow(Math.abs(y1-y2),2))+Math.pow(Math.abs(z1-z2),2));
     }

     public static void printDistance (Atom a, Atom b) {
          System.out.println ("Distance between " + a.toString() + " and " + b.toString() + " is " + a.distanceFrom(b));
     }
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7994533
You don't say what difficulty though!

1. There's no need to use the specialised LineNumberReader - you're not using its specialised functionality. Use BufferedReader instead.

2. You must close the reader when you've finished with it.

3. Create a separate AtomParser class that returns an Atom instance - it should not be the reponsibility of a class' constructor to parse its own arguments.

String line = null;
while ((line = in.readLine) != null) {
  Atom a = AtomParser.getAtom(line);
}
// Now close the reader
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 7997684
Since the constructor of Atom class takes two arguments, It should be :-

public AtomDistanceTest1() {
         try {
             LineNumberReader reader = new LineNumberReader(new FileReader("model2test.pdb"));
                   String line = reader.readLine();
                   int count=0;
                   while(line !=null) {
              Atom a = new Atom(line, String.valueOf(count));
                       line = readLine();
         }} catch (Exception ex) {
              System.out.println (ex);
         }
    }
0
 

Author Comment

by:indie_campbell
ID: 8009551
yes, i totally agree with u on the Reader situation

but i dont understand the use of parsing here, could u expand??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8010562
Are you talking to me? (no i'm not standing looking in a mirror wearing a shoulder holster ;-))
0
 

Author Comment

by:indie_campbell
ID: 8011439
this is what i have, put comments where problem

thanks

import java.util.*;
import java.io.*;
import java.lang.*;

public class AtomDistanceTest1edit {
     public static void main(String[] args) {
          new AtomDistanceTest1edit();
                   
     }

     public AtomDistanceTest1edit() {
          try {
              BufferedReader reader = new BufferedReader(new FileReader("model2test.pdb"));
              String line = null;
              while((line = reader.readLine()) !=null) {
               Atom a = AtomParser.getAtom(line);
                                                      }
             
          reader.close();
              }

                  catch (Exception ex) {
               System.out.println (ex);
          }
               }
}
     

class AtomParser {

System.out.println("Parsing...");

          //loop to go through all lines of the file
          for (int i = 0; i < AtomParser.size(); i++) {
               ??? = AtomParser.elementAt(i).toString();    // this is the problem area???


}




class Atom {
     public double x;
     public double y;
     public double z;
     public String name;

     public Atom(String s, String name) throws IllegalArgumentException {
     
     

     try {
               this.name = name;
               StringTokenizer t = new StringTokenizer (s, " ");
               for (int j=0; j<5; j++) t.nextToken(); //skip first 5 tokens
               this.x = new Double(t.nextToken()).doubleValue();
               this.y = new Double(t.nextToken()).doubleValue();
               this.z = new Double(t.nextToken()).doubleValue();
          } catch (Exception ex) {
               throw new IllegalArgumentException ("Illegal atom description");
          }
     }

     public String toString() {
   

          return "atom : " + name + "(x=" + x + " y=" + y + " z=" + z + ")";
     }

     public double distanceFrom (Atom other) {
          return calculateDistance (x, y, z, other.x, other.y, other.z);
     }

     public static double calculateDistance (double x1, double y1, double z1, double x2, double y2, double z2) {
          return Math.sqrt(Math.sqrt(Math.pow(Math.abs(x1-x2),2)+Math.pow(Math.abs(y1-y2),2))+Math.pow(Math.abs(z1-z2),2));
     }

     public static void printDistance (Atom a, Atom b) {
          System.out.println ("Distance between " + a.toString() + " and " + b.toString() + " is " + a.distanceFrom(b));
     }
}

0
 

Expert Comment

by:tomArghh
ID: 8011525
Be careful, you're double-posting. Click on 'Reload This Question' top left.
0
 

Expert Comment

by:tomArghh
ID: 8011596
You need to put the 'getAtom' stuff into the AtomParser class. Make it a static method.
0
 

Expert Comment

by:CleanupPing
ID: 9059170
indie_campbell:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9582180
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept girionis's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question