CS99

Fundamental Programming Concepts
Summer 2001

 

Lab 6: Solutions


Part I: Separating Characters In A String

Part A: (not required)
/*
Part1A accepts as input a five-digit number then separates the number into its individual digits 
and prints the digits separated from one another by three spaces each.
*/ 


class Part1A {

    public static void main( String[] args) {
       TokenReader in = new TokenReader( System.in );
        System.out.print("Enter a five-digit number: " ); 
        String digit = in.readString();
 
        if ( digit.length() != 5 )
 
            System.out.println( "Error: 5-digit number not entered!" );
 
        else {

            System.out.println("You entered: \n"); 
            System.out.print( digit.charAt( 0 ) + " " + digit.charAt( 1 ) + " " +
                digit.charAt( 2 ) + " " + digit.charAt( 3 ) + " " + digit.charAt( 4 ));

         }
     } 
 }
Part B:
/*
Part1B accepts as input a number of unknown size, then separates the number into its individual digits and prints them separated from each other by three spaces each. 
*/
class Part1B { 
    public static void main( String[] args) { 
        /* variable to accept standard input from the keyboard */
        TokenReader in = new TokenReader( System.in ); 
        /* prompt user for digit */
        System.out.print("Enter a digit: " ); 
        String digit = in.readString(); 
        /* store the length of the String int variable n*/
        int n = digit.length(); 
        int j = 0; 
        System.out.println("You entered: \n"); 
        /* print out each of the characters of String digit separated by 3 spaces */
        while ( j < n ) { 
            System.out.print( digit.charAt( j ) + "   " ); j
            ++; 
        }
    }
 }

Part II: Finding The Largest Value

Part A: 

/* class Part2A accepts as input a series of 10 positive numbers and determines and prints the largest of the numbers. */ 
class Part2A {
   public static void main( String[] args) { 
     /* Variable to accept standard input from the keyboard */
        TokenReader in = new TokenReader( System.in ); 
        /* Prompt user for 10 numbers */
        /* give variable largest the smallest possible value allowable */
        int largest = Integer.MIN_VALUE; 
        int count = 1;
        System.out.print("Enter in 10 numbers: " ); 

        do{
            int number = in.readInt();
        
            /* update largest if latest number is larger than it */
            if ( number > largest ) 
                largest = number; 
        
            count++; 
        
        } while ( count <= 10 ); 
        
        System.out.println("The largest number entered was " + largest +"."); 
    }
 }
Part B:
/*
class Part2B reads in a sequence of numbers terminated by -1, and prints the largest of the values entered.
 */
class Part2B {
 
    public static void main( String[] args) { 
        /* Variable to read standard input from the keyboard */
        TokenReader in = new TokenReader( System.in ); 
        /* Prompt user for sequence of non-negative numbers */
        System.out.print("Enter a sequence of non-negative numbers: " ); 
        
        int largest = Integer.MIN_VALUE; 
        int number = in.readInt(); 
        /* Process numbers until a negative value is read */
        while ( number >= 0 ) { 
            /* Update largest if latest number is larger than it */
            if ( number > largest ) 
                largest = number; 
            
            number = in.readInt(); 
        } 
        System.out.println("The largest number entered was " + largest +"."); 
    } 
}

Part III: Hi-Lo Guessing Game

/*
Class HiLo chooses a random number between 1 and 100 (inclusive) and asks the user repeatedly to guess what that number is.  For each guess, the program tells the user if he is correct, too high, or too low.  The program continues to accept guesses until the user either guesses correctly or the user decides to quit by entering in 0.  If the user guesses right, the program tells the user how many guesses it took and asks the user if he wants to play again. 
*/
class HiLo{ 
    public static void main( String[] args ) { 
        /* Variable to read standard input */
        TokenReader in = new TokenReader( System.in ); 
        
        /* Assign a random integer from 1 to 100  to variable secretNum */
        int secretNum = (int) ( 1 + 100*Math.random() ); 

        /* Prompt user for first guess */
        System.out.print( "Enter a guess: " ); 
        int guess = in.readInt(); 

        /* numOfGuesses contains the number of wrong guesses made by user */
        int numOfGuesses = 1;
        
        while( guess != 0 ) { 
            if ( guess > secretNum ) { 
                System.out.println("That guess was too high."); 
                numOfGuesses++; 
            } else if ( guess < secretNum ) { 
                System.out.println("That guess was too low."); 
                numOfGuesses++; 
            } else { 
                System.out.println("Correct! That took " + numOfGuesses + " guesses." ); 
                System.out.println("If you would like to quit enter 0," 
                    + " otherwise guess the new number."); 
                /* Reset the secret number and the numOfGuesses */
                secretNum = (int) ( 1 + 100*Math.random() ); 
                numOfGuesses = 1; 
            } 
            System.out.print("Enter a guess: " ); 
            guess = in.readInt(); 
        } 
        
        System.out.println( "Bye! Thanks for playing. " ); 
    } 
}


Part IV: Printing stars, nested while-loops
There is a variety of solutions to each of these parts; we're only showing one.  So don't worry if your solutions don't look like ours.
 Part A:
**
*    CS99 Lab 6: Triangles
*    Author: your name here
*    NetID: your NetID here
*    Date: today's date
*/
class Triangles {
     public static void main( String[] args ) {
          TokenReader in = new TokenReader( System.in );

          System.out.print("Enter a  number: " );
          int n = in.readInt();

          int rows = 1;

          while ( rows <= n ) {
System.out.println();            
int cols = 1;
              while ( cols <= rows ) {
                   System.out.print( "*");
                   cols++;
              }
               rows++;
          }
     }
}
Part B:
class TrianglesB {
     public static void main( String[] args ) {
          TokenReader in = new TokenReader( System.in );

          System.out.print("Enter a  number: " );
          int n = in.readInt();

          int rows = 1;

          while ( rows <= n ) {
System.out.println();            
int cols = 1;
              while ( cols <= n - rows + 1) {
                   System.out.print( "*");
                   cols++;
              }
               rows++;
          }
     }
}
Part C:
class TrianglesC {
     public static void main( String[] args ) {
          TokenReader in = new TokenReader( System.in );

          System.out.print("Enter a  number: " );
          int n = in.readInt();

          int rows = 1;

          while ( rows <= n  ) {
System.out.println();                            
int cols = 1;
              while ( cols <= n) {        
                   if ( cols <= row -1 )
                        System.out.print(" ");
                    else
                        System.out.print( "*");
                   cols++;
              }
               rows++;
          }
     }
}
Part D:

class TrianglesD {
     public static void main( String[] args ) {
          TokenReader in = new TokenReader( System.in );

          System.out.print("Enter a  number: " );
          int n = in.readInt();

          int rows = 1;

          while ( rows <= n ) {
System.out.println();            
int cols = 1;
              while ( cols <= n ) {
                    if ( cols <= n - row )
                        System.out.print(" ");
                    else
                         System.out.print( "*");
                   cols++;
              }
               rows++;
          }
     }
}