
Write a JAVA program that implements the NumberFormatter interface.
public interface NumberFormatter {
String format(int n);
}
Include three classes that implement this interface. The DefaultFormatter class formats an integer in the usual way. The DecimalFormatter formats an integer with decimal separators (a comma). The AccountingFormatter formats negative values by surrounding the value with parentheses (not a minus sign).
The main method should instantiate an instance of each of the formatters and use it to format a large value (e.g., 2 million), a negative value (e.g., minus 1365), and a small positive integer (e.g., 256). The results should be right aligned and look like the example below.
For example, using the above values, the DefaultFormatter would result in:
2000000
-1365
256
The DecimalFormatter would result in:
2,000,000
-1,365
256
The AccountingFormatter would result in:
2000000
(1365)
256

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

- An incomplete program named w_pl java, which must include three methods, is provided. You must implement two methods within the program as described below. The third method, which is a main method, is already written in the program and you can use it to test your program. The two methods you mast inplement are: • find method • Signature: public statie vold find(int[] a, int x) • Behavior: • Reccives an array of integers a and an integer x • Performs a linear search on a for x. • If x is in a, print the indexes of the aray slots where x is stored (there may be multiple x's in a) on the screen. • Ifx is not in a, print the message "x does not exist" on the screen. Note that you should not use Java's built-in method. Your program must scan and search the array. • isPrifie method • Signature: public statie boolean isPrefix(String s1, String s2) • Behavior: • Receives two strings sl and s2. Assume that length of sl length of s2. Returns true if sl is a prefix of s2 and returns false…arrow_forwardThis is in Java. The assignement is to create a class. I am confused on how to write de code for an array that is up to 5. Also I am confused regarding how to meet the requirements of the constructor. This is what I have. public class InventoryOnShelf { //fields private int[] itemOnShelfList []; private int size; public InventoryOnShelf() { }arrow_forwardWrite a java class method named capitalizeWords that takes one parameter of type String. The method should print its parameter, but with the first letter of each word capitalized. For example, the call capitalizeWords ("abstract window toolkit"); will produce the following output: Abstract Window Toolkitarrow_forward
- Java provides a mechanism that enables a program to be executed from the command line, as an application. Java runtime execution works on the basis that the class executed must have a: (constructor? inner class? method?) named `main’ that is: (final? static? local?) , public and void. This should take an : (StringBuffer? ArrayList? Array?) of Strings as its only argument. Execution is begun by (calling this method? clicking the BlueJ Run button? selecting this method from the dropdown menu?) . Select the correct answer.arrow_forwardJava Problem Create an object of type FitnessExperiment that stores an array ofStepsFitnessTracker, DistanceFitnessTracker, andHeartRateFitnessTracker objects. In the FitnessExperiment class Add to your FitnessExperimentclass a method called getTotalDistance() that calculates the total distance walked by all theobjects stored in your FitnessExperiment. appropriately both values (total number of steps and total distance walked in that experiment) usingthe printExperimentDetails() method. FitnessExperiemnt.java public class FitnessExperiment {FitnessTracker[] fitnessTrackers;public static void main(String[] args) {FitnessTracker[] trackers ={ new StepsFitnessTracker("steps", new Steps(230)),new StepsFitnessTracker("steps2", new Steps(150)),new StepsFitnessTracker("steps2", new Steps(150)),new HeartRateFitnessTracker("hr", new HeartRate(80)),new HeartRateFitnessTracker("hr", new HeartRate(80)),new DistanceFitnessTracker("dist1", new Distance(1000)),new DistanceFitnessTracker("dist2",…arrow_forwardWrite all the code within the main method in the TestTruck class below. Implement the following functionality. Construct two Truck objects: one with any make and model you choose and the second object with gas tank capacity 10. If an exception occurs, print the stack trace. Print both Truck objects that were constructed. import java.lang.IllegalArgumentException ; public class TestTruck { public static void main( String[] args ) { // write your code herearrow_forward
- Write a java class method named countOccurences that returns the number of times a particular integer occurs in an array of integers. The integer and the array will both be parameters.arrow_forwardWrite in JAVAarrow_forwardComputer Programming Java Write a method named FirstHalf that receives a String parameter named word and then returns the first half of the string. You are guaranteed as a precondition that word has an even length greater than 1.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





