Question

CPS 2231-03/Spring 2017 Computer Organization and Programming Dr. Huang Program #5 (Term project) The purpose of this project is to give students more 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 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 will produce a report of animals and their owners sorted by their outstanding balances from the data in the flat text file Program requirements and grading A class named Animal with the following details/subclasses (1) a character string o Name (owner o birth year numeric o bill balance numeric a character string species o Special specie feature Mammal legs or al blood type Constructor method(s) of all classes (10 Accessor and mutator methods of all classes(10 An array of Animal objects (10 Read an input text file from h 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. (10 One method for inputting each Animal object (10%) One method for producing an output report-formatting is one Animal per line and at most 40 Animals per page. (20%) One method for sorting the array of Animals.(10%) One simple main method that: 1) calls for all input, 2) calls a sort method, and 3) calls for the report of the sorted list. (10%) Record your planning time, coding time, testing time and bug fixing time. Put these information in the comments at the top of the program. (10%) Design thoughts The use of methods and subclasses is very beneficial as programs become larger and their logic becomes more difficult In fact, different industries/companies have their own software development requirements (standards) to improve readability, testability, maintainability and overall design. It is up to you to logically dissect the problem and determine the subclasses and methods you will be using in your design. I suggest that you use the Animal class as a super class and create new subclasses How to submit: Please name the Java Class as xxxx Program5 and file name as xxxx Program5.java where XXXX is your Kean email ID Please submit the program online at h edustudents Assigned and Due date (by submit timestamp0 Assigned on 4/10/2017 Due 11:59pm on 5/01/2017 10 points off per week, no late submission after 5/13 Late penalty

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

Executable code

//Include the needed packages
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.SpringLayout;

//Class
public class XXXX_Program5
{
    //Class
    class Animals
    {
        //Declare needed variables
        private String ownerName;
        private int birthYear;
        private double billBalance;
        private String species;
        private String speciesSpecialFeature;
     
        //Parameter constructor
        public Animals(String ownerName,
        int birthYear,
        double billBalance,
        String species,
        String speciesSpecialFeature)
        {
            //Initialize
            super();
            this.ownerName = ownerName;
            this.birthYear = birthYear;
            this.billBalance = billBalance;
            this.species = species;
            this.speciesSpecialFeature = speciesSpecialFeature;
        }
       
        //Getter method
        public String getOwnerName()
        {
            //Return
            return ownerName;
        }
       
        //Setter method
        public void setOwnerName(String ownerName)
        {
            //Set
            this.ownerName = ownerName;
        }
       
        //Getter method
        public int getYear()
        {
            //Return
            return birthYear;
        }
       
        //Setter method
        public void setYear(int birthYear)
        {
            //Set
            this.birthYear = birthYear;
        }
       
        //Getter method
        public double getBillbalance()
        {
            //Return
            return billBalance;
        }
       
        //Setter method
        public void setBillbalance(double billBalance)
        {
            //Set
            this.billBalance = billBalance;
        }
       
        //Getter method
        public String getSpecies()
        {
            //Return
            return species;
        }
       
        //Setter method
        public void setSpecies(String species)
        {
            //Set
            this.species = species;
        }
       
        //Getter method
        public String getSpecialFeature()
        {
            //Return
            return speciesSpecialFeature;
        }
       
        //Setter method
        public void setSpecialFeature(String speciesSpecialFeature)
        {
            //Set
            this.speciesSpecialFeature = speciesSpecialFeature;
        }
       
        //Method toString()
        public String toString()
        {
            //Declare a variable
            String retStr = "";
           
            //Update retStr
            retStr =" Owner : "+this.getOwnerName()+
            " Year : "+this.getYear()+
            " BillBalance : "+this.getBillbalance()+
            " Species : "+this.getSpecies();
           
            //Return
            return retStr;
        }   
    }
   
    //Derived class
    class Mammals extends Animals
    {
        //Parameter constructor
        public Mammals(String ownerName,
        int birthYear,
        double billBalance,
        String species,
        String speciesSpecialFeature)
        {
            //Initialize
            super(ownerName, birthYear, billBalance, species, speciesSpecialFeature);
        }
       
        //Method toString()
        public String toString()
        {
            //Declare a variable
            String retStr = "";
           
            //Update retStr
            retStr =" Owner : "+this.getOwnerName()+
            " Year : "+this.getYear()+
            " BillBalance : "+this.getBillbalance()+
            " Species : "+this.getSpecies();
           
            //Update retStr
            retStr = retStr + " No. of Legs : "+this.getSpecialFeature();
           
            //Return
            return retStr;
        }    
    }
   
    //Derived class
    class NonMammals extends Animals
    {
        //Parameter constructor
        public NonMammals(String ownerName,
        int birthYear,
        double billBalance,
        String species,
        String speciesSpecialFeature)
        {
            //Initialize
            super(ownerName, birthYear, billBalance, species, speciesSpecialFeature);
        }
       
        //Method toString()
        public String toString()
        {
            //Declare a variable
            String retStr = "";
           
            //Update retStr
            retStr =" Owner : "+this.getOwnerName()+
            " Year : "+this.getYear()+
            " BillBalance : "+this.getBillbalance()+
            " Species : "+this.getSpecies();
           
            //Update retStr
            retStr = retStr +" Blood Type : "+this.getSpecialFeature();
           
            //Return
            return retStr;
        }
    }

    //Method readAnimalsfromFile()
    public static Animals[] readAnimalsfromFile()
    {
        //Array
        Animals [] animalArr = null;
       
        //Create instance
        XXXX_Program5 obj1 = new XXXX_Program5();
       
        //Create instance
        BufferedReader br1 = null;
       
        //Create instance
        FileReader fr1 = null;
       
        //Declare variables
        int lineCounter =0;
        int numOfAnimals = 0;
       
        //Try
        try
        {
            //Input file
            fr1 = new FileReader("program5.txt");
           
            //Buffered reader
            br1 = new BufferedReader(fr1);
           
            //Declare variables and initialize
            String ownerName= "";
            int birthYear = 0;
            double billBalance=0;
            String species = "";
            String speciesSpecialFeature="";
            String presentLine;
           
            //Loop
            while ((presentLine = br1.readLine()) != null)
            {
                //Condition check               
                if(lineCounter == 0)
                {
                    //Update
                    numOfAnimals =Integer.valueOf(presentLine);
                   
                    //Update
                    animalArr = new Animals[numOfAnimals];
                }
               
                //Otherwise
                else
                {
                    //Declare a variable
                    String[] inArr = presentLine.split(" ");
                   
                    //Loop
                    for(int i1=0;i1<inArr.length;i1++)
                    {
                        //Condition check
                        if(0 == i1)
                        {
                            //Update
                            ownerName = inArr[i1];
                        }
                       
                        //Condition check
                        else if(1 == i1)
                        {
                            //Update
                            birthYear = Integer.valueOf(inArr[i1]);
                        }
                       
                        //Condition check
                        else if(2 == i1)
                        {
                            //Update
                            billBalance = Double.valueOf(inArr[i1]);
                        }
                       
                        //Condition check
                        else if(3 == i1)
                        {
                            //Update
                            species = inArr[i1];
                        }
                       
                        //Condition checkc
                        else if(4 == i1)
                        {
                            //Update
                            speciesSpecialFeature=inArr[i1];
                        }
                    }
                   
                    //Create instance
                    Animals newAnimals =null;
                   
                    //Condition check
                    if("M".equalsIgnoreCase(species))
                    {
                        //Update
                        newAnimals = obj1.new Mammals(ownerName, birthYear, billBalance, species, speciesSpecialFeature);
                    }
                   
                    //Condition check
                    else if("N".equalsIgnoreCase(species))
                    {
                        //Update
                        newAnimals = obj1.new Mammals(ownerName, birthYear, billBalance, species, speciesSpecialFeature);
                    }
                   
                    //Update
                    animalArr[lineCounter-1] =newAnimals;
                }
               
                //Increment
                lineCounter++;
            }
        }
       
        //Catch block
        catch (IOException e)
        {
           //Error
           e.printStackTrace();       
        }
       
        //Return
        return animalArr;
    }

    //Method sortAnimalsArray()
    public static Animals[] sortAnimalsArray(Animals [] animalArr )
    {
        //Declare variables
        int n = animalArr.length;
        double tmp = 0;
       
        //Loop
        for(int i1=0; i1 < n; i1++)
        {
            //Loop
            for(int j1=1; j1 < (n-i1); j1++)
            {
                //Condition check
                if(animalArr[j1-1].getBillbalance() > animalArr[j1].getBillbalance())
                {
                    //Swap
                    tmp = animalArr[j1-1].getBillbalance();
                    animalArr[j1-1].setBillbalance(animalArr[j1].getBillbalance());
                    animalArr[j1].setBillbalance(tmp);
                }
            }
        }
       
        //Return
        return animalArr;
    }

    //Method displayArray()
    public static void displayArray(Animals [] animalArr)
    {
        //Loop
        for(int i1=0; i1 < animalArr.length; i1++)
        {
            //Display
            System.out.print(animalArr[i1] + "\n");
        }
    }
   
    //Driver
    public static void main(String [] args)
    {
        //Function call to read file input
        Animals [] animalArr = readAnimalsfromFile();
               
        //Display
        System.out.println("List of Animals Before Sorting : ");
       
        //Function call
        displayArray(animalArr);
       
        //Function call
        animalArr = sortAnimalsArray(animalArr );
       
        //Display
        System.out.println("List of Animals After Sorting : ");
       
        //Function call
        displayArray(animalArr);
    }
}

Add a comment
Know the answer?
Add Answer to:
The purpose of this project is to give students more exposure to object oriented design and...
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
  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • Write in java language ================== Office Supplies Inc., an office supply store, services many customers. As...

    Write in java language ================== Office Supplies Inc., an office supply store, services many customers. As customers’ orders for office supplies are shipped, information is entered into a file. Office Supplies bills their customers once each month. At the end of each month, the Chief Executive Officer requests a report of all customers sorted by their customer id (from lowest to highest). The report includes their bill balance and tax liability. Write a program to produce the outstanding balance report...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • The goal of this homework is to write a small database system for an Animal Shelter....

    The goal of this homework is to write a small database system for an Animal Shelter. This is a no-kill shelter like the one just west of the Addition Airport. You will accept animal “donations” to the shelter, but mostly only dogs and cats. (But yes, there may be the occasional hamster or other nondog, non-cat animal donation.) At present, all we need to do is write the skeleton of a database that will track all the animals in the...

  • This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...

    This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...

  • Hi this program must be completed using a Eclipes Compiler for Java only . This is...

    Hi this program must be completed using a Eclipes Compiler for Java only . This is and intro to java class , so only intro methods should be used for this assignment. Please include a copy of the input data and a sample of the out put data . Thanks a bunch Homework-Topic 9-Donations Write a complete program to do the following: The main program calls a method to read in (Erom an input file) a set of people's three-digit...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

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
ADVERTISEMENT