A program that inputs a string that represents a binary

Assignment Help Computer Engineering
Reference no: EM132210184

Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements.

If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains.

If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.

Examples of invalid binary numbers:
abc
10102011
10101FF
0000 1111 (note: contains a space)

Examples of valid, rejected binary numbers:
00000000
1111
01110000001

Examples of valid, accepted binary numbers:
1000001
1100

Your program will only test one input for each run of the program. Testing each of the test cases requires a separate run of your program for each one.

The following 3 test runs illustrate the format of the program's console input and output:

Enter a binary number > abc
Invalid binary number.

Enter a binary number > 01110000001
Rejected

Enter a binary number > 1000001
Accepted

Make sure you study the Final Project Guidance document.

Course Project Guidance ======================= Getting the input ----------------- Use the Scanner class nextLine method to input the binary number as a String.

Use the String charAt() method in a "for" loop that varies the index of the character being examined. If your loop control variable is "i", then you will use charAt(i) in the loop body to be able to examine the character at position "i". As the loop progresses, you are able to get each character one-by-one. The characters in a String are indexed from 0 to 1 less than the number of characters in the String.

The number of characters in the String can be determined by using the String length() method. Suppose you named the input String sInput. Then your "for" loop header would look like: for (int i = 0; i <= sInput.length() - 1; i++) Note that charAt() returns a character as type "char".

For comparison, remember that character literals are enclosed in single quotation marks, like this: '1'. Error message ------------- When an invalid string is detected, display an error message, and then use a "return" statement to exit from the main method (ending the program).

Purpose of "for" loop --------------------- You must think about and understand the purpose of your "for" loop. Don't try to fit your entire program into it. For example, the "for" loop could count the number of invalid characters (anything other than a '0' or a '1') and also it could count the number of '1's it found.

Then, after the "for" loop is finished, that is after its closing brace, you could have other code with "if" and/or "else" statements to decide what to output. Of course, that means any variables you use in your post-"for" loop code must be declared prior to the start of the "for" loop so they will still be in scope after it finishes.

Reference no: EM132210184

Questions Cloud

What does adichie mean by the danger of a single story : What does Adichie mean by 'the danger of a single story'? She speaks of "how impressionable and vulnerable we are in the face of a story.
Corporate performance and stakeholder expectations : Define public issues. what are the gaps between corporate performance and stakeholder expectations?
Explain the optimal number of cosmeticlogy sets : Explain the optimal number of Cosmeticlogy sets, tTouchables and B. Frankline box sets to be ordered, sold and the optimal profit in the coming month.
Develop a research proposal based on a business : In this class, you will develop a research proposal based on a business, organizational problem, or topic that you want to explore. If you need help in finding.
A program that inputs a string that represents a binary : Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces.
Private sector employees to organize-bargain collectively : Contrast the rights of public employees with private sector employees to organize and bargain collectively. What authority do the unions have in each sector?
What role do you think media plays in most people lives : What did you learn? Did anything surprise you? Did you notice any patterns in your media use? Has your media use changed over time?
Write a program in java that draws a certain pattern : Write a program in java that draws a certain pattern. Based on the number entered by the user, display a set of (reversed) triangles on the screen.
Evaluate the research methodologies used : Evaluate the research methodologies used. In your evaluation, make sure that you: Examine the theoretical framework used in the study and assess its appropriate

Reviews

Write a Review

Computer Engineering Questions & Answers

  Write down a code segment using a while loop

Write a code segment using a as loop that outputs the numbers from -10 to 10.

  Implementing autocomplete with a trie

COMP 250: Introduction to Computer Science Assignment. Implementing Autocomplete with a Trie  In this question, you will work on the problem of storing a set of n words (also called keys) in a tree data structure, and a method for efficiently findi..

  System will allow for scheduling of special events

The loan period of any object cannot overlap any scheduled conservation. All objects are subject to inspection prior to leaving the museum and upon their return from the loan.

  Determine the new optimum solution

For the diet model, suppose that the daily availability of corn is limited to 450 lb. Identify the new solution space, and determine the new optimum solution.

  Design a program which asks for the number of tickets sold

a dramatic theater has three seating sections and it changes the following prices for tickets in each section section a

  Display the average word length to one decimal place

Display the average word length to one decimal place. Put the source code of calcWordAvg.c in your answer sheet.

  Discuss the social and economic impact of technology

Assuming that you are in this class because you are interested in becoming a programmer. So, what does it take to be a good programmer?

  Explain the concept of privacy and its legal protections

This assignment consists of two (2) sections: a written paper and a PowerPoint presentation. You must submit both sections as separate files for the completion.

  Data integration and etl

Write down the data integration and ETL. How are they related? What is the data warehouse and state its benefits?

  How do you display a list of all processes without the ps

How do you display a list of all processes without the ps header line where processes with the same name are grouped together?

  What does the execute bit mean for a directory

What does the 'execute' bit mean for a directory? Show how the execute bit works for referencing files within directories. Does the execute bit need to be set for ANY directory on the path to a file you wish to reference?

  Create a java project in eclipse

Create a Java project in Eclipse, called Lab 2. Next create a java class called Dashboard.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd