























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Unit1.Program to Display any Message and Program to Check two Strings are Equal or Not Unit 2.Increment, Decrement, Arithmetic Relational and Bitwise Operators.From Manonmaniam Sundaranar University India
Typology: Exercises
1 / 95
This page cannot be seen from the preview
Don't miss anything!
Lab Exercises
Object Oriented Programming with Java
Object Oriented Programming with Java
Lab Exercises
Lab Exercises
Object Oriented Programming with Java
Producer addStock() Stock getStock() Consumer Notify() wait()
Lab Solutions
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(); }