0% found this document useful (0 votes)
0 views

java lab manual

The document is a lab manual for Object Oriented Programming with Java, detailing instructions for creating, saving, compiling, and running Java projects using Netbeans. It includes various lab exercises that cover fundamental programming concepts such as data types, operators, control statements, and arrays, with example code provided for each exercise. The manual serves as a practical guide for students to learn and apply Java programming skills.

Uploaded by

mule0992584278
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

java lab manual

The document is a lab manual for Object Oriented Programming with Java, detailing instructions for creating, saving, compiling, and running Java projects using Netbeans. It includes various lab exercises that cover fundamental programming concepts such as data types, operators, control statements, and arrays, with example code provided for each exercise. The manual serves as a practical guide for students to learn and apply Java programming skills.

Uploaded by

mule0992584278
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Object

Oriented
Programming
with Java
Lab manual
Netbeans Instructions For Java Programs
Creating a NEW Java Project

1. Select File > New Project... From the main


menu. This will open the New Project dialog box

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.

You can begin adding new source code to your project.


Saving a Java Project / Source File
1. Select the Tab that contains the Source Code you wish to save.
2. Select File > Save from the main menu. This will automatically save the source
code in that is open in the active tab of your Netbeans workspace.

Compiling and Running a Java Program


1. To build your program, press the Build icon ( ) in the main menu. This will
allow you to build a .class file which will be saved in your project directory tree
under build/classes. At the bottom of your Netbeans window you should see the
results of the build process which should look similar to the results shown in the
Output Tab as shown below which indicate that your program has been built
successfully:

2. To Build and Run your program, press the Run icon ( )


in the main menu. You should now see the results at the bottom
of your Netbeans application window in the Output Tab as
shown
below which indicate that your program has been built and run
successfully:
Lab Exercises
Lab unit one
1.1 Write a program to display any message:
class Simout
{
public static void main (String args[ ] )
{
System.out.println (“Welcome to Java programming”);
}
}
1.2 Write a Java program to display default value of all primitive data types of java

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 };

System.out.println ("Length of a1 is" + a1.length);


System.out.println (" Length of a2 is" + a2.length);
System.out.println ("Length of a3 is" + a3. length);
}
}
2.3.2 Demonstrate a one-dimensional array.
class Array
{
public static void main (String args [ ] )
{
int num[];
int size =5;
num = new int [size];
num [0] = 10;
num [1] = 20;
num[2] = 30;
num [3] = 40;
num [4]= 50;
for (int i =0; i<size; i+ +)
System.out.println ("num [" + i +" ] =" + num [ i ]);
}
}
2.3.3 Demonstrate a two-dimensional array.
class TwoDArray
{
public static void main (String args[])
{
int twoD[][] = new int[3][3];
int i, j , k = 0;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
{
twoD[i][j] = k;
k++;
}
for (i=0; i< 3; i++)
{
for ( j= 0; j < 3; j++)
System.out.print (twoD[i][j] + " ");
System.out.println();
}
}
}

You might also like