Click here to Skip to main content
15,844,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am only showing the spaceport and fileIO. I want the first line of my txt file to show the number of spaceship to create an array then choose what type of spaceship. I did the best i can and now i am completly stuck its not working. Can Someone please help. The Instruction are:
The relationSpaceShips between the classes in this project is as follows:
(1) SpacePort has-many SpaceShip
(2) SpaceShip has-a Captain
(3) SpaceShip has-a Engine
(4) SpaceShip has-a RegistrationDate
(5) Impulse is-a Engine
(6) Warp is-a Engine

(7) SpaceShip has-many Crewman
(6) Person has-a pid (int)
(9) Person has-a DateOfBirth
(10) Captain is-a Officer
(11) Officer is-a Person

(12) DateOfBirth is-a StarDate
(13) RegistrationDate is-a StarDate
(14) StarDate has-a day (int)
(15) StarDate has-a month (int)
(16) StarDate has-a year (int)

(17) Enterprise is-a SpaceShip
(18) WarBird is-a SpaceShip
(19) RedShirt is-a Crewman
(20) Crewman is-a Person

NOTE:every object has:
private String objName, and
public String getName() {return objName;}


Create a test program that creates an SpacePort object, and prompts for
the user to enter the path of a data file. The program then reads the data from the data
file for the SpacePort.
Then, print:
1. all SpaceShip objects belonging to the SpacePort, sorted by getName(), and
2. all Person objects belonging to the SpacePort, sorted by getName()

Test your program by creating a data file and filling it in
with the data that answers the questions. Include the
data file and run.bat in the project folder.
C#
package space_shipPKG;
import fileIOPKG.*;
import jdUtil.*;
public class SpacePort  {
    
    private SpaceShip[] ship;
    private int num_ships;
    
    public SpacePort(){
        
        readFromFile();
  
    }
        
              public void readFromFile(){
                  try {
                      String s = Utils.getSystemIn("Please enter the name of the file you would like. The file name is Assign02:");
                      FileIO fio=new FileIO(s);
                      while(fio.hasNextLine())
                      {
                           if(fio.hasNextInt()==false){
                                  throw new Exception("Dealer.readFromFile:missing numships");
                                }
                                 int num_ships = fio.getNextInt();
                                 ship = new SpaceShip[num_ships];
                                 for(int i=0;i<ship.length;i++){
                                  if(fio.getNextLine()==null){
                                    throw new Exception("Dealer.readFromFile:missing numships");
                                  }
                                  String cartype=fio.getNextLine();
                                  if(cartype.equals("Enterprise")){
                                    ship[i]= new Enterprise();
                                  }else{
                                    ship[i]= new WarBird();
                                  }
                                  ship[i].readFromFile();
                                }//for each car in carrArray
                                
                               }//readFromFile
                      
                  } catch (Exception e) {
                      System.out.println("\nin MainClass e=\n"+e.toString());
                      e.printStackTrace();
                  }
              }
              
          
          
     
    private String objName;
    public String getName()
     {
         return objName;
     }
        
     
     public String toString(){
         
         String test="??????";
         return test;
            
            }
    
          
}

 package fileIOPKG;
     import java.util.*;
     import java.io.*;
     public class FileIO {
	//this class acts as a static "producer" class that,after being
	//initialized with a filename,
	private FileReader freader;
	private Scanner scan=null;
	public FileIO(){
		freader=null;
		scan=null;
	}//default constructor
	public FileIO(String filename) throws FileNotFoundException{

		File infile;

		FileReader reader;		
		infile = new File(filename);

		reader = new FileReader(infile);
		scan = new Scanner(reader);
	}//(String) constructor
	public boolean hasNextInt() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			return scan.hasNextInt();
		}
	}//hasNextInt
	public int getNextInt() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			int iv=scan.nextInt();
			System.out.println("FileIO.getNextInt:"+iv);
			return iv;
		}
	}//getNextInt
	
	public boolean hasNextLine() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			return scan.hasNextLine();
		}
	}//hasNextLine
	public String getNextLine() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			String result=scan.nextLine().trim();
			System.out.println("FileIO.getNextLine:"+result);
			return result;
		}
	}//getNextLine

	
	public boolean hasNextDouble() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			return scan.hasNextDouble();
		}
	}//hasNextDouble
	public double getNextDouble() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			double dv=scan.nextDouble();
			System.out.println("FileIO.getNextDouble:"+dv);
			return dv;
		}
	}//getNextDouble
	public void close(){
		try {
			freader.close();
		} catch (IOException e) {
			//ignore
		}
	}//close

}//class FileIO
Posted

1 solution

polska03 wrote:
I did the best i can and now i am completly stuck its not working.


OK, but you really need to help us by explaining what is not working. When you say 'not working' that could mean anything from "prints a message" to "sets my house on fire". Try to indicate where in your code the error occurs, what the actual error is, and anything else that you observe; remember to use the debugger to assist. And if you do post any compiler, exception or system messages, ensure you use copy and paste so we can see the exact wording of the message.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900