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

OOP Lab: Java Programming Tasks

This document provides code examples and instructions for 3 Java programming exercises as part of an introductory lab on object-oriented programming. The first exercise converts inches to meters, the second converts days to months and days, and the third calculates body mass index (BMI) based on user-input weight and height. Each exercise includes sample input/output and the Java code to perform the given calculation.

Uploaded by

Karan Kukreja
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)
23 views4 pages

OOP Lab: Java Programming Tasks

This document provides code examples and instructions for 3 Java programming exercises as part of an introductory lab on object-oriented programming. The first exercise converts inches to meters, the second converts days to months and days, and the third calculates body mass index (BMI) based on user-input weight and height. Each exercise includes sample input/output and the Java code to perform the given calculation.

Uploaded by

Karan Kukreja
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

Introduction to OOP Lab #1

LAB # 1

INTRODUCTION TO OOP

OBJECTIVE:
To understand OOP(Java Environment),Data Types and Mixed Arthematic
Expression.

LAB TASK

[Link] a Java program that reads a number in inches, converts it to meters.


Note: One inch is 0.0254 meter.

Example:
Test Data
Input a value for inch: 1000.
Expected Output:
1000.0 inch is 25.4 meters.

CODE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q1;

/**
*
* @author Karan kumar
*/
import [Link];
public class Q1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner number = new Scanner ([Link]);
[Link]("enter value in inches: ");
int num = [Link]();
double digit= num*(0.0254);
[Link](digit +"meters");

Object Oriented Programming - OOPs 1


Introduction to OOP Lab #1

OUTPUT:

[Link] a Java program to convert days into number of months and days.
Example:
Test Data
Input the number of days: 69.
Expected Output:
69 days are 2 months and 9 days.

CODE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q2;

/**
*
* @author Karan kumar
*/
import [Link];
public class Q2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Scanner in = new Scanner([Link]);


[Link]("Enter days: ");
int days = [Link]();

int month = (days % 365) / 30;


days = (days % 365) % 7;

[Link]("months = " + month);

Object Oriented Programming - OOPs 2


Introduction to OOP Lab #1

[Link]("Days = " + days);


}
}

OUTPUT:

3. Write a Java program to compute body mass index (BMI).


Note: weigh in kilogram = weight * 0.45359237.
Hight in feet= inches * 0.0254.
BMI= weight in kilogram/(hight in fee)^2.

CODE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q3;

/**
*
* @author Karan kumar
*/
import [Link];
public class Q3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner input = new Scanner([Link]);
[Link]("Input weight in pounds: ");
double weight = [Link]();

[Link]("Input height in inches: ");


double inches = [Link]();

double BMI = weight * 0.45359237 / (inches * 0.0254 * inches * 0.0254);


[Link]("Body Mass Index is " + BMI+"\n");
}
}

OUTPUT:

Object Oriented Programming - OOPs 3


Introduction to OOP Lab #1

Object Oriented Programming - OOPs 4

Common questions

Powered by AI

The program uses division to estimate the number of months by dividing the total days by 30 and the modulus operation to find remaining days after accounting for these full 30-day months. This approach simplifies calculations but lacks precision for non-30 day months or different annual lengths. Advanced calculations would need conditional logic to compute months correctly based on varied month lengths.

The Scanner class is used to capture user input in Java. It reads the input provided via the console by the user, allowing the programs to interact dynamically with input data such as numbers or strings. In these programs, Scanner is crucial for reading input values such as inches, days, weight, and height.

The Java program converts a number from inches to meters by multiplying the input value in inches by the conversion factor 0.0254, as 1 inch is equal to 0.0254 meters. The code reads an integer input from the user and then calculates the length in meters using the formula: meters = inches * 0.0254.

Error handling can be integrated using try-catch blocks to manage non-integer or unexpected inputs, along with input validation to guide users towards correct data entry. Providing informative error messages and using input loops to allow users to re-enter data can improve robustness and user experience. Moreover, input constraints and prompts should clarify expected data types before user interaction.

The program calculates BMI by first converting weight from pounds to kilograms using the conversion constant 0.45359237, and then converting height from inches to meters using the constant 0.0254. The BMI is computed using the formula: BMI = (weight in kg) / (height in meters)^2.

Conversion constants are essential for ensuring that units are correctly converted to maintain accuracy in calculations. Using exact values, such as 0.0254 for converting inches to meters, guarantees that calculations adhere to standardized measures, reducing the risk of errors in results due to incorrect or approximate constant values.

The program's method for converting days to months and days is limiting because it assumes each month consists of exactly 30 days. This can lead to inaccurate results for full-year calculations. An improvement would be to use the actual days of each month for more precise conversion, especially considering leap years that affect February's length.

The program calculates the number of months and remaining days by assuming each month has 30 days on average. It divides the total days by 30 to get the months and uses the remainder to calculate the days. This approach doesn't account for months with varying lengths (28, 29, 30, or 31 days), which can affect accuracy.

The program incorrectly calculates remaining days after determining months using a modulus operation assuming every month has 30 days. The logic can be improved by first accounting for complete years, followed by using an array of days (31 for Jan, 28/29 for Feb, etc.) to iteratively subtract months until the remaining days are fewer than the next month's total.

The exercises introduce fundamental OOP concepts, though they primarily focus on procedural aspects of programming in Java. The structure highlights object-oriented plans through the organization of code into classes and methods which simulate real-world objects and operations, aligning with OOP principles. However, advanced OOP features like inheritance and polymorphism are not covered in these tasks.

You might also like