Question
Write a method that takes an array of integers and a NumberFormatter object and prints each number on a separate line, formatted with the given
Write a method that takes an array of integers and a NumberFormatter object and prints each number on a separate line, formatted with the given formatter. The numbers should be right aligned.
CLASS
NumberFormatter.java
public interface NumberFormatter { public String format(int n); }
CLASS
DecimalSeparatorFormatter.java
// Declare a class. class DecimalSeparatorFormatter implements NumberFormatter { // Method of the class public String format(int n) { return String.format("%,d", n); } }
CLASS
AccountingFormatter.java
class AccountingFormatter implements NumberFormatter { // Method of the class public String format(int n) { if (n < 0) { return String.format("(%d)", 0 - n); } return String.format("%d", n); } }
CLASS
BaseFormatter.java
// Declare a class. class BaseFormatter implements NumberFormatter { int base; // Constructor of the class. public BaseFormatter(int b) { base = b; // check the condition. if (base < 2 || base > 36) base = 2; } // Method of the class public String format(int n) { // Return value. return Integer.toString(n, base); } }
CLASS
NumberFormatterTester.java
// Declare a class. class DefaultFormatter implements NumberFormatter { // Method of the class public String format(int n) { return String.valueOf(n); } } public class NumberFormatterTester { public static void main(String[] args) { final int small = 5; final int neg = -10000; final int large = 1000000; final int maxBase = 36; NumberFormatter numFormat; numFormat = new DefaultFormatter(); System.out.println("Default: " + numFormat.format(small) + " " + numFormat.format(neg) + " " + numFormat.format(large) + " " + numFormat.format(maxBase)); System.out.println("Expected: 5 -10000 1000000 36"); numFormat = new DecimalSeparatorFormatter(); System.out.println("Decimal: " + numFormat.format(small) + " " + numFormat.format(neg) + " " + numFormat.format(large) + " " + numFormat.format(maxBase)); System.out.println("Expected: 5 -10,000 1,000,000 36"); numFormat = new AccountingFormatter(); System.out.println("Accounting: " + numFormat.format(small) + " " + numFormat.format(neg) + " " + numFormat.format(large) + " " + numFormat.format(maxBase)); System.out.println("Expected: 5 (10000) 1000000 36"); numFormat = new BaseFormatter(2); // Binary System System.out.println("Base 2 : " + numFormat.format(small) + " " + numFormat.format(neg) + " " + numFormat.format(large) + " " + numFormat.format(maxBase)); System.out.println("Expected: 101 -10011100010000 11110100001001000000 100100"); numFormat = new BaseFormatter(8); // Octal System System.out.println("Base 8 : " + numFormat.format(small) + " " + numFormat.format(neg) + " " + numFormat.format(large) + " " + numFormat.format(maxBase)); System.out.println("Expected: 5 -23420 3641100 44"); numFormat = new BaseFormatter(20); // Base 20 System.out.println("Base 20 : " + numFormat.format(small) + " " + numFormat.format(neg) + " " + numFormat.format(large) + " " + numFormat.format(maxBase)); System.out.println("Expected: 5 -1500 65000 1g"); numFormat = new BaseFormatter(36); // Base 36 System.out.println("Base 36 : " + numFormat.format(small) + " " + numFormat.format(neg) + " " + numFormat.format(large) + " " + numFormat.format(maxBase)); System.out.println("Expected: 5 -7ps lfls 10"); } }
JUNIT Test
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class NumberFormatterTests { @Test public void AccountingFormatterTests() { AccountingFormatter af = new AccountingFormatter(); assertEquals("1",af.format(1)); assertEquals("(1)",af.format(-1)); assertEquals("1234",af.format(1234)); assertEquals("(1234)",af.format(-1234)); assertEquals("1234567",af.format(1234567)); assertEquals("(1234567)",af.format(-1234567)); } @Test public void BaseFormatterTests() { // Test base 2 BaseFormatter bf2 = new BaseFormatter(2); assertEquals("10",bf2.format(2)); assertEquals("-10",bf2.format(-2)); assertEquals("100",bf2.format(4)); assertEquals("111",bf2.format(7)); assertEquals("111111",bf2.format(63)); assertEquals("11111000000",bf2.format(1984)); assertEquals("-11111000000",bf2.format(-1984)); // Test Base 8 BaseFormatter bf8 = new BaseFormatter(8); assertEquals("2",bf8.format(2)); assertEquals("-2",bf8.format(-2)); assertEquals("77",bf8.format(63)); assertEquals("-77",bf8.format(-63)); assertEquals("3700",bf8.format(1984)); assertEquals("-3700",bf8.format(-1984)); assertEquals("3745",bf8.format(2021)); assertEquals("23417",bf8.format(9999)); // Test Base 16 BaseFormatter bf16 = new BaseFormatter(16); assertEquals("2",bf16.format(2)); assertEquals("-2",bf16.format(-2)); assertEquals("3f",bf16.format(63)); assertEquals("-3f",bf16.format(-63)); assertEquals("7c0",bf16.format(1984)); assertEquals("-7c0",bf16.format(-1984)); assertEquals("7e5",bf16.format(2021)); assertEquals("270f",bf16.format(9999)); // Test Base 32 BaseFormatter bf32 = new BaseFormatter(32); assertEquals("2",bf32.format(2)); assertEquals("-2",bf32.format(-2)); assertEquals("1v",bf32.format(63)); assertEquals("-1v",bf32.format(-63)); assertEquals("1u0",bf32.format(1984)); assertEquals("-1u0",bf32.format(-1984)); assertEquals("1v5",bf32.format(2021)); assertEquals("9of",bf32.format(9999)); } @Test public void DecimalSeparatorTests() { DecimalSeparatorFormatter dsf = new DecimalSeparatorFormatter(); assertEquals("123",dsf.format(123)); assertEquals("1,234",dsf.format(1234)); assertEquals("2,021",dsf.format(2021)); assertEquals("-1,234",dsf.format(-1234)); assertEquals("12,345",dsf.format(12345)); assertEquals("123,456",dsf.format(123456)); } @Test public void DefaultFormatter() { DefaultFormatter df = new DefaultFormatter(); assertEquals("123",df.format(123)); assertEquals("1234",df.format(1234)); assertEquals("12345",df.format(12345)); assertEquals("2021",df.format(2021)); assertEquals("-2021",df.format(-2021)); assertEquals("-42",df.format(-42)); } }
Sample Run:
public static void main(String [] args) { int [] test1 = {1, -20, 42, 123, 1234, -1234,2021,12345,-12345}; AccountingFormatter af = new AccountingFormatter(); BaseFormatter bf2 = new BaseFormatter(2); BaseFormatter bf16 = new BaseFormatter(16); DecimalSeparatorFormatter dsf = new DecimalSeparatorFormatter(); DefaultFormatter df = new DefaultFormatter();
System.out.println("AccountingFormatter"); formatInts(test1,af); System.out.println("------------------------------------");
System.out.println("BaseFormatter - base 2"); formatInts(test1,bf2); System.out.println("------------------------------------");
System.out.println("BaseFormatter - base 16"); formatInts(test1,bf16); System.out.println("------------------------------------");
System.out.println("DecimalSeparatorFormatter"); formatInts(test1,dsf); System.out.println("------------------------------------");
System.out.println("DefaultFormatter"); formatInts(test1,df); System.out.println("------------------------------------"); }
When run produces
AccountingFormatter 1 (20) 42 123 1234 (1234) 2021 12345 (12345) ------------------------------------ BaseFormatter - base 2 1 -10100 101010 1111011 10011010010 -10011010010 11111100101 11000000111001 -11000000111001 ------------------------------------ BaseFormatter - base 16 1 -14 2a 7b 4d2 -4d2 7e5 3039 -3039 ------------------------------------ DecimalSeparatorFormatter 1 -20 42 123 1,234 -1,234 2,021 12,345 -12,345 ------------------------------------ DefaultFormatter 1 -20 42 123 1234 -1234 2021 12345 -12345 ------------------------------------
Comment blocks are appreciated.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started