The purpose of this project is to give students exposure to object oriented desi
ID: 3819135 • Letter: T
Question
The purpose of this project is to give students exposure to object oriented design and programming using classes and polymorphism in a realistic application that involves arrays of objects and sorting arrays containing objects. You will write several classes for Program 10. Please submit all classes in one file. The name of the physical program file submitted xxxx program 10, where xxxx is your Kean email id Assignment: A 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 a listing of all pets sorted by their outstanding bill balance. You have to write a program to produce a report of animals and their owners sorted by their outstanding bill balances from the data in the flat text file. Below is a description of the information on the text file: The first entry is the number of animals on the file (numeric) The fields below repeat for each animal o Owner name (String) Birth year (numeric) Bill balance (numeric) Species (String) Special feature (numeric or String) The animals serviced by the veterinarian are of two types: mammals and non-mammals. For a mammal the special feature field on the flat file is the number of legs of the animal (numeric). For a non-mammal the special feature field is the blood type, warm-blooded or cold-blooded (String). Program requirements and grading: From the information provided, write a solution that includes the following: A suitable inheritance hierarchy which represents the pets serviced by the veterinarian. It is up to you how to design the inheritance hierarchy. I suggest an Animal class and appropriate subclasses. For all classes include the following: Instance variables (5 points) Constructors (7.5 points)
Explanation / Answer
Hi ,
Please see below the Program. Please comment for any queries/feedbacks.
Thanks,
Anita
xxxx_Program10.java
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.