Programming is Fun
 Programming is Fun!

www.anandsingh.net
Subscribe For Free Updates!

We'll not spam mate! We promise.

Friday, September 12, 2014

Exercise CircleComputation (User Input)

Write a program called CircleComputation, which prompts user for a radius (of double) and compute the area and perimeter of a circle. The output shall look like:
Enter the radius: 1.2
The area is 4.5239
The perimeter is 7.5398223686155035
Hints: π is kept in a constant called Math.PI.

Exercise FileScanner (File Input)

Write a program called FileScanner to read an int, a double, and a String form a text file called "in.txt", and produce the following output:
The integer read is 12
The floating point number read is 33.44
The String read is "Peter"
Hi! Peter, the sum of 12 and 33.44 is 45.44
You need to create a text file called "in.txt" (in Eclipse, right-click on the "project" ⇒ "New" ⇒ "File") with the following contents:
12
33.44
Peter
import java.util.Scanner;              // Needed to use Scanner for input
import java.io.File;                   // Needed to use File
import java.io.FileNotFoundException;  // Needed for file operation
   
public class FileScanner { 
   public static void main(String[] args) 
         throws FileNotFoundException {  // Needed for file operation
      int num1;
      double num2;
      String name;
      double sum;
   
      // Setup a Scanner to read from a text file
      Scanner in = new Scanner(new File("in.txt"));
      num1 = in.nextInt();      // use nextInt() to read int
      num2 = in.nextDouble();   // use nextDouble() to read double
      name = in.next();         // use next() to read String
   
      // Display
      ......
   }
}

Exercise KeyboardScanner (Keyboard Input)

Write a program called KeyboardScanner to prompt user for an int, a double, and a String. The output shall look like (the inputs are shown in bold):
Enter an integer: 12
Enter a floating point number: 33.44
Enter your name: Peter
Hi! Peter, the sum of 12 and 33.44 is 45.44
Hints:
import java.util.Scanner;    // needed to use Scanner for input
public class KeyboardScanner {
   public static void main(String[] args) {
      int num1;
      double num2;
      String name;
      double sum;
    
      // Setup a Scanner called in to scan the keyboard (System.in)
      Scanner in = new Scanner(System.in);
      System.out.print("Enter an integer: ");
      num1 = in.nextInt();     // use nextInt() to read int
      System.out.print("Enter a floating point number: ");
      num2 = in.nextDouble();  // use nextDouble() to read double
      System.out.print("Enter your name: ");
      name = in.next();        // use next() to read String
   
      // Display
      ......
   }
}

Exercise TimeTable (nested-loop)

Write a program called TimeTable to produce the multiplication table of 1 to 9 as shown using two nested for-loops:
 * |  1  2  3  4  5  6  7  8  9
-------------------------------
 1 |  1  2  3  4  5  6  7  8  9
 2 |  2  4  6  8 10 12 14 16 18
 3 |  3  6  9 12 15 18 21 24 27
 4 |  4  8 12 16 20 24 28 32 36
 5 |  5 10 15 20 25 30 35 40 45
 6 |  6 12 18 24 30 36 42 48 54
 7 |  7 14 21 28 35 42 49 56 63
 8 |  8 16 24 32 40 48 56 64 72
 9 |  9 18 27 36 45 54 63 72 81
Modify the program to print the multiplication table of 1 to 12.

Exercise CheckerBoard (nested-loop)

Write a program called CheckerBoard that displays the following n×n (n=7) checkerboard pattern using two nested for-loops.
# # # # # # #
 # # # # # # #
# # # # # # #
 # # # # # # #
# # # # # # #
 # # # # # # #
# # # # # # #
Your program should use only three output statements, one EACH of the followings:
System.out.print("# ");   // print # and a space, without newline
System.out.print(" ");    // print a space, without newline
System.out.println();     // print a newline
Hints:
public class CheckerBoard {   // saved as "CheckerBoard.java"
   public static void main (String[] args) {
      int size = 7;    // size of the board

      for (int row = 1; ......; ......) {
         // Use modulus 2 to find alternate lines
         if ((row % 2) == 0) {   // row 2, 4, 6, 8
            ......
         }
         for (int col = 1; ......; ......) {
            ......
         }
         ......
      }
   }
}

Exercise SquareBoard (nested-loop)

Write a program called SquareBoard that displays the following n×n (n=5) pattern using two nested for-loops.
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
Your program should use only two output statements, one EACH of the followings:
System.out.print("# ");   // print # and a space, without newline
System.out.println();     // print a newline
Hints:
public class SquareBoard {   // saved as "SquareBoard.java"
   public static void main (String[] args) {
      int size = 5;    // size of the board
      for (int row = 1; ......; ......) {
         for (int col = 1; ......; ......) {
            ......
         }
         ......
      }
   }
}

Extracting Digits

Write a program to extract each digit from an int, in the reverse order. For example, if the int is 1542, the output shall be "2,4,5,1", with a comma separating the digits.
Hints: Use n % 10 to extract a digit; and n = n / 10 to discard the last digit.
int n = ....;
while (n > 0) {
   int digit = n % 10;  // Extract the last digit
   ......
   .....
   n = n / 10;          // Drop last digit and repeat the loop
}

Saturday, September 6, 2014

Exercise Fibonacci (Loop)

Write a program called Fibonacci to display the first 20 Fibonacci numbers F(n), where F(n)=F(n–1)+F(n–2) and F(1)=F(2)=1. Also compute their average. The output shall look like:
The first 20 Fibonacci numbers are:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
The average is 885.5
Hints:
public class Fibonacci {
   public static void main (String args[]) {
      int n = 3;          // the index n for F(n), starting from n=3
      int fn;             // F(n) to be computed
      int fnMinus1 = 1;   // F(n-1), init to F(2)
      int fnMinus2 = 1;   // F(n-2), init to F(1)
      int nMax = 20;      // maximum n, inclusive
      int sum = fnMinus1 + fnMinus2;
      double average;
 
      System.out.println("The first " + nMax + " Fibonacci numbers are:");
      ......
 
      while (n <= nMax) {
         // Compute F(n), print it and add to sum
         ......
         // Adjust the index n and shift the numbers
         ......
      }
 
      // Compute and display the average (=sum/nMax)
      ......
   }
}
Tribonacci numbers are a sequence of numbers T(n) similar to Fibonacci numbers, except that a number is formed by adding the three previous numbers, i.e., T(n)=T(n-1)+T(n-2)+T(n-3)T(1)=T(2)=1, and T(3)=2. Write a program called Tribonacci to produce the first twenty Tribonacci numbers.

Exercise PrintPatterns

Write a method to print each of the followings patterns using nested loops in a class called PrintPatterns. The signatures of the methods are:
public static void printPatternX(int size)  // 'X' from 'A' to ..., size is a positive integer.
#                    # # # # # # # #      # # # # # # # #                    #
# #                  # # # # # # #          # # # # # # #                  # #
# # #                # # # # # #              # # # # # #                # # #
# # # #              # # # # #                  # # # # #              # # # #
# # # # #            # # # #                      # # # #            # # # # #
# # # # # #          # # #                          # # #          # # # # # #
# # # # # # #        # #                              # #        # # # # # # #
# # # # # # # #      #                                  #      # # # # # # # #
     (a)                   (b)                  (c)                  (d)
   
Hints: On the diagonal, row = col.  On the opposite diagonal, row + col = size + 1.
   
# # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #
#           #      #                        #        #       #      # #       # #
#           #        #                    #            #   #        #   #   #   #
#           #          #                #                #          #     #     #
#           #            #            #                #   #        #   #   #   #
#           #              #        #                #       #      # #       # #
# # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #
     (e)               (f)             (g)              (h)              (i)
   
# # # # # # # # # # #                #                          #
  # # # # # # # # #                # # #                      # # #
    # # # # # # #                # # # # #                  # # # # #
      # # # # #                # # # # # # #              # # # # # # #
        # # #                # # # # # # # # #          # # # # # # # # #
          #                # # # # # # # # # # #      # # # # # # # # # # #
         (j)                        (k)                 # # # # # # # # #
                                                          # # # # # # #
                                                            # # # # #
                                                              # # #
                                                                #
                                                               (l)
   
1                    1 2 3 4 5 6 7 8                    1      8 7 6 5 4 3 2 1
1 2                    1 2 3 4 5 6 7                  2 1      7 6 5 4 3 2 1
1 2 3                    1 2 3 4 5 6                3 2 1      6 5 4 3 2 1
1 2 3 4                    1 2 3 4 5              4 3 2 1      5 4 3 2 1
1 2 3 4 5                    1 2 3 4            5 4 3 2 1      4 3 2 1
1 2 3 4 5 6                    1 2 3          6 5 4 3 2 1      3 2 1
1 2 3 4 5 6 7                    1 2        7 6 5 4 3 2 1      2 1
1 2 3 4 5 6 7 8                    1      8 7 6 5 4 3 2 1      1
     (m)                  (n)                    (o)                (p)
   
              1                    1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
            1 2 1                    1 2 3 4 5 6 7 6 5 4 3 2 1
          1 2 3 2 1                    1 2 3 4 5 6 5 4 3 2 1
        1 2 3 4 3 2 1                    1 2 3 4 5 4 3 2 1
      1 2 3 4 5 4 3 2 1                    1 2 3 4 3 2 1
    1 2 3 4 5 6 5 4 3 2 1                    1 2 3 2 1
  1 2 3 4 5 6 7 6 5 4 3 2 1                    1 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1                    1
             (q)                                (r)
   
1                           1      1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2                       2 1      1 2 3 4 5 6 7   7 6 5 4 3 2 1
1 2 3                   3 2 1      1 2 3 4 5 6       6 5 4 3 2 1
1 2 3 4               4 3 2 1      1 2 3 4 5           5 4 3 2 1
1 2 3 4 5           5 4 3 2 1      1 2 3 4               4 3 2 1
1 2 3 4 5 6       6 5 4 3 2 1      1 2 3                   3 2 1
1 2 3 4 5 6 7   7 6 5 4 3 2 1      1 2                       2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1      1                           1
             (s)                                (t)
   
              1
            2 3 2
          3 4 5 4 3
        4 5 6 7 6 5 4
      5 6 7 8 9 8 7 6 5
    6 7 8 9 0 1 0 9 8 7 6
  7 8 9 0 1 2 3 2 1 0 9 8 7 
8 9 0 1 2 3 4 5 4 3 2 1 0 9 8
             (u)
Comment your Answers :)

Exercise CozaLozaWoza (Loop & Condition)

Write a program called CozaLozaWoza which prints the numbers 1 to 110, 11 numbers per line. The program shall print "Coza" in place of the numbers which are multiples of 3, "Loza" for multiples of 5, "Woza" for multiples of 7, "CozaLoza" for multiples of 3 and 5, and so on. The output shall look like:
1 2 Coza 4 Loza Coza Woza 8 Coza Loza 11 
Coza 13 Woza CozaLoza 16 17 Coza 19 Loza CozaWoza 22 
23 Coza Loza 26 Coza Woza 29 CozaLoza 31 32 Coza
......
Hints:
public class CozaLozaWoza {   // saved as "CozaLozaWoza.java"
   public static void main(String[] args) {
      int lowerbound = 1;
      int upperbound = 110;
      for (int number = lowerbound; number <= upperbound; ++number) {
         // Print "Coza" if number is divisible by 3
         if (......) {   
            System.out.print("Coza");
         }
         // Print "Loza" if number is divisible by 5
         if (......) {
            System.out.print(.....);
         }
         // Print "Woza" if number is divisible by 7
         ......
         // Print the number if it is not divisible by 3, 5 and 7
         if (......) {
            ......
         }
         // Print a newline if number is divisible by 11; else print a space
         if (......) {
            System.out.println();
         }
      }
   }
}
TRY: Modify the program to use nested-if (if ... else if ... else if ... else) instead.