Question

Step One This is the Measurable interface we saw in class; you will need to provide this class; this is it in its entirety (t

Step Three Write a main program that will use your PiggyBank class. It will ask the user for the name of a file. The file wil

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note :We have paste the input file in the project folder so that our program can able to detect.

Could you plz go through this code and let me know if u need any changes in this.Thank You

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

v PiggyBank Measurable Comparable v src v #org.students > Measurable.java > PiggyBank.java > D Test.java EJRE System Library


// bankData.txt (Input file)

4
4 1 0 25
2 10 0 120
3 19 7 8
6 0 5 12

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

// Measurable.java

public interface Measurable {
double getMeasure();
}

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

// PiggyBank.java

import java.text.DecimalFormat;


public class PiggyBank implements Comparable<PiggyBank> {
private int quarters;
private int dimes;
private int nickels;
private int pennies;
  
  
  
   /**
   * @param quarters
   * @param dimes
   * @param nickels
   * @param pennies
   */
   public PiggyBank(int quarters, int dimes, int nickels, int pennies) {
       this.quarters = quarters;
       this.dimes = dimes;
       this.nickels = nickels;
       this.pennies = pennies;
   }


public double getMeasure()
{
   double val= quarters*0.25+dimes*0.10+nickels*0.05+pennies*0.01;
   return val;
}
   @Override
   public int compareTo(PiggyBank pb) {
   if(this.getMeasure()<pb.getMeasure())
   {
       return -1;
   }
   else if(this.getMeasure()>pb.getMeasure())
   {
       return 1;
   }
   else
   {
   return 0;
   }
   }


   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       // DecimalFormat class is used to format the output
               DecimalFormat df = new DecimalFormat(".00");
       return "$"+df.format(getMeasure())+" ("+ quarters + " quarters=, "+ dimes
               + " dimes, " + nickels + " nickels, " + pennies + " pennies)";
   }
  
  

}

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

// Test.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
String filename;
int quarters,dimes,nickels,pennies;
int size;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       ArrayList<PiggyBank> arl=new ArrayList<PiggyBank>();
       System.out.print("Enter the filename :");
       filename=sc.next();
      
       try {
           Scanner infile = new Scanner(new File(filename));
           size=infile.nextInt();
           for(int i=0;i<size;i++)
           {
               quarters=infile.nextInt();
               dimes=infile.nextInt();
               nickels=infile.nextInt();
               pennies=infile.nextInt();
               PiggyBank p=new PiggyBank(quarters, dimes, nickels, pennies);
               arl.add(p);
           }
           infile.close();
           sc.close();
          
           sort(arl);
           for(int i=0;i<size;i++)
           {
               System.out.println(arl.get(i));
           }
          
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
      

   }

   private static void sort(ArrayList<PiggyBank> arl) {
       //This Logic will Sort the Array of elements in Decending order
       PiggyBank temp1;
       for (int i = 0; i < arl.size(); i++)
   {
   for (int j = i + 1; j < arl.size(); j++)
   {
   if (arl.get(i).getMeasure() > arl.get(j).getMeasure())
   {
   temp1 = arl.get(i);
   arl.set(i,arl.get(j));
   arl.set(j,temp1);
   }
   }
   }
      
   }

}

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

Output:

Enter the filename :bankData.txt
$1.35 (4 quarters=, 1 dimes, 0 nickels, 25 pennies)
$1.87 (6 quarters=, 0 dimes, 5 nickels, 12 pennies)
$2.70 (2 quarters=, 10 dimes, 0 nickels, 120 pennies)
$3.08 (3 quarters=, 19 dimes, 7 nickels, 8 pennies)

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Step One This is the Measurable interface we saw in class; you will need to provide...
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
  • implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...

    implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...

  • Write a program called CountCoins.java that prompts the user for the input file name (you can...

    Write a program called CountCoins.java that prompts the user for the input file name (you can copy the getInputScanner() method given in the ProcessFile assignment) then reads the file. The file contains a series of pairs of tokens, where each pair begins with an integer and is followed by the type of coin, which will be “pennies” (1 cent each), “nickels” (5 cents each), “dimes” (10 cents each), or “quarters” (25 cents each), case-insensitively. Add up the cash values of...

  • implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...

    implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...

  • Problem: Implement (in C) the dynamic program algorithm for the coin-change algorithm, discussed in class. Assume...

    Problem: Implement (in C) the dynamic program algorithm for the coin-change algorithm, discussed in class. Assume that the coins with which you make change are quarters, dimes, nickels and pennies. Thus you are going to set n = 4 in your program. The amount k for which you have to make change will be provided by the user and your program will return the minimum number of coins needed and also the break-up of the change in terms of the...

  • Create a data class named Automobile that implements the Comparable interface. Give the class data fields...

    Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by...

  • Visual Basic Programming Step 1-2 not important, it's just file naming.

    Visual Basic Programming Step 1-2 not important, it's just file naming. 3. Form contains nine Labels, one TextBox, and three Button controls. You use labels to let user know what to enter and what will be displayed; TextBoxes to input a number between 1 and 99. Buttons to Calculate change. Clear Input and Exit program. See below Form Layout with Controls for more details 4. Declare variables to store the entered value in TextBox, and what will be displayed in...

  • Visual Basic Programming Step 1-2 not important, it's just file naming. 3. Form contains nine Labels,...

    Visual Basic Programming Step 1-2 not important, it's just file naming. 3. Form contains nine Labels, one TextBox, and three Button controls. You use labels to let user know what to enter and what will be displayed; TextBoxes to input a number between 1 and 99. Buttons to Calculate change. Clear Input and Exit program. See below Form Layout with Controls for more details 4. Declare variables to store the entered value in TextBox, and what will be displayed in...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

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