The Artima Developer Community
Sponsored Link

Java Answers Forum
could anyone help me to slove this problem??

2 replies on 1 page. Most recent reply: Aug 5, 2002 8:44 AM by Chet Underwood, Esq.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
henry

Posts: 3
Nickname: hlz3
Registered: Jul, 2002

could anyone help me to slove this problem?? Posted: Aug 4, 2002 9:35 PM
Reply to this message Reply
Advertisement
Design and implement a program that allows the user to input two numbers in binary (as strings) and displays their sum in binary as a string.

Examples:

input1 input2 output
0 0 0
1 0 1
1 1 10
10101 10101 101010
111 1 1000

input 1 1111111111111111111111111111111111111111111 input 2 1 output10000000000000000000000000000000000000000000

Your program must have one function method that returns the sum (in binary as a string), given the two numbers (in binary as strings).

Good solutions may have even more functions.

Your program may have a text or graphical user interface.

Hint: The obvious strategy of converting both strings to numbers, adding them, and converting the sum to a string will take more time than a stragegy that just works with the characters in the strings and will not be able to cope with very large binary numbers. The all-character method should be able to cope with very long numbers.


Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: could anyone help me to slove this problem?? Posted: Aug 5, 2002 6:08 AM
Reply to this message Reply
The obvious is:

int value1 = Integer.parseInt( args[0]);
int value2 = Integer.parseInt( args[1]);
int output = value1 +value2;
String binOut = Integer.toBinaryString( output )

HTH

Chet Underwood, Esq.

Posts: 14
Nickname: chet
Registered: Mar, 2002

Re: could anyone help me to slove this problem?? Posted: Aug 5, 2002 8:44 AM
Reply to this message Reply
The even more obvious answer is that if you are posting such simple homework assignments to forums (without even making a pretense at asking a legitimate question), you need to change your major.

Flat View: This topic has 2 replies on 1 page
Topic: getRuntime().exec(cmdline) hangs subprocess Previous Topic   Next Topic Topic: Need helps in Java+SQL

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use