Click here to Skip to main content
15,891,033 members
Home / Discussions / Java
   

Java

 
GeneralRe: Setting Path Variable Permanently?? Pin
Richard MacCutchan24-Feb-10 4:03
mveRichard MacCutchan24-Feb-10 4:03 
GeneralRe: Setting Path Variable Permanently?? Pin
AmbiguousName4-Mar-10 6:26
AmbiguousName4-Mar-10 6:26 
AnswerRe: Setting Path Variable Permanently?? Pin
Fredrik Bornander21-Feb-10 5:53
professionalFredrik Bornander21-Feb-10 5:53 
GeneralRe: Setting Path Variable Permanently?? Pin
AmbiguousName24-Feb-10 3:31
AmbiguousName24-Feb-10 3:31 
AnswerRe: i want toimplement dfs and bfs code in java by reading data from files Pin
Richard MacCutchan20-Feb-10 4:15
mveRichard MacCutchan20-Feb-10 4:15 
QuestionMultiObject Problem Pin
002comp17-Feb-10 1:52
002comp17-Feb-10 1:52 
AnswerRe: MultiObject Problem Pin
Nagy Vilmos22-Feb-10 21:59
professionalNagy Vilmos22-Feb-10 21:59 
QuestionWhat is this project asking me to do??? Pin
polska0316-Feb-10 10:36
polska0316-Feb-10 10:36 
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.




Rules:
1. Each class above includes, in addition to the constructors:

public String toString(){
//return a string of the classname, the name, and toString() of all
parts.
}
public void readFromFile(){
//read the data from the user, including objName
}//readIn

2. The data members of each class MUST be declared "private" and you may not create
a public function that returns the value of any private data object, other than
getName() above.


3. All classes, except MainClass, should be in a package. Your project should have
at least 3 packages.


4. if you are reading in an element of class "C", where "C" is a superclass
of some other classes, then you must ask the user which of the subclasses
they want.

For example, suppose we have these relationSpaceShips:
Dealer has-many Car
Ford is-a Car
Honda is-a Car

then the readFromFile() function for Dealer might look like this:

Code:

 public void readFromFile(FileIO fio) thows Exception{
      if(fio.hasNextInt()==false){
	    throw new Exception("Dealer.readFromFile:missing ncars");
	  }
       int ncars = fio.getNextInt();
       carArray = new Car[ncars];
       for(int i=0;i<carArray.length;i++){
        if(fio.getNextLine()==false){
	      throw new Exception("Dealer.readFromFile:missing ncars");
	    }
        String cartype=fio.getNextLine();
        if(cartype.equals("f")){
          carArray[i]= new Ford();
        }else{
          carArray[i]= new Honda();
        }
        carArray[i].readFromFile(fio);
      }//for each car in carrArray
      
     }//readFromFile
	 
	 You may assume that class fileioPkg.FileIO is part of your project:
     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 const
	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



So far I created all the classes such as SpacePort, Crewman etc. I created the super classes for example SpaceShip extends SpacePort. I created a mainclass which asks the user to enter a txt file. But fromt his spot I am stuck on what I am so post to do. I have no idea how the txt file is supposed to look like and what the program is to do. Can someone please exaplin this to me in idiot proof terms. Sorry I am still new to java.
AnswerRe: What is this project asking me to do??? Pin
Richard MacCutchan16-Feb-10 12:03
mveRichard MacCutchan16-Feb-10 12:03 
QuestionJLayer Pane Pin
Jay-Tech15-Feb-10 17:21
Jay-Tech15-Feb-10 17:21 
Question== String comparison [modified] Pin
AnnnS11-Feb-10 2:28
AnnnS11-Feb-10 2:28 
AnswerRe: == String comparison Pin
Richard MacCutchan11-Feb-10 4:26
mveRichard MacCutchan11-Feb-10 4:26 
GeneralRe: == String comparison Pin
AnnnS11-Feb-10 4:31
AnnnS11-Feb-10 4:31 
AnswerRe: == String comparison Pin
Nagy Vilmos22-Feb-10 22:32
professionalNagy Vilmos22-Feb-10 22:32 
GeneralRe: == String comparison Pin
AmbiguousName7-Mar-10 20:41
AmbiguousName7-Mar-10 20:41 
GeneralRe: == String comparison Pin
Nagy Vilmos7-Mar-10 22:22
professionalNagy Vilmos7-Mar-10 22:22 
QuestionJavascript File upload Pin
Kushi Bobby7-Feb-10 17:18
Kushi Bobby7-Feb-10 17:18 
AnswerRe: Javascript File upload Pin
42774807-Feb-10 18:06
42774807-Feb-10 18:06 
AnswerRe: Javascript File upload Pin
Richard MacCutchan7-Feb-10 22:31
mveRichard MacCutchan7-Feb-10 22:31 
JokeRe: Javascript File upload Pin
42774808-Feb-10 0:21
42774808-Feb-10 0:21 
GeneralRe: Javascript File upload Pin
Richard MacCutchan8-Feb-10 0:35
mveRichard MacCutchan8-Feb-10 0:35 
GeneralRe: Javascript File upload Pin
42774808-Feb-10 0:38
42774808-Feb-10 0:38 
QuestionPhyton to Java Pin
BombayGuy5-Feb-10 12:32
BombayGuy5-Feb-10 12:32 
AnswerRe: Phyton to Java Pin
42774805-Feb-10 20:32
42774805-Feb-10 20:32 
QuestionSlot Machine Java Help [modified] Pin
bmnot4-Feb-10 23:48
bmnot4-Feb-10 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.