Question

In: Computer Science

This question concerns the construction of a NumberUtils class declaration that contains a collection of useful...

This question concerns the construction of a NumberUtils class declaration that contains a collection of useful routines. Write a class declaration that satisfies the following specification: Class NumberUtils The NumberUtils class contains a collection of routines for working with integers. Instance variables None Constructors private NumberUtils() {} // A private, empty-bodied constructor prevents NumberUtil objects from being created. Methods public static int[] toArray(int number) // Given a number that is n digits in length, maps the digits to an array length n. // e.g. given the number 5678, the result is the array {5, 6, 7, 8}. public static int countMatches(int numberA, int numberB) // Given two numbers, count the quantity of matching digits – those with the same value and // position. For example, given 39628 and 79324, there are 2 digits in common: x9xx2x. // It is assumed that the numbers are the same length and have no repeating digits. public static int countIntersect(int numberA, int numberB) // Count the quantity of digits that two numbers have in common, regardless of position. // For example, given 39628 and 97324, there are 3 digits in common: 3, 7, 2. // It is assumed that the numbers are the same length and have no repeating digits. You should make a simple test program (which you do not need to submit) to check your code.

Solutions

Expert Solution

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

class NumberUtils {

  private NumberUtils() {
    //preventing object creation
  }

  public static int[] toArray(int number) {
    String num = String.valueOf(number);
    int length = num.length();
    int arr[] = new int[length];
    for (int i = 0; i < length; i++) {
      arr[i] = Integer.parseInt(Character.toString(num.charAt(i)));
    }
    return arr;
  }

  public static int countMatches(int numberA, int numberB) {
    String n1 = String.valueOf(numberA);
    String n2 = String.valueOf(numberB);
    int len = n1.length(), count = 0; // as mentioned, length of both is same. we can take length of any
    for (int i = 0; i < len; i++) {
      if (n1.charAt(i) == n2.charAt(i)) {
        count++;
      }
    }
    return count;
  }

  public static int countIntersect(int numberA, int numberB) {
    String n1 = String.valueOf(numberA);
    String n2 = String.valueOf(numberB);
    int len = n1.length(), count = 0; // as mentioned, length of both is same. we can take length of any
    for (int i = 0; i < len; i++) {
      for (int j = 0; j < len; j++) {
        if (n1.charAt(i) == n2.charAt(j)) {
          count++;
        }
      }
    }
    return count;
  }

  // just for testing, please remove as this is not required as per the program
  public static void main(String[] args) {
    System.out.println("Testing toArray ->");
    for (int i = 0; i < toArray(5678).length; i++) {
      System.out.print(toArray(5678)[i] + " ");
    }
    System.out.println();
    System.out.println("Testing countMatches ->");
    System.out.println(countMatches(39628, 79324));
    System.out.println("Testing countIntersect ->");
    System.out.println(countIntersect(39628, 97324));
  }
}

Related Solutions

JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a method void addCoin(String coinName). Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime]. Write a method reverse that reverses the sequence of coins in a purse. Implement a TestPurse class with a main method in it. It will use the toString method to...
Which of the following is one of the focal concerns of thelower-class culture?Question 2...
Which of the following is one of the focal concerns of the lower-class culture?Question 2 options:SexualitySmartnessSecurityAcheivementAll of the above are focal concerns
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default    public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this Book...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the main module that creates and manipulates CVehicle objects cars.dat -- a text file that contains name data for the main module 4th file is cvehicle.cpp and it needs to be created from scratch, and cvehicle.h needs to be filled in This was the test drive: carOne = Hyundai Sonata carTwo = Hyundai Sonata carThree = Enter the make and model of a vehicle: toyota...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default        public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this...
The Declaration of Independence contains the famous words that “all men are created equal” and that...
The Declaration of Independence contains the famous words that “all men are created equal” and that all “unalienable rights to life, liberty, and the pursuit of happiness.” Has American society lived up to the ideals mentioned in this sacred document or has the nation fallen short? You may discuss this question up through the modern era.
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include <iostream> using namespace std; class Person { private:         string fName;         string lName;         int birthYear;         int birthMonth;         int birthDay; public:         Person();         void setName(string, string);         void setBirthDate(int, int, int);         string getFullName();         string getBirthDate(); }; // File name: Person.cpp // Person class definition. Person is the base class. #include "Person.h" Person::Person() { fName = ""; lName =...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class...
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class variables, map and guess that are two dimensional arrays of ints. Part B: In the constructor of the Player class, initialize the map and guess arrays with the appropriate size: map: 10 by 10 (10 outer arrays with 10 elements each) guess: 5 by 2 (5 outer arrays with 2 elements each) Step 2: Player.readPlayerSheet() Part A The readPlayerSheet method in the Player class...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT