179k views
2 votes
Write and test a Java project that satisfies the following requirements:Create a class definition file and a test driver 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) constructorA constructor with parameter values for all the fieldA toString( ) method that uses all the fields.An equals( ) method that is based on both the pet type and pet nameA compareTo( ) method that is based only on the cost to adoptOnce 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- example is posted on Canvas and was assigned for Activity 5). Use the data read in to send to the Pet constructor.NOTE WELL: The StringTokenizer method nextToken( ) returns a String. To send the cost valueto the Pet constructor you must convert this field to a double. TO do this here is an example: strCost = stok.nextToken( ); doubleCost = Double.parseDouble(strCost);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. Be sure your print statements are clearly labeled so it is obvious what was tested.

1 Answer

2 votes

Answer:

import java.io.*;

import java.util.*;

class Pet {

String type;

String name;

double adoptcost;

Pet() {

this.adoptcost = 0.0;

this.name = "";

this.type = "";

}

Pet(String name, String type, double cost) {

this.adoptcost = cost;

this.name = name;

this.type = type;

}

public String toString() {

return "Name: " + this.name + "\\Type: " + this.type + "\\Cost: " + this.adoptcost + "\\";

}

public boolean equals(Object p) {

if (p == this) {

return true;

}

if (!(p instanceof Pet)) {

return false;

}

Pet a = (Pet) p;

return this.type.equals(a.type) && this.name.equals(a.name);

}

public double compareTo(Pet p) {

if (this.adoptcost == p.adoptcost)

return 0.0;

else

return (this.adoptcost - p.adoptcost);

}

public static void main(String args[]) throws IOException {

BufferedReader fr = new BufferedReader(new FileReader(new File("rescue.txt")));

String line, name, type, tmp;

double cost;

ArrayList<Pet> arr = new ArrayList<>();

while ((line = fr.readLine()) != null) {

StringTokenizer st = new StringTokenizer(line, ",");

name = st.nextToken();

type = st.nextToken();

tmp = st.nextToken();

cost = Double.parseDouble(tmp);

arr.add(new Pet(name, type, cost));

}

for (Pet i : arr) {

System.out.println(i);

}

System.out.println("Equals check for 1st and 2nd pet object: " + arr.get(0).equals(arr.get(1)));

System.out.println("CompareTo check for 1st and 3rd pet object: " + arr.get(0).compareTo(arr.get(2)));

System.out.println("CompareTo check for 1st and 4th pet object: " + arr.get(0).equals(arr.get(3)));

fr.close();

}

}

User Zhar
by
5.5k points