3 Replies - 307 Views - Last Post: 01 February 2013 - 04:54 AM Rate Topic: -----

#1 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

objects , writing files

Posted 01 February 2013 - 01:31 AM

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.
 

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]);
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: objects , writing files

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: objects , writing files

Posted 01 February 2013 - 02:24 AM

Are there any error messages you are getting? If so, please post them here.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

Re: objects , writing files

Posted 01 February 2013 - 02:29 AM

the program doesnt run as is , i dont think im reading in the files wrong, but thats something i think i can fix later. just trying to get how to write the file out. im supposed to write out the info as follows.

1. Write the data out to a file called pot4.out.txt. This file should be in a format that is
similar to the input file. More specifically it should have a line containing the keyword triangle
followed by three lines, each with information for each vertex.
2. Perform computation to generate some statistics about the data model and save the results
to a file pot4.info.txt. This file should contain the following: max,min, average

This post has been edited by zerocool18: 01 February 2013 - 02:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

Re: objects , writing files

Posted 01 February 2013 - 04:54 AM

i understand how to write out to files. the code below is how id write to a file. but i dont get how to write out the file i read in( in my case im reading in a file called pot4.asc) , into my outfile ( aka my pot4.out.txt); im supposed to just regurgitate whatever i read in , into the pot4.out.txt. the other write out method , writes the data like the min and max to the out file pot4.info.txt.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class practice {

   
    public static void main(String[] args) {
       
        String fileName = "pot4.out.txt"; // file ur writing to
        try {
            PrintWriter outPut = new PrintWriter(fileName); // creating printwriter
            outPut.println("Hi there file"); // this is how you write to it , using ouput that u created.
            outPut.close(); // always have to close the stream
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1