Upload files to "OOP/Java/Lab/Week2"

This commit is contained in:
Aadit Agrawal 2024-08-05 01:05:13 +05:30
parent 40f695b27c
commit 24683dac5a
3 changed files with 85 additions and 0 deletions

View File

@ -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+".");
}
}

View File

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

View File

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