Exercise Java PrintCommand
Exercise Java PrintCommand
EXERCISE SHEET 1
Ex1. Write a Java program to print your name and age in two separate lines.
Solution:
Another Solution:
Page 1 of 4
MIS 202 – Introduction to Programming Exercise sheet 1
Another Solution:
int number1 = 22
Ex2. Write a Java program to display the asterisk pattern as shown below:
*****
*****
*****
*****
*****
Solution:
System.out.println("*****");
System.out.println("*****");
Page 2 of 4
MIS 202 – Introduction to Programming Exercise sheet 1
System.out.println("*****");
System.out.println("*****");
System.out.println("*****");
Solution 1:
System.out.println(“ * “);
System.out.println(“ * * “);
System.out.println(“* *“);
System.out.println(“ * * “);
System.out.println(“ * “);
Solution 2:
System.out.println(“ * “);
Page 3 of 4
MIS 202 – Introduction to Programming Exercise sheet 1
System.out.println(“*****“);
System.out.println(“ * “);
Ex4. Use escape characters to print your name, ID, and the course’s name is a box, which should look
like this:
Suggested Solution: (there are other ways to solve this problem.. you don’t necessarily have to use the
\t escape character)
public class Exercise4
{
public static void main(String args[])
{
System.out.println( "+-----------------------+" );
System.out.println( "|\t\t\t\t\t\t|" );
System.out.println( "|\tAhmed Mohamed\t\t|" );
System.out.println( "|\t\t\t\t\t\t|" );
System.out.println( "|\t20151234\t\t\t|" );
System.out.println( "|\t\t\t\t\t\t|" );
System.out.println( "|\tJava Programming\t|" );
System.out.println( "+-----------------------+" );
}
}
Page 4 of 4