this may be a long shot , but im totally stuck and need help. i have a farely large project where we have to read in some some numbers, and these numbers represent triangles and will be used to make a picture of a tea pot. ive got about 65% of the project done , except i dont understand how to write to a file. there are 2 methods in my model class , which are writeDatatoFile and writeInfotoFile. write data to file just writes back whatever ive read in and should have a line containing the keyword triangle followed by three lines, each with information for each vertex. i think once i figure out how to do this part , i can figure out the writeInfoToFile method , which writes all the into out ( like the max,min). i know how t write out to a file , but im having problems seeing how to implement it. i just cant see the big picture. ill paste the code below , its split into 6 classes. App, D3V, Model,Triangle, vector and vertex.
this is my model class , this is were the 2 write to file methods are.
triangle class
vector class
vertex class
D3V class
app class , which runs it all
this is my model class , this is were the 2 write to file methods are.
import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Model { ArrayList<Triangle> triangles = new ArrayList<Triangle>(); void readFromFile(String filename) { File file = new File(filename); readFromFile(file); } void writeDataToFile(File file) { } void writeInfoToFile(File file) { // please implement this method... // double a = m.computeSurfaceArea(); // System.out.println("total surface area = " + a); } void readFromFile(File file) { try { Scanner s2 = new Scanner(file); while (s2.hasNext()) { s2.nextLine(); String[] threeLines = { s2.nextLine(), s2.nextLine(), s2.nextLine() }; triangles.add(new Triangle(threeLines)); } } catch (FileNotFoundException e) { e.printStackTrace(); } } double computeSurfaceArea() { double v = 0; // loop through each triangle and sum up their areas for ( Triangle t : triangles ) { double sArea = t.computeArea(); v += sArea; } return v; } double averageSurface(){ double v = 0; int count = 0; for ( Triangle t : triangles){ double aAverage =t.computeArea(); count++; v += aAverage; } return v/count; } double computeMax(){ double maxValue= 0; for ( Triangle t : triangles) { double areaT = t.computeArea(); if (areaT> maxValue ){ maxValue =areaT; } } return maxValue; } double computeMin(){ double minValue= 0; for ( Triangle t : triangles) { double areaT = t.computeArea(); if (areaT < minValue ){ minValue =areaT; } } return minValue; } }
triangle class
public class Triangle { Triangle(String[] lines) { this.points = new Vertex[3]; for (int i = 0; i < 3; i++) this.points[i] = new Vertex(lines[i]); } Vertex points[]; // array of vertices public String toString() { String buffer = ""; for (Vertex point : points) { buffer += point.toString() + "\n"; } return buffer; } double computeArea() { double area = 0.0; D3V uu = D3V.sub(points[0].position, points[2].position); D3V vv = D3V.sub(points[1].position, points[2].position); area = 0.5 * Vector.length(Vector.cross(uu,vv)); return area; } }
vector class
public class Vector extends D3V { public Vector(double d, double e, double f) { super(d, e, f); } static double length(Vector u) { return Math.sqrt(u.x*u.x + u.y*u.y + u.z*u.z); } public static Vector cross(D3V u, D3V v) { return new Vector( u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x ); } public static Vector dot(D3V u, D3V v) { return new Vector(u.x*v.x,u.y*v.y,u.z*v.z); } }
vertex class
public class Vertex { D3V position; D3V normal; double u; double v; Vertex() { position = new D3V(); normal = new D3V(); } public String toString() { String buffer = ""; // fill in buffer with P(x,y,z), N(x,y,z), u, and v return buffer; } double X() { return position.x; } double Nx() { return normal.x; } Vertex(String line) { String[] array = line.split("\\s+"); int idx = 0; position.x = Double.parseDouble(array[idx++]); position.y = Double.parseDouble(array[idx++]); position.z = Double.parseDouble(array[idx++]); normal.x=Double.parseDouble(array[idx++]); normal.y=Double.parseDouble(array[idx++]); normal.z=Double.parseDouble(array[idx++]); // etc.. } public Vertex(double x, double y, double z) { position = new D3V(x,y,z); } public Vertex(double px, double py, double pz, double nx, double ny, double nz) { position = new D3V(px,py,pz); normal = new D3V(nx,ny,nz); } }
D3V class
public class D3V { double x, y, z; public D3V() { x = 0; y = 0; z = 0; } public String toString() { // convert x, y, z to string return ""; } public D3V(double d, double e, double f) { x = d; y = e; z = f; } public static D3V add(D3V a, D3V B)/>/> { return new D3V (a.x + b.x, a.y + b.y, a.z + b.z); } public static D3V sub(D3V a, D3V B)/>/> { return new D3V(a.x - b.x, a.y - b.y, a.z - b.z); } }
app class , which runs it all
import java.io.File; public class App { String changeExtension(String filename, String originalExtension, String newExtension) { int lastDot = filename.lastIndexOf("."); if (lastDot != -1) { return originalExtension.replace(originalExtension, newExtension); } else { return originalExtension + newExtension; } } public void Run(String inputFilename) { Model m = new Model(); m.readFromFile(inputFilename); // write model's data String dataOutFilename = changeExtension(inputFilename, "asc", "out.txt"); m.writeDataToFile(new File(dataOutFilename)); // write model's info String infoOutFilename = changeExtension(inputFilename, "asc", "info.txt"); m.writeInfoToFile(new File(infoOutFilename)); } public static void main(String[] args) { App app = new App(); app.Run(args[0]); } }