0% found this document useful (0 votes)
40 views

OOP Lab 3

This document contains source code for 3 tasks in an OOP lab. Task 1 asks the user to input their weight and height and calculates their BMI, printing whether they are underweight, normal, overweight, or obese. Task 2 tracks multiple trips by asking for kilometers and liters each time, calculating the km/L for each trip and a total km/L. Task 3 stores 10 username/password pairs by prompting the user, then allows login by checking the input against the stored values.

Uploaded by

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

OOP Lab 3

This document contains source code for 3 tasks in an OOP lab. Task 1 asks the user to input their weight and height and calculates their BMI, printing whether they are underweight, normal, overweight, or obese. Task 2 tracks multiple trips by asking for kilometers and liters each time, calculating the km/L for each trip and a total km/L. Task 3 stores 10 username/password pairs by prompting the user, then allows login by checking the input against the stored values.

Uploaded by

Mohammad Anas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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)

System.out.println("Your BMI: "+bmi); // Printing the final result to the user

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

}//end of public class Task1

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;

System.out.print("Enter distance traveled in kilometers (as integer) or -1 to quit: ");


kilometers = input.nextInt();
if (kilometers != -1) {
System.out.print("Enter trip 1 litres (as integer): ");
litres = input.nextInt();
trips++;
}

// While the user has not yet entered the sentinel


while (kilometers != -1) {
total_kilometers += kilometers;
total_litres += litres;
total_k_per_l = (double) total_kilometers / total_litres;

k_per_l = (double) kilometers / litres;

System.out.printf("Trip %d's kilometers per litres is %.1f\n", trips, k_per_l);

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
*/

public class Lab4 {


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
String[] users = new String[10];
String[] password = new String[10];

for (int i= 0; i < 10; i++){

System.out.println("Enter the username");


String username = input.nextLine();
users[i] = username;

System.out.println("Enter your password");


String passwordUser = input.nextLine();
password[i] = passwordUser;

int z = 0;
while(z < 2){

System.out.println("Enter your username");


String username = input.nextLine();

System.out.print("Enter your password");


String passwordUser = input.nextLine();
for(int i = 0; i < users.length;i++){
if(users[i].equals(username)){
if(password[i].equals(passwordUser)){
System.out.print("Welcome to the console");
z = 3;
break;
}else{
System.out.print("The Password is incorrect");
}
}else{
System.out.print("The username is incorrect");
}
}
}
}
}

You might also like