Question

I'm a beginner Computer Science student and I found a interesting project that I cannot seem...

  • I'm a beginner Computer Science student and I found a interesting project that I cannot seem to figure out the code for. I have the other classes created like pokemonTrainer and pokemonGymLeader that have constructors that take have parameters fName, lName, trainerID.
    For example: pokemonTrainer (String fName, String lName, int trainerID)

    I want to write a program that has a method called readFromFile that reads a text file called pokemon1.txt. The program should be able to read each line and create objects of the specific type.
  • After that, if a line is successfully read it should return a string called trainerRecord
  • I also want A boolean method called createPlayer that has the parameter trainerRecord creates a player object (based on if they are either a trainer or a gym leader) and adds player to a ArrayList called Allplayers. It also throws an exception if player is not a trainer or a gym leader. The method returns true if the player was created successful or false otherwise.
    For example: if (trainer_or_gym = trainer) {
    pokemonTrainer PT = new pokemonTrainer
  • Then another method called writeToFile should retrieve the information from the Allplayers arraylist and writes the specific object as a comma delimited text file. Each players data is one line.

pokemon1.txt contents:

fname lname trainerID trainer_or_gym   gender    year gymBadges_or_opponents_beate

ash ketchum 01234 trainer male junior 8

misty water 2345 gym female senior 11

brock rock 3457 gym male senior 10

Jesse rocket 01235 trainer female Sophomore 4

If you need information, please let me know !

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class ObjectIOExample {

private static final String filepath="C:\\Users\\test\\Desktop\\pokemon1.txt";
ArrayList<Player> AllPlayers;

public ObjectIOExample() {
this.AllPlayers = new ArrayList<Player>;
}
  
boolean createPlayer(String trainerRecord)
{
Player p = new Player(trainerRecord);
AllPlayers.add(p);
if (trainerRecord.compareToIgnoreCase("trainer")!=0 || trainerRecord.compareToIgnoreCase("gym leader")!=0)
{
ArithmeticException e = new ArithmeticException("test");
throw e;
}
  
  
if(p!=null)
return true;
else
return false;
}
  
public static void main(String args[]) {

ObjectIOExample objectIO = new ObjectIOExample();

//Read object from file
pokemonTrainer st = (pokemonTrainer) objectIO.ReadObjectFromFile(filepath);
System.out.println(st.trainerRecord); //the string trainerRecord should be present in the class as a data member.
  
System.out.println(createPlayer(st.trainerRecord));
}
public void WriteObjectToFile() {
try {
for (int i=0; i<AllPlayers.size(); i++)
{
Player p = AllPlayers.toArray(i);
  
           FileOutputStream fos = new FileOutputStream(filepath);
           ObjectOutputStream oos = new ObjectOutputStream(fos);
           // write object to file
           oos.writeObject(p);
           System.out.println("Done");
           // closing resources
           oos.close();
           fos.close();
       } }catch (IOException e) {
           e.printStackTrace();
       }
}

public Object ReadObjectFromFile(String filepath) {

try {

FileInputStream fileIn = new FileInputStream(filepath);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);

Object obj = objectIn.readObject();

System.out.println("The Object has been read from the file");
objectIn.close();
  
return obj;

} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}

the code is dependent on other code. therefore no output.

COMMENT DOWN FOR ANY QUERIES AND,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.

Add a comment
Know the answer?
Add Answer to:
I'm a beginner Computer Science student and I found a interesting project that I cannot seem...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT