1) Write a program in Java to print “Hello World”
PROGRAM/CODE :
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Result:
To Compile :
C:\Users\VIHAAN\Desktop\GSK>javac HelloWorld.java
To Run :
C:\Users\VIHAAN\Desktop\GSK>java HelloWorld
OUTPUT :
Hello World
2) Write a program in Java to print Area of a triangle
PROGRAM/CODE :
import java.util.Scanner;
class AreaOfTriangle{
public static void main(String[] args) {
int base,height;
double area;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base of the triangle :");
base = sc.nextInt();
System.out.println("Enter the height of triangle :");
height = sc.nextInt();
area = (height * base)/2;
System.out.println("Area of the triangle is ::"+area);
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac AreaOfTriangle.java
C:\Users\VIHAAN\Desktop\GSK>java AreaOfTriangle
Enter the base of the triangle :
10
Enter the height of triangle :
20
Area of the triangle is ::100.0
3. Write a program in Java to print Fibonacci series till number N.
PROGRAM/CODE :
import java.util.Scanner;
class Fibonacci{
public static void main(String args[])
{
int N, counter=0;
int num1=0,num2=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter N:");
N = sc.nextInt();
while (counter < N) {
System.out.print(num1 + " ");
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac Fibonacci.java
C:\Users\VIHAAN\Desktop\GSK>java Fibonacci
Enter N:
10
0 1 1 2 3 5 8 13 21 34
4) Write a program in Java to create a class Student with data name, roll_no and age
along with a method to display the data. Create two objects to access Student data.
class Student {
int roll_no,age;
String name;
void display()
{
System.out.println("Student Name :"+name);
System.out.println("Student Roll No :"+roll_no);
System.out.println("Student Age :"+age);
}
}
class StudentData{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.name = "Naveen";
s1.roll_no = 50;
s1.age = 20;
s1.display();
s2.name = "Ram";
s2.roll_no = 40;
s2.age = 21;
s2.display();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac StudentData.java
C:\Users\VIHAAN\Desktop\GSK>java StudentData
Student Name :Naveen
Student Roll No :50
Student Age :20
Student Name :Ram
Student Roll No :40
Student Age :21
5) Write a program in Java to create a class with a constructor.
class Student {
String name;
int roll_no, age;
Student(String n,int r, int a)
{
this.name=n;
this.roll_no=r;
this.age=a;
}
void display()
{
System.out.println("Student Name :"+name);
System.out.println("Student Roll No :"+roll_no);
System.out.println("Student Age :"+age);
}
}
class StudentDataCon{
public static void main(String args[])
{
Student s1 = new Student("Alexa",50,20);
s1.display();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac StudentDataCon.java
C:\Users\VIHAAN\Desktop\GSK>java StudentDataCon
Student Name :Alexa
Student Roll No :50
Student Age :20
6) Write a program in java to demonstrate the Method Over Loading
class Student {
String name;
int roll_no;
int age;
void display(String n, int r)
{
name = n;
roll_no = r;
System.out.println("Student Name :"+name);
System.out.println("Student Roll No :"+roll_no);
}
void display(String n, int r, int a)
{
name = n;
roll_no = r;
age = a;
System.out.println("Student Name :"+name);
System.out.println("Student Roll No :"+roll_no);
System.out.println("Student Age :"+age);
}
}
class OverLoadingMeth{
public static void main(String args[])
{
Student s1 = new Student();
Student s2 = new Student();
s1.display("Karan",50);
s2.display("Aryan",55,23);
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac OverLoadingMeth.java
C:\Users\VIHAAN\Desktop\GSK>java OverLoadingMeth
Student Name :Karan
Student Roll No :50
Student Name :Aryan
Student Roll No :55
Student Age :23
7) Write a java program to demonstrate the Constructor Over Loading.
class Student {
String name;
int roll_no;
int age;
Student(String n, int r)
{
name = n;
roll_no = r;
}
Student(String n, int r, int a)
{
name = n;
roll_no = r;
age = a;
}
void display()
{
System.out.println("Student Name :"+name);
System.out.println("Student Roll No :"+roll_no);
System.out.println("Student Age :"+age);
}
}
class OverLoadingConEx{
public static void main(String args[])
{
Student s1 = new Student("Karan",50);
Student s2 = new Student("Aryan",55,23);
s1.display();
s2.display();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac OverLoadingConEx.java
C:\Users\VIHAAN\Desktop\GSK>java OverLoadingConEx
Student Name :Karan
Student Roll No :50
Student Age :0
Student Name :Aryan
Student Roll No :55
Student Age :23
8) Write a Java program to illustrate the concept of single inheritance.
class Animal {
String name;
public void eat()
{
System.out.println("I can eat");
}
}
class Dog extends Animal {
public void display()
{
System.out.println("My name is " + name);
}
}
class InheritEx {
public static void main(String[] args) {
Dog labrador = new Dog(); // create an object of the subclass
labrador.name = "Luna"; // access field of superclass
labrador.display(); // call method of subclass using object of subclass
labrador.eat(); // call method of superclass using object of subclass
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac InheritEx.java
C:\Users\VIHAAN\Desktop\GSK>java InheritEx
My name is Luna
I can eat
9) Write a Java program to demonstrate method overriding in Java
import java.io.*;
class Animal {
void eat()
{
System.out.println("eat method of base class");
System.out.println("eating.");
}
}
class Dog extends Animal {
void eat()
{
System.out.println("eat() method of derived class");
System.out.println("Dog is eating.");
}
}
class MethodOverEx {
public static void main(String args[])
{
Dog d1 = new Dog();
Animal a1 = new Animal();
d1.eat();
a1.eat();
Animal animal = new Dog();
// eat() method of animal class is overridden by
// base class eat()
animal.eat();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac MethodOverEx.java
C:\Users\VIHAAN\Desktop\GSK>java MethodOverEx
eat() method of derived class
Dog is eating.
eat method of base class
eating.
eat() method of derived class
Dog is eating.
10) Write a Java program to demonstrate use of super with variables, methods and
constructors
class Vehicle //base class
{
int maxSpeed = 90;
}
class Car extends Vehicle //sub class
{
int maxSpeed = 120;
void display()
{
System.out.println("Maximum Speed: "+ super.maxSpeed);
}
}
class SuperVarEx // Main program
{
public static void main(String[] args)
{
Car s = new Car();
s.display();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac SuperVarEx.java
C:\Users\VIHAAN\Desktop\GSK>java SuperVarEx
Maximum Speed: 90
11) Write a Java program in which you will declare two interface sum and Add inherits
these interface through class A1 and display their content.
interface sum
{
int a = 6;
int b = 4;
void suma();
}
interface add
{
int c = 10;
int d = 10;
void adda();
}
class A1 implements add ,sum
{
public void suma()
{
int res = a+b;
System.out.println(+res);
}
public void adda()
{
int result = c+d;
System.out.println(+result);
}
}
class InterEx{
public static void main(String arr[])
{
A1 n= new A1();
n.suma();
n.adda();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac InterEx.java
C:\Users\VIHAAN\Desktop\GSK>java InterEx
10
20