In: Computer Science
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.
---------
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.
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());
}
}
}