0% found this document useful (0 votes)
102 views

Exercise Java PrintCommand

This document contains 5 programming exercises from an introductory Java programming course. It provides the instructions for each exercise and multiple solutions in Java code. The exercises include printing a name and age, displaying asterisk patterns, printing in a box format using escape characters, and displaying a multiplication table. The solutions show different approaches to completing the same tasks in Java.

Uploaded by

hmasry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Exercise Java PrintCommand

This document contains 5 programming exercises from an introductory Java programming course. It provides the instructions for each exercise and multiple solutions in Java code. The exercises include printing a name and age, displaying asterisk patterns, printing in a box format using escape characters, and displaying a multiplication table. The solutions show different approaches to completing the same tasks in Java.

Uploaded by

hmasry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MIS 202 – Introduction to Programming Exercise sheet 1

EXERCISE SHEET 1
Ex1. Write a Java program to print your name and age in two separate lines.

Solution:

public class Exercise1

public static void main(String args[])

System.out.printf(“My name is Ahmed Mohamed”);

System.out.printf(“I am 22 years old”);

Another Solution:

public class Exercise1

public static void main(String args[])

System.out.printf(“My name is” + “ Ahmed Mohamed”);

System.out.printf(“I am” +” 22”+” years old”);

Page 1 of 5
MIS 202 – Introduction to Programming Exercise sheet 1

Another Solution:

public class Exercise1

public static void main(String args[])

int number1 = 22

System.out.printf(“%s %s”, “My name is” , “ Ahmed Mohamed”);

System.out.printf( “%s%d %s”, “I am” + number1+” years old”);

Ex2. Write a Java program to display the asterisk pattern as shown below:

*****

*****

*****

*****

*****

Solution:

public class Exercise2

public static void main(String[] args)

System.out.println("*****");

System.out.println("*****");

Page 2 of 5
MIS 202 – Introduction to Programming Exercise sheet 1

System.out.println("*****");

System.out.println("*****");

System.out.println("*****");

Ex3. Write a Java program to display a diamond shape with asterisks.

Solution 1:

public class Exercise3 //this exercise will display an empty diamond

public static void main(String args[])

System.out.println(“ * “);

System.out.println(“ * * “);

System.out.println(“* *“);

System.out.println(“ * * “);

System.out.println(“ * “);

Solution 2:

public class Exercise3 //this exercise will display a full diamond

public static void main(String args[])

System.out.println(“ * “);

Page 3 of 5
MIS 202 – Introduction to Programming Exercise sheet 1

System.out.println(“ *** “);

System.out.println(“*****“);

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 5
MIS 202 – Introduction to Programming Exercise sheet 1

Ex5. Write a Java program to display a multiplication table for values 1 to 5, that the output should look
like this:

Solution:
public class Exercise5
{
public static void main(String args[])
{
System.out.println( "======================================" );
System.out.println( "| || 1 | 2 | 3 | 4 | 5 |" );
System.out.println( "======================================" );
System.out.println( "| 1 || 1 | 2 | 3 | 4 | 5 |" );
System.out.println( "--------------------------------------" );
System.out.println( "| 2 || 2 | 4 | 6 | 8 | 10 |" );
System.out.println( "--------------------------------------" );
System.out.println( "| 3 || 3 | 6 | 9 | 12 | 15 |" );
System.out.println( "--------------------------------------" );
System.out.println( "| 4 || 4 | 8 | 12 | 16 | 20 |" );
System.out.println( "--------------------------------------" );
System.out.println( "| 5 || 5 | 10 | 15 | 20 | 25 |" );
System.out.println( "======================================" );
}
}

Page 5 of 5

You might also like