Practical_1_P&PS Aim: Introduction of Java Programming Language. Theory: Introduction: 1. Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. 2. It is intended to let application developers write once, and run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. 3. Java was first released in 1995 and is widely used for developing applications for desktop, web, and mobile devices. 4. Java is known for its simplicity, robustness, and security features, making it a popular choice for enterprise-level applications. History: 1. Java’s history is very interesting. It is a programming language created in 1991. 2. James Gosling, Mike Sheridan, and Patrick Naughton, a team of Sun engineers known as the Green team initiated the Java language in 1991. 3. Sun Microsystems released its first public implementation in 1996 as Java 1.0. 4. Java is the name of an island in Indonesia where the first coffee(named java coffee) was produced. And this name was chosen by James Gosling while having coffee near his office. Note that Java is just a name, not an acronym. Implementation of a Java application program involves the following step. They include: 1. Creating the program 2. Compiling the program 3. Running the program Features of Java 1. Platform Independent: Compiler converts source code to bytecode and then the JVM executes the bytecode generated by the compiler. This bytecode can run on any platform be it Windows, Linux, or macOS which means if we compile a program on Windows, then we can run it on Linux and vice versa. Each operating system has a different JVM, but the output produced by all the OS is the same after the execution of the bytecode. That is why we call java a platform-independent language. 1 2. Object-Oriented Programming Language: Organizing the program in the terms of a collection of objects is a way of object-oriented programming, each of which represents an instance of the class. The four main concepts of Object-Oriented programming are: • Abstraction • Encapsulation • Inheritance • Polymorphism 3. Simple: Java is one of the simple languages as it does not have complex features like pointers, operator overloading, multiple inheritances, and Explicit memory allocation. 4. Robust: Java language is robust which means reliable. It is developed in such a way that it puts a lot of effort into checking errors as early as possible, that is why the java compiler is able to detect even those errors that are not easy to detect by another programming language. 5. Secure: In java, we don’t have pointers, so we cannot access out-of-bound arrays i.e it shows ArrayIndexOutOfBound Exception if we try to do so. That’s why several security flaws like stack corruption or buffer overflow are impossible to exploit in Java. 6. Distributed: We can create distributed applications using the java programming language. Remote Method Invocation and Enterprise Java Beans are used for creating distributed applications in java. The java programs can be easily distributed on one or more systems that are connected to each other through an internet connection. 7. Multithreading: Java supports multithreading. It is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of the CPU. 8. Portable: As we know, java code written on one machine can be run on another machine. The platformindependent feature of java in which its platform-independent bytecode can be taken to any platform for execution makes java portable. 9. High Performance: Java architecture is defined in such a way that it reduces overhead during the runtime and at sometimes java uses Just In Time (JIT) compiler where the compiler compiles code ondemand basics where it only compiles those methods that are called making applications to execute faster. 10. Dynamic flexibility: Java being completely object-oriented gives us the flexibility to add classes, new methods to existing classes, and even create new classes through sub-classes. Java even supports functions written in other languages such as C, C++ which are referred to as native methods. Conclusion: In this way we have studied introduction of java programming language. 2 Practical_2_P&PS Aim: Installation of JDK Theory: Introduction: In this section, we will learn how to write the simple program of Java. We can write a simple hello Java program easily after installing the JDK. To create a simple Java program, you need to create a class that contains the main method. Let's understand the requirement first. The requirement for Java Hello World Example For executing any Java program, the following software or application must be properly installed. o Install the JDK if you don't have installed it, download the JDK and install it. o Set path of the jdk/bin directory. o Create the Java program. o Compile and run the Java program. Creating Hello World Example Let's create the hello java program: 1. class Simple{ 2. public static void main(String args[]){ 3. System.out.println("Hello Java"); 4. } 5. } Save the above file as Simple.java. To compile: javac Simple.java To execute: java Simple Output: Hello Java Compilation Flow: When we compile Java program using javac tool, the Java compiler converts the source code into byte code. 3 o class keyword is used to declare a class in Java. o public keyword is an access modifier that represents visibility. It means it is visible to all. o static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main() method is executed by the JVM, so it doesn't require creating an object to invoke the main() method. So, it saves memory. o void is the return type of the method. It means it doesn't return any value. o main represents the starting point of the program. o String[] args or String args[] is used for command line argument. We will discuss it in coming section. o System.out.println() is used to print statement. Here, System is a class, out is an object of the PrintStream class, println() is a method of the PrintStream class. We will discuss the internal working of System.out.println() statement in the coming section. To write the simple program, you need to open notepad by start menu -> All Programs -> Accessories -> Notepad and write a simple program as we have shownbelow: As displayed in the above diagram, write the simple program of Java in notepad and saved it as Simple.java. In order to compile and run the above program, you need to open the command prompt 4 by start menu -> All Programs -> Accessories -> command prompt. When we have done with all the steps properly, it shows the following output: To compile and run the above program, go to your current directory first; my current directory is c:\new. Write here: To compile: javac Simple.java To execute: java Simple Program for execution: 1. Java Program to Find Sum of Natural Numbers. 2. Program to swap two numbers. 3. Java Program to Find Sum of Natural Numbers 4. Java Program to Find Largest of Three Numbers 5. Java Program to Check if a Number is Positive or Negative Instruction: Copy paste the program in the template and attach output screenshot. Conclusion: In this way we have studied how to install JDK and run the java program. 5 Practical_3_P&PS Aim: Introduction of Control Statements in Java Programming Language. Theory: Introduction: Java compiler executes the code from top to bottom. The statements in the code are executed according to the order in which they appear. However, Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program. Java provides three types of control flow statements. 1. Decision Making statements. o if statements o switch statement 2. Loop statements o do while loop o while loop o for loop o for-each loop 3. Jump statements o break statement o continue statement 1. Decision Making statements 1) Simple if statement: It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true. Syntax of if statement is given below. 1. if(condition) { 2. statement 1; //executes when condition is true 3. } 2) if-else statement 6 The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false. Syntax: 1. if(condition) { 2. statement 1; //executes when condition is true 3. } 4. else{ 5. statement 2; //executes when condition is false 6. } 3) if-else-if ladder: The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true. We can also define an else statement at the end of the chain. Syntax of if-else-if statement is given below. 1. if(condition 1) { 2. statement 1; //executes when condition 1 is true 3. } 4. else if(condition 2) { 5. statement 2; //executes when condition 2 is true 6. } 7. else { 8. statement 2; //executes when all the conditions are false 9. } 4. Nested if-statement In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement. Syntax of Nested if-statement is given below. 1. if(condition 1) { 2. statement 1; //executes when condition 1 is true 3. if(condition 2) { 4. statement 2; //executes when condition 2 is true 5. } 7 6. else{ 7. statement 2; //executes when condition 2 is false 8. } 9. } 5.Switch Statement: In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. The syntax to use the switch statement is given below. 1. switch (expression){ 2. case value1: 3. statement1; 4. break; 5. . 6. . 7. . 8. case valueN: 9. statementN; 10. break; 11. default: 12. default statement; 13. } 2. Loop statements 6.Java for loop It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want to execute the block of code. 1. for(initialization, condition, increment/decrement) { 2. //block of statements 3. } 7.Java for-each loop Java provides an enhanced for loop to traverse the data structures like array or collection. In the for-each loop, we don't need to update the loop variable. The syntax to use the for-each loop in java is given below. 8 1. for(data_type var : array_name/collection_name){ 2. //statements 3. } 8.Java while loop The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. The syntax of the while loop is given below. 1. while(condition){ 2. //looping statements 3. } 9.Java do-while loop The do-while loop checks the condition at the end of the loop after executing the loop statements. When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop. The syntax of the do-while loop is given below. 1. do 2. { 3. //statements 4. } while (condition); 3. Jump statements 10.Java break statement The break statement cannot be used independently in the Java program, i.e., it can only be written inside the loop or switch statement. 11.Java continue statement Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of the loop immediately. 9 Problem Statement: Write a program called IncomeTaxCalculator for salaried Employee. Design employee class with attributes like Emp_id, Emp_name, Age, Annual_income, Tax etc. and calculate total income tax for a particular financial year. (Use java methods) Hint: Slab rate IT rate Up to Rs. 5 lacs Nil From Rs. 5 lacs- Rs. 10 lacs 10% on additional amount From Rs. 10 lacs- Rs. 50 lacs 20% on additional amount Above Rs. 50 lacs 30% on additional amount Instruction: Execute the program then paste it in the template and attach output screenshot. Conclusion: In this way we have studied Control statements in Java Programming Language. 10