Hi. I'm writing a program that reads information from three seperate classes.
public class Animal
{
protected int id,animalTotal;
protected String type;
protected double mass;
//------------------------------------------------------------------------
// Sets up an animal with the specified ID number, type and weight.
//------------------------------------------------------------------------
public Animal (int animalID, String animalType, double weight)
{
id = animalID;
type = animalType;
mass = weight;
}
//------------------------------------------------------------------------
// Returns information about this animal as a string.
//------------------------------------------------------------------------
public String toString()
{
String description = "ID: " + id + "\n";
description += "Type: " + type + "\n";
description += "Weight: " + mass + "\n";
return description;
}
public String createOutputFile()
{
String count = "";
count += "\nNumber of animals total: " + ProgThree.animalTotal + ".";
count += "Number of pets total: " + Pet.petTotal + ".";
count += "Number of zoo animals total: " + ProgThree.zooTotal + ".";
return count;
}
}
public class Pet extends Animal
{
private String title;
private String own;
//------------------------------------------------------------------------
// Sets up a pet using the specified information.
//------------------------------------------------------------------------
public Pet (int animalID, String animalType, double weight, String name, String owner)
{
super (animalID, animalType, weight);
title = name;
own = owner;
}
//------------------------------------------------------------------------
// Returns information about this pet as a string.
//------------------------------------------------------------------------
public String toString()
{
String description = super.toString();
description += "Name: " + title + "\n";
description += "Owner: " + own + "\n";
return description;
}
}
public class Pet extends Animal
{
private String title;
private String own;
//------------------------------------------------------------------------
// Sets up a pet using the specified information.
//------------------------------------------------------------------------
public Pet (int animalID, String animalType, double weight, String name, String owner)
{
super (animalID, animalType, weight);
title = name;
own = owner;
}
//------------------------------------------------------------------------
// Returns information about this pet as a string.
//------------------------------------------------------------------------
public String toString()
{
String description = super.toString();
description += "Name: " + title + "\n";
description += "Owner: " + own + "\n";
return description;
}
}
//********************************************************************
// ZooAnimal.java
//
// Program 3
//
// @Author: Preza
//
// @Version: 12/01/12
//********************************************************************
public class ZooAnimal extends Animal
{
private int cage;
private String train;
//------------------------------------------------------------------------
// Sets up a zoo animal using the specified information.
//------------------------------------------------------------------------
public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
{
super (animalID, animalType, weight);
cage = cageNumber;
train = trainer;
}
//------------------------------------------------------------------------
// Returns information about this zoo animal as a string.
//------------------------------------------------------------------------
public String toString()
{
String description = super.toString();
description += "Cage: " + cage + "\n";
description += "Trainer: " + train + "\n";
return description;
}
}
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;
public class ProgThree
{
public static void main (String[] args) throws FileNotFoundException, IOException
{
Scanner scan = new Scanner(new File("animal.txt"));
String animalType = null, name = null, trainer = null, owner = null, lineIn = null;
int animalID = 0, cageNumber = 0, animalTotal = 0, petTotal = 0, zooTotal = 0;
double weight = 0.0;
StringTokenizer st = null;
Animal animal = null;
Pet pet = null;
ZooAnimal zooAnimal = null;
while (scan.hasNextLine())
{
lineIn = scan.nextLine();
st = new StringTokenizer(lineIn, ",");
animalID = Integer.parseInt(st.nextToken());
animalType = st.nextToken();
weight = Double.parseDouble(st.nextToken());
if (animalID >= 8000 && animalID <= 9999)
{
cageNumber = Integer.parseInt(st.nextToken());
trainer = st.nextToken();
zooAnimal = new ZooAnimal(animalID, animalType, weight, cageNumber, trainer);
System.out.println(zooAnimal);
zooTotal++;
}
if (animalID >= 3000 && animalID <= 7999)
{
name = st.nextToken();
owner = st.nextToken();
pet = new Pet(animalID, animalType, weight, name, owner);
System.out.println(pet);
petTotal++;
}
if (animalID >= 1000 && animalID <= 2999)
{
animal = new Animal (animalID, animalType, weight);
System.out.println(animal);
animalTotal++;
}
FileWriter fw = new FileWriter("animalout.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(animal.createOutputFile());
pw.close();
}
}
}
1023,Monkey,38.6
1111,Ape,65.2,
4000,Dog,13.4,Thomas,George,
8100,Giraffe,130.3,103,Samuel,
How do I make this program write an output file that counts how many animals, pets, and zoo animals?
sorry i do not know how to separate these 4 codes.