diff --git a/OOP/Java/Lab/Week2/BooleanTypecast.java b/OOP/Java/Lab/Week2/BooleanTypecast.java new file mode 100644 index 0000000..9a44ce2 --- /dev/null +++ b/OOP/Java/Lab/Week2/BooleanTypecast.java @@ -0,0 +1,14 @@ +public class BooleanTypecast { + public static void main(String args[]){ + //boolean x = true; + //int y = x; + + System.out.println("For the first code, the output is: Type mismatch: cannot convert from boolean to int at BooleanTypecast.main(BooleanTypecast.java:4)"); + + // boolean x = true; + //int y = (int) x; + + System.out.println("For the second code, the output is: Cannot cast from boolean to int at BooleanTypecast.main(BooleanTypecast.java:9)"); + + } +} diff --git a/OOP/Java/Lab/Week2/LeapYear.java b/OOP/Java/Lab/Week2/LeapYear.java new file mode 100644 index 0000000..171e480 --- /dev/null +++ b/OOP/Java/Lab/Week2/LeapYear.java @@ -0,0 +1,30 @@ +import java.util.Scanner; // Import the Scanner class + +class LeapYear { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int year1; + + boolean flag; + + System.out.println("Enter the year you want to check: "); + year = sc.nextInt(); + + if(year%4 != 0){ + flag = false; + }else if(year%100 != 0){ + flag = true; + }else if(year%400 != 0){ + flag = false; + }else{ + flag = true; + } + + if(flag == false){ + System.out.println("The year "+year+" is not a leap year."); + }else{ + System.out.println("The year "+year+" is a leap year."); + } + + } +} \ No newline at end of file diff --git a/OOP/Java/Lab/Week2/Operators.java b/OOP/Java/Lab/Week2/Operators.java new file mode 100644 index 0000000..6ebca57 --- /dev/null +++ b/OOP/Java/Lab/Week2/Operators.java @@ -0,0 +1,28 @@ +import java.util.Scanner; +public class Operators { + + public static void main(String args[]){ + + Scanner sc = new Scanner(System.in); + + System.out.println("Enter the value of a:"); + int a = sc.nextInt(); + + System.out.println("Enter the value of b:"); + int b = sc.nextInt(); + + int c = (a<<2) + (b>>2); + System.out.println("Output of (a<<2) + (b>>2) is: "+c); + + boolean d = (b > 0); + System.out.println("Output of (b>0) is: "+d); + + int e = (a + b * 100) / 10; + System.out.println("Output of ((a + b * 100) / 10) is: "+e); + + int f = a & b; + System.out.println("Output of (a & b) is: "+f); + + } + +} diff --git a/OOP/Java/Lab/Week2/Ternary.java b/OOP/Java/Lab/Week2/Ternary.java new file mode 100644 index 0000000..bee0113 --- /dev/null +++ b/OOP/Java/Lab/Week2/Ternary.java @@ -0,0 +1,23 @@ +import java.util.Scanner; +public class Ternary { + + public static void main(String args[]){ + + Scanner sc = new Scanner(System.in); + + System.out.println("Enter the first number: "); + int a = sc.nextInt(); + + System.out.println("Enter the second number: "); + int b = sc.nextInt(); + + System.out.println("Enter the third number: "); + int c = sc.nextInt(); + + int largest = (a>b)?((b>c)?a:c):((b>c)?b:c); + + System.out.println("The largest number is: "+largest); + + } + +} \ No newline at end of file diff --git a/OOP/Java/Lab/Week2/TypeConversion.java b/OOP/Java/Lab/Week2/TypeConversion.java new file mode 100644 index 0000000..4314fdf --- /dev/null +++ b/OOP/Java/Lab/Week2/TypeConversion.java @@ -0,0 +1,34 @@ +import java.util.Scanner; + +public class TypeConversion { + public static void main (String args[]){ + Scanner sc = new Scanner(System.in); + + System.out.println("Enter an integer value: "); + int a = sc.nextInt(); + + System.out.println("Enter a decimal value:"); + double b = sc.nextDouble(); + + System.out.println("Enter a letter:"); + char c = sc.next().charAt(0); + + // int to byte + byte d = (byte) a; + System.out.println("Converting the given integer into a byte value, we get: "+d); + + // char to int + int e = (int) c; + System.out.println("Converting the given character into an int value, we get: "+e); + + // double to byte + byte f = (byte) b; + System.out.println("Converting the given double value into a byte value, we get: "+f); + + // double to int + int g = (int) b; + System.out.println("Converting the given double value into an int value, we get: "+g); + + } + +}