Question

In: Computer Science

This activity will require you to apply your knowledge of interfaces – including the Comparable interface...

This activity will require you to apply your knowledge of interfaces – including the Comparable interface – in order to leverage the capabilities of the ArrayList data structure.

---------

You may work individually or with a partner, but be sure that each student submits their own assignment in Canvas (and leave a comment with your partner's name if you worked with one).

---------

Description:

Interfaces are used to make a Class implement a behavior or set of behaviors. Recall the example from class in which the Gradable Interface was used to ensure that the calculateGrade() and getAbsences() methods were implemented by the Student class.

Your goal for this assignment is to create your own example of an Interface and a Class that implements it. You may not use Gradable / Student or Drivable / Vehicle.

  • Decide on the Interface that you will be creating. It should include at least three methods and one attribute, and represent a type of behavior. Please use the convention of ending your Interface name in “able” (e.g. drivable). Create this interface in its own file as shown in class.
  • Decide on a Class that will implement this interface. In addition to implementing the Interface that you created, you should implement the Comparable interface for your class (as demonstrated in class). Override the compareTo() method in a manner that makes sense for your Class. Also, override the toString() method in a way that displays all relevant information for your Class.  Note that your methods don't need to be exceedingly complex -- just make sure that they do something that makes sense in the context of your class and interface.
  • In your main method, create an ArrayList containing 5 instances of your Class. You may hard-code the values for your class attributes in your main method. Sort your ArrayList as demonstrated in class, and display the contents using the enhanced for-loop syntax and overridden toString() method that you created.

---------

You are welcome to use either VSCode or NetBeans to complete this assignment. If you use VSCode, upload your Java files. If you use NetBeans, export your project to a ZIP file and upload that. Please do not upload any compression formats other than ZIP files. Other formats (e.g. 7zip, tar.gz, etc.) will not be accepted and will result in a grade of zero.

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU



Growable.java

interface Growable
{
   final double DEFAULT_FACTOR=1;
   final double DEFAULT_VALUE=0;
   final double DEFAULT_PERCENT=100;
   public void growByFactor(double factor);
   public void growByValue(double value);
   public void growByPercent(double percent);
}

Fortune.java

class Fortune implements Growable,Comparable<Fortune>
{
   double balance;
   Fortune(double balance)
   {
       this.balance = balance;
   }
  
   public void growByFactor(double factor)
   {
       balance = balance * factor;
   }
  
   public void growByValue(double value)
   {
       balance = balance + value;
   }
  
   public void growByPercent(double percent)
   {
       balance = balance * percent/100.0;
   }
  
   public int compareTo(Fortune obj)
   {
       if (balance<obj.balance) return -1;
       else if (balance>obj.balance) return 1;
       return 0;
   }
  
   public String toString()
   {
       return "Fortune : $"+String.valueOf(balance);
   }  
}

TestFortune.java

import java.util.ArrayList;
import java.util.Collections;
class TestFortune
{
   public static void main(String[] args)
   {
       ArrayList<Fortune> persons = new ArrayList<Fortune>(5);
       Fortune obj;
      
       //Creating 5 objects
       obj = new Fortune(3000.0);
       //obj.growByValue(1000); //will become 4000
       persons.add(obj);
      
       obj = new Fortune(15000.0);
       persons.add(obj);
      
       obj = new Fortune(12000.0);
       //obj.growByFactor(1.50); //will become 18000
       persons.add(obj);
      
       obj = new Fortune(9000.0);
       persons.add(obj);
      
       obj = new Fortune(7500.0);
       //obj.growByPercent(Growable.DEFAULT_PERCENT); //will remain 7500
       persons.add(obj);
      
      
       //Sorting the Arraylist
       Collections.sort(persons);
      
       //Printing the contents of ArrayList
      
       for (Fortune y : persons)
       {
           System.out.println(y.toString());
       }
      
   }
}

Related Solutions

For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method,...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class. The exercises, for those without the text: 19. Modify the Point class from Chapter 8 so that it defines...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method,...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class. The exercises, for those without the text: 19. Modify the Point class from Chapter 8 so that it defines...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer for all of them please. answer for all of them please
In this assignment you will apply your knowledge and understanding of the AD/AS model to a...
In this assignment you will apply your knowledge and understanding of the AD/AS model to a hypothetical scenario. Write a response to the following scenario on a word file or equivalent and submit it as an 'upload file'. Please note you can only submit the following file types, doc, docx, pdf, txt, png, and jpeg. Graphs can be hand-drawn or computer graphics. Remember to correctly label all your graphs! For example, what the x and y axis represent. The COVID-19...
Objective: In this assignment you will apply your knowledge and understanding of the AD/AS model to...
Objective: In this assignment you will apply your knowledge and understanding of the AD/AS model to a hypothetical scenario. Instruction: Write a response to the following scenario on a word file or equivalent and submit it as an 'upload file'. Please note you can only submit the following file types, doc, docx, pdf, txt, png, and jpeg. Graphs can be hand-drawn or computer graphics. Remember to correctly label all your graphs! For example, what the x and y axis represent....
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
In this assignment you will apply your economics knowledge to explain and interpret a recently published...
In this assignment you will apply your economics knowledge to explain and interpret a recently published economics article. You will research and select one article published within the past year that discusses an economics issue from the list provided below and explain the economics of the article. The article may be obtained from a major business or economics journal, magazine or newspaper (The Economist, Wall Street Journal, Businessweek, Times, Fortune, etc.). Using your own words as much as possible, you...
Objective: In this assignment, you will apply your knowledge and understanding of poverty and income inequality...
Objective: In this assignment, you will apply your knowledge and understanding of poverty and income inequality to real world issues. I nstruction: Watch and read the following materials before you write your response: The Costs of Inequality - Joseph Stiglitz (Links to an external site.) Three main philosophies of redistribution of income: Utilitarianism: the government should chose policies that maximize the total utility of everyone in society. For example, giving $100 to a homeless person would have a bigger impact...
Describe how you will apply your acquired knowledge out of psychology class to your patients when...
Describe how you will apply your acquired knowledge out of psychology class to your patients when you start working in your medical field of your choice? My medical field of choice is Associates of Applied Science Medical Assistant.
Blog Activity Identify the major themes presented in the reading Apply knowledge gained from the article...
Blog Activity Identify the major themes presented in the reading Apply knowledge gained from the article to project on your development as a marketing professional In this activity, you are expected to write a thoughtful response of at least 250 words incorporating the following two questions: In what ways is the material covered in this article relevant to the principles of marketing? In what ways can you utilize the ideas and tools covered in this reading in your development as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT