ALGORITHM DEVELOPMENT CS 139
Popular in Course
Popular in ComputerScienence
This 3 page Class Notes was uploaded by Mable Windler on Saturday September 26, 2015. The Class Notes belongs to CS 139 at James Madison University taught by Staff in Fall. Since its upload, it has received 8 views. For similar materials see /class/214136/cs-139-james-madison-university in ComputerScienence at James Madison University.
Reviews for ALGORITHM DEVELOPMENT
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/26/15
C8139 1232009 Base Conversion Background Today s lab comes from a lab manual that goes along with our Lewis and Loftus book It has you program a method for computing the base 10 number to any base This lab has some limitations due to the few tools that you have learned in java But based on what you have learned you should be able to apply the method from the Schaum s Outline book to this assignment Submission You will submit this lab by handing in the source listing in hardcopy form for this program Be sure that you test your program using a series of data I will be evaluating this lab on the basis of its correctness and also on how closely you matched the Style Guide DUE Tuesday Sept 28 2004 Grading I will be grading this lab for quality Make sure that you use correct naming conventions appropriate headers document your main method input output and behavior use proper indenting etc Points will be taken off for work that does not conform to the standards when you work on Programming tests Be sure to include your own comments for the code that you write Special Instructions for this class Before you begin to program you need to understand the algorithm for converting a base 10 number to a different base Review that section in the Schaum s Outline and then answer the questions below The algorithm for converting a base 10 number is 1 Integer divide the decimal number by the base You obtain a quotient and a remainder 2 Record the remainder as the rightmost digit of the converted number 3 While you do not have a quotient of zero a Divide the quotient by the base You obtain a quotient and a remainder b Record the remainder directly to the left of the last digit written in the converted number Example Convert 59700 to base 2 lnumber1001010101 lpositionl 9 8 I 7 I 6 I 5 3 59722Q298R1 29822Ql49R0 14922Q074R1 C8139 1232009 742Q 37 R0 372Q 18 R1 182Q 9R0 92Q 4R1 42Q 2R0 22Q 1R0 12Q 0R1 stop since Q 0 Checking 1quot 291quot 261quot 241quot 221quot 20 512641641 597 Base Conversion This algorithm for converting a base 10 number to another base 1 involves repeatedly dividing by b Each time a division is performed the remainder and quotient are saved At each step the dividend is the quotient from the preceding step the divisor is always I The algorithm stops when the quotient is 0 The number in the new base is the sequence of remainders in reverse order the last one computed goes first the first one goes last In this exercise you will use this algorithm to write a program that converts a base 10 number to a 4digit number in another base you don39t know enough programming yet to be able to convert any size number The base 10 number and the new base between 2 and 9 will be input to the program The start of the program is in the file BaseCanverI java Save this file to your directory then modify it one step at a time as follows 1 The program will only work correctly for base 10 numbers that fit in 4 digits in the new base We know that in base 2 the maximum unsigned integer that will fit in 4 bits is 1111 which equals 15 in base 10 In base 8 the maximum number is 7777 which equals 4095 in base 10 In general the maximum base 10 number that fits in 4 base b digits is b4 e 1 Work out an algorithm which given any number from 2 e 9 you can compute this maximum number Add an assignment statement or statements to the program to compute this value for the base that is input and assign it to the variable maxNumber Add a statement that prints out the result appropriately labeled Compile and run the program to make sure it is correct so far N Now add the code to do the conversion The comments below guide you through the calculationsi replace them with the appropriate Java statements First compute placeO the units place Remember this comes from the first division so it is the remainder when the base 10 number is divided by the base HINT Then compute the quotient integer division will do itl You should declare a new variable for the quotient so that you can leave the original base 10 number intact Now compute placel this is the remainder when the quotient C3139 1232009 from the preceding step is divided by the base Then compute the new quotient Repeat the idea from above to compute p1ace2 and the next quotient Repeat again to compute p1ace3 3 So far the program does not print out the answer Recall that the answer is the sequence of remainders written in reverse orderi note that this requires concatenating the four digits that have been computed Since they are each integers if we just add them the computer will perform arithmetic instead of concatenation So we will use a variable of type String Note near the top of the program a variable named baseBNum has been declared as an object of type String and initialized to an empty string Add statements to the program to concatenate the digits in the new base to baseBNum and then print the answer Your print should be in the form of The decimal number ddd is equal to the base b number xxxx Compile and run your program Test it using the following values Enter 2 for the base and 13 for the base 10 numberithe program should print 1101 as the base 2 value enter 8 for the base and 1878 for the numberithe program should print 3526 for the base 8 value enter 3for the base and 50 for the numberithe program should print 1212
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'