Any ideas of how to even start this program I am lost and I

Any ideas of how to even start this program, I am lost and I dont even know how to start!!

Any help would be amazing, thanks

The program should allow the user to convert decimal numbers to binary numbers as long as the user desires. You should implement two methods: one for converting decimal numbers to binary numbers, and another for printing binary numubers. The binary numbers will be represented as int arrays. In addition, you should use the getInt method from p. 348 (3rd edition; or 338 for 2nd edition) of your textbook.

Your program should ask the user to enter a positive int. Continue asking the user until a positive int is entered. Use the book\'s getInt method rather than calling console.nextInt().

This int should then be converted to a binary number, storing the 0s and 1s in an array. Then the array should be printed starting from the last index to index 0.

Your program should have a while loop so that the user can continue to enter integers. Ask the user whether he or she wants to continue. Here is an example interaction (user input is underlined).

Details

Representing Binary Numbers as Arrays

Consider the binary number 111010. Your program will use an array to store the individual bits of this binary number in the following way.

Note that the bits are stored in reverse order. For example, the 0 bits in 111010 are stored at index 0 and index 2 in the array.

Method for Converting a Decimal Number to Binary

This method should have an integer parameter and return an integer array.

First, determine how many bits are needed, that is, the length of the array to store the bits. This can be done by a loop that repeatatly divides the parameter value by 2 until it reaches zero. However, we should make sure that the original value is saved for the next part of the process.

Save decimal in decimalCopy.

Initialize len to 0.

Loop until decimal is 0.

Divide decimal by 2.

Increment len.

Store decimalCopy in decimal.

Seccond, convert the integer value to a binary number based on the following pseudocode. It first creates a bit array of length len (see p. 427 for examples of creating arrays). Then index is initialized to 0. Similar to the previous loop, this loop also repeatedly divides decimal by 2 until it is 0. In this case, before each division, the body of the loop tests whether the int is odd or not. If it is odd, then a 1 is stored in the array at the index; else a 0 is stored in the array at the index. The array is the return value of the method.

Create an int array with length len.

Initialize index to 0.

Loop until decimal is zero:

If the value is odd:
then store a 1 in the array at index,
else store a 0 in the array at index,

Divide decimal by 2.

Increment index.

Return the array.

Method for Printing a Binary Array

This method should have an integer array parameter and return nothing. It should use System.out.print to print the values in the array.

To print a binary array, start at the last index (the array length minus one) and go backward to index 0. If the array is named foo, then the for loop would look like:

Do not print any spaces between the bits.

index 0 1 2 3 4 5
value 0 1 0 1 1 1
ng Concepts: Discussion and w Chapter 5 Program Logic and Indefinite Loops This pseudocode is fairly easy to turn into actual Java code 348 Scanner console new scanner (system in) System. out.print (\"How old are you? while console. hasNextInt the input console. next to discard again. out.println (\"Not an integer; try System. System out. print (\"How old are you? int age console next Int In fact, this is such a common operation that it is worth turning i a prompts until a valid number is entered String prompt) public static int getInt (Scanner console, System.out. print (prompt); while (Iconsole.has NextInt console next to discard the input system. out.println (\"Not an in eger try again System.out.print (prompt); return console.nextInt Using this method, we can rewrite our original code as follows: Scanner console new Scanner (System.in) int age a getInt ole, How old are you? When you execute this code, the interaction looks like this: How old are you what? Not an integer; try again How old are you? 18.4 Not an integer; try again How old are you? ten Not an integer try again. How old are you? darn! Not an integer try again. How old are you? help Not an integer try again. How old are you? 19

Solution

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int age = getInt(console,\"How old are you?\");
int[] binary = convertToBinary(age);
printBinaryArray(binary);
}
//prompts until a valid number is entered
public static int getInt(Scanner console,String prompt){
   System.out.print(prompt);
   while(!console.hasNextInt()){
       console.next(); // discard input
       System.out.println(\"Not an integer, try again.\");
       System.out.print(prompt);
   }
   return console.nextInt();
}
public static int[] convertToBinary(int decimal){
int decimalCopy = decimal;
int len = 0;
while(decimal!=0){
   decimal = decimal/2;
   len+=1;
}
decimal = decimalCopy;
int[] bit = new int[len];
int ind = 0;
while(decimal!=0){
   if(decimal%2==0)
       bit[ind++] = 0;
   else
       bit[ind++] = 1;
   decimal = decimal/2;
}
return bit;
}
public static void printBinaryArray(int[] binary){
for (int i = binary.length - 1; i >= 0; i--) {
System.out.print(binary[i]);
}
System.out.println();
}
}

/*
sample output
How old are you? 4
100

How old are you? 23
10111
*/

Any ideas of how to even start this program, I am lost and I dont even know how to start!! Any help would be amazing, thanks The program should allow the user t
Any ideas of how to even start this program, I am lost and I dont even know how to start!! Any help would be amazing, thanks The program should allow the user t
Any ideas of how to even start this program, I am lost and I dont even know how to start!! Any help would be amazing, thanks The program should allow the user t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site