Answered step by step
Verified Expert Solution
Question
1 Approved Answer
COMP: (Please use java) For the Fruit java class and the TropicalFruit java class, write a test class called FruitTest and TropicalFruitTest that tests the
COMP: (Please use java) For the Fruit java class and the TropicalFruit java class, write a test class called FruitTest and TropicalFruitTest that tests the method toString() in each. Ensure you test it with three different inputs. Submit only the test java files. Make sure you test all boundary conditions. (Below are the codes for each)
/** * Write a description of class Fruits here. * * @author (your name) * @version (a version number or a date) */ public class Fruit { String fruitName; String color; public Fruit () { fruitName=""; color=""; } public Fruit(String name, String c) { fruitName=name; color=c; } public String toString() { return("The fruit "+fruitName+" is of color "+color); } }
--------------------------------------------------------------------------------------------------------------
public class TropicalFruit extends Fruit { double price; public TropicalFruit() { super(); price=0; } public TropicalFruit(String name,String c, double p) { super(name,c); price=p; } public String toString() { return (super.toString()+" and it costs "+price+" dollars"); } public static void main(String[] args) { Fruit f=new Fruit("apple","red"); TropicalFruit tf = new TropicalFruit("mango", "yellow", 3.2); Fruit fr = tf; // polymorphism tf = (TropicalFruit)f; // ok to have parent class on the left of assignment and subclass on the right // of assignment. Syntax error the other way around System.out.println(f.toString()); System.out.println(tf.toString()); System.out.println(fr.toString()); // the last print statement can either call Fruit's toString or // TropicalFruit's toString() method. // fr is a Fruit reference //but fr is referencing a TropicalFruit object // is the reference going to be the decider for the method call // or the object????? /// Here the object method will be the one to be called // this is called dynamic binding } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started