java lab manual
java lab manual
Oriented
Programming
with Java
Lab manual
Netbeans Instructions For Java Programs
Creating a NEW Java Project
2. Select the Java from the list of Categories and Java Application from the
list of Projects available in the New Project dialog box.
3. Press the Next button to continue.
4. Enter the Name of your NEW Java project in the Project Name field in
New Java Application dialog box.
5. Modify the Location of where your Java project is being saved in the
Project Location field. To save it in your Windows account replace C:\Users\
student\Documents with z:. To save it to your USB, replace C:\Users\student\
Documents with the drive letter that represents your USB drive.
6. Press the Finish button to complete the Java Project setup process.
At this point in time your NEW Java Project should appear as an asset in the
Project Tab on the left hand side of your Netbeans application window and
your Main.java should appear in a source code window on the right
hand side.
class Default
{
private short s;
private int i;
private long l;
private float f;
private double d;
private char c;
private String str;
private boolean b;
public static void main (String args[ ])
{
Default df = new Default( );
System.out.println (“\n short s =” + df.s);
System.out.println (“\n int i =” + df.i);
System.out.println (“\n long l =” + df.l );
System.out.println (“\n float f =” + df.f);
System.out.println (“\n double d =” + df.d);
System.out.println (“\n char c =” + df.c);
System.out.println (“\n String s =” + df.str);
System.out.println(“\n boolean b =” + df.b);
}
}
1.3 Write a program check two strings are equal or not.
class Streq
{
public static void main (String args [ ])
{
String str1 = "Good";
String str2 = "Good";
System.out.println ("\n str1 :"+str1);
System.out.println ("\n str2 :"+str2);
System.out.println ("\n str1 == str2 : " + str1 == str2);
System.out.println ("\n str1.equals(str2): " + str1.equals(str2));
}
}
Lab unit 2
2.1 Write a program to give the examples of operators.
2.1.1 Increment and decrement operators.
class IncDec
{
public static void main (String args [ ] )
{
int x = 8, y = 13;
System.out.println ("x =" + x);
System.out.println ("y =" +y);
System.out.println ("++x =" + ++x);
System.out.println ("y++ =" + y++);
System.out.println ("x =" + x);
System.out.println ("y =" + y);
}
2.1.2 Bitwise Complement Operator. }
class BitWiseComplement
{
public static void main (String args [ ] )
{
int x = 8;
System.out.println ("x =" + x);
int y = ~x;
System.out.println ("y =" + y);
}
}
2.1.3 arithmetic operator.
class FloatMath
{
public static void main ( String args [ ] )
{
float x = 23.5f, y = 7.3f;
System.out.println ("x =" + x);
System.out.println ("y =" + y);
System.out.println ("x + y =" + ( x + y) ) ;
System.out.println ("x - y =" + (x - y) ) ;
System.out.println (" x * y =" +( x* y) );
System.out.println (" x / y =" + ( x / y ) );
System.out.println (" x % y =" + ( x % y ) );
}
}
2.1.4 Relational Operator
class Relational
{
public static void main (String args [ ] )
{
int x = 7, y = 11, z = 11;
System.out.println (" x=" + x);
System.out.println ("y =" + y);
System.out.println ("x < y =" + ( x < y ) );
System.out.println (" x > z =" + (x > z) );
System.out.println (" x <= z =" + (y <= z) );
System.out.println (" x >= y =" + (x >= y ) );
System.out.println ( " y == z =" + (y ==z) );
System.out.println (" x != z =" + (x != z) );
}}
2.1.5 Bitwise operator
class Bitwise
{
public static void main ( String args [ ] )
{
int x = 5, y = 6;
System.out.println (" x =" +x);
System.out.println (" y =" + y );
System.out.println (" x & y =" + ( x & y) ) ;
System.out.println (" x | y =" + ( x | y ) );
System.out.println (" x ^ y =" +( x ^ y) );
}}
2.1.6 Conditional Operator
class Conditional
{
public static void main (String args [ ] )
{
int x = 0;
boolean isEven = false;
System.out.println ("x =" + x);
x = isEven ? 4: 7;
System.out.println ("x =" + x);
}
}
2.2 Write a program to give the example of control statements.
2.2.1 If statements
class IfTest
{
public static void main ( String args [ ] )
{
int x = 4;
int y = 10;
if (x > y )
{
System.out.println ("x is greater than y" );
}
else {
System.out.println ("X is lesser than y");
}
}
}
2.2.2 Switch Statements.
class SwitchTest
{
public static void main (String args [ ] )
{
char ch = 'A';
switch (ch)
{
case 'A':
System.out.println ("Value is A");
break;
case 'B':
System.out.println ("Value is B");
break;
default:
System.out.println ("Unknown Value");
}
}}
2.2.3 For loop.
class ForTest
{
public static void main (String args [ ] )
{
int i= 0;
int sum = 0;
for( i = 0; i <= 10; i++)
sum += i;
System.out.println ("The sum of first 10 Nos =" + sum );
}
}
2.2.4 While Statements.
class WhileTest
{
public static void main (String args [ ] )
{
int i=1;
while (i<=5)
{
System.out.println ("i =" + i);
i++;
}
}
}
2.2.5 Do statements
class BreakLoop
{
public static void main (String args [ ])
{
int i= 0;
do {
System.out.println ("I’m stuck !" ) ;
i++;
if (i > 5)
break;
} while (true);
}
}
2.3 Write a program to calculate the following
2.3.1 Find the length of array
class Length
{
public static void main (String args [ ] )
{
int a1[ ] = new int [10];
int a2 [ ] = { 3,5,7, 1, 8, 99 , 44, -10 };
int a3 [ ] = { 4, 3, 2, 1 };