The following list presents some of the Java Code Examples.
Basic Java Code Examples
- In order to find area of a rectangle, create a class Rectangle with data members length and breadth and member functions getDimensions() for reading input and findArea() to display the output.
- Write a program to demonstrate the use of method overloading to calculate area of square, rectangle and triangle.
- Develop a program in Java to demonstrate the use of static variable, static method and static block.
- Write a program to demonstrate concept of “this“ keyword in Java.
- Create a program to demonstrate multi-level and hierarchical inheritance.
- Write a program to use super() to invoke base class constructor.
- Further, write a program to demonstrate run-time polymorphism.
- Write a program to demonstrate the concept of aggregation.
- Develop a program to demonstrate the concept of abstract class with constructor and “final“ method.
- Write a program to demonstrate the concept of interface when two interfaces have unique methods and same data members.
- Create a program to demonstrate checked exception during file handling.
- Write a program to demonstrate unchecked exception
- Similarly, write a program to demonstrate creation of multiple child threads.
- Write a program to use Byte stream class to read from a text file and display the content on the output screen.
- Create a program to demonstrate any event handling.
Advanced Java Code Examples
- Write a program to create a class employee which have name, age and address of employee, include methods getdata() and showdata(), getdata() takes the input from the user, showdata() display the data in following format: Name: Age: Address:
- Furthermore, write a Java program to perform basic Calculator operations. Make a menu driven program to select operation to perform (+ – * / ). Take 2 integers and perform operation as chosen by user.
- Write a Java program to find out whether the given String is Palindrome or not.
- Likewise, write a program to make use of BufferedStream to read lines from the keyboard until ‘STOP’ is typed.
- Write a program declaring a Java class called SavingsAccount with members “accountNumber“ and “Balance“. Provide member functions as “depositAmount ()“ and “withdrawAmount ()“. If user tries to withdraw an amount greater than their balance then throw a user-defined exception.
- Develop a program creating 2 threads using Runnable interface. Print your name in “run ()“ method of first class and “Hello Java” in “run ()“ method of second thread.
- Write a program to implement inter-thread communication.
- Create program that uses swings to display combination of RGB using 3 scrollbars.
- Develop a swing application that uses at least 5 swing controls
- Write a program to implement border layout using Swing.