From 24683dac5ae3d42491d37899201795400d861c76 Mon Sep 17 00:00:00 2001 From: aadit Date: Mon, 5 Aug 2024 01:05:13 +0530 Subject: [PATCH] Upload files to "OOP/Java/Lab/Week2" --- OOP/Java/Lab/Week2/BitwiseMulDiv.java | 21 +++++++++++ OOP/Java/Lab/Week2/Calculator.java | 47 ++++++++++++++++++++++++ OOP/Java/Lab/Week2/TypecastAnalysis.java | 17 +++++++++ 3 files changed, 85 insertions(+) create mode 100644 OOP/Java/Lab/Week2/BitwiseMulDiv.java create mode 100644 OOP/Java/Lab/Week2/Calculator.java create mode 100644 OOP/Java/Lab/Week2/TypecastAnalysis.java diff --git a/OOP/Java/Lab/Week2/BitwiseMulDiv.java b/OOP/Java/Lab/Week2/BitwiseMulDiv.java new file mode 100644 index 0000000..0a806d4 --- /dev/null +++ b/OOP/Java/Lab/Week2/BitwiseMulDiv.java @@ -0,0 +1,21 @@ +import java.util.Scanner; + +public class BitwiseMulDiv { + public static void main(String args[]){ + Scanner sc = new Scanner(System.in); + + int a,b,c; + + System.out.println("Enter the value you want as an input: "); + a = sc.nextInt(); + + b = a >> 1; + + System.out.println("When divided by 2, the result is "+b+"."); + + c = a << 1; + + System.out.println("When divided by 2, the result is "+c+"."); + } + +} diff --git a/OOP/Java/Lab/Week2/Calculator.java b/OOP/Java/Lab/Week2/Calculator.java new file mode 100644 index 0000000..f7a3169 --- /dev/null +++ b/OOP/Java/Lab/Week2/Calculator.java @@ -0,0 +1,47 @@ +import java.util.Scanner; + +public class Calculator { + + public static void main(String args[]){ + Scanner sc = new Scanner(System.in); + char ch = 'y',f; + do{ + System.out.println("Enter the first number: "); + float a = sc.nextFloat(); + + System.out.println("Enter the operator (+,-,/,*): "); + char b = sc.next().charAt(0); + + if((b != '+') && (b != '-') && (b != '/') && (b != '*') && (b != 'x')){ + System.out.println("Invalid operator."); + } + + System.out.println("Enter the second number: "); + float c = sc.nextFloat(); + + switch(b){ + case '+': + float d = a+c; + System.out.println(+a+" + "+c+" = "+d); + break; + case '-': + float e = a-c; + System.out.println(+a+" - "+c+" = "+e); + break; + case '/': + float g = a / c; + System.out.println(+a+" / "+c+" = "+g); + break; + case 'x': + case '*': + float h = a*c; + System.out.println(+a+" * "+c+" = "+h); + break; + } + + System.out.println("Do you want to perform another calculation (y/n)?"); + f = sc.next().charAt(0); + } while(f == ch); + + } +} diff --git a/OOP/Java/Lab/Week2/TypecastAnalysis.java b/OOP/Java/Lab/Week2/TypecastAnalysis.java new file mode 100644 index 0000000..b1734d6 --- /dev/null +++ b/OOP/Java/Lab/Week2/TypecastAnalysis.java @@ -0,0 +1,17 @@ +public class TypecastAnalysis { + public static void main(String args[]){ + + int x = 10; + double y = x; + System.out.println("For the first code, the output is:"+y); + + //double a = 10.0; + //int b = a; + //System.out.println(b); + System.out.println("For the second code, the output is: Type mismatch: cannot convert from double to int at TypecastAnalysis.main(TypecastAnalysis.java:11)"); + + double c = 10.0; + int d = (int) c; + System.out.println("For the third code, the output is: "+d); + } +}