Results 1 to 2 of 2
  1. #1
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Best way to get data from a text file and then put it in an array of Solids?

    I am writing a program that uses inheritance and polymorphism to sort solids.

    Here is my Solids class, which is the parent of Rectangular, Sphere, and Cube.

    Java Code:
    import java.text.DecimalFormat;
    
    public abstract class Solid {
    
    	private String solidName;
    	DecimalFormat roundHundreth = new DecimalFormat("#.##");
    	
    	//constructor for setting name to a default value
    	//preconditions:none
    	//postconditions:sets solidName to undefined
    	public Solid(){
    		solidName = "undefined";
    	}
    	
    	//method for setting name to method argument
    	//preconditions: must enter a String argument
    	//postconditions: will set solidName to name
    	public void setName(String name){
    		solidName = name;
    	}
    	
    	//getter method for Solid name
    	//preconditions: none
    	//postconditions: will return solidName
    	public String getName(){
    		return solidName;
    	}
    	
    	
    	//abstract method declarations
    	public abstract double volume();
    	
    	public abstract double surface();
    	
    	public abstract void display();
    		
    	
    	
    	
    }







    And here is what one of child classes look like.

    Java Code:
    public class Rectangular extends Solid {
    	
    	private double length;
    	private double width;
    	private double height;
    	
    	public Rectangular(){
    		setName("undefined");
    	}
    	
    	public Rectangular(double len, double wid, double hei){
    		
    		setName("Rectangular");
    		length = len;
    		width = wid;
    		height = hei;
    	}
    	
    
    	public double volume() {
    		//method to return volume of a rectangular prism
    		//preconditions: must use defined constructor while instantiating Rectangular 
    		//postconditions: computes and returns volume
    		
    		double vol = length*width*height;
    		return vol;
    	}
    
    	
    	public double surface() {
    		//method to return surface area of a rectangular prism
    		//preconditions: must use defined constructor while instantiating Rectangular 
    		//postconditions: computes and returns surface area
    		
    		double surfaceArea = 2*length*height + 2*height*width + 2*length*width;
    		return surfaceArea;
    	}
    
    	
    	public void display() {
    		//method to display info on a rectangular prism
    		//postconditions: none
    		//postconditions: displays information related to a rectangular prism
    		
    		System.out.printf("Rectangular Prism -- Length: %s   Width: %s   Height: %s   Volume: %s   Surface Area: %s\n", roundHundreth.format(length), roundHundreth.format(width), roundHundreth.format(height), roundHundreth.format(volume()), roundHundreth.format(surface()));
    		
    	}
    }







    I am wondering what the most efficient way to sort this text into an object of their respective class. (S = Sphere, etc) I already have a way to do it, but I feel like it could be far more efficient. How would you go about it?

    S 0.5
    R 1.5 5.2 3.75
    R 40.0 1.0 1.0
    C 0.75
    C 7.5
    C 75.0
    S 37.5
    S 1.0
    S 1.25
    R 74.0 76.0 11.5
    C 3.14
    C 1.77
    Last edited by jubbiejub; 04-26-2012 at 10:10 PM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Best way to get data from a text file and then put it in an array of Solids?

    What I see of your code seems ok so far - though I would change the name display() to toString(). What part of the posted code is your concern?

Similar Threads

  1. Calling data from an array to a text area
    By toamille in forum New To Java
    Replies: 5
    Last Post: 04-13-2012, 04:40 PM
  2. read text data and store to array
    By Ericyue in forum New To Java
    Replies: 3
    Last Post: 04-09-2012, 03:07 PM
  3. Replies: 2
    Last Post: 03-25-2011, 03:36 PM
  4. Creating an array from data in a text file
    By Ryanas in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 03:46 PM
  5. Replies: 6
    Last Post: 12-01-2010, 06:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •