JAVA CORE JAVA FULL STACK DEVELOPMENT
Objectives of this Lab Manual This lab manual includes - Exercises for hands on practice Guidelines to complete the exercise Learning Outcome
Contents This lab manual includes the following exercises- 1. First Java Program 2. Class Design: Class & Objects 3. Polymorphism 4. Inheritance 5. Exception Handling 6. Threads: Multithreading 7. Java.lang: String & String Builder
First Java Program
Problem What will you do? Statement Write a Program to Print “I Love Java” in Java.
Solution What will be the steps Steps to get the output? • Step-1: Define a Class • Step-2: Begin and call the main function • Step-3: Write a command to print \"I Love Java\" to the terminal window.
Code // This is a simple Java program. // FileName : \"I Love Java.java\" class ILoveJava { public static void main(String args[]) { System.out.println(\"I Love Java\"); } }
Output Output for the Adedxetercxitsheewreil:l- be :- I Love Java Will be displayed.
Learning Outcome AddSteuxmt mheinreg:-Up After performing this exercise, you’ll be able to write basic programs in Java.
Class & Objects
Problem What will you do? Statement Write a basic program to declare a class named “Dog”, display the dog’s information such as breed, age, and color, and actions such as barking, playing, and sleeping.
Solution What will be the steps Steps to get the output? • Step-1: Define a Class • Step-2: Enter the information you want to display about the class in the field section. • Step-3: Enter the Actions performed by the dog in the methods section.
Code Code for Step 3: public class Dog { String breed; int age; String color; void barking() { } void playing() { } void sleeping() { } }
Learning Outcome AddSteuxmt mheinreg:-Up After performing this exercise, you will be able to declare a class, its field, and methods successfully.
Polymorphism
Problem What will you do? Statement Create two methods, the First method performs the addition of two numbers and the second method performs the addition of three numbers.
Solution What will be the steps Steps to get the output? • Step-1: We have to create two methods. The first, add() method performs the addition of two numbers and the second add method performs the addition of three numbers. • Step-2: The second add method performs the addition of three numbers. • Step-3: Run this code in the console and you will get the output
Code class Adder{ static int add(int a,int b){return a+b;} static int add(int a,int b,int c){return a+b+c;} } class TestOverloading1{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(11,11,11)); }}
Learning Outcome AddSteuxmt mheinreg:-Up After performing this exercise, you will be able to create methods and implement polymorphism successfully.
Inheritance
Problem What will you do? Statement Create a program where the programmer is an employee and it returns the salary of the programmer and bonus of the programmer.
Solution What will be the steps Steps to get the output? • Step-1: create Programmer as a subclass and Employee as a superclass. The relationship between the two classes is Programmer IS-A Employee. It means that a Programmer is a type of Employee. • Step-2: Enter the code to perform this problem • Step-3: Run this code in the console and you will get the output
Code class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println(\"Programmer salary is:\"+p.salary); System.out.println(\"Bonus of Programmer is:\"+p.bonus); }
Learning Outcome AddSteuxmt mheinreg:-Up After performing this exercise, you will be able to create a subclass and superclass and implement inheritance successfully.
Exception Handling
Problem What will you do? Statement Throw an exception (ArithmeticException) by dividing by zero. Surround the division operation inside a try block. Followed by a try block, create a catch block to handle the exception. Also, create a “finally” block and print a message through it.
Solution What will be the steps Steps to get the output? • Step-1: Create your class and main method. • Step-2: Create a try block and declare 3 int variables a, b and c. Initialize a and b with values 10 and 0 respectively. Then, divide a by b and store it in c in order to create an arithmetic exception. • Step-3: Create a catch block to handle the exception. Print the exception that is caught. • Step-4: Create a finally block and print a message displaying: “Inside finally block” inside it. • Step-5: Close all the method and class brackets and execute the program.
Code Code for Step 1: class ExceptionTest { public static void main(String[] args){ } } Code for Step 2: try { int a,b; a = 10; b = 0; c = a / b;
Code Code for Step 3: catch(Exception e) { System.out.println(e); } Code for Step 4: finally { System.out.println(“Inside finally block”); }
Output Output for the Adedxetercxitsheewreil:l- be :- java.lang.ArithmeticException: / by zero Inside finally block
Learning Outcome AddSteuxmt mheinreg:-Up In this lab exercise we learnt: ⦁ Exception handling ⦁ Try, catch and finally block
Multithreading
Problem What will you do? Statement Create a thread by extending the Thread class. Assign the thread some suitable name and print its name 3 times. In each iteration, put the thread to sleep for 2 seconds.
Solution What will be the steps Steps to get the output? • Step 1: Create a myThread class and extend the Thread class upon it. Define the run method to print the name 3 times through a for loop. In each iteration call the sleep method and pass 2000 as a parameter that specifies to sleep for 2 seconds. • Step 2: Create your driver class. Create an object of the myThread class. Set the name of the thread as “Java Thread”. Call start method on the thread. • Step 3: Close all the method and class brackets and execute the program.
Code Code for step 1: class myThread extends Thread { public void run() { String name = Thread.currentThread().getName(); for(int i=0;i<3;i++) { System.out.println(name); try { Thread.sleep(2000); }
Code Code for step 1: catch(Exception e) { System.out.println(e); } } } }
Code Code for step 2: class ThreadDemo { public static void main(String[] args) { myThread t1 = new myThread(); t1.setName(\"Java Thread\"); t1.start(); } }
Learning Outcome AddSteuxmt mheinreg:-Up Hence, in this lab exercise, you learnt: • Multithreading • How to create a thread • Setting and getting the name of the thread • Implementing sleep method
String and String Builder
Problem What will you do? Statement Create a String object by character array method and store “Hello World” in the object. Also, create a String Builder object and pass the same String created above. Display the length of the String. Display the character in the second position. Display the String in uppercase. Split the string at the space character. Append “, How are you?” to the String Builder. Replace “Hello” with “Hi” in the String Builder. Now, delete the “Hi” substring. Finally, display the String Builder in reverse order.
Solution What will be the steps Steps to get the output? • Step 1: Create your class and the main method. • Step 2: Firstly, create the character array representing “Hello World” then convert it into a String. Create an object of String Builder by passing the String created above. • Step 3: Next perform the operations on String. First, display the String. Display the length using the length method. Display the character at 1st index using charAt method. Display the String in uppercase using toUpperCase method. Split the String using the split method and pass the whitespace character(“ “). Iterate through the array of split String and print each part. • Step 4: Next perform the operations on String Builder. First, display the String Builder. • Step 5: Close all the method and class brackets and execute the program.
Code Code for step 1: class StringDemo { public static void main(String[] args) { } } Code for step 2: char[] chArray = {'H','e','l','l','o',' ','W','o','r','l','d'}; String str = new String(chArray); StringBuilder sb = new StringBuilder(str);
Code Code for step 3: System.out.println(\"String : \" + str); System.out.println(\"Length of the String : \" + str.length()); System.out.println(\"Displaying the character at second position : \" + str.charAt(1)); System.out.println(\"String in uppercase : \" + str.toUpperCase()); String[] splitString = str.split(\" \"); System.out.print(\"Split String : \"); for(String s : splitString) System.out.print(s + \", \"); System.out.println(\"\\n----------------------------------------------------\");
Code Code for step 4: System.out.println(\"String Builder : \" + sb); sb.append(\", How are you?\"); System.out.println(\"Appended String Builder : \" + sb); sb.replace(0,5,\"Hi\"); System.out.println(\"Updated String Builder : \" + sb); sb.delete(0,2); System.out.println(\"Delted String Builder : \" + sb); sb.reverse(); System.out.println(\"Reverse String Builder : \" + sb);
Output Output for the Adedxetercxitsheewreil:l- be :- String : Hello World Length of the String : 11 Displaying the character at second position : e String in uppercase : HELLO WORLD Split String : Hello, World,
Output Output for the Adedxetercxitsheewreil:l- be :- String Builder : Hello World Appended String Builder : Hello World, How are you? Updated String Builder : Hi World, How are you? Deleted String Builder : World, How are you? Reverse String Builder : ?uoy era woH ,dlroW
Output Output for the Adedxetercxitsheewreil:l- be :- String Builder : Hello World Appended String Builder : Hello World, How are you? Updated String Builder : Hi World, How are you? Delted String Builder : World, How are you? Reverse String Builder : ?uoy era woH ,dlroW
Learning Outcome AddSteuxmt mheinreg:-Up In this lab exercise we learnt: • Declaration of String and implementing String methods • Declaration of String Builder and implementing String Builder methods
Happy Learning!
Search
Read the Text Version
- 1 - 49
Pages: