Lab Sesion-3
Basic JAVA Programming and Object Orientation
1. Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BML. (1 pound = 0.45359237 Kg and 1 inch = 0.0254 meters.)
Program Code :
import java.util.Scanner;
public class BMI {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
// Prompt the user to enter weight in pounds
System.out.print("Enter weight in pounds : ");
double weight = input.nextDouble();
System.out.print("Enter height in inches : ");
double height = input.nextDouble();
final double KILOGRAMS_PER_POUND = 0.4535937;
final double METERS_PER_INCH = 0.0254;
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
double heightInMeters = height * METERS_PER_INCH;
double bmi = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("BMI is" + bmi);
if (bmi < 18.5)
System.out.println("Under weight");
else if (bmi <25)
System.out.println("Normal");
else if (bmi <30)
System.out.println("Over weight");
else
System.out.println("Obses" );
}
}
OUTPUT :
2. Create a student class that has following properties
a. First Name
b. Last Name
c. Enrollment Number
d. College Name
e. Branch Name
f. Hometown
g. Current Percentage (CPI)
1. Use idea of Encapsulation and provide getter and setter method for all properties.
2. Create a second class (which is your public class with main method) and create five objects of students and set the values for above properties .
3. Ask the user to enter first name of the student to search.
4. If the first name entered by user matches with the first name of any of the five student objects, all the properties of those object should get pronted. Else print error message saying none of the exiting student objects matches with search criteria.
Program Code :
package Test.java;
public class EncapsulationDemo
{
private String first_Name;
private String last_Name;
private long enrolment_Number;
private String college_Name;
private String branch_Name;
private String hometown;
private float current_Percentage;
public String getFirst_Name(){
return first_Name;
}
public String getLast_Name(){
return last_Name;
}
public long getEnrolment_Number(){
return enrolment_Number;
}
public String getCollege_Name(){
return college_Name;
}
public String getBranch_Name(){
return branch_Name;
}
public String getHometown(){
return hometown;
}
public float getCurrent_Percentage(){
return current_Percentage;
}
public void setFirst_Name(String name){
first_Name = name;
}
public void setLast_Name(String name){
last_Name = name;
}
public void setEnrolment_Number(long number){
enrolment_Number = number;
}
public void setCollege_Name(String name){
college_Name = name;
}
public void setBranch_Name(String name){
branch_Name = name;
}
public void setHometown(String name){
hometown = name;
}
public void setCurrent_Percentage(float number){
current_Percentage = number;
}
public static void main(String[] args){
EncapsulationDemo s1 = new EncapsulationDemo();
s1.setFirst_Name("ABC");
s1.setLast_Name("XYZ");
s1.setEnrolment_Number(100000000001l);
s1.setCollege_Name("Your College Name");
s1.setBranch_Name("Your Branch");
s1.setHometown("Your Homwtown");
s1.setCurrent_Percentage(10.0f);
System.out.println("first_Name : "+s1.getFirst_Name());
System.out.println("last_Name : "+s1.getLast_Name());
System.out.println("enrolment_Number : "+s1.getEnrolment_Number());
System.out.println("college_Name : "+s1.getCollege_Name());
System.out.println("branch_Name : "+s1.getBranch_Name());
}
}
OUTPUT :
No comments:
Post a Comment