Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

altering code to read a string, distance calculations

Posted on 2003-02-21
16
Medium Priority
?
191 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
  • 6
  • 2
  • 2
  • +4
15 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

577 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