Question

The digital hardness of an integer is measured by examining its representation in binary (base 2). Working from the ends of the representation inward, we remove/count corresponding pairs of leading and trailing 1s until we can no longer do so. The total number of 1s (bits) removed is the original numbers digital hardness value. For example, suppose that we have an integer whose binary representation is 110101010101 (the binary representation always begins with a leading 1). We remove the leading and trailing 1s to get 1010101010. This value does not begin and end with 1, so we stop. We have only successfully removed one pair of 1s (in the first step), so the digital hardness measure is 2. Complete the hardness() method below, which takes a single String argument containing only 1s and Os (e.g., 111001101010). The method returns the digital hardness of this argument, using the calculation process described above. You do not need to modify the original argument; just use two variables that track the current first and lastindex values within your String. public static int hardness (String number)
0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class MeasureHardness {

   public static int hardness(String number) {
       int i = 0, j = number.length() - 1;
       int hardness = 0;
       while (i < j) {

           if (number.charAt(i) != number.charAt(j))
               return hardness;
           else if (number.charAt(i) == '1' && number.charAt(j) == '1')
               hardness = hardness + 2;

           ++i;
           --j;

       }
       return hardness;

   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       System.out.println("Hardness is : " + hardness("11111000000111111"));

       System.out.println("Hardness is : " + hardness("111001101010"));

       System.out.println("Hardness is : " + hardness("110101010101"));

   }

}




======================================================================================================
See Output


Thanks

Add a comment
Know the answer?
Add Answer to:
The digital hardness of an integer is measured by examining its representation in binary (base 2)....
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 Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • Stuck on this computer science assignment Write a program that demonstrates binary searching through an array...

    Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...

  • In the program, the zipcode will be represented by an integer and the corresponding barcode will...

    In the program, the zipcode will be represented by an integer and the corresponding barcode will be represented by a string of digits. The digit 1 will represent the long bar, and the digit 0 will represent the short bar, The first and last digits of a POSTNET code are always 1. Stripping these leaves 25 digits, which can be split into groups of 5. The above example translates into the following string and groups of five: 101100100010010101100110001 01100 10001...

  • This project is meant to give you experience writing linked lists and graphs. As such, you...

    This project is meant to give you experience writing linked lists and graphs. As such, you are not permitted to use arrays or any data structure library. You may, however, make use of code presented in class and posted to Blackboard. Objective Your goal for this project is to take a block of text, analyze it, and produce random sentences in the style of the original text. For example, your program, given Wizard of Oz, might produce: how quite lion...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

  • Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of...

    Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of up to 100 digits (plus a sign). The calculator has a simple user interface, and 10 \variables" (n0, n1, ..., n9) into which hexadecimal integers can be stored. For example a session with your calculator might look like: mac: ./assmt1 > n0=0x2147483647 > n0+0x3 > n0? 0x214748364A > n1=0x1000000000000000000 > n1+n0 > n1? 0x100000000214748364A > n0? 0x214748364A > exit mac: Note: \mac: " is...

  • Correct answer for this Java problem will get thumbs up and eternal thanks Problem Description Over...

    Correct answer for this Java problem will get thumbs up and eternal thanks Problem Description Over the course of this semester you will write a chess game database that will import chess games in PGN (http://www.saremba.de/chessgml/standards/pgn/pgn- complete.htm) format. As a first step you will write code to read PGN games and resolve board positions. Solution Description Write a class called PgnReader that contains the following public static methods: tagValue takes two String arguments: a tag name and a String which...

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

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