OOPJ Assignment 2
OOPJ Assignment 2
> Program:
package day2;
import java.util.*;
public class leapYear {
public static void main(String args []){
System.out.println("Enter the year: ");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if(( year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ){
System.out.println("The year is leap year.");
}
else{
System.out.println("This year is not a leap year. ");
}
}
}
>Output:
2)Implement a program that calculates the Body Mass Index (BMI) based on height and weight input
using if-else to classify the BMI int categories (underweight, normal weight, overweight,etc).
> Program:
package day2;
import java.util.*;
public class BMI {
public static void main(String args[]){
System.out.println("Enter Height(in meter): ");
Scanner sc = new Scanner(System.in);
int height = sc.nextInt();
System.out.println("Enter Weight(in kg): ");
int weight = sc.nextInt();
float BMI= weight/(height*height);
>Output:
3)Write a program that checks if a person is eligible to vote based on their age.
> Program:
package day2;
import java.util.Scanner;
public class vote {
public static void main(String args[]){
System.out.println("Enter your age: ");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if(age>=18){
System.out.println("You are eligible to vote.");
}else{
System.out.println("You are not eligible to vote.");
}
}
}
>Output:
4)Write a program that takes a month (1-12) and prints the corresponding season (Winter, Spring,
Summer, Autumn) using a switch case.
> Program:
package day2;
import java.util.Scanner;
public class season {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of the month : ");
int num = sc.nextInt();
String season;
switch(num){
case 1: case 2 : case 3 :
season = "Winter";
System.out.println("The season is" +season);
break;
case 4: case 5: case 6 :
season = "Spring";
System.out.println("The season is" +season);
break;
case 7: case 8 : case 9 :
season = "Summer";
System.out.println("The season is" +season);
break;
case 10: case 11 : case 12:
season = "Autumn";
System.out.println("The season is" +season);
break;
default :
System.out.println("Invalid Input");
break;
}
sc.close();
}
}
Output:
5)Write a program that allows the user to select a shape (Circle, Square, Rectangle, Triangle) and then
calculates the area based on user-provided dimensions using a switch case.
>Program:
package day2;
import java.util.*;
public class a1{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the shape of whose area you want.");
String shape = sc.nextLine();
switch(shape){
case "Circle":
System.out.println("Enter the radius of Circle (in cms): ");
int r = sc.nextInt();
double area0 = (3.14*r*r);
System.out.println("Area of Circle is "+ area0 + "cm^2");
break;
case "Square" :
System.out.println("Enter the length of side of a Square (in cms)");
int s = sc.nextInt();
int area1 = (s*s);
System.out.println("Area of Square is " + area1 );
break;
case "Rectangle" :
System.out.println("Enter the length of Rectangle.");
int l = sc.nextInt();
System.out.println("Enter the breadth of Rectangle.");
int b = sc.nextInt();
int area2 = (l*b);
System.out.println("The area of Rectangle is " + area2 );
break;
case "Triangle" :
System.out.println("Enter the length of base of the Triangle: ");
int a = sc.nextInt();
System.out.println("Enter the length of the Triangle: ");
int c = sc.nextInt();
double area3 = 0.5 *a*c;
System.out.println("The area of Triangle is " + area3 );
break;
default:
System.out.println("Invalid Input");
}
sc.close();
}
}
Output:
• New possibility:
Error :
Fix:
Reference: https://stackoverflow.com/questions/43120687/resource-leak-sc-is-never-closed