Java Programs and Outputs


Program 1: display simple “Hello JAVA” Message.
Input:
class Hsimple
{
public static void main(String args[])
{
System.out.println("Hello JAVA");
}
}
Output:


Program 2: perform basic arithmetic Operation in java.
Input:

class Basicmath
{
public static void main(String args[])
{
System.out.println("\t\tArithmatic Operator\n");
System.out.println("Integer Arithmatic");
int a = 1+1;
int b = a*3;
int c = b/4;
int d = c-a;
int e = -d;
System.out.println("a = " +a);
System.out.println("b = " +b);
System.out.println("c = " +c);
System.out.println("d = " +d);
System.out.println("e = " +e);

}
}
Output:
                        

Program 3: Implementation of Class, Object & Methods in java.
Input:

class Box
{
double width;
double height;
double depth;
}
class BoxDemo{

public static void main(String args[])
{
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume in" + vol);
}
}
Output:


Input:
class Box
{
double width;
double height;
double depth;
void volume()
{
System.out.print("Volume is");
System.out.println(width * height * depth);
}
}
class BoxDemo2
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();

mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;

mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;

mybox1.volume();
mybox2.volume();
}
}
Output:


Input:
class Box
{
double width;
double height;
double depth;
}
class BoxDemo3
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();

double volume;

mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;

mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;

volume = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + volume);

volume = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + volume);

}
}
Output:


Program 4:- Implementation of Constructor & Parameterized Constructor in java.

Input:

class boxd
{
double height;
double width;
double depth;
}
class box1
{
public static void main(String args[])
{
boxd b1=new boxd();
b1.height=100;
b1.width=200;
b1.depth=15;
                                                                                                                                                                                                                                  

double volume=b1.height * b1.width * b1.depth;
System.out.println("Height of box="+b1.height);
System.out.println("Width of box="+b1.width);
System.out.println("Width of box="+b1.depth);
System.out.println("Volume of Box is="+volume);

boxd b2=new boxd();
b2.height=10;
b2.width=25;
b2.depth=12;
                                                                                                                                                                                                                                  

double volume1=b2.height * b2.width * b2.depth;
System.out.println("Height of box="+b2.height);
System.out.println("Width of box="+b2.width);
System.out.println("Width of box="+b2.depth);
System.out.println("Volume of Box is="+volume1);
}
}

Output:

Using constructor
Input:

class cons
{
double height;
double width;
double depth;

cons()
{
width=12;
height=14;
depth=16;
}
double volume()
{
return (height * width * depth);
}
}
class box11
{
public static void main(String args[])
{
cons b1=new cons();
double vol1;
vol1=b1.volume();
System.out.println("Volume of Box is="+vol1);
}
}


Output:

Using parametrized constructor
Input:

class cons
{
double height;
double width;
double depth;

cons(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
double volume()
{
return (height * width * depth);
}
}
class box1
{
public static void main(String args[])
{
cons b1=new cons(10,20,30);
cons b2=new cons(90,80,70);
double vol1;
vol1=b1.volume();
System.out.println("Volume of Box is="+vol1);
double vol2;
vol2=b2.volume();
System.out.println("Volume of Box is="+vol2);
}
}

Output:             





Using copy constructor
Input:

import java.io.*;
import java.lang.*;

class student
{
int id;
String name;

student(int i,String n)
{
id=i;
name=n;
}
student(student s)
{
id=s.id;
name=s.name;
}
void display()
{
System.out.println("Student ID is="+id);
System.out.println("Student Name is="+name);
}
}
class s13
{
public static void main(String args[])
{
student s1=new student(2062,"Prachi");
student s2=new student(2060,"Monika");
System.out.println("Information of Student 1 is:");
s1.display();
System.out.println("Information of Student 2 is:");
s2.display();
}
}

Output:











Comments

Popular posts from this blog

NSS CA1 Answers

Database Introduction and Relational Database ppt Notes

BC Mid Sem Ans