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

This commit is contained in:
Aadit Agrawal 2024-08-05 01:04:41 +05:30
parent 730413c501
commit 40f695b27c
5 changed files with 129 additions and 0 deletions

View 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)");
}
}

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

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

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

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