[G.
SRAVAN KUMAR, Assistant professor, ECED,
MVSR Engineering College, Nadergul.]
_________________________________________
1) Write a program in Java to print “Hello World”
PROGRAM/CODE :
public class HelloWorld{
public static void main(String[] args) {
[Link]("Hello World");
}
}
Result:
To Compile :
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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 [Link];
class AreaOfTriangle{
public static void main(String[] args) {
int base,height;
double area;
Scanner sc = new Scanner([Link]);
[Link]("Enter the base of the triangle :");
base = [Link]();
[Link]("Enter the height of triangle :");
height = [Link]();
area = (height * base)/2;
[Link]("Area of the triangle is ::"+area);
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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 [Link];
class Fibonacci{
public static void main(String args[])
{
int N, counter=0;
int num1=0,num2=1;
Scanner sc = new Scanner([Link]);
[Link]("Enter N:");
N = [Link]();
while (counter < N) {
[Link](num1 + " ");
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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()
{
[Link]("Student Name :"+name);
[Link]("Student Roll No :"+roll_no);
[Link]("Student Age :"+age);
}
}
class StudentData{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link] = "Naveen";
s1.roll_no = 50;
[Link] = 20;
[Link]();
[Link] = "Ram";
s2.roll_no = 40;
[Link] = 21;
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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)
{
[Link]=n;
this.roll_no=r;
[Link]=a;
}
void display()
{
[Link]("Student Name :"+name);
[Link]("Student Roll No :"+roll_no);
[Link]("Student Age :"+age);
}
}
class StudentDataCon{
public static void main(String args[])
{
Student s1 = new Student("Alexa",50,20);
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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;
[Link]("Student Name :"+name);
[Link]("Student Roll No :"+roll_no);
}
void display(String n, int r, int a)
{
name = n;
roll_no = r;
age = a;
[Link]("Student Name :"+name);
[Link]("Student Roll No :"+roll_no);
[Link]("Student Age :"+age);
}
}
class OverLoadingMeth{
public static void main(String args[])
{
Student s1 = new Student();
Student s2 = new Student();
[Link]("Karan",50);
[Link]("Aryan",55,23);
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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()
{
[Link]("Student Name :"+name);
[Link]("Student Roll No :"+roll_no);
[Link]("Student Age :"+age);
}
}
class OverLoadingConEx{
public static void main(String args[])
{
Student s1 = new Student("Karan",50);
Student s2 = new Student("Aryan",55,23);
[Link]();
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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()
{
[Link]("I can eat");
}
}
class Dog extends Animal {
public void display()
{
[Link]("My name is " + name);
}
}
class InheritEx {
public static void main(String[] args) {
Dog labrador = new Dog(); // create an object of the subclass
[Link] = "Luna"; // access field of superclass
[Link](); // call method of subclass using object of subclass
[Link](); // call method of superclass using object of subclass
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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 [Link].*;
class Animal {
void eat()
{
[Link]("eat method of base class");
[Link]("eating.");
}
}
class Dog extends Animal {
void eat()
{
[Link]("eat() method of derived class");
[Link]("Dog is eating.");
}
}
class MethodOverEx {
public static void main(String args[])
{
Dog d1 = new Dog();
Animal a1 = new Animal();
[Link]();
[Link]();
Animal animal = new Dog();
// eat() method of animal class is overridden by
// base class eat()
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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()
{
[Link]("Maximum Speed: "+ [Link]);
}
}
class SuperVarEx // Main program
{
public static void main(String[] args)
{
Car s = new Car();
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
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;
[Link](+res);
}
public void adda()
{
int result = c+d;
[Link](+result);
}
}
class InterEx{
public static void main(String arr[])
{
A1 n= new A1();
[Link]();
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
C:\Users\VIHAAN\Desktop\GSK>java InterEx
10
20
12) Write a Java program to demonstrate use of final variable.
public class Vehicle{
final int speedlimit = 60;
void Speed()
{
speedlimit = 150; // changing final variable value creates error
}
}
class FinalVarEx{
public static void main(String args[]) {
Vehicle v = new Vehicle();
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
[Link][Link] error: cannot assign a value to final variable speedlimit
speedlimit = 150; // changing final variable value creates error
^
1 error
13) Write a Java Program to find the average of numbers in an array.
public class Average {
public static void main(String[] args) {
double[] numArray = { 45.3, 67.5, 45.6, 20.34, 33.0, 45.6 };
double sum = 0.0;
for (double num: numArray) {
sum = sum + num;
}
double average = sum / [Link];
[Link]("The average is: %.2f", average);
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
C:\Users\VIHAAN\Desktop\GSK>java Average
The average is: 42.89
14) Write a program in Java if number is less than 10 and greater than 50 it generate
the exception out of range. Else it displays the square of number.
import [Link].*;
class MyException extends Exception
{
public String getMessage()
{
return "number is out of range";
}
}
class ExceptionEx
{
public static void main(String...args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter a number between 10 and 50");
int a = [Link]();
try
{
if((a < 10) || (a > 50))
throw new MyException();
else
[Link]("Square of the entered number "+a+" is:"+(a*a));
}
catch(MyException me)
{
[Link]("Error caught="+me);
}
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
C:\Users\VIHAAN\Desktop\GSK>java ExceptionEx
enter a number between 10 and 50
23
Square of the entered number 23 is:529
15) Write a Java Program in Java to create Package.
15b) Write a Java Program in Java to a package name.
// Name of the package must be same as the directory
// in which this file is saved
package myPack;
public class MyClass
{
public void display(String s)
{
[Link](s);
}
}
// Save the following file as [Link]
import myPack.*;
public class PrintName
{
public static void main(String args[])
{
String name = "MVSR ENGG COLLEGE";
MyClass m = new MyClass();
[Link](name);
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
C:\Users\VIHAAN\Desktop\GSK>java PrintName
MVSR ENGG COLLEGE
16) Write a Java to create a Thread
class ThreadEx extends Thread
{
static
{
Thread t = [Link]();
[Link]("thread test is loaded by "+[Link]()+" thread");
[Link]("MVSR");
[Link]("changed the name of thread");
[Link]("suspending thread for 5 sec");
try
{
[Link](5000);
}
catch(Exception ex){}
}
public static void main(String arr[])
{
Thread t=[Link]();
[Link]("main() is invoked in "+[Link]()+ " thread...");
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
C:\Users\VIHAAN\Desktop\GSK>java ThreadEx
thread test is loaded by main thread
changed the name of thread
suspending thread for 5 sec
main() is invoked in MVSR thread...
17) Write a Java program to create a file and write the text in it and save the file.
import [Link];
class FileWriterDemo
{
public static void main(String[] args) throws Exception
{
FileWriter fw= new FileWriter("[Link]");
[Link]("Welcome to JAVA");
[Link]();
}
}
Result:
C:\Users\VIHAAN\Desktop\GSK>javac [Link]
C:\Users\VIHAAN\Desktop\GSK>java FileWriterDemo