Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a Java program A large veterinarian services many pets and their owners.

ID: 3824521 • Letter: T

Question

This is a Java program

A large veterinarian services many pets and their owners. As new pets are added to the population of pets being serviced, their information is entered into a flat text file. Each month the vet requests and updates listing of all pets sorted by their outstanding bill balance.. You are to write a program which in produce a report of animals and their owners sortedby their outstanding balances from the data in the flat text file.

Program requirements:

A class named Animal with the following details/subclasses

Name(owner)   a character String

birth year      numeric

bill balance   numeric

species      a character String

Special specie feature       Mammal legs or nonMammal blood type

Constructor methods of all classes

Accessor and mutator methods of all classes

An array of Animal objects

Read a input text file from http://imc.kean.edu/CPS2231/program5.txt

with the ordering as above, one grouping for each animal will be provided. Also, the first item in the file is the number of animals. You should test your program with the input data.

One method for inputting each Animal object

One method for producing an output report - formatting is one Animal per line and at most 40 Animals per page

One method for sorting the array of Animals

One simple main method that calls for all input, calls a sort method, and calls for the report of the sorted list.

Record your planning time, coding time, testing time, and bug fixing time. Put these information in the comments at the top of the program.


Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.SpringLayout;


public class xxxx_Program10 {
   class Animal{
       private String owner;
       private int year;
       private double billbalance;
       private String species;
       private String specialFeature;
      
       //Parameter constructor
       public Animal(String owner, int year, double billbalance,
               String species, String specialFeature) {
           super();
           this.owner = owner;
           this.year = year;
           this.billbalance = billbalance;
           this.species = species;
           this.specialFeature = specialFeature;
       }
       //Getters and setters
       public String getOwner() {
           return owner;
       }
       public void setOwner(String owner) {
           this.owner = owner;
       }
       public int getYear() {
           return year;
       }
       public void setYear(int year) {
           this.year = year;
       }
       public double getBillbalance() {
           return billbalance;
       }
       public void setBillbalance(double billbalance) {
           this.billbalance = billbalance;
       }
       public String getSpecies() {
           return species;
       }
       public void setSpecies(String species) {
           this.species = species;
       }
       public String getSpecialFeature() {
           return specialFeature;
       }
       public void setSpecialFeature(String specialFeature) {
           this.specialFeature = specialFeature;
       }
       //ToString method
       public String toString(){
           String retString = "";
           retString =" Owner : "+this.getOwner()+" Year : "+this.getYear()+" BillBalance : "+this.getBillbalance()+" Species : "+this.getSpecies();  
           return retString;
       }
      
   }
   class Mammal extends Animal{

      
       //Parameter constructor
       public Mammal(String owner, int year, double billbalance,
               String species, String specialFeature) {
           super(owner, year, billbalance, species, specialFeature);
       }
       //ToString method for Mammal
       public String toString(){
           String retString = "";
           retString =" Owner : "+this.getOwner()+" Year : "+this.getYear()+" BillBalance : "+this.getBillbalance()+" Species : "+this.getSpecies();
           retString = retString + " No. of Legs : "+this.getSpecialFeature();
           return retString;
       }
      
   }
   class NonMammal extends Animal{

       //Parameter constructor
       public NonMammal(String owner, int year, double billbalance,
               String species, String specialFeature) {
           super(owner, year, billbalance, species, specialFeature);
       }
      
       //ToString method for NonMammal
       public String toString(){
           String retString = "";
           retString =" Owner : "+this.getOwner()+" Year : "+this.getYear()+" BillBalance : "+this.getBillbalance()+" Species : "+this.getSpecies();  
           retString = retString +" Blood Type : "+this.getSpecialFeature();
           return retString;
       }
   }
   /**
   * to read the set of animals from program10.txt
   * @return Animal[]
   */
   public static Animal[] readAnimalfromFile(){
       Animal [] animalArray = null;
       xxxx_Program10 xxxx_Program10_obj = new xxxx_Program10();
       //Reading from the file
       BufferedReader br = null;
       FileReader fr = null;
       int countLine =0;
       int noOfAnimals = 0;

       try {

           fr = new FileReader("program10.txt");
           br = new BufferedReader(fr);
           String owner= "";
           int year = 0;
           double billbalance=0;
           String species = "";
           String specialFeature="";
          

           String sCurrentLine;

           while ((sCurrentLine = br.readLine()) != null) {
               if(countLine == 0){
                   noOfAnimals =Integer.valueOf(sCurrentLine);
                   animalArray = new Animal[noOfAnimals];
               }
               else{
                   String[] inputArr = sCurrentLine.split(" ");
                   for(int i=0;i<inputArr.length;i++){
                       if(0 == i){
                           owner = inputArr[i];
                       }
                       else if(1 == i){
                           year = Integer.valueOf(inputArr[i]);
                       }
                       else if(2 == i){
                           billbalance = Double.valueOf(inputArr[i]);
                       }
                       else if(3 == i){
                           species = inputArr[i];
                       }
                       else if(4 == i){
                           specialFeature=inputArr[i];
                       }
                   }
                  
                   Animal animalNew =null;
                   if("M".equalsIgnoreCase(species)){
                       animalNew = xxxx_Program10_obj.new Mammal(owner, year, billbalance, species, specialFeature);
                   }
                   else if("N".equalsIgnoreCase(species)){
                       animalNew = xxxx_Program10_obj.new Mammal(owner, year, billbalance, species, specialFeature);
                   }
                  
                   animalArray[countLine-1] =animalNew;
               }
               countLine++;
           }

       } catch (IOException e) {

           e.printStackTrace();

       }
      
       return animalArray;
   }
   /**
   * sortAnimalArray - to sort an array of animals
   * @param animalArray
   * @return Animal[]
   */
   public static Animal[] sortAnimalArray(Animal [] animalArray ){
       int n = animalArray.length;
   double temp = 0;
   for(int i=0; i < n; i++){
   for(int j=1; j < (n-i); j++){
   if(animalArray[j-1].getBillbalance() > animalArray[j].getBillbalance()){
   //swap elements
   temp = animalArray[j-1].getBillbalance();
   animalArray[j-1].setBillbalance(animalArray[j].getBillbalance());
   animalArray[j].setBillbalance(temp);
   }
  
   }
   }
   return animalArray;
   }
   /**
   * To print an array of animals
   * @param animalArray
   */
   public static void printArray(Animal [] animalArray){
       for(int i=0; i < animalArray.length; i++){
System.out.print(animalArray[i] + " ");
       }
   }
   /**
   * Main
   * @param args
   */
   public static void main(String [] args){
      
       Animal [] animalArray = readAnimalfromFile();
      
       //Printing before sorting
       System.out.println("List of Animals Before Sorting : ");
      
       printArray(animalArray);
       //Calling the sort function
       animalArray = sortAnimalArray(animalArray );
      
       //Printing after sorting
       System.out.println("List of Animals After Sorting : ");
       printArray(animalArray);
   }

}

Sample input:

4
Helen 2000 700 M 4
Walter 2010 100 N cold-blooded
Rot 2011 800 M 4
Cooper 2015 600 N warm-blooded

Sample output:

List of Animals Before Sorting :
Owner : Helen Year : 2000 BillBalance : 700.0 Species : M No. of Legs : 4
Owner : Walter Year : 2010 BillBalance : 100.0 Species : N No. of Legs : cold-blooded
Owner : Rot Year : 2011 BillBalance : 800.0 Species : M No. of Legs : 4
Owner : Cooper Year : 2015 BillBalance : 600.0 Species : N No. of Legs : warm-blooded
List of Animals After Sorting :
Owner : Helen Year : 2000 BillBalance : 100.0 Species : M No. of Legs : 4
Owner : Walter Year : 2010 BillBalance : 600.0 Species : N No. of Legs : cold-blooded
Owner : Rot Year : 2011 BillBalance : 700.0 Species : M No. of Legs : 4
Owner : Cooper Year : 2015 BillBalance : 800.0 Species : N No. of Legs : warm-blooded

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote