You are on page 1of 20

Java Programming (3350703)

Experiment-1

AIM: Write a simple Java program which prints “Hello World”.


Program:-
class A
{
Public static void main(string arg[])
{
System.out.println(“Hello World”);
}
}

Output:-
Hello World

Computer Engineering Department Page 1


Java Programming (3350703)

Experiment-2

AIM: Write a program in Java to find maximum of three numbers using


conditional operator.

Program:-
importjava.util.Scanner;
class exp2
{
public static void main(String arg[])
{
inta,b,c,max;

System.out.println("Enter the value of a,b,c");


Scanner s1=new Scanner(System.in);
a=s1.nextInt();
b=s1.nextInt();
c=s1.nextInt();

max=(a>b)?((a>c)?a:c):((b>c)?b:c);
System.out.println("Maximum value is:"+max);
}
}
Output:-

Computer Engineering Department Page 2


Java Programming (3350703)

Experiment-3
AIM: Write a program in Java to reverse the digits of a number using while
loop.

Program:-
Import java.util.Scanner;
class exp3
{
public static void main(String arg[])
{
intn,ans=0,r;
System.out.println("Enter value");
Scanner s1=new Scanner(System.in);
n=s1.nextInt();

while(n>0)
{
r=n%10;
ans=ans*10+r;
n=n/10;
}
System.out.println("Answer is:"+ans);
}
}

Output:-

Computer Engineering Department Page 3


Java Programming (3350703)

Experiment-4

AIM: Write a program in Java to add two 3*3 matrices.


Program:-
import java.util.Scanner;
class AddMatrix
{
public static void main(String args[])
{
int row, col,i,j;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows");


row = in.nextInt();

System.out.println("Enter the number columns");


col = in.nextInt();

int mat1[][] = new int[row][col];


int mat2[][] = new int[row][col];
int res[][] = new int[row][col];

System.out.println("Enter the elements of matrix1");


i=0;
while ( i < row)
{
j=0;
while(j < col)
{
mat1[i][j] = in.nextInt();
j++;
}
i++;

Computer Engineering Department Page 4


Java Programming (3350703)

}
System.out.println("Enter the elements of matrix2");

i=0;
while ( i < row)
{
j=0;
while(j < col)
{
mat2[i][j] = in.nextInt();
j++;
}
i++;

i=0;
while ( i < row)
{
j=0;
while(j < col)
{

res[i][j] = mat1[i][j] + mat2[i][j] ;


j++;
}
i++;

System.out.println("Sum of matrices:-");
i=0;
while ( i < row)
{
j=0;
Computer Engineering Department Page 5
Java Programming (3350703)

while(j < col)


{

System.out.print(res[i][j]+"\t");
j++;
}
System.out.println();
i++;

}
}

Computer Engineering Department Page 6


Java Programming (3350703)

Experiment-5

AIM: Write a program in Java to generate first n prime numbers.


Program:-
Import java.util.Scanner;

class exp5
{
staticint count=0;

public static void main(string arg[])


{
intn,j=2;
System.out.println("Enter number:");
Scanner s1=new scanner(System.in);
n=s1.nextInt();

while(count<n)
{
prime(j);
j++;
}
}

public static void prime(int x)


{
int flag=0;
for(int i=2;i<x;i++)
{
if(x%i==0)
{
flag=1;
break;

Computer Engineering Department Page 7


Java Programming (3350703)

}
}
if(flag==0)
{
count++;
System.out.println(x);
}
}
}

Output:-

Computer Engineering Department Page 8


Java Programming (3350703)

Experiment-6

AIM: Write a program in Java which has a class Student having two instance
variables enrollmentNo and name. Create 3 objects of Student class in main
method and display student’s name.

Program:-

public class Student {


static String enrollmentno;
static String name;
void set(String en, String n)
{
enrollmentno = en;
name = n;
}
void get()
{
System.out.println("Student Enrollment no is: " + enrollmentno);
System.out.println("Student Name is: "+ name);
}
public static void main(String args[])
{
Student s = new Student();
s.set("170423107007","Vihan");
s.get();
Student s1 = new Student();
s1.set("190423108009","Jeck");
s1.get();
Student s2 = new Student();
s2.set("200423107008","John");
s2.get();
}
}
Output:-

Computer Engineering Department Page 9


Java Programming (3350703)

Experiment-7
Computer Engineering Department Page 10
Java Programming (3350703)

AIM: Write a program in Java which has a class Rectangle having two
instance variables height and weight. Initialize the class using constructor.
Program:-
public class Rectangle {
private int height;
private int width;

public Rectangle(int h, int w) {


height = h;
width = w;
}
public int getHeight() {
return height;
}

public int getWidth() {


return width;
}
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 10);
System.out.println("Height: " + r.getHeight());
System.out.println("\nWidth: " + r.getWidth());
}
}
OUTPUT:

Experiment-8

Computer Engineering Department Page 11


Java Programming (3350703)

AIM: Write a program in Java demonstrate the use of “this” keyword.


Program:-
class Demo_this_keyword
{
int a;
int b;
// Parameterized constructor
Demo_this_keyword(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
//Displaying value of variables a and b
System.out.println("I Use 'this' keyword in this Program for access class
instance value :");
System.out.println("a = " + a + " b = " + b);
}
public static void main(String[] args)
{
Demo_this_keyword object = new Demo_this_keyword(10, 20);
object.display();
}
}
OUTPUT:-

Experiment-9

Computer Engineering Department Page 12


Java Programming (3350703)

AIM: Write a program in Java to demonstrate the use of “static” keyword.


Program:-
public class Main {
public static void main(String[] args) {
MAX_MIN obj = new MAX_MIN();

// access the non-static variable


System.out.println("Access min value using object(Instance variable) =" +
(obj.min));

// access the static variable


System.out.println("\nAccess max value using Class (Static variable) = " +
(MAX_MIN.max));
}
}
class MAX_MIN {

// static variable
static int max = 10;

// non-static variable
int min = 5;
}

Experiment-10
AIM: Write a program in Java to demonstrate the use of "final" keyword.
Computer Engineering Department Page 13
Java Programming (3350703)

Program:-
1. Java final Variable
class Main {
public static void main(String[] args) {

// create a final variable


final int AGE = 32;

// try to change the final variable


AGE = 45;
System.out.println("Age: " + AGE);
}
}

2. Java final Method


class FinalDemo {
// create a final method
public final void display() {
System.out.println("This is a final method.");
}
}

class Main extends FinalDemo {


// try to override final method

Computer Engineering Department Page 14


Java Programming (3350703)

public final void display() {


System.out.println("The final method is overridden.");
}

public static void main(String[] args) {


Main obj = new Main();
obj.display();
}
}

3. Java final Class


final class FinalClass {
public void display() {
System.out.println("This is a final method.");
}
}

// try to extend the final class


class Main extends FinalClass {
public void display() {
System.out.println("The final method is overridden.");
}

public static void main(String[] args) {


Main obj = new Main();

Computer Engineering Department Page 15


Java Programming (3350703)

obj.display();
}
}

Experiment-11

Computer Engineering Department Page 16


Java Programming (3350703)

AIM: Write a program in Java which has a class Shape having 2 overloaded
methods area(float radius) and area(float length, float width). Display the
area of circle and rectangle using overloaded methods.
Program:-
class Shape
{

void area(float radius)


{
double ans = 3.14 * radius * radius;
System.out.println("The area of the circle is "+ans+" sq units");
}
void area(float length, float width)
{
System.out.println("\nThe area of the rectangle is "+length*width+" sq
units");
}
}
class Overload
{
public static void main(String args[])
{
Shape ob = new Shape();
ob.area(5);
ob.area(11,12);
}
}

Experiment-12

Computer Engineering Department Page 17


Java Programming (3350703)

AIM: Write a program in Java to demonstrate the constructor overloading.


Program:-
public class Constructor_Demo
{
Constructor_Demo() {
System.out.println("I am Without parameter constructor");
}
Constructor_Demo(String name) {
System.out.println("I am With parameter constructor -"+name);
}

public static void main(String[] args)


{
Constructor_Demo p1 = new Constructor_Demo();
Constructor_Demo p2 = new Constructor_Demo("John");
}
}

Experiment-13

Computer Engineering Department Page 18


Java Programming (3350703)

AIM: Write a java program to demonstrate use of “String” class methods :


chatAt(), contains(), format(), length(), split()
Program:-
class Main {
public static void main(String[] args) {

// create a string
String str = "Hello! World";
System.out.println("String: " + str);

// get the length


int length = str.length();
System.out.println("Length: " + length);
// get the specific index character
System.out.println("\n Get Specific index character : " +str.charAt(7) );
// get the split string
String[] split = str.split("World");
System.out.println("\n Get Split String : ");
for (String obj: split) {
System.out.println(obj);
}
// get containing string
System.out.println("\nContain String : "+str.contains("Hel"));

int num = 7044;


String str1 = String.format("%07d", num);
System.out.println("\n Format String : "+str1);
}
}

Computer Engineering Department Page 19


Java Programming (3350703)

Computer Engineering Department Page 20

You might also like