1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
prohojiy [21]
3 years ago
15

Consider an interface p ublic interface NumberFormatter { String format (in n); } Provide four classes that implement this inter

face. A DefaultFormatter formats an integer in the usual way. A DecimalSeparatorFormatter formats an integer with decimal separators ; for example, one million as 1,000,000. An AccountingFor matter formats negative numbers with parenthesis; for example, - 1 as (1). A BaseFormatter formats the number as base n, where n is any number between 2 and 36 that is provided in the constructor.
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. Provide a test class th at shows the program working with an array of numbers.
Computers and Technology
1 answer:
sesenic [268]3 years ago
3 0

Answer and Explanation:

Here is the code for the question with output.

public interface NumberFormatter {

  public String format(int n);

}

class DefaultFormatter implements NumberFormatter

{

  public String format(int n)

  {

      return String.valueOf(n);

  }

}

class DecimalFormatter implements NumberFormatter

{

  public String format(int n)

  {

      return String.format("%,d",n); //formats the number by putting comma for 3 digits

  }

}

class AccountingFormatter implements NumberFormatter

{

  public String format(int n)

  {

      return String.format("(%d)",n);

  }

}

class BaseFormatter implements NumberFormatter

{

  int base;

 

  public BaseFormatter(int b)

  {

         

      base = b;

      if(base < 2 || base > 36) //if values out of range 2-36 inclusive are given, set default to 2

          base = 2;

  }

  public String format(int n)

  {

      return Integer.toString(n, base);      

  }

  int getBase()

  {

      return base;

  }

}

public class TestNumberFormatter {

  private static void print(int n, NumberFormatter formatter)

  {

      String s = String.format("%15s",formatter.format(n));

      System.out.print(s);

  }

  public static void main(String[] args) {

      int numbers[]= {100,55555,1000000,35,5};

      DefaultFormatter defFmt = new DefaultFormatter();

      DecimalFormatter deciFmt = new DecimalFormatter();

      AccountingFormatter accFmt = new AccountingFormatter();

      BaseFormatter baseFmt=new BaseFormatter(16) ; //base 16

      String s;

     

      System.out.println("\tDefault \t Decimal \tAccounting \t Base("+baseFmt.getBase()+")");

      for(int i = 0; i < numbers.length; i++)

      {

          print(numbers[i], defFmt);

          print(numbers[i], deciFmt);

          print(numbers[i], accFmt);

          print(numbers[i], baseFmt);

          System.out.println();//new line

      }

     

  }

}

output

  Default    Decimal    Accounting    Base(16)

100 100 (100) 64

55555 55,555 (55555) d903

1000000 1,000,000 (1000000) f4240

  35 35 (35) 23

5 5 (5) 5

You might be interested in
Please help This is a homework in ICT class
Pavlova-9 [17]

Answer:

ejwksjnsskawii28393939393030jedjdjjccjncncnccnnccn

Explanation:

jsskaaaahujpjbhdueiwiwopwpwpwwieuehbfcnnccjxkxooss8ahwhwwnekdodod8djhdmslwoaishwhwkqoq18829293i4644673819191jednsmzljesksowlwlwpwpwnwkpwwwww waaaaayushsjsnmssoss ss sosijw

8 0
3 years ago
What hand glove is used to prevent electric shock and why​
Ostrovityanka [42]

Answer:

Insulated gloves because their made of synthetic rubber.

Explanation:

7 0
3 years ago
Read 2 more answers
What is one difference between the Simple Gear Train with Same Size Gears and Simple Gear Train with Different Size Gears?
polet [3.4K]

Answer:

One difference between a Simple Gear train with Same Size Gears and Simple Gear Train with Different Size Gears is the that the number of rotations of both shafts in the Simple Gear Train with Same Size Gears is the same the number of rotations of both shafts in the simple Gear Train with Different Size Gears are different

Explanation:

A simple gear train is one in which each shaft has only one shaft, and the motion of one shaft in transmitted to the other when the gears are in contact

5 0
3 years ago
Which of the following data types can hold a fractional (decimal) number?
zvonat [6]
A float data type can hold a decimal number.
6 0
2 years ago
Complete the statement below with the correct term.
nikitadnepr [17]

Answer:

DOMAIN

Explanation:

A utility that provides names to each computer on a network is called a DOMAIN naming service.

4 0
3 years ago
Other questions:
  • Write a function max arguments. write a program that reads three floating-point numbers, uses the max function, and displays the
    5·1 answer
  • Name three types of data stored on a computer’s hard disk
    11·1 answer
  • Which of the following is not a benefit of normalization?
    5·1 answer
  • What is most important for you to choose before you build a network?
    6·1 answer
  • Which are two main areas of the properties inspector
    7·1 answer
  • What will be the results of executing the following statements? x.setEditable(true); x.setText("Tiny Tim"); a. The text field x
    7·1 answer
  • 7.5 Code practice Plz answer ASAP
    15·1 answer
  • How much would it cost to get the screen replaced on a Moto G7?
    10·1 answer
  • Which of the following terms means the computer operating system automatically detects and installs the proper driver for a new
    9·1 answer
  • The three main objectives of information security are.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!