M105 Exercise 01 Solution

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

M105 – Meeting 1 - Extra Exercises

Notes: 1) You need to install JDK + NetBeans IDE to your machine in order to solve the above
problems. The file “How to install JDK and NetBeans” could help you. 2) You could access the
website of the text book and make a quick tour to view its contents. The file “How to access
_Java How to Program_ website” could help you.
-------------------- Exercise 1: Write a Java program to
print your name and your student ID on the standard output in 2 lines. Solution
public class Q1 {
public static void main(String[] args) {
System.out.println("My name is Ali"); System.out.println("My Student ID is 00000000"); } }
Exercise 2: Write a Java program to print the first letter of your name on the standard output in a
nice format: Assume your name is Ayman, so you should print the letter A as follows:
A A A A A AAAAA A A A A Solution
public class Q2 {
public static void main(String[] args) {
System.out.println(" A "); System.out.println(" A A "); System.out.println(" A A ");
System.out.println(" AAAAA "); System.out.println(" A A "); System.out.println("A A"); /////OR
//System.out.printf("%6S\n%7S\n%8s\n%9S\n%10s\n","A","A A","A A","AAAAA","A A", "A
A"); } }
1
Exercise 3: Write a single Java statement to print exactly the following message on the standard
output: The path of my TMA file is: "c:\M105\TMA" Solution
Exercise 4: What is the exact output of the following piece of code? System.out.print("Java is ");
System.out.println("a"); System.out.print(" programming language");
Solution
Exercise 5: The following code includes 10 compilation errors (an error per line). a) Discover
and correct them. b) Give the exact output after correcting the code. / Try to find the errors
Public class Test1 {
public static main(String[] a) {
System.out.println('Welcome'); System.out.printIn("to"); System.out.println("M105);
System.out.print("Nice ") System.out.println(Program); { ]
public class Q3 {
public static void main(String[] args) {
System.out.println("The path of my TMA file is:\n\"C:\\M105\\TMA\"");
}
}
Java is a
Programming language
2
Solution
/ Try to find the errors Comment must be //
Correction: // Try to find the errors Public class
Test1 { P in Public must be small letter (p)
Correction : public class Test1 { public static
main(String[] a) { missing void between static and main
Correction: public static void main(String[] a)
{ System.out.println('Welcome'); 'Welcome' must be "Welcome"
Correction: System.out.println(“Welcome”);
System.out.printIn("to"); printIn must be println
Correction: System.out.println("to");
System.out.println("M105); missing "
Correction: System.out.println("M105”);
System.out.print("Nice ") missing ;
Correction: System.out.print("Nice ");
System.out.println(Program); missing ""
Correction: System.out.println(“Program”);
{ must be } ] must be }
The output: Welcome to M105 Nice Program
3

You might also like