CS99

Fundamental Programming Concepts
Summer 2001

 

Lab 7: Solutions


Part I: More Star patterns

Part A: Hollow Square

Solution 1.
int n = in.readInt();        
int row = 1;        
while ( row <= n ) {            
   int col = 1;
   /* Print n stars if we're in the first or last row */        
   if (  row == 1 || row == n )
        while( col <= n ) {
           System.out.print("*");
           col++;
         }          
   else {
      /* Print 1 star, followed by n-2 spaces, followed by 1 star */
      System.out.print("*");  
      while ( col <= n-2 ) {                
          System.out.print(" ");   
          col++;
      }         
      System.out.print(" ");
   }                           
   System.out.println();           
    row++;        
}      

Solution 2.
int n = in.readInt();        
int row = 1;        
while ( row <= n ) {            
   int col = 1;            
   while ( col <= n ) {                
      if ( col == 1 || col == n || row == 1 || row == n )
         System.out.print("*");                
      else                    
         System.out.print(" ");                
      col++;            
   }            
   System.out.println();           
    row++;        
}      

Part B: Equilateral Triangle

Solution
int n = in.readInt();        
int row = 1;        
while ( row <= n ) {            
   int col = 1;      
   /* Print n-row spaces */
  while( col <= n - row ) {
     System.out.print(" ");
     col++;
   }          
  /* Print row 2*row -1 stars */
  col = 1;
  while ( col <= 2*row - 1 ) {                
       System.out.print("*");   
       col++;
   }         
   System.out.println();           
    row++;        
}      

Solution 2
int n = in.readInt();        
int row = 1;        
while ( row <= n ) {            
   int col = 1;      
   while( col <= n + row -1 ) {
      if ( col <= n - row )
         System.out.print(" ");
      else              
         System.out.print("*");   
      col++;
   }         
   System.out.println();           
    row++;        
}      

Part C: Diamond

Solution
int n = in.readInt();        
/* Print first equilateral triangle */
int row = 1;
while ( row <= n ) {            
   int col = 1;      
   while( col <= n + row -1 ) {
      if ( col <= n - row )
         System.out.print(" ");
      else              
         System.out.print("*");   
      col++;
   }         
   System.out.println();           
    row++;        
}
/* Print second equilateral triangle upside down */
row = 1;
while ( row <= n ) {            
   int col = 1;      
   while( col <= row - 1 ) {
      System.out.print(" ");
      col++;
    }
   col = 1;
   while( col <= (2*n - row) + 1 ) {             
         System.out.print("*");   
         col++;
   }         
   System.out.println();           
    row++;        
}      

Part D: Checkerboard

Solution
int N = 8;
int row = 1;        
while ( row <= N ) {            
   int col = 1;      
   while( col <= N ) {
      if ( (row+col)%2 != 0)
         System.out.print("* ");
      else
         System.out.print("  ");
      col++;
   }          
   System.out.println();           
    row++;        
}      

 


Part II: Vowels

class Vowels {

   public static void main( String[] args) {
      TokenReader in = new TokenReader( System.in );
      System.out.print("Please enter a String: ");
      String s = in.readLine();
      /* variables to contain the counts for each vowel */
      int aCount = 0;
      int eCount = 0;
      int iCount = 0;
      int oCount = 0;
      int uCount = 0;
      int cCount = 0;
      int j = 0;
      while ( j < s.length() ) {
         if ( s.charAt( j ) == 'a' )
            aCount++;
         else if ( s.charAt( j ) == 'e' )
            eCount++;
         else if ( s.charAt( j ) == 'i' )
            iCount++;
         else if ( s.charAt( j ) == 'o' )
            oCount++;
         else if ( s.charAt( j ) == 'u' )
            uCount++;
         else
            cCount++;
         j++;
      }
      System.out.println("There are " + aCount + " a's, " + eCount + " b's, " +
         iCount + " i's, " + oCount + " o's, " + uCount + " u's, and " + cCount +
         "  non-vowel characters n the String " + s );
   }

}


Part III: Finding the Mode

class Mode {

      public static void main( String[] args ) {
        TokenReader in = new TokenReader( System.in );
       
        /* initialize variables */
        int grade = in.readInt();
        int mode = grade;
        int modeFreq = 1;
        int prev = grade;
        int prevFreq = 0;

        /* Process grades until user enters -1 */
        while ( grade != -1 ) {
            /* Process current grade */
            if ( grade == prev )
                prevFreq++;
            else
                prevFreq = 1;
            /* if we have new candidate mode, update mode info */
            if ( prevFreq > modeFreq ) {
                mode = prev;
                modeFreq = prevFreq;
            }
            /* update previous grade, read next grade */
            prev = grade;
            grade = in.readInt();
         }
         /* print out mode info if user entered in data */
         if ( mode == -1 )
            System.out.println("No data entered.");
         else
            System.out.println("The mode is " + mode + " and it occurred with frequency " + modeFreq + ". " );
      }

}