Question

Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

Suppose we have the following Java Interface:

public interface Measurable {   

double getMeasure(); // An abstract method   

static double average(Measurable[] objects) { // A static method   

double sum = 0;   

for (Measurable obj : objects) {   

sum = sum + obj.getMeasure();   

}   
if (objects.length > 0) { return sum / objects.length; }   

else { return 0; }   
}
}
Write a class, called Person, that has two instance variables, name (as String) and age (as double), that implements Measurable interface. Implement required method from the Measurable interface, such that it returns the age of the person.
Then complete the following tester class:
public class MeasurableTester {   

public static void main(String[] args) {   

// Calling the static average method with an array of Person objects   

Measurable[] persons = new ____________[3];   

persons[0] = new _________(10);   

persons[1] = new _________(25);   

persons[2] = new _________(13);   

double averageAge = _____________________________;   

System.out.println("Average ages: " + ______________); }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Measurable.java
public interface Measurable {

  double getMeasure(); // An abstract method
  static double average(Measurable[] objects) { // A static method

    double sum = 0;

    for (Measurable obj : objects) {

      sum = sum + obj.getMeasure();

    }
    if (objects.length > 0) { return sum / objects.length; }

    else { return 0; }
  }
}

==========================================

//Person.java
public class Person implements Measurable {
  String name;
  public double age;

  public Person(double age) {
    this.age = age;
  }


  public double getMeasure() {
    return age;
  }
}

=====================================

//MeasurableTester.java
public class MeasurableTester {

  public static void main(String[] args) {

// Calling the static average method with an array of Person objects

    Measurable[] persons = new Person[3];

    persons[0] = new Person(10);

    persons[1] = new Person(25);

    persons[2] = new Person(13);

    double averageAge = Measurable.average(persons);

    System.out.println("Average ages: " + averageAge);
  }

}

Average ages: 16.0

Add a comment
Know the answer?
Add Answer to:
Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public...

    Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public void setImmune(); public boolean isImmune(); public Point getPosition(); } Movable.java interface Movable { public void move(int step); } public class Point { private int xCoordinate; private int yCoordinate; /** * Creates a point at the origin (0,0) and colour set to black */ public Point(){ xCoordinate = 0; yCoordinate = 0; } /** * Creates a new point at a given coordinate and colour...

  • I Have a problem with my JAVA class "Directivo". In the " public double sueldo() "...

    I Have a problem with my JAVA class "Directivo". In the " public double sueldo() " method, the instruction say "Calculate the salary of a Directivo the following way: Invoke method salary of the father and add him the extra bonus" My question is : How can I do this ? how can i implement that instruction in the public double sueldo() method public class Directivo extends Planta implements Administrativo{    private double bonoExtra;    public Directivo( String nom, String...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • Treat.java HairballRemedy.java ScoobyTreat.java Munchies.java public interface Treat { String getColor(); String getSize(); } public class HairballRemedy...

    Treat.java HairballRemedy.java ScoobyTreat.java Munchies.java public interface Treat { String getColor(); String getSize(); } public class HairballRemedy implements Treat { public String getColor() { return "blue"; } public String getSize() { return "0.5 oz"; } } public class ScoobyTreat implements Treat { public String getColor() { return "green"; } public String getSize() { return "2 oz"; } } public class Munchies { public static void main(String[] args) { HairballRemedy a=new HairballRemedy(); ScoobyTreat b=new ScoobyTreat(); Treat c=a; Treat d=b; System.out.println(a.getColor()); System.out.println(b.getColor()); System.out.println(c.getColor());...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • m The interface Measurable is defined as the following: /** An interface for methods that return...

    m The interface Measurable is defined as the following: /** An interface for methods that return the perimeter and area of an object. */ public interface Measurable { public double getPerimeter(); public double getArea(); } The class Rectangle implements the interface. The incomplete program is written as follows: 1 public class Rectangle 2 { private double width; private double height; public Rectangle(double w, double h) { width = w; height h; } { 3 4 5 6 7 8 9...

  • In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate...

    In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate the pension to date as the equivalent of a monthly salary for every year in service. 4) Print the calculated pension for all employees. ************** I added the interface and I implemented the interface with Employee class but, I don't know how can i call the method in main class ************ import java.io.*; public class ManagerTest { public static void main(String[] args) { InputStreamReader...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT