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

Programming PDF

PROGRAMING

Uploaded by

moamenasam234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Programming PDF

PROGRAMING

Uploaded by

moamenasam234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Principles of

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();

System out print

package class
method

class class method method


Output Data / Print

1 System.out.println() 2 System.out.print() 3 System.out.printf()


Prints a string to the Prints a string to the The printf() function in Java
console, followed by a console, without a stands for “print
newline character. newline character. formatted.” It allows you to
print text to the console
with specific formatting.
This means you can control
how the output looks
Escape Sequence
\n Newline

\t Tab

Carriage return —> System.out.print("hello\rworld");


\r
// Output : world

\\ print backslash character

\” print double-quote character


Formatting With printf()
Specifier Explanation
%c Format characters
%d Format decimal (integer) numbers (base 10)
%e Format exponential floating-point numbers
%f Format floating-point numbers
%o Format octal numbers (base 8)
%s Format string of characters
%x Format numbers in hexadecimal (base 16)
%n add a new line character
Formatting a string

1 System.out.printf("%s%n", "hello world!"); 2 System.out.printf("'%S' %n", "hello world!");


Prints a string to the console, followed by a newline
Prints a string to the console, followed by a
character.
newline character and enclosed in single quotes.

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", "*", "***", "*****" );

You might also like