0% found this document useful (0 votes)
7 views4 pages

Java Programs for Year, BMI, Voting, Seasons, Area

Uploaded by

Aaditya Sutar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Java Programs for Year, BMI, Voting, Seasons, Area

Uploaded by

Aaditya Sutar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1)Write a program that checks if a given year is a leap year or not using both if-else and switch-case.

> Program:
package day2;
import [Link].*;
public class leapYear {
public static void main(String args []){
[Link]("Enter the year: ");
Scanner sc = new Scanner([Link]);
int year = [Link]();
if(( year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ){
[Link]("The year is leap year.");
}
else{
[Link]("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 [Link].*;
public class BMI {
public static void main(String args[]){
[Link]("Enter Height(in meter): ");
Scanner sc = new Scanner([Link]);
int height = [Link]();
[Link]("Enter Weight(in kg): ");
int weight = [Link]();
float BMI= weight/(height*height);

if( BMI <= 18.5f){


[Link]("You are underweight.");
}else if ( 18.6f <= BMI && BMI <=24.9f){
[Link]("You are Normal Weight.");
}else{
[Link]("You are over weight.");
}
}
}

>Output:
3)Write a program that checks if a person is eligible to vote based on their age.
> Program:
package day2;
import [Link];
public class vote {
public static void main(String args[]){
[Link]("Enter your age: ");
Scanner sc = new Scanner([Link]);
int age = [Link]();
if(age>=18){
[Link]("You are eligible to vote.");
}else{
[Link]("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 [Link];
public class season {
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
[Link]("Enter number of the month : ");
int num = [Link]();
String season;
switch(num){
case 1: case 2 : case 3 :
season = "Winter";
[Link]("The season is" +season);
break;
case 4: case 5: case 6 :
season = "Spring";
[Link]("The season is" +season);
break;
case 7: case 8 : case 9 :
season = "Summer";
[Link]("The season is" +season);
break;
case 10: case 11 : case 12:
season = "Autumn";
[Link]("The season is" +season);
break;
default :
[Link]("Invalid Input");
break;
}
[Link]();
}
}

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 [Link].*;
public class a1{
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
[Link]("Enter the shape of whose area you want.");
String shape = [Link]();
switch(shape){

case "Circle":
[Link]("Enter the radius of Circle (in cms): ");
int r = [Link]();
double area0 = (3.14*r*r);
[Link]("Area of Circle is "+ area0 + "cm^2");
break;

case "Square" :
[Link]("Enter the length of side of a Square (in cms)");
int s = [Link]();
int area1 = (s*s);
[Link]("Area of Square is " + area1 );
break;

case "Rectangle" :
[Link]("Enter the length of Rectangle.");
int l = [Link]();
[Link]("Enter the breadth of Rectangle.");
int b = [Link]();
int area2 = (l*b);
[Link]("The area of Rectangle is " + area2 );
break;
case "Triangle" :
[Link]("Enter the length of base of the Triangle: ");
int a = [Link]();
[Link]("Enter the length of the Triangle: ");
int c = [Link]();
double area3 = 0.5 *a*c;
[Link]("The area of Triangle is " + area3 );
break;

default:
[Link]("Invalid Input");
}
[Link]();
}
}

Output:

• New possibility:
Error :

Fix:

Reference: [Link]

Common questions

Powered by AI

The decision-making process uses a switch-case to identify the shape and prompt specific dimensions: radius for Circle, side for Square, length and breadth for Rectangle, and base and height for Triangle. Each case is distinct and calculates the respective area using appropriate formulas, ensuring tailored inputs and outputs for each shape, enhancing the program's flexibility and user interaction .

Switch-case is less ideal when handling non-discrete or range-based input scenarios, such as validating user input within a range that needs customized error checking and handling. It also lacks flexibility for dynamically checking conditions that arise from multiple factors or intervals. Instead, if-else statements or more complex logic might be needed for checking such conditions effectively .

A Java program can determine if a year is a leap year using conditional logic through two statements: If the year is divisible by 400, it is a leap year. If the year is divisible by 4 but not by 100, it is also a leap year. Otherwise, it is not a leap year. This logical flow ensures the correct identification of leap years according to the Gregorian calendar .

Using integers for height can lead to significant inaccuracies in BMI calculations because height is typically measured with greater precision (e.g., meters with decimals). This might round down values precipitously affecting BMI results. Using floating-point numbers (e.g., floar or double) for both height and weight inputs can increase accuracy by accommodating finer measurements .

Closing a Scanner is critical to prevent resource leaks, as opened resources like Scanner, linking to system input, can remain unused and occupied, increasing system load and potentially leading to memory leaks. Proper management, such as using 'sc.close()', ensures efficient resource allocation and program performance .

The algorithm for classifying BMI uses the formula BMI = weight/(height*height) and follows these categories: BMI <= 18.5 is classified as underweight, 18.6 <= BMI <= 24.9 as normal weight, and any BMI above that as overweight. These categories help in assessing health risk based on body weight compared to height, which can guide personal health decisions and interventions .

Switch-case statements are more efficient for multiple distinct, flat conditions like choosing among known shape types. Compared to if-else, which is better suited for range-based or complex logical decisions, switch-cases enhance readability and execution time when options are mutually exclusive and predictable. Thus, they are particularly well-suited to the explicit user input-driven selection in area calculations .

Error handling mitigates incorrect user inputs by preemptively checking input validity and providing feedback or corrections, preventing runtime exceptions and ensuring continued operation. For instance, verifying numerical inputs or guiding users when errors occur helps maintain program flow without crashes, crucial for robustness and user experience reliability .

A Java program determines voting eligibility by checking if the age input is 18 or above. If so, it outputs that the person is eligible to vote; otherwise, it states they are not eligible. This binary decision uses simple conditional logic with an if-else statement .

A switch-case statement maps month numbers to seasons by handling cases for groups of consecutive months: 1-3 for Winter, 4-6 for Spring, 7-9 for Summer, and 10-12 for Autumn. This structure improves clarity and maintainability over complex if-else chains by clearly associating specific inputs with their corresponding outputs .

You might also like