Colorado State University

Recitation R23 - Miscellaneous Java and Start P8
Fall 2013

CS160: Foundations in Programming


The purpose of this lab is to start on P8 and touch on some miscellaneous Java language features:

PART A - Getting started on P8


See the P8 assignment on the Progress tab on the course website for instructions. Your TA will make sure you have P8 setup, and help will be available for questions related to P8.

PART B - Miscellaneous Java


Binary Literals

Java allows programmers to specify ints, doubles and chars down to the bit level. There are 4 kinds of numeric literals in Java:

Bitwise Operations

In addition to the usual operators +, -, *, /, &&, ||, there are also bitwise operators:

Ternary Operator

The ternary operator is an arithmatic if-else operator that can be used in assignment operations. It has the form:
conditional ? ifTrue expression : ifFalse expression

For example, this code:
int a = -5;
int b = 6;
int c = -10;
int absSum = 0;
if( a < 0 ){
    absSum += -a;
} else {
    absSum += a;
}
if( b < 0 ){
    absSum += -b;
} else {
    absSum += b;
}
if( c < 0 ){
    absSum += -c;
} else {
    absSum += c;
}
can be written as this code:
int a = -5;
int b = 6;
int c = -10;
int absSum = ( a < 0 ? -a : a) + (b < 0 ? -b : b)+ (c < 0 ? -c : c);

Writing a Number Converter

  1. Create a new R23 project in Eclipse and an associated class.
  2. Add methods with the following signatures:

    // This converts an int (decimal value) to a String representing the binary value
    public static String toBinaryString( int decimalNumber ){ }

    // This converts an int (decimal value) to a String representing the hexadecimal value.
    public static String toHexString( int decimalNumber ){ }

    // This converts a String representing a hexadecimal value to an int (decimal value)
    public static int parseHexString( String hexString ){ }

    // This converts a String representing a binary value to an int (decimal value)
    public static int parseBinaryString( String binaryString ){ }

  3. After the TA shows you a simple algorithm for converting a number between bases, implement the methods above.
  4. Test your methods in main

Wrapper Classes

While primitives are the basis of all the data we use, they lack the functionality of classes like String. Wrapper classes are designed to give primitives some life. Probably the most useful feature of these classes are their parse______( String s ) methods. Integer, Double, and Boolean, all have have these methods allowing for Strings to be turned into their primitive representations.
Integer also includes several methods that make writing number converters unnecessary.

Inside of main, use the parseInt( string, radix ), toBinaryString( i ), and toHexString( i ) methods of the Integer class to check your code from above. Your results should be the same as the results from the Integer methods, though you may have leading zeros.
Show your R23.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2013 CS160 Colorado State University. All Rights Reserved.