Upload files to "OOP/Java/Lab/Week2"
This commit is contained in:
parent
40f695b27c
commit
24683dac5a
21
OOP/Java/Lab/Week2/BitwiseMulDiv.java
Normal file
21
OOP/Java/Lab/Week2/BitwiseMulDiv.java
Normal 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+".");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
47
OOP/Java/Lab/Week2/Calculator.java
Normal file
47
OOP/Java/Lab/Week2/Calculator.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
17
OOP/Java/Lab/Week2/TypecastAnalysis.java
Normal file
17
OOP/Java/Lab/Week2/TypecastAnalysis.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user