Home Javajdktutorials C2 Java Programming: Solution to Programming Exercise



Java Programming: Solution to Programming Exercise
Posted on: April 18, 2011 at 12:00 AM
This page discusses - Java Programming: Solution to Programming Exercise

Solution for
Programming Exercise 2.1


THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.

Exercise 2.1: Write a program that will print your initials to standard output in letters that are nine lines tall. Each big letter should be made up of a bunch of *'s. For example, if your initials were "DJE", then the output would look something like:

           ******           *************        **********
           **    **                **            **
           **     **               **            **
           **      **              **            **
           **      **              **            ********
           **      **       **     **            **
           **     **         **    **            **
           **    **           **  **             **
           *****               ****              **********
           

Discussion

This is a very simple program. It consists of nothing but a sequence of System.out.println statements. (Alternatively, you could use TextIO.putln.) The hard part is designing the letters. You have to experiment in your text editor until you get letters that look right to you. Add System.out.println(" at the beginning of the first line, then copy-and-paste it to the beginning of each of the other lines. (You don't need to type it out on each line! Creative use of copy-and-paste can save you a lot of work.) Then add "); to the end of each line. In my program, I've also added a System.out.println() statement at the beginning and the end. The extra blank lines make the output look better.

Don't forget that in order to have a complete program, you have to put the program statements in a main subroutine and put that subroutine in a public class.


The Solution

    public class PrintInitials {
      
       /*  This program prints my initials (DJE) in big letters,
           where each letter is nine lines tall.
       */
    
       public static void main(String[] args) {
          System.out.println();
          System.out.println("           ******           *************        **********");
          System.out.println("           **    **                **            **");
          System.out.println("           **     **               **            **");
          System.out.println("           **      **              **            **");
          System.out.println("           **      **              **            ********");
          System.out.println("           **      **       **     **            **");
          System.out.println("           **     **         **    **            **");
          System.out.println("           **    **           **  **             **");
          System.out.println("           *****               ****              **********");
          System.out.println();
       }  // end main()

    }  // end class

[ Exercises | Chapter Index | Main Index ]

Related Tags for Java Programming: Solution to Programming Exercise:


More Tutorials from this section

Ask Questions?    Discuss: Java Programming: Solution to Programming Exercise  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.