Unformatted text preview: CSE 110 Laboratory Assignment 6
This assignment is due by 11:59 PM on Sunday, October 24. Submit your .java ﬁle through Blackboard —
be sure to submit through the link that corresponds to your lab day and time.
Lab Description
Earlier this semester, we learned how to convert numbers from base 10 to other bases using division. In
this weekʼs lab, we will write a small Java program to automate this task.
So far, all of the variables we have used have been numeric types: integers and ﬂoatingpoint (decimal)
values. If we want to represent other kinds of data (letters, symbols, or words), we can use two other
types of variables: char and String. char variables store a single character, while String variables
can store a sequence of characters (like a word or a complete sentence).
You can declare and initialize a String variable just like the other kinds of variables that weʼve seen:
String x = “abcd”;
Note the double quotes; they tell Java that everything inside them is a sequence of characters.
We can use the addition operator (+) to join two Strings together. The second value is appended to the
end of the ﬁrst one:
String a = “abcd” + “efgh”; // a is now “abcdefgh”
String variables are “larger” than any other type of variable. If we add anything to a String, the result
will also be a String. For example,
23 + “abcd”
will produce a new String:
23abcd Lab Instructions
1. At the end of this assignment, you will ﬁnd skeleton code for your lab program. Copy and paste it into a
new class to begin Lab 6, which solves the problem described in the steps that follow.
2. As we discussed in class earlier this semester, you can convert a base 10 (decimal) value to any other
base n by repeatedly dividing the original number by n. When weʼre ﬁnished, the remainders left by
each division step will form the translated number, in base n.
Since we donʼt know exactly how many remainders will be generated by this process, our program will
use a String to “catch” each remainder as it is generated. At the end of the program, this String will
contain the entire translated number.
In pseudocode, this algorithm can be described as follows:
Set answer to the empty String (“”)
value ← base 10 value to translate
n ← base to translate into while (value > 0):
quotient ← value divided by n
remainder ← value modulo n
value ← quotient
answer ← remainder + answer
Print answer
3. Complete the main() method in your program skeleton by doing the following:
a. Use the Scanner to read two integer values from the user. One integer value represents the base
10 number to convert. The second integer represents the base that you want to convert to. You
may assume that the user will always enter a positive value for the base 10 number. You may
assume that the base will always be a positive integer less than 10.
b. Translate the algorithm above into Java code using a while or for loop.
c. When your program is done, it should print out the translated value.
d. You can test your code using the following values, or make up your own test values:
i. 345 in base 10 is 531 in base 8
ii. 111 in base 10 is 1233 in base 4
iii. 76 in base 10 is 1001100 in base 2
4. Extra Credit: Modify your program to handle base conversions up to base 16 (hexadecimal). Use if
statements to substitute letters for numbers when the remainders are 10 or greater (for example, a
remainder of 10 should be represented as “A”). This change is worth an additional 50%.
NOTE: For this lab, all of your code should be placed inside the main() method.
When you are ﬁnished, please submit the contents of your Converter.java ﬁle through
Blackboard. Lab 6 Program Skeleton
// Skeleton code for CSE 110 Lab 6
//
// Name:
// Lab day and time:
import java.util.*; // needed for Scanner
public class Converter
{
public static void main (String [ ] args)
{
// Use this Scanner to read from the keyboard
Scanner input = new Scanner(System.in);
// PUT ALL OF YOUR CODE FOR THE ASSIGNMENT AFTER THIS POINT
// AND BEFORE THE CLOSING CURLY BRACES
}
} ...
View
Full
Document
This note was uploaded on 12/06/2011 for the course CSE 110 taught by Professor Shaunakpawagi during the Fall '08 term at SUNY Stony Brook.
 Fall '08
 SHAUNAKPAWAGI

Click to edit the document details