0% found this document useful (0 votes)
85 views15 pages

JAVA LAB GUIDE Final

The document contains 12 code examples in Java that demonstrate various concepts: 1) A simple "Hello World" program. 2) A program to calculate the area of a triangle. 3) A program to print the Fibonacci series up to a given number. 4) A Student class with data fields and a display method to demonstrate object creation. 5) A Student class with a constructor. The examples cover concepts like method overloading, constructor overloading, single inheritance, method overriding, use of super keyword, interfaces, and final variables.
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)
85 views15 pages

JAVA LAB GUIDE Final

The document contains 12 code examples in Java that demonstrate various concepts: 1) A simple "Hello World" program. 2) A program to calculate the area of a triangle. 3) A program to print the Fibonacci series up to a given number. 4) A Student class with data fields and a display method to demonstrate object creation. 5) A Student class with a constructor. The examples cover concepts like method overloading, constructor overloading, single inheritance, method overriding, use of super keyword, interfaces, and final variables.
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
You are on page 1/ 15

[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) {
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

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();
v.Speed();
}
}

Result:

C:\Users\VIHAAN\Desktop\GSK>javac Vehicle.java
Vehicle.java:6: 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 / numArray.length;
System.out.format("The average is: %.2f", average);
}
}

Result:
C:\Users\VIHAAN\Desktop\GSK>javac Average.java
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 java.util.*;
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(System.in);
System.out.println("enter a number between 10 and 50");
int a = sc.nextInt();
try
{
if((a < 10) || (a > 50))
throw new MyException();
else
System.out.println("Square of the entered number "+a+" is:"+(a*a));
}
catch(MyException me)
{
System.out.println("Error caught="+me);
}
}
}

Result:
C:\Users\VIHAAN\Desktop\GSK>javac ExceptionEx.java
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)
{
System.out.println(s);
}
}

// Save the following file as PrintName.java


import myPack.*;
public class PrintName
{
public static void main(String args[])
{
String name = "MVSR ENGG COLLEGE";
MyClass m = new MyClass();
m.display(name);
}
}

Result:
C:\Users\VIHAAN\Desktop\GSK>javac PrintName.java
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 = Thread.currentThread();
System.out.println("thread test is loaded by "+t.getName()+" thread");
t.setName("MVSR");
System.out.println("changed the name of thread");
System.out.println("suspending thread for 5 sec");
try
{
Thread.sleep(5000);
}
catch(Exception ex){}
}
public static void main(String arr[])
{
Thread t=Thread.currentThread();
System.out.println("main() is invoked in "+t.getName()+ " thread...");
}
}

Result:
C:\Users\VIHAAN\Desktop\GSK>javac ThreadEx.java
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 java.io.FileWriter;
class FileWriterDemo
{
public static void main(String[] args) throws Exception
{
FileWriter fw= new FileWriter("File.txt");
fw.write("Welcome to JAVA");
fw.close();
}
}

Result:

C:\Users\VIHAAN\Desktop\GSK>javac FileWriterDemo.java
C:\Users\VIHAAN\Desktop\GSK>java FileWriterDemo

You might also like