Programming PDF
Programming PDF
programming
Important point
1-Any program we have in Java 2- Any program must have at 3- Any code you write in
must have something least one class Java must be inside a
called a class. it could be 10, 20, or 100 class
Class consist of: classes
.Methods Data items
4- If I have more than one 5- Any method or class must 6- My statement must end
have a beginning and an end with a semi-colon ";“
class in the same program
it's no problem, the important thing is that start with "{ " and end with
it has a main method, which is the " }“
entry point or the starting point for
executing the program.
7- Keep in mind that Java is case-sensitive 8- Any code you write will be stored in a
(System not system) file with the extension .java (mo.java)
Output / Print
System.out.print();
package class
method
\t Tab
hello world!
'HELLO WORLD!'
Formatting numbers
• System.out.printf("it is an integer: %d%n", 10000);
- System.out.printf("%f%n", 3.1423);
- System.out.printf("%3.2f'%n", 3.1423);
Comments in Java
public class MyFirstJavaProgram {
/* This is my first java program. * This will print 'Hello World' as the output * This is an
example of multi-line comments. */
public static void main(String []args) {
// This is an example of single line comment
/* This is also an example of single line comment. */
System.out.println("Hello World");
}
}
First program
public class MyFirstJavaProgram {
/* This is my first java program. * This will print 'Hello World' as the output */
public static void main(String []args)
{
System.out.println("Hello World"); // prints Hello World
}
}
Ex 1
Write an application that displays the numbers 1 to 4 on the same line,with
each pair of adjacent numbers separated by one space. Use the following
techniques:
a) Use one System.out.println statement.
b) Use four System.out.print statements.
c) Use one System.out.printf statement.
Ex 3
What does the following code print?
System.out.println( "*\n**\n***\n****\n*****" );
Ex 4
What does the following code print?
System.out.println( "*" );
System.out.println( "*\n**" );
System.out.println( "*****" );
System.out.println( "****" );
System.out.println( "**" );
Ex 6
What does the following code print?
System.out.printf( "%s\n%s\n%s\n", "*", "***", "*****" );