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

Java help! Write and test a Java project that satisfies the following requiremen

ID: 3858439 • Letter: J

Question


Java help!

Write and test a Java project that satisfies the following requirements:

Create a class definition file and a test file as follows:

Create a class to hold and manipulate data for a Pet for a pet adoption service. The class will have these fields:
        pet type (a String), pet name (a String), cost to adopt(a double)

Write the following methods for the Pet class:

A default (no-args) constructor
A constructor with parameter values for all the field
A toString( ) method that uses all the fields.
An equals( ) method that is based on pet type and pet name
A compareTo( ) method that is based on the cost to adopt

Once the Pet class compiles write a Driver class with the main( ) method.

The main( ) method will declare three Pet reference variables. It will then create the Pet objects reading the data from the file named rescue.txt. The file is comma delimited. (use the StringTokenizer class

After the objects have been created print the state of the three Pet objects using the toString( ) method.

Test the equals( ) method using the first Pet object and the second Pet object. Test the compareTo( ) method by using the first Pet object and the third Pet object. Print the results of these two tests.

(The text file is saved as rescue.txt and the file reads as:

Lab dog,Charley,65.00
Tabby cat,Honey,45.00
Collie dog,Susie,65.00)

Explanation / Answer

Please find the required solution with sample output: import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.StringTokenizer; /* * Driver class to read pets from file and test the equals and compare methods */ public class Driver { public static void main( String[] args ) throws IOException { // give absolute path of rescue method, otherwise you an exception will be thrown Scanner fileIn = new Scanner(new File("rescue.txt")); //for each line initialize pets Pet pet1 = null, pet2 = null, pet3 = null; if( fileIn.hasNextLine() ) pet1 = getPet(new StringTokenizer(fileIn.nextLine(), ",")); if( fileIn.hasNextLine() ) pet2 = getPet(new StringTokenizer(fileIn.nextLine(), ",")); if( fileIn.hasNextLine() ) pet3 = getPet(new StringTokenizer(fileIn.nextLine(), ",")); fileIn.close(); // test toString methods equals and compareTo methods System.out.println(pet1); System.out.println(pet2); System.out.println(pet3); System.out.println("pet1 and pet2 with equals method:" + pet1.equals(pet2)); System.out.println("pet1 and pet3 with compare method:" + pet1.compareTo(pet3)); } // util method to split initialise static Pet getPet( StringTokenizer tokenizer ) { Pet pet = new Pet(); if( tokenizer.hasMoreTokens() ) pet.petName = tokenizer.nextToken(); if( tokenizer.hasMoreTokens() ) pet.petType = tokenizer.nextToken(); if( tokenizer.hasMoreTokens() ) pet.costToAdopt = Double.valueOf(tokenizer.nextToken()); return pet; } } //Pet class class Pet { //instance variables String petType; String petName; Double costToAdopt; // default constructor public Pet() { } //parameterized constructor public Pet( String petType, String petName, Double costToAdopt ) { this.petType = petType; this.petName = petName; this.costToAdopt = costToAdopt; } //overridden to string method @Override public String toString() { return "Pet{" + "petType='" + petType + ''' + ", petName='" + petName + ''' + ", costToAdopt=" + costToAdopt + '}'; } // equals method based on petType and petName @Override public boolean equals( Object o ) { if( this == o ) return true; if( o == null || getClass() != o.getClass() ) return false; Pet pet = (Pet) o; if( petType != null ? !petType.equals(pet.petType) : pet.petType != null ) return false; return petName != null ? petName.equals(pet.petName) : pet.petName == null; } // compareTo method to compare base on costToAdopt public int compareTo( Pet o ) { return o == null || o.costToAdopt == null || this.costToAdopt == null ? 0 : this.costToAdopt.compareTo(o.costToAdopt); } } Sample Output: Pet{petType='Charley', petName='Lab dog', costToAdopt=65.0} Pet{petType='Honey', petName='Tabby cat', costToAdopt=45.0} Pet{petType='Susie', petName='Collie dog', costToAdopt=65.0} pet1 and pet2 with equals method:false pet1 and pet3 with compare method:0

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