0% found this document useful (0 votes)
8 views1 page

Java Programming Practical Exercises

The document is a practical file from Agarwal P.G. College, Jaipur, containing a list of Java programming exercises. It includes tasks such as printing messages, swapping numbers, creating calculators, and implementing various programming concepts like arrays, inheritance, and multithreading. Each exercise aims to enhance students' understanding of Java programming fundamentals.

Uploaded by

faraz212830
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Java Programming Practical Exercises

The document is a practical file from Agarwal P.G. College, Jaipur, containing a list of Java programming exercises. It includes tasks such as printing messages, swapping numbers, creating calculators, and implementing various programming concepts like arrays, inheritance, and multithreading. Each exercise aims to enhance students' understanding of Java programming fundamentals.

Uploaded by

faraz212830
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭Agarwal P.G.

College‬

‭(Jaipur)‬

‭Practical File‬

‭ .‬ ‭Write a Java program to print “Welcome To The Java”.‬


1
‭2.‬ ‭Write a Java program to swap two numbers without the 3rd variable.‬
‭3.‬ ‭Write a Java program to create a calculator for all arithmetic operations.‬
‭4.‬ ‭Write a Java program to find the largest of three numbers.‬
‭5.‬ ‭Write a Java program to check whether a number is even or odd.‬
‭6.‬ ‭Write a Java program to determine if a year is a leap year.‬
‭7.‬ ‭Write a Java program to find the grade of a student based on marks.‬
‭8.‬ ‭Write a Java program to check if a character is a vowel or consonant.‬
‭9.‬ ‭Write a Java program to Check if a given number is positive, negative, or zero.‬
‭10.‬ ‭Write a Java program to print the first N Fibonacci numbers.‬
‭11.‬ ‭Write a Java program to Check if a given number is an armstrong number.‬
‭12.‬ ‭Write a Java program to Create Student Object class & Create Student Objects.‬
‭13.‬ ‭Write a java program to create a Display() methode to print Objects.‬
‭14.‬ ‭Write a Java program to create Array.‬
‭15.‬ ‭Write a Java program to create 2d Array.‬
‭16.‬ ‭Write a Java program to print Array using For loop & Foreach loop.‬
‭17.‬ ‭Write a program to create an Array of Object of Employe class.‬
‭18.‬ ‭Write a Java program to create String and apply the following methods.‬
‭length(), charAt(), substring(), toLowerCase(), toUpperCase(), trim(), split(), replace(), indexOf()‬

‭19.‬ ‭Write a Java program to apply all types of inheritance.‬


‭1. Single level 2. Multi level [Link] Inheritance 4. Hybrid Inheritance‬

‭20.‬ ‭Write a Java program to achieve polymorphism‬


‭1. RunTime polymorphism 2. CompileTime polymorphism‬

‭21.‬ ‭Write a java program to use Multithreading programing and use following methods.‬
‭1. run() 2. Start() 3. Sleep() 4. Join()‬

Common questions

Powered by AI

Multithreading in Java allows multiple threads to run concurrently, enhancing program efficiency by utilizing CPU resources better, reducing time for task completion, and allowing procedures to run asynchronously. Essential methods include `run()` for executing the thread's operation, `start()` to begin a thread's execution, `sleep()` to pause a thread temporarily, and `join()` which allows one thread to wait for another to complete. These methods help manage thread lifecycle, synchronization, and execution order, which are crucial for optimal multithreaded program performance .

A `for` loop is useful for iterating through an array when index-based access is needed, allowing for operations that require the current index or traversing only parts of an array. A `foreach` loop, or enhanced for loop, simplifies syntax when you need to traverse every element in an array without modifying it. The `foreach` loop is more appropriate when you only need the values directly. However, to modify elements or require the index, a `for` loop is more suitable .

Arrays in Java provide a way to store multiple elements of the same type together efficiently, which allows for easy iteration and management of data. To implement a 2D array to represent matrix data, declare it using `int[][] matrix = new int[rows][columns];` where `rows` and `columns` determine the matrix size. Elements can be accessed and modified using two indices, e.g., `matrix[i][j]`. This provides a straightforward method for creating and manipulating matrices in problems involving linear algebra or grid-based tasks .

Polymorphism in Java can be demonstrated by compile-time polymorphism through method overloading, where methods have the same name but different parameters within the same class. For runtime polymorphism, method overriding is used, where a subclass provides a specific implementation of a method already defined in its superclass. An example involves creating a superclass with a method and extending it in subclasses with overridden methods, then using a superclass reference pointing to subclass objects to call the overridden methods .

A year is a leap year if it is divisible by 4 but not divisible by 100, except if it is also divisible by 400. In Java, this can be implemented as follows: if `(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)`, then it is a leap year. The year check can be implemented within a function that takes an integer input for the year and returns a boolean indicating if it's a leap year .

An Armstrong number is a number that equals the sum of its own digits each raised to the power of the number of digits. In Java, this involves extracting each digit of the number, raising it to the power of the number of digits, and summing the result. If the sum equals the original number, it is an Armstrong number. This can be implemented by looping through each digit, using modulus and division for extraction, and keeping a count of digits. Armstrong numbers are significant in number theory for their mathematical properties and applications in digital systems .

To find the largest of three numbers, you can use nested conditional statements (if-else) in Java. For numbers `a`, `b`, and `c`, you can implement: `if (a >= b && a >= c)`, then `a` is the largest; `else if (b >= a && b >= c)`, then `b` is; else `c` is the largest. This uses pairwise comparison to determine the largest number and can be encapsulated within a method for clarity .

A basic calculator in Java can be implemented using methods for each arithmetic operation: addition, subtraction, multiplication, and division. The structure involves a main class with methods for each operation, receiving two parameters of the number type. Use a switch statement or if-else conditions within a loop to determine which operation the user selects. Input can be handled via Scanner for interactive usage or method parameters for non-interactive situations. This modular approach allows easy extension for additional operations or UI integration .

Creating objects from a Student class in Java demonstrates several core principles of object-oriented programming, including encapsulation, inheritance, and polymorphism. Encapsulation is shown by defining class fields as private and providing public methods for access and modification. Inheritance could be used if the Student class extends a more generic Person class. Polymorphism is utilized if the Student class overrides methods or is part of an interface implementation. These principles promote reusability, flexibility, and maintainability in Java applications .

To swap two numbers without using a third variable in Java, you can use arithmetic operations or bitwise XOR operation. For instance, using arithmetic operations, let `a` and `b` be the numbers, you can swap them as follows: `a = a + b; b = a - b; a = a - b;`. This works by first summing `a` and `b` and storing it in `a`, then subtracting the new `a` from `b` to get the original `a`, and finally subtracting the modified `b` from the new `a` to get the original `b`. Alternatively, you can use XOR operation: `a = a ^ b; b = a ^ b; a = a ^ b;` which follows similar logical manipulations .

You might also like