Driver: /* * To change this license header, choose License Headers in Project Pr
ID: 3707720 • Letter: D
Question
Driver:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kerlin
*/
public class Driver
{
/**
* Main method to run our code
* @param args NOT USED
*/
public static void main(String[] args)
{
Orange myFruit = new Orange(14, 4);
System.out.println("Printing My fruit: " + myFruit);
System.out.println("Juicing My fruit: " + myFruit.juice());
Fruit yourFruit = new Fruit(20);
System.out.println("Printing Your fruit: " +yourFruit);
yourFruit.setSeeds(5);
System.out.println("Your fruit should now have 5 seeds: " + yourFruit.getSeeds());
myFruit.setDiameter(10);
System.out.println("My fruit should now be 10 units in diameter: " + myFruit.getDiameter());
System.out.println("My fruit should now juice 5 units: " + myFruit.juice());
}
}
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Fruit.java
==========
public class Fruit {
private int seeds;
public Fruit(int seeds)
{
this.seeds = seeds;
}
public int getSeeds() {
return seeds;
}
public void setSeeds(int seeds) {
this.seeds = seeds;
}
public String toString()
{
return "Fruit with " + seeds + " seeds";
}
}
Juiceable.java
-============
public interface Juiceable {
public int juice();
}
Orange.java
============
public class Orange extends Fruit implements Juiceable {
private int diameter;
public Orange(int seeds) {
super(seeds);
diameter = 4; //default diameter
}
public Orange(int seeds, int dia) {
super(seeds);
diameter = dia;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
@Override
public int juice() {
return (int) Math.ceil(diameter / 4.0 * 2); //2 units of juice for every 4 units of diameter
}
public String toString()
{
return "Orange " + super.toString();
}
}
outupt of Driver.java
=====
Printing My fruit: Orange Fruit with 14 seeds
Juicing My fruit: 2
Printing Your fruit: Fruit with 20 seeds
Your fruit should now have 5 seeds: 5
My fruit should now be 10 units in diameter: 10
My fruit should now juice 5 units: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.