SAP ID -500063751
Roll no.- 32
Java Programming Lab
Name- Lucky Kumar
Stream- [Link] 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;
[Link]("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)
[Link]("larger no. is a:"+a);
else
[Link]("Larger no. is b:"+b);
}
}
[Link] odd
public class Exp1_c {
public static void main(String[] args) {
SAP ID -500063751
Roll no.- 32
int a=70;
if(a%2==0)
[Link]("A is even");
else
[Link]("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)
[Link](a+ "is larger");
else if (b>c&&b>a)
[Link]( b+"is larger");
else
[Link](c+"is larger");
}
}
e. Table of 1
public class Exp_e {
public static void main(String[] args) {
int a=1;
while (a<=10)
{
[Link](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++)
[Link](i);
SAP ID -500063751
Roll no.- 32
for (i=2;i<=20;i+=2)
[Link](i);
for (i=5;i<=50;i+=5)
[Link](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=[Link](); //125
[Link]("Volume of box1="+volume);
volume=[Link]();
[Link] 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)
{
[Link]=length;
[Link]=breadth;
[Link]=height;
}
public Dimensions(){length=9;breadth=9;height=9;}
public void vol(){
float v;
v=length*breadth*height;
[Link]("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);
[Link]();
[Link]();
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();
[Link]();
[Link]();
}
}
class figure1{
int len;
int br;
public void show(){
[Link]("Value of len and br" +len +br );
}
figure1(){
[Link]("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(){
[Link](" Inside Box class");
[Link](len);
[Link](br);
[Link](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;
[Link]();
r=B1;
[Link]();
}
}
class figure2{
int len;
int br;
public void show(){
[Link]("Value of len and br" +len +br );
}
figure2(){
[Link]("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(){
[Link](" Inside Box class");
[Link](len);
[Link](br);
[Link](ht);
[Link]();
}
}
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;
[Link]();
}
}
class figure3{
int len;
int br;
public void show(){
[Link]("Value of len and br" +len +br );
}
figure3(){
[Link]("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(){
[Link](" Inside Box class");
[Link](len);
[Link](br);
[Link](ht);
[Link]();
}
}
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;
[Link](“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)
//[Link](“Name”);
String s = “Chandigarh”;
char result = [Link](5);
[Link](result); // CharAt function
s = [Link](“ is good”);
[Link](s); //Concatenation function
String x = new String(“Strings are immutable”);
String y = new String(“Integers are not immutable”);
int res = [Link](y);
[Link](res); //compareTo function
String e = “Patiala”;
String f = “is very big not Patiala”;
StringBuffer g = new StringBuffer(“Patiala”);
boolean ans = [Link](g);
[Link](ans);
ans = [Link](g);
[Link](ans); //contentEquals
[Link](“String Length:”);
[Link]([Link]()); //Find String length using length function
Output:
SAP ID -500063751
Roll no.- 32