1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
cestrela7 [59]
3 years ago
11

Write a Java program that reads an 8-bit binary number from the keyboard as a string and then converts it into decimal. For exam

ple, if the input is “01001101”, the output should be “77”. (Hint: Break the string into substrings and then convert each substring to a value for a single bit (i.e., 0 or 1). If the bits from right to left are b0, b1, …, b7; the decimal equivalent value is b0 + 2b1 + 4b2 + 8b3 + 16b4 + 32b5 +64b6 + 128b7.
Computers and Technology
1 answer:
cestrela7 [59]3 years ago
5 0

Answer:

public class Brainly

{

 public static void main(String[] args)

 {

   BinaryConverter conv = new BinaryConverter();

   String binStr = "01001101";

   System.out.print(binStr + " in decimal is "+conv.BinToDec(binStr));

 }

}

public class BinaryConverter

{

 public int BinToDec(String binStr)

 {

   int d = 0;

   while(binStr.length() > 0)

   {

     d = (d << 1) + ((binStr.charAt(0) == '1') ? 1: 0);

     binStr = binStr.substring(1);

   }

   return d;

 }

}

Explanation:

The program "eats" the string from left to right, and builds up the integer representation in variable "d" on the go. While there are digits left, it shifts the previous result to the left and sets the least signficant bit to 1 only if the corresponding string character is a 1.

You might be interested in
Banking Account
satela [25.4K]

Answer:

Here is the Python script solution.

Explanation:

#!/usr/bin/python

"""Types of bank accounts"""

# Assignment:

# Bank Account Manager - Create a class called Account which will be an abstract

# class for three other classes called CheckingAccount, SavingsAccount and

# BusinessAccount. Manage credits and debits from these accounts through an ATM

# style program.

from __future__ import print_function

def pct_to_dec(num):

"""Returns decimal version of percent"""

dec = float(num) / 100

return dec

class Account(object):

"""Creates an Account"""

def __init__(self, balance, int_rate, act_type, min_balance, **kwargs):

self.balance = balance

self.int_rate = int_rate

self.act_type = act_type

self.min_balance = min_balance

super(Account, self).__init__(**kwargs)

def __str__(self):

"""Returns formatted account type and balance"""

# Charge $25 fee if balance drops below minimum

if self.balance < self.min_balance:

self.balance -= 25

# Add interest

self.balance += round(self.balance * pct_to_dec(self.int_rate), 2)

return '{0}: ${1}'.format(self.act_type, self.balance)

class CheckingAccount(Account):

"""Creates a CheckingAccount Account"""

def __init__(self, **kwargs):

super(CheckingAccount, self).__init__(**kwargs)

class SavingsAccount(Account):

"""Creates a SavingsAccount Account"""

def __init__(self, **kwargs):

super(SavingsAccount, self).__init__(**kwargs)

class BusinessAccount(Account):

"""Creates a BusinessAccount Account"""

def __init__(self, **kwargs):

super(BusinessAccount, self).__init__(**kwargs)

# pylint: disable=C0103

ca1 = CheckingAccount(balance=500, int_rate=0.25, act_type='Checking Account',

min_balance=0)

sa1 = SavingsAccount(balance=50, int_rate=0.50, act_type='Savings Account',

min_balance=0)

ba1 = BusinessAccount(balance=4000, int_rate=0.75, act_type='Business Account',

min_balance=5000)

# Month #1 statement, initial deposits plus interest

print(ca1)

print(sa1)

print(ba1)

print('-------------')

# Month #2 statement plus interest

# Make deposit into checking

setattr(ca1, 'balance', (ca1.balance + 1000))

# Withdraw from checking

setattr(ca1, 'balance', (ca1.balance - 500))

# Make a deposit into savings

setattr(sa1, 'balance', (sa1.balance + 100))

print(ca1)

print(sa1)

print(ba1)

print('-------------')

# Month #3 statement plus interest

# Make deposit into checking

setattr(ca1, 'balance', (ca1.balance + 2500))

# Withdraw from checking

setattr(ca1, 'balance', (ca1.balance - 700))

# Make a deposit into savings

setattr(sa1, 'balance', (sa1.balance + 100))

# Make a deposit into business

setattr(ba1, 'balance', (ba1.balance + 1000))

print(ca1)

print(sa1)

print(ba1)

4 0
3 years ago
Public class Bird
Anna007 [38]

Answer:

Answer below.

Explanation:

You are creating an object of bird "b" with a talon strength of 5 and a beak of 5.

public Hawk(int talon, int beak)

{

super(beak);

talonStrength = talon;

}

Please mark brainliest if this is the answer you were looking for

Please mark brainliest if this is the answer you were looking for

6 0
2 years ago
A_______VPN connection is a private network that uses a public
goldfiish [28.3K]

Answer:

letter c. point to site po sagot diyan

3 0
2 years ago
5. The shell of a document that may be used over and over in desktop publishing is called a
Blababa [14]
The answer is B) Template. 

Various templates are available in most all desktop publishing programs, from resume templates in Word to Income Statement templates in Excel. Templates are useful for skipping the general "setup" process of making a document, and allow you to focus more on filling the document with the relevant data and polishing it to your specific needs once most of the legwork is done. 
6 0
2 years ago
Read 2 more answers
How do you change the desktop background in the macos mojave operating system?
sleet_krkn [62]

Answer: Click the System Preferences icon on the dock. In the first row, click Desktop & Screen Saver.

Explanation: When it comes to macOS versions, Mojave and High Sierra are very comparable. The two have a lot in common, unlike Mojave and the more recent Catalina.

8 0
1 year ago
Other questions:
  • If you know the unit prices of two different brands of an item you are better able to
    8·1 answer
  • What do you call the combination of title, description, tags, and thumbnail?
    6·1 answer
  • An ___ is any person who spends times using technology at home
    10·1 answer
  • What is a method whereby new problems are solved based on the solutions from similar cases solved in the past?
    9·1 answer
  • Assume that you have created a class named DemoCar. Within the Main() method of this class, you instantiate a Car object named m
    12·1 answer
  • ___ involves connecting geographically remote computers into a single network and combining the computational power of all compu
    14·1 answer
  • What are the purposes of a good web page design?
    9·2 answers
  • I need some asap pls
    9·2 answers
  • How does Shakespeare immediately introduce Tybalt as a menacing character?
    9·1 answer
  • 80. A .......... is used to read or write data.<br>A. CD B. VDU C. ROM D. RAM​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!