OOP Lab 3
OOP Lab 3
Task 1:
Source Code:
import java.util.Scanner;
/**
*
* @author Mohammad Anas
*/
public class Task1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in); // To get input from the user
System.out.print("Enter your weight (in kilograms): "); // Asking the user to enter their
weight in kilograms
float weight = input.nextFloat();
System.out.print("Enter your height (in meters): "); // Asking the user to enter their height
in meters
float height = input.nextFloat();
float bmi;
bmi = weight/(height*height); // Calculating the Body Mass Index (BMI)
if (bmi < 18.5) // Using control flow structure to tell user if their BMI refers to Underweight,
Normal, Overweight or Obese
System.out.println("You are Underweight.");
else if (bmi > 18.5 && bmi < 24.9)
System.out.println("Your BMI is normal.");
else if (bmi > 25 && bmi < 29.9)
System.out.println("You are overweight.");
else if (bmi > 30)
System.out.println("You are Obese.");
}//end of main method
Task 2:
Source 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 task_2;
import java.util.Scanner;
/**
*
* @author Mohammad Anas
*/
public class Task_2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
int kilometers = 0;
int litres = 0;
double k_per_l = 0.0;
int total_kilometers = 0;
int total_litres = 0;
double total_k_per_l = 0.0;
int trips = 0;
if (trips > 1) {
System.out.printf("\n Total kilometers of your %d trips is %d\n", trips,
total_kilometers);
System.out.printf(" Total litres of your %d trips is %d\n", trips, total_litres);
System.out.printf(" Combined kilometers per litre of your %d trips is %.1f\n", trips,
total_k_per_l);
}
// Prompt the user for the next trip miles (possibly the sentinel)
trips++;
System.out.printf("\nEnter distance traveled in kilometers (as integer) or -1 to quit: ",
trips);
kilometers = input.nextInt();
if (kilometers != -1) {
System.out.printf("Enter trip %d litres (as integer): ", trips);
litres = input.nextInt();
}
}
if (total_kilometers != 0) {
System.out.printf("\nTotal kilometers driven is: %d\n", total_kilometers);
System.out.printf("Total litres used is: %d\n", total_litres);
System.out.printf("Combined kilometers per litre is: %.1f\n\n", total_k_per_l);
} else {
System.out.printf("No trip information was entered.\n\n");
}
}
}
Task 3:
Source Code:
import java.util.Scanner;
/**
*
* @author Mohammad Anas
*/
int z = 0;
while(z < 2){