SDSU CS 596 Java Programming
Fall Semester, 1998
Assignment 1 Solution
To Assignment Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 13-Oct-98

Doc A, Assignment 1 Solution Slide # 1
1. What is the smallest integer type in Java needed to store:
The USA's national debt?
The national debt is well over a trillion dollars ($1,000,000,000,000). Hence, we need a long to store this value.
The population of the USA?
The population of the USA is between 200,000,000 and 300,000,000. Hence, we need an int to store this value.
The number of states in the USA?
There are 50 states. The smallest integer type is byte, which can handle this number.

2. Explain how the intern() method in java.lang.String can improve runtime performance of a program.

intern() returns a canonical representation of a string. We can compare two canonical string representations using the "==" operator. This operator just compares the address that the two string variables refer to. This is much faster than comparing the strings character by character.


Doc A, Assignment 1 Solution Slide # 2
3. In some languages the operators "&&" (and) and "||" (or) are short-circuited. That is once the result of a boolean expression of &&s and ||s is known, the remainder of the boolean expression will not be evaluated. For example in (( x > x ) && ( z == y) ) the expression (z ==y) will not be evaluated. Write a Java method that will test if boolean expressions in Java are short circuited. Explain how your program will determine if boolean expressions are short-circuited.

If boolean expressions are short circuited then the ( y++ > 0 ), in the code below, will not be performed since (x ==y ) is true. Hence, y will have value 0. If boolean expressions are not short-circuited, then ( y++ > 0 ) will be performed. This means the value of y will be 1. Hence isShortCircuited() will return true if boolean expressions are short circuited and false otherwise.

   public static boolean isShortCircuited()
      {
      int x = 0;
      int y = 0;
      
      if ( ( x == y ) || ( y++ > 0 ) )
         ; // null statement
      
      if ( y == 1 )
         return false;
      else
         return true;
      }


Doc A, Assignment 1 Solution Slide # 3
4. In both parts of the problem, do not perform any IO in the methods.
  1. Write a Java method that has a float parameter and returns the float converted to a string.
  2. Write a Java method that has a string parameter and returns the string converted to an int. Assume that the string represents an integer base 10 in the correct range for an int.

The following class does a), b) and includes a test of the methods.

public class Converter {
   public static String toString( float value ){
      return String.valueOf( value );
   }
   
   public static int toInt( String intString ){
      Integer integerValue = Integer.valueOf( intString );
      return integerValue.intValue();
   }
   public static void main( String args[] ) {
      String intString = "123";
      int theInt = Converter.toInt( intString);
      System.out.println( "The string " + intString + " the int " + theInt );
      float theFloat = 4.56F;
      String floatString = Converter.toString( theFloat );
      System.out.println( "The string " + floatString + 
                        " the float " + theFloat );
   }
}

Doc A, Assignment 1 Solution Slide # 4
5. Write a Java class that stores two shorts in a single int. Write methods putHigh( short highValue), putLow( short lowValue), getHigh(), and getLow() to store and retrieve short values from the int. Do not perform any IO in these four methods. Show the value of your int with the following values for the low and high short.

A solution followed by a Test class.

public class PackedInt {
   private static final int LOW_BITS_ON =  0x0000FFFF;
   private static final int HIGH_BITS_ON =  0xFFFF0000;
   private int packed;
   
   public void setHigh( short value ) {
      int shifted = value << 16;
      packed = ( packed & LOW_BITS_ON) | (shifted  );
   }
   public short getHigh() {
      return (short) (packed  >>> 16);
   }   
   public short getLow() {
      return (short) (packed & LOW_BITS_ON);
   }   
   public void setLow( short value ) {
      packed = ( packed & HIGH_BITS_ON) | 
                     (value & LOW_BITS_ON  );
      }
   public String toString(){
      return "Hi: " + getHigh() + ", Low: " + getLow() +
         " Packed Int: " + packed;
   }   
}

Doc A, Assignment 1 Solution Slide # 5
public class Test {
   public static void main( String args[] ) {
      PackedInt test = new PackedInt();
      System.out.println( test );
      test.setLow( (short)10 );
      System.out.println( test );
      test.setHigh(  (short) 10 );
      System.out.println( test );
      test.setHigh( (short) 32767 );
      System.out.println( test );
      test.setLow( (short) -1 );
      System.out.println( test );
      test.setLow( (short) 32767 );
      System.out.println( test );
      test.setHigh( (short) -32768 );
      System.out.println( test );
      test.setLow( (short) 0 );
      System.out.println( test );
      test.setLow( (short) -32768 );
      System.out.println( test );
   }
}
Output
Hi: 0, Low: 0 Packed Int: 0
Hi: 0, Low: 10 Packed Int: 10
Hi: 10, Low: 10 Packed Int: 655370
Hi: 32767, Low: 10 Packed Int: 2147418122
Hi: 32767, Low: -1 Packed Int: 2147483647
Hi: 32767, Low: 32767 Packed Int: 2147450879
Hi: -32768, Low: 32767 Packed Int: -2147450881
Hi: -32768, Low: 0 Packed Int: -2147483648
Hi: -32768, Low: -32768 Packed Int: -2147450880

Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 13-Oct-98