Java Programming Principles PDF
Document Details
Uploaded by AuthoritativeDemantoid
Horus University in Egypt
Tags
Related
- Advanced Java Programming (22517) Past Paper Chapter 1 PDF
- Introduction to Java Programming and Data Structures (2019) by Y. Daniel Liang - PDF
- Concepts des langages objet - Programmation Orientée Objet - JAVA PDF
- Java Programming Concepts (Lesson Plan)
- Java Programming in JAVA (CSE2006) PDF
- CST8116 Intro to Comp. Prog. Programming Overview PDF
Summary
This document provides an overview of core Java programming concepts, including output formatting techniques, such as using println, print, and printf. Escape sequences and examples of basic Java coding are also covered. It appears to be a set of lecture notes or study materials, rather than a formal exam.
Full Transcript
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...
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 class in the same program have a beginning and an end with a semi-colon ";“ 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 console, followed by a console, without a Java 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 { public static void main(String []args) { // This is an example of single line comment System.out.println("Hello World"); } } First program public class MyFirstJavaProgram { 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 2 What does the following code print? System.out.println( "*\n**\n***\n****\n*****" ); Ex 3 What does the following code print? System.out.println( "*" ); System.out.println( "***" ); System.out.println( "*****" ); System.out.println( "****" ); System.out.println( "**" ); Ex 4 What does the following code print? System.out.printf( "%s\n%s\n%s\n", "*", "***", "*****" );