JAVA :ECLIPSE PROGRAM: Sophie has decided to open an animal washing service. She
ID: 3673625 • Letter: J
Question
JAVA :ECLIPSE PROGRAM:
Sophie has decided to open an animal washing service. She will bathe a dog for $0.50. Cats will be bathed for $3.00 per pound.
Write a class CashBox that records the total number of washes and amount of money Sophie has taken in.
Write a test class for CashBox. Your tester should include several cats and dogs and test that CashBox correctly reports the total washes and amount of money taken in.
Your solution should include at least four classes/interfaces. Follow all Java naming conventions, and use appropriate access control.
Your solution should include at least 3 of the following. Note that your use of these features should be legitimate, but if they're a bit contrived, that's ok.
superclass
abstract class
interface
generic class
generic method
JUnit test
Explanation / Answer
program:
import java.io.*;
interface Animal
{
public double getWashRate();
}
class Cat implements Animal
{
public final double rate=3.0;
public double getWashRate()
{
return rate;
}
}
class Dog implements Animal
{
public final double rate=0.5;
public double getWashRate()
{
return rate;
}
}
class CashBox
{
double total;
CashBox()
{
total=0;
}
public void addCash(Animal a)
{
total=total+a.getWashRate();
}
public double getTotal()
{
return total;
}
}
public class TestCashBox {
public static void main(String[] args)throws IOException
{
CashBox c=new CashBox();
Animal d1=new Dog();
c.addCash(d1);
Animal c1=new Cat();
c.addCash(c1);
Animal d2=new Dog();
c.addCash(d2);
Animal c2=new Cat();
c.addCash(c2);
System.out.print("total cash is:$"+c.getTotal());
}
}
output:
total cash is:$7.0BUILD SUCCESSFUL (total time: 1 second)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.