Sei sulla pagina 1di 15

SAP ID -500063751

Roll no.- 32

Java Programming Lab

Name- Lucky Kumar


Stream- B.Tech CSE G&G 2nd Year
SAP ID -500063751
Roll no.- 32
Submitted to: Rajeev Tiwari sir
SAP ID -500063751

Roll no.- 32

Experiment 01
Date: 10/01/2019

Aim 1: Basic Java Programming

a. Sum
public class Exp1 {

public static void main(String[] args) { // TODO Auto-generated method stub


int a=20;
int b=30;
int c=a+b;
System.out.println("Sum is:"+c); /* out here is interface println is a method*/
}
}
b. Greatest no.
public class Exp1_b {
public static void main(String[] args) {
int a=12;
int b=20;
if (a>b)
System.out.println("larger no. is a:"+a);
else
System.out.println("Larger no. is b:"+b);
}
}

c.Even odd

public class Exp1_c {

public static void main(String[] args) {


SAP ID -500063751

Roll no.- 32

int a=70;
if(a%2==0)
System.out.println("A is even");
else
System.out.println("A is odd");
}
}
d. Greatest no. in 3 numbers.

public class Exp_d {

public static void main(String[] args) {


int a=12;
int b=13;
int c=14;
if(a>b&&a>c)
System.out.println(a+ "is larger");
else if (b>c&&b>a)
System.out.println( b+"is larger");
else
System.out.println(c+"is larger");
}
}
e. Table of 1

public class Exp_e {

public static void main(String[] args) {


int a=1;
while (a<=10)
{
System.out.println(a);
a++;
}
}
}
f. Table of 1,2,5.

public class Exp1_f {

public static void main(String[] args) {


int i;
for(i=1;i<11;i++)
System.out.println(i);
SAP ID -500063751

Roll no.- 32

for (i=2;i<=20;i+=2)
System.out.println(i);
for (i=5;i<=50;i+=5)
System.out.println(i);
}
}

Experiment 02
Date: 17/01/2019

Aim 2.1: A hands on Constructor and its Types.(Default,Paramerterized)

Coding: code created


class Dimensions{
int length;
int breadth;
int height;
public Dimensions(){ // Default constructor
length=5;
breadth=5;
height=5;
}

public float vol(){


float v;
v=length*breadth*height;
return(v);
}
public Dimensions( final int l, final int b, final int h) // Parameterized constructor
{
length=l;
breadth=b;
SAP ID -500063751

Roll no.- 32

height=h;
}
}
public class Exp2_1 {
public static void main(String[] args) {
Dimensions box1=new Dimensions();
Dimensions box2;
box2=new Dimensions(8,7,6);
float volume;
volume=box1.vol(); //125
System.out.println("Volume of box1="+volume);
volume=box2.vol();
System.out.pri ntln("Volume of box2="+volume); //336
}
}
Result:
SAP ID -500063751

Roll no.- 32

Aim 2.2: A hands on Function overloading.

Coding: code created


class Dimensions{ //FUNCTION OVERLOADING
int length;
int breadth;
int height;
public Dimensions( int length,int breadth,int height)
{
this.length=length;
this.breadth=breadth;
this.height=height;
}
public Dimensions(){length=9;breadth=9;height=9;}
public void vol(){
float v;
v=length*breadth*height;
System.out.println("value of v="+v);
}
}
public class Exp2_2 {

public static void main(String[] args) {


Dimensions box1=new Dimensions();
Dimensions box2=new Dimensions(10,5,5);
box1.vol();
box2.vol();
SAP ID -500063751

Roll no.- 32

}
}

Result:

Experiment 03
Aim: Use inheritance and Overriding Concept

Coding: Code created


public class Exp3_b{

public static void main(String[] args) {


figure1 fig=new figure1();
Box B1=new Box();
fig.show();
B1.show();
}
}
class figure1{
int len;
int br;
public void show(){
System.out.println("Value of len and br" +len +br );
}
figure1(){
System.out.println("This is Superclass Constant");
len=-1;
br=-1;
SAP ID -500063751

Roll no.- 32

}
figure1(int l ,int b){
len=l;
br=b;
}
}
class Box extends figure1{
int ht;
Box(){
len=6;
br=9;
ht=5;
}
public void show(){
System.out.println(" Inside Box class");
System.out.println(len);
System.out.println(br);
System.out.println(ht);
}
}

Aim3.2: use super and dynamic method dispatch.

Coding: Code created


public class Exp3_c{ //Dynamic method Dispatch

public static void main(String[] args) {


figure2 fig=new figure2();
Box2 B1=new Box2();
figure2 r;
SAP ID -500063751

Roll no.- 32

r=fig;
r.show();
r=B1;
r.show();
}
}
class figure2{
int len;
int br;
public void show(){
System.out.println("Value of len and br" +len +br );
}
figure2(){
System.out.println("This is Superclass Constant");
len=-1;
br=-1;
}
figure2(int l ,int b){
len=l;
br=b;
}
}
class Box2 extends figure2{
int ht;
Box2(){
len=6;
br=9;
ht=510;
}
public void show(){
System.out.println(" Inside Box class");
System.out.println(len);
System.out.println(br);
System.out.println(ht);
super.show();
}
}
SAP ID -500063751

Roll no.- 32

Aim 3.3: use final in class and method.

Coding: Code created


public class Final{ //Final

public static void main(String[] args) {


figure3 fig=new figure3();
Box3 B1=new Box3();
r=B1;
r.show();
}
}
class figure3{
int len;
int br;
public void show(){
System.out.println("Value of len and br" +len +br );
}
figure3(){
System.out.println("This is Superclass Constant");
len=-1;
br=-1;
}
figure3(int l ,int b){
len=l;
br=b;
}
SAP ID -500063751

Roll no.- 32

}
final class Box3 extends figure3{
int ht;
Box3(){
len=6;
br=9;
ht=510;
}
public final void show(){
System.out.println(" Inside Box class");
System.out.println(len);
System.out.println(br);
System.out.println(ht);
super.show();
}
}

Experiment 04
Aim: Code using throws, throw, try, catch and final. Create an Exception handling code

Code implementation:
SAP ID -500063751

Roll no.- 32

class Dimension extends Figure{

private int detail;

Dimension(int a)

{ detail = a;

public String toString()

{ return “Dimension-for rectangle”;

class Rectangle

{ int L;

int B;

Int area;

Rectangle()

{ L = 2;

B = 3;

Rectangle(int l, int b)

{ L = l;

B = b;

void ar() throws Dimension{

if((L==0) || (B==0)){

throw new Dimension(L) ;


SAP ID -500063751

Roll no.- 32

area = L * B;

System.out.println(“The area of rectangle is :” + area);}

Experiment 05
Aim: Use 5 string function.

Code implementation:
SAP ID -500063751

Roll no.- 32

public class Test {

public static void main(String[] args)

//System.out.println(“Name”);

String s = “Chandigarh”;

char result = s.charAt(5);

System.out.println(result); // CharAt function

s = s.concat(“ is good”);

System.out.println(s); //Concatenation function

String x = new String(“Strings are immutable”);

String y = new String(“Integers are not immutable”);

int res = x.compareTo(y);

System.out.println(res); //compareTo function

String e = “Patiala”;

String f = “is very big not Patiala”;

StringBuffer g = new StringBuffer(“Patiala”);

boolean ans = e.contentEquals(g);

System.out.println(ans);

ans = f.contentEquals(g);

System.out.println(ans); //contentEquals

System.out.println(“String Length:”);

System.out.println(x.length()); //Find String length using length function

Output:
SAP ID -500063751

Roll no.- 32

Potrebbero piacerti anche