Program : 1 Write a program to print Hello World in Java.
Code:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Output: Hello, World!
Program : 2 Write a program to add two numbers
Code:
public class JavaExample {
public static void main(String[] args) {
int num1 = 5, num2 = 15, sum;
sum = num1+num2;
[Link]("Sum of " +num1+" and "+num2+" is: "+sum);
}
}
Output:
Sum of 5 and 15 is: 20
Program : 3 Write a program to demonstrate the different access specifiers
Code:
class Data {
private String name;
public String getName() {
return [Link];
}
public void setName(String name) {
[Link]= name;
}
}
public class Main {
public static void main(String[] main){
Data d = new Data();
[Link]("Programiz");
[Link]([Link]());
}
}
Output: Programiz
Program : 4 Write a program to find the factorial of a number.
Code:
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;
for(i=1;i<=number;i++){
fact=fact*i;
}
[Link]("Factorial of "+number+" is: "+fact);
}
}
Output: Factorial of 5 is: 120
Program : 5 Write a program to calculate Fibonacci series
Code:
class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
[Link](n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
}
}
}
Output: 0 1 1 2 3 5 8 13 21 34
Program : 6 Write a program to add n numbers and series
Code:
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter how many numbers: ");
int n = [Link]();
int sum = 0;
for(int i = 0; i < n; i++) {
sum = sum + [Link]();
}
[Link]("Sum = " + sum);
}
}
Output: Enter how many numbers: 5
1
2
3
4
5
Sum = 15
Program : 7 Write a program which uses different packages
Code:
import static [Link];
class SortArray {
public static void main(String args[]) {
int [] arr = {30,40,10,20,50};
sort(arr);
for(int i : arr)
[Link](i + " ");
}
}
Output: 10 20 30 40 50
Program : 8 Write a program to demonstrate inheritance.
Code:
class A {
private int i;
protected int j;
public int m;
int n;
void set(int i){
this.i=i;
}
void showDataMember() {
[Link]("i,j, m and n: " + i + " " + j +" "+m+" " +n);
}
}
class B extends A {
int k;
void showk() {
[Link]("k: " + k);
}
void sum() {
[Link]("j+k: " + (j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
[Link](10);
superOb.j = 20;
[Link]("Contents of superOb: ");
[Link]();
[Link]();
[Link](7);
subOb.j = 8;
subOb.k = 9;
[Link]("Contents of subOb: ");
[Link]();
[Link]();
[Link]();
[Link]("Sum of i, j and k in subOb:");
[Link]();
}
}
Output:
Contents of superOb:
i,j, m and n: 10 20 0 0
Contents of subOb:
i,j, m and n: 7 8 0 0
k: 9
Sum of i, j and k in subOb:
j+k: 17
Program : 9 Write a java program to demonstrate Inheritance (Single Inheritance) for Student
details
Code:
class Person {
String name;
int age;
void displayPerson() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
class Student extends Person {
int rollNo;
void displayStudent() {
[Link]("Roll No: " + rollNo);
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Student s = new Student();
[Link] = "XYZ";
[Link] = 20;
[Link] = 101;
[Link]();
[Link]();
}
}
Output:
Name: XYZ
Age: 20
Roll No: 101
Program : 10 Write a program to demonstrate Method Overriding (Runtime Polymorphism)
for Shape → Circle.
Code:
class Shape {
void draw() {
[Link]("Drawing a shape");
}
}
class Circle extends Shape {
@Override
void draw() {
[Link]("Drawing a circle");
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
Shape s;
s = new Circle();
[Link]();
}
}
Output:
Drawing a circle
Program : 11 Write a program to perform Polymorphism with Multiple Child Classes.
Code:
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
void sound() {
[Link]("Cat meows");
}
}
public class AnimalDemo {
public static void main(String[] args) {
Animal a;
a = new Dog();
[Link]();
a = new Cat();
[Link]();
}
}
Output:
Dog barks
Cat meows
Program : 12 Write a program for Abstraction (Using Abstract Class) for Shape → Circle &
Rectangle
Code:
abstract class Shape {
abstract void draw();
void message() {
[Link]("This is a shape");
}
}
class Circle extends Shape {
void draw() {
[Link]("Drawing a Circle");
}
}
class Rectangle extends Shape {
void draw() {
[Link]("Drawing a Rectangle");
}
}
public class AbstractionDemo {
public static void main(String[] args) {
Shape s;
s = new Circle();
[Link]();
[Link]();
s = new Rectangle();
[Link]();
}
}
Output:
Drawing a Circle
This is a shape
Drawing a Rectangle
Program : 13 Write a Java Program for Encapsulation student class.
Code:
class Student {
private String name;
private int age;
public void setName(String name) {
[Link] = name;
}
public void setAge(int age) {
[Link] = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class EncapsulationDemo {
public static void main(String[] args) {
Student s = new Student();
[Link]("Kavi");
[Link](20);
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
}
}
Output:
Name: Kavi
Age: 20
Program : 14 Write a program to demonstrate switch case, if, if-else and for loop
Code:
import [Link];
class ControlDemo {
public static void main(String args[]) {
int num = 10;
if (num > 5) {
[Link]("Number is greater than 5");
}
if (num % 2 == 0) {
[Link]("Even number");
} else {
[Link]("Odd number");
}
for (int i = 1; i <= 3; i++) {
[Link]("For Loop: " + i);
}
int choice = 2;
switch (choice) {
case 1:
[Link]("Choice is 1");
break;
case 2:
[Link]("Choice is 2");
break;
default:
[Link]("Invalid choice");
}
}
}
Output:
Number is greater than 5
Even number
For Loop: 1
For Loop: 2
For Loop: 3
Choice is 2
Program : 15 Write a program to demonstrate the working of methods.
class Main {
public int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
public static void main(String[] args) {
int num1 = 25;
int num2 = 15;
Main obj = new Main();
int result = [Link](num1, num2);
[Link]("Sum is: " + result);
}
}
Output:
Sum is: 40
Program : 16 Write a program which has four methods – add(), subtract(), multiply() and
divide() and demonstrate a simple console calculator.
Code:
import [Link];
class Calculator {
double add(double a, double b) {
return a + b;
}
double subtract(double a, double b) {
return a - b;
}
double multiply(double a, double b) {
return a * b;
}
double divide(double a, double b) {
return a / b;
}
public static void main(String[] args) {
Calculator obj = new Calculator();
Scanner input = new Scanner([Link]);
[Link]("Enter two numbers:");
double num1 = [Link]();
double num2 = [Link]();
[Link]("Addition: " + [Link](num1, num2));
[Link]("Subtraction: " + [Link](num1, num2));
[Link]("Multiplication: " + [Link](num1, num2));
[Link]("Division: " + [Link](num1, num2));
[Link]();
}
}
Output:
Enter two numbers:
10
5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0
Program : 17 Write program to print * pattern using command line argument
Code:
class StarPattern {
public static void main(String args[]) {
if ([Link] == 0) {
[Link]("Please provide a number as command line argument.");
return;
}
int n = [Link](args[0]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Output: //Running at cmd prompt
javac [Link]
java StarPattern 5
*
**
***
****
*****