Upload files to "OOP/Java/Lab/Week2"
This commit is contained in:
parent
730413c501
commit
40f695b27c
14
OOP/Java/Lab/Week2/BooleanTypecast.java
Normal file
14
OOP/Java/Lab/Week2/BooleanTypecast.java
Normal file
@ -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)");
|
||||
|
||||
}
|
||||
}
|
30
OOP/Java/Lab/Week2/LeapYear.java
Normal file
30
OOP/Java/Lab/Week2/LeapYear.java
Normal file
@ -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.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
28
OOP/Java/Lab/Week2/Operators.java
Normal file
28
OOP/Java/Lab/Week2/Operators.java
Normal file
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
23
OOP/Java/Lab/Week2/Ternary.java
Normal file
23
OOP/Java/Lab/Week2/Ternary.java
Normal file
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
34
OOP/Java/Lab/Week2/TypeConversion.java
Normal file
34
OOP/Java/Lab/Week2/TypeConversion.java
Normal file
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user