Download Object Oriented Programming with Java: Lab Exercises and more Exercises Java Programming in PDF only on Docsity!
Lab Exercises
Lab Exercise
Object Oriented Programming with Java
Java.
1.3 Write a program check two strings are equal or not.
2.1 Write a program to give the examples of operators.
2.1.1 Increment and decrement operators.
2.1.2 Bitwise Complement Operator.
2.1.3 Arithmetic operator.
2.1.4 Relational Operator
2.1.5 Bitwise operator.
2.1.6 Conditional Operator.
2.2 Write a program to give the example of control statements.
2.2.1 If statements.
2.2.2 Switch Statements.
2.2.3 For loop.
2.2.4 While Statements.
2.2.5 Do statements
2.3 Write a program to calculate the following
2.3.1 Find the length of array.
2.3.2 Demonstrate a one-dimensional array.
2.3.3 Demonstrate a two-dimensional array.
2.3.4 Demonstrate a multi-dimensional array.
2.4 Write a program give example for command line arguments.
Lab Unit – 1 (2 Hrs actual Time)
1.1 Write a program to display any message:
1.2 Write a Java program to display default value of all primitive data types of
Lab Unit – 2 (2 Hrs actual
Time)
Object Oriented Programming with Java
2.4.1 To find the sum of command line arguments and count the
invalid integers entered.
2.4.2 To get the name using command line.
2.5 Write a program to print the following triangle of binary digits.
2.6. Write a program to the find the following
2.6.1 Prime number checking
2.6.2 Sum of digit
2.7 Write a program to arrange the numbers in ascending order.
2.8 Write a program to calculate the roots of Quadratic equations.
2.9 Write a program for calculating Matrix Operations.
2.9.1 Addition.
2.9.2 Multiplication.
Lab Unit – 3 (2 Hrs Real Time)
3.1 Write a program to create a room class, the attributes of this class is
roomno, roomtype, roomarea and ACmachine. In this class the member
functions are setdata and displaydata.
Object Oriented Programming with Java
Lab – 6 (2 Hrs Real Time)
6.1 Write a program to create interface A in this interface we have two
method meth1 and meth2. Implements this interface in another class
named MyClass.
6.2 Write a program to give example for multiple inheritance in Java.
6.3 Write a program to create interface named test. In this interface the
member function is square. Implement this interface in arithmetic class.
Create one new class called ToTestInt in this class use the object of
arithmetic class.
6.4 Create an outer class with a function display, again create another class
inside the outer class named inner with a function called display and call
the two functions in the main class.
Lab – 7 (2 Hrs Real Time)
7.1 Write a class called ColourChecking. Define a color with red = 193, green
=255 and blue = 183. No separate the rgb values. Find the Hue, saturation
and brightness of this color.
7.2 Write a program to check the font class method as follows: Create a font
TimesRoman bold and Italic size 12. In this object use the font methods.
Then display the attributes of the font.
7.3 Write a program to find the solution for the following problems using
Recursion.
7.3.1 Find the maximum of an array. Let a[] be an array of integers. if n= 1,
a[0] is the only number in the array and so, maximum = a[0]. if n > 1 ,
then do the following: find the maximum of n-1 entries of the array.
Compare this maximum with the last entry a[n-1] and finalize.
7.3.2 Find the Fibonacci numbers are defined as F 0 =1,F 1 =1 and F i=F i-1 +F i-
for i >2.
Lab Exercises
Lab – 8 (2 Hrs Real Time)
8.1 Create class point with following instance variable and methods.
Instance variable: private int x,y
Constructors : public Point(), Point(int x, int y)
Methods : public void setX(int x), setY(int y), setXY(int x,
int y)
8.2 Create class Number with only one private instance variable as a double
primitive type. To include the following methods (include respective
constructors) isZero( ), isPositive(), isNegative( ), isOdd( ), isEven( ),
isPrime(), isAmstrong() the above methods return boolean primitive type.
getFactorial(), getSqrt(), getSqr(), sumDigits(), getReverse() the above
methods return double primitive type. void listFactor(), void dispBinary().
8.3 Write a program to create a package named mypack and import it in circle
class.
8.4 Write a program to create a package named pl, and implement this
package in ex1 class.
Lab – 9 (2 Hrs Real Time)
9.1 Write a program to create automatic type conversions apply to overriding.
9.2 Create class box and box3d. box3d is extended class of box. The above
two classes going to pull fill following requirement
Include constructor.
set value of length, breadth, height
Find out area and volume.
Note: Base class and sub classes have respective methods and instance
variables.
9.3 Write a program using vector class.
Lab Exercises
12.2 Write a program to accept specified number of characters as input and
converts them into uppercase characters.
12.3 Write a program to get the input from the user and store it into file.
Using Reader and Writer file.
Lab – 13 (2 Hrs Real Time)
13.1 Write a program to illustrate creation of threads using runnable
class.(start method start each of the newly created thread. Inside the run
method there is sleep() for suspend the thread for 500 milliseconds).
13.2 Write a program to create a class MyThread in this class a constructor,
call the base class constructor, using super and starts the thread. The run
method of the class starts after this. It can be observed that both main
thread and created child thread are executed concurrently.
Lab – 14 (2 Hrs Real Time)
14.1 Write a program to get the reference to the current thread by calling
currentThread() method.
14.2 Write a program to create two threads. In this class we have one
constructor used to start the thread and run it. Check whether these two
threads are run are not.
Lab – 15 (2 Hrs Real Time)
15.1 Create a multithreaded program by creating a subclass of Thread and
then creating, initializing, and staring two Thread objects from your
class. The threads will execute concurrently and display Java is hot,
aromatic, and invigorating to the console window.
15.2 Create a multithreaded program as in the previous exercise by creating
the MyThread subclass of Thread. But create threads as objects of the
class MyClass, which is not a subclass of Thread. MyClass will
implement the runnable interface and objects of MyClass will be
executed as threads by passing them as arguments to the Thread
constructor.
Object Oriented Programming with Java
Lab – 16 (2 Hrs Real Time)
16.1 Write a program for inventory problem in this to illustrates the usage of
synchronized keyword.
16.2 Write a program for interthread communication process. In this they
have three classes consumer, producer and stock.
Producer addStock() Stock getStock() Consumer Notify() wait()
16.3 Write a program to show how synchronized methods and objects
monitors are used to coordinate access to a common object by multiple
threads. Clue use first program of this section for use will synchronized
methods.
16.4 Write a complex program to illustrate how the thread priorities? Imagine
that the first thread has just begun to run, even before it has a chance to
do anything. Now comes the higher priority thread that wants to run as
well. Now the higher priority thread has to do its work before the first
thread starts.
Lab – 17 (2 Hrs Real Time)
17.1 Write a Applet program to display the “Hello World “ in the browser.
17.2 Write a Applet program that automatically display the text with Font
Style, Font type.
Lab – 18 (2 Hrs Real Time)
18.1 Write a Applet program that automatically display the text with Font
Style, Font type Using getParameter Method.
18.2 Write a program that displays the menu bar and when You click the
options it has to display a dialog box stating which option has been
clicked.
18.3 Write a program that has menubar and also a quit option and if the user
clicks the quite option the applet should quit.
Lab Solutions
Lab Solutions
Object Oriented Programming with Java
Lab - 1 (2 hrs real time)
Ex - 1.
class Simout { public static void main (String args[ ] ) { System.out.println (“Welcome to Java programming”); } }
Ex - 1.
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); } }
Ex - 1.
class Streq { public static void main (String args [ ])
Object Oriented Programming with Java
{ 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 – 2 (2 Hrs Real Time)
Ex - 2.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); } }
Ex - 2.1.
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); } }
Ex - 2.1.
Arithmetic operators: class FloatMath { public static void main ( String args [ ] )
Object Oriented Programming with Java
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); } }
Ex - 2.2.
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"); } } }
Ex - 2.2.
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"); } }
Lab Solutions
}
Ex – 2.2.
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 ); } }
Ex – 2.2.
class WhileTest { public static void main (String args [ ] ) { int i=1; while (i<=5) { System.out.println ("i =" + i); i++; } } }
Ex – 2.2.
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); } }
Ex – 2.3.
Lab Solutions
System.arraycopy (a, 0, b, 0 ,a.length); System.out.print ("After ArrayCopy a - - >" ); System.out.print (a); System.out.print ("After ArrayCopy b - ->" ); System.out.println (b); } }
Ex – 2.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(); } } }
Ex – 2.4.
public class Summation { public static void main(String a[]) { int sum = 0; int invalid = 0; for(int I=0; I<a.length;I++) { try { sum += Integer.parseInt(a[I]); } catch(NumberFormatException e) { invalid++;
Object Oriented Programming with Java
} } System.out.println(“Total no. of arguments :” + a.length); System.out.println(“Invalid Integers:” + invalid); System.out.println(“Sum :”+sum); } }
Ex – 2.4.
class cmdline { public static void main (String args [ ]) { for ( int i=0 ; i < args.length ; i++) System.out.println (args [ i ]); } }
Ex – 2.5.
public class BinaryTriangle { public static void main (String arg [ ] ) { String k = “1”, l = “ “, s = “1”; int m = 0; int n = 5; //* if necessary change the value of n** // for (int i = 0; i < n; i++) { for (int j = 1; j < m; j++) { l+= “0”; } System.out.println (k + l + s + “\n”); l = “”; m += 2; } } }
Ex – 2.5.
public class NumberReverseTriangle { public static void main (String arg[ ]) { String k= “” ;
Object Oriented Programming with Java
class prime { public static void main (String args [ ]) { int i, j, n, lastn; double a; boolean flag; for (i=0;i<1000;i++) { a = i; a = Math.sqrt (a); lastn = (int)a; flag =true; for (j=2;j<=lastn; j+ +) { if(i != j) { if(i % j = =0) { flag = false; break; } } } if (flag) System.out.println (“\n” + i ); } } }
Ex – 2.
class exarray { public static void main (String args [ ] ) { int [ ] arr = {234,6,846,85,96,198,545,12,60,34,4,87,7,1}; int i, j, l, temp; l= arr.length; for (i=0;i<l-1;i++) { for (j=i+1;j<l;j++) { temp = arr [i]; arr[i] = arr[j]; arr[j] = temp; } } for (i=0;i<l;i++) { System.out.println (arr[i]);
Lab Solutions
} } }
Ex – 2.
public class Quad { double a, b, c; Quad(double a, double b, double c) { a = a; b = b; c = c; } void root() { double r1, r2, d, rp, ri; d= a* a – 4 * ac; if (d<0) { rp = -b/(2.0 * a); ri = Math.sqrt(Math.abs(d))/(2.0a); System.out.println(“ The roots are complex conjugates”); System.out.println(“Roots1 = “+ rp + “i” + ri); System.out.println(“Roots2 = “+ rp + “i”+ ri); } if (d==0) { r1= -b/(2.0a); System.out.println(“The roots are real and equal”); System.out.println(“Root = “ + r1); } if (d>0) { r1 = (-b +Math.sqrt(d))/(2.0a); r2 = (-b-Math.sqrt(d))/(2.0*a); System.out.println(“Root1 = “ + r1 + “\n Root2 = “ + r2); } } public static void main(String a[]) { Quad q1,q2,q3; q1 = new Quad(1.0, -5.0, 6.0); q2 = new Quad(4.0, -20.0, 25.0); q3 = new Quad(2.0, 1.0, 2.0); q1.root(); q2.root(); q3.root(); }