Question

Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number...

Problem 1:

regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number from the keyboard as a string and then converts it into a decimal number. For example, if the input is 1100, the output should be 12. (Hint: Break the string into substrings and then convert each substring to a value for a single bit. If the bits are b0, b1, b2, and b3, the decimal equivalent is 8b0+ 4b1+ 2b2+ b3.)

Problem 2:

Suppose you attend a party. To be sociable, you will shake hands with everyone else. Write the algorithm that will compute the total number of handshakes that occur. (Hint: Upon arrival, each person shakes hands with everyone who is already there. Use the loop to find the total number of handshakes as each person arrives.)

Problem 3:

Imagine yourself in the middle of Manhattan, where the streets are perpendicular on avenues. You are in a grid of streets, somewhat lost, and you randomly pick one of four directions and walk to the next intersection. Not knowing where you really want to go, you again randomly pick one of the four directions, and so on. After repeating the same movement for a number of times, you may want to know how far you got from the original point.

Represent locations as integer pairs(x,y). Create an algorithm that implements your movement through New York City, over 100 intersections, starting at (0,0) and print the ending location, taking into consideration that each movement, from one intersection to another will be one mile.

please create a pseudocode or flowchart. for the problems.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Random;
import java.util.Scanner;

public class ProblemDriver {
public static void main(String[] args) {
  
   problem1();
   problem2();
   problem3();
}

public static void problem3() {
   int x=0;
   int y=0;
  
   Random random=new Random();
  
   for(int i=1; i<=100; i++) {
       //for 100 intersections
       int choice=random.nextInt(4);//generates a number between 0 or 1 or 2 or 3
       switch(choice)
       {
       case 0://increment x//move right
           x++;break;
       case 1://decrement x//move left
           x--;break;
       case 2://increment y//move up
           y++;break;
       case 3://decrement y//move down
           y--;break;
          
          
       }
   }
  
   System.out.println("\n\nFinal position after 100 moves: (" + x + ", " + y + ")");
}

public static void problem2() {
   System.out.println("\n\nEnter number of persons.");
   Scanner input = new Scanner(System.in);
  
   int persons=input.nextInt();
   int totalHandShakes=0;
  
   for(int i=0; i<persons; i++) {
       totalHandShakes+=i;
   }
  
   System.out.println("Total number of handshakes: " + totalHandShakes);
}

public static void problem1() {
   System.out.println("\n\nEnter a binary number.");
   Scanner input = new Scanner(System.in);
  
   String binary=input.nextLine();
   int decimal=0;
  
   for(int i=binary.length()-1; i>=0; i--) {
       if(binary.charAt(i)=='1')
       decimal=decimal+(int)Math.pow(2, binary.length()-1-i);
   }
  
   System.out.println("Decimal Value: " + decimal);
}
}

SCREENSHOTS:

OUTPUT:

FLOWCHARTS:

In case of any doubts, do let me know in the comments section. I'll get it resolved. Don't forget to give a Thumbs Up! :)

Add a comment
Know the answer?
Add Answer to:
Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number...
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
  • Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following...

    Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments): 1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random) 2. A method to compute...

  • Problem 4 (10 points): 1. Consider the numbers 23.724 and 0.3344770219. Please normalize both 2. ...

    please help Problem 4 (10 points): 1. Consider the numbers 23.724 and 0.3344770219. Please normalize both 2. Calculate their sum by hand. 3. Convert to binary assuming each number is stored in a 16-bit register. Half-precision binary floating-point has: sign bit: lbit, exponent width: 5bits and a bias of 15, and significand 10 bits (16 bits total) 4. Show cach step of their binary addition, assuming you have one guard, one round, and one sticky bit, rounding to the nearest...

  • number 4 and 5 please! PROBLEM STATEMENT A logic circuit is needed to add multi-bit binary...

    number 4 and 5 please! PROBLEM STATEMENT A logic circuit is needed to add multi-bit binary numbers. A 2-level circuit that would add two four-bit numbers would have 9 inputs and five outputs. Although a 2-level SOP or POS circuit theoretically would be very fast, it has numerous drawbacks that make it impractical. The design would be very complex in terms of the number of logic gates. The number of inputs for each gate would challenge target technologies. Testing would...

  • Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +...

    Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +1 in order, except that one of the numbers is missing. Find the miss sorted ing mber. Your algorithm should run in time (log n). (Hint: Modify Binary search). A pseudocode means an algorithm with if statements and loops, etc. Don't just write a paragraph. Also, if your...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task spe...

    Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task specified. You can draw the flowcharts with a pen/pencil on a piece of paper and scan it for submission. Please ensure that the scanned file and your handwriting are clear and legible. However, it is preferable to draw flowcharts using a drawing software. Here are links to some free drawing tools https://www.draw.io/ www.lucidchart.com http://dia-installer.de/ https://pencil.evolus.vn/ ---------------------------------DON'T NEED THE PYTHON CODE... JUST THE ALGORITHM...

  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • pls answer all questions 1) A step-by-step solution to a problem is called a. hardware b....

    pls answer all questions 1) A step-by-step solution to a problem is called a. hardware b. an operating system c. a computer language d. an algorithm 2) separated the programming task from the computer operation tasks. a. Algorithms b. Data processors c. High-level programming languages d. Operating systems 3) is a 16-bit code that can represent symbols in languages other than English. a. ASCII b. Extended ASCII c. EBCDIC d. Unicode 4) When you want to download music to a...

  • Mini Case Building Shared Services at RR Communications4 4 Smith, H. A., and J. D. McKeen....

    Mini Case Building Shared Services at RR Communications4 4 Smith, H. A., and J. D. McKeen. “Shared Services at RR Communications.” #1-L07-1-002, Queen’s School of Business, September 2007. Reproduced by permission of Queen’s University, School of Business, Kingston, Ontario, Canada. Vince Patton had been waiting years for this day. He pulled the papers together in front of him and scanned the small conference room. “You’re fired,” he said to the four divisional CIOs sitting at the table. They looked nervously...

  • Mini Case Building Shared Services at RR Communications4 4 Smith, H. A., and J. D. McKeen....

    Mini Case Building Shared Services at RR Communications4 4 Smith, H. A., and J. D. McKeen. “Shared Services at RR Communications.” #1-L07-1-002, Queen’s School of Business, September 2007. Reproduced by permission of Queen’s University, School of Business, Kingston, Ontario, Canada. Vince Patton had been waiting years for this day. He pulled the papers together in front of him and scanned the small conference room. “You’re fired,” he said to the four divisional CIOs sitting at the table. They looked nervously...

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