Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

B.M.S.

COLLEGE OF ENGINEERING

(Autonomous college under VTU)

Bull Temple Rd, Basavanagudi, Bengaluru, Karnataka 560019

2023-2025

Department of Computer Applications

“JAVA PROGRAMMING”

(22MCA2PCJP)

BY

Vandana B Gowda

1BM23MC110
MCA 2ND SEM
B SECTION

Under the Guidance

Prof. R.V.Raghavendra Rao

(Assistant Professor)
CONTENTS

SL. Programs Page No.


No.

1 Write a program to calculate eligibility of a student to appear SEE 1-2

2 Write a program to calculate taxation on electronic product 3-6

3 Write a program to prepare insurance premium of a particular person 7-10


based on age, habits, pre medical check-up details

4 Create a java program to suggest a person's BMI information with 11-15


necessary suggestions

5 Write a java program to enable unit conversions: 16-19


a. Kilometer to Meter
b. Weights to Pounds
c. Kilometer to Miles
d. Celcius to Fahrenheit
22MCA2PCJP: Java Programming

1. Write a program to calculate eligibility of a student to appear SEE.

package practice;

import java.util.Scanner;

public class StudentEligibility {

private String name;


private int totalClasses;
private int attendedClasses;
private int internalMarks;

public StudentEligibility(String name, int totalClasses, int attendedClasses, int


internalMarks) {
this.name = name;
this.totalClasses = totalClasses;
this.attendedClasses = attendedClasses;
this.internalMarks = internalMarks;
}

public double calculateAttendancePercentage() {


return ((double) attendedClasses / totalClasses) * 100;
}

public boolean isEligible() {


double attendancePercentage = calculateAttendancePercentage();
return attendancePercentage >= 75 && internalMarks >= 40;
}

public String getName() {


return name;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter student's name: ");


String name = scanner.nextLine();

System.out.print("Enter total number of classes: ");


int totalClasses = scanner.nextInt();

System.out.print("Enter number of classes attended: ");


int attendedClasses = scanner.nextInt();

Department Of Computer Applications, BMSCE 1


22MCA2PCJP: Java Programming

System.out.print("Enter internal assessment marks: ");


int internalMarks = scanner.nextInt();

StudentEligibility student = new StudentEligibility(name, totalClasses,


attendedClasses, internalMarks);

System.out.println(student.getName() + " eligibility: " + (student.isEligible()


? "Eligible" : "Not Eligible"));

scanner.close();
}

Department Of Computer Applications, BMSCE 2


22MCA2PCJP: Java Programming

2. Write a program to calculate taxation on electronic product.

package producttax;

public class Product {

private String name;


private double price;
private double taxRate;

public Product(String name, double price, double taxRate) {


this.name = name;
this.price = price;
this.taxRate = taxRate;
}

public double calculateTax() {


return (price * taxRate) / 100;
}

public double getTotalPrice() {


return price + calculateTax();
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}

public double getTaxRate() {


return taxRate;
}

public double getTaxAmount() {


return calculateTax();
}

Department Of Computer Applications, BMSCE 3


22MCA2PCJP: Java Programming

package producttax;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TaxCalculator {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
List<Product> products = new ArrayList<>();

System.out.print("Enter the number of products: ");


int numOfProducts = scanner.nextInt();
scanner.nextLine(); // Consume newline

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


System.out.println("Enter details for product " + (i + 1) + ":");
System.out.print("Name: ");
String name = scanner.nextLine();

System.out.print("Price: ");
double price = scanner.nextDouble();

System.out.print("Tax Rate (in percentage): ");


double taxRate = scanner.nextDouble();
scanner.nextLine(); // Consume newline

products.add(new Product(name, price, taxRate));


}

double totalOriginalPrice = 0;
double totalTaxAmount = 0;
double totalPriceAfterTax = 0;

System.out.println("\nReceipt:");
System.out.println("-------------------------------------------------");
for (Product product : products) {
double taxAmount = product.calculateTax();
double totalPrice = product.getTotalPrice();

System.out.println("Product: " + product.getName());


System.out.println("Original Price: $" + product.getPrice());
System.out.println("Tax Rate: " + product.getTaxRate() + "%");
System.out.println("Tax Amount: $" + taxAmount);

Department Of Computer Applications, BMSCE 4


22MCA2PCJP: Java Programming

System.out.println("Total Price after Tax: $" + totalPrice);


System.out.println("-------------------------------------------------");

totalOriginalPrice += product.getPrice();
totalTaxAmount += taxAmount;
totalPriceAfterTax += totalPrice;
}

System.out.println("Summary:");
System.out.println("Total Original Price: $" + totalOriginalPrice);
System.out.println("Total Tax Amount: $" + totalTaxAmount);
System.out.println("Total Price after Tax: $" + totalPriceAfterTax);

scanner.close();
}

Department Of Computer Applications, BMSCE 5


22MCA2PCJP: Java Programming

Department Of Computer Applications, BMSCE 6


22MCA2PCJP: Java Programming

3. Write a program to prepare insurance premium of a particular person based on


age, habits, pre medical check-up details.

package insurancepremium;

public class Person {

private String name;


private int age;
private boolean smokes;
private boolean drinks;
private boolean hasPreExistingCondition;
private char gender;

public Person(String name, int age, boolean smokes, boolean drinks, boolean
hasPreExistingCondition, char gender) {
this.name = name;
this.age = age;
this.smokes = smokes;
this.drinks = drinks;
this.hasPreExistingCondition = hasPreExistingCondition;
this.gender = gender;
}

public double calculatePremium() {


double basePremium = gender == 'M' ? 5000 : 4000;

if (age < 25) {


basePremium += 1000;
} else if (age <= 40) {
basePremium += 2000;
} else if (age <= 60) {
basePremium += 3000;
} else {
basePremium += 4000;
}

if (smokes) {
basePremium += 2000;
}
if (drinks) {
basePremium += 1500;
}

Department Of Computer Applications, BMSCE 7


22MCA2PCJP: Java Programming

if (hasPreExistingCondition) {
basePremium += 2500;
}

return basePremium;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}

public boolean isSmokes() {


return smokes;
}

public boolean isDrinks() {


return drinks;
}

public boolean hasPreExistingCondition() {


return hasPreExistingCondition;
}

public char getGender() {


return gender;
}

package insurancepremium;

import java.util.Scanner;

public class InsuranceCalculator {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

Department Of Computer Applications, BMSCE 8


22MCA2PCJP: Java Programming

System.out.print("Enter the person's name: ");


String name = scanner.nextLine();

System.out.print("Enter the person's age: ");


int age = scanner.nextInt();

System.out.print("Enter the person's gender (M/F): ");


char gender = scanner.next().charAt(0);

System.out.print("Does the person smoke? (true/false): ");


boolean smokes = scanner.nextBoolean();

System.out.print("Does the person drink alcohol? (true/false): ");


boolean drinks = scanner.nextBoolean();

System.out.print("Does the person have any pre-existing medical conditions?


(true/false): ");
boolean hasPreExistingCondition = scanner.nextBoolean();

Person person = new Person(name, age, smokes, drinks, hasPreExistingCondition,


gender);
double premium = person.calculatePremium();

System.out.println("\nInsurance Premium Calculation:");


System.out.println("-------------------------------------------------");
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("Gender: " + (person.getGender() == 'M' ? "Male" : "Female"));
System.out.println("Smokes: " + (person.isSmokes() ? "Yes" : "No"));
System.out.println("Drinks: " + (person.isDrinks() ? "Yes" : "No"));
System.out.println("Pre-existing Medical Condition: " +
(person.hasPreExistingCondition() ? "Yes" : "No"));
System.out.println("-------------------------------------------------");
System.out.println("Calculated Premium: $" + premium);

scanner.close();
}

Department Of Computer Applications, BMSCE 9


22MCA2PCJP: Java Programming

Department Of Computer Applications, BMSCE 10


22MCA2PCJP: Java Programming

4. Create a java program to suggest a person's BMI information with necessary


suggestions.

BMICalculator:
package bmical;

public class BMICalculator {

protected double height;


protected double weight;
protected double bmi;

public BMICalculator(double height, double weight) {


this.height = height;
this.weight = weight;
this.bmi = calculateBMI();
}

public double calculateBMI() {


return weight / (height * height);
}

public String getBMISuggestion() {


return "This method should be overridden in derived classes.";
}

MaleBMICalculator:
package bmical;

public class MaleBMICalculator extends BMICalculator{

public MaleBMICalculator(double height, double weight) {


super(height, weight);
}

@Override
public String getBMISuggestion() {
if (bmi < 18.5) {
return "You are underweight. It's important to eat a balanced diet and consult with
a healthcare provider.";
} else if (bmi >= 18.5 && bmi < 24.9) {

Department Of Computer Applications, BMSCE 11


22MCA2PCJP: Java Programming

return "You have a normal weight. Keep up the good work maintaining a healthy
lifestyle!";
} else if (bmi >= 25 && bmi < 29.9) {
return "You are overweight. Consider a balanced diet and regular exercise to
improve your health.";
} else {
return "You are obese. It's important to seek advice from a healthcare provider for
a suitable plan to improve your health.";
}
}

FemaleBMICalculator:
package bmical;

public class FemaleBMICalculator extends BMICalculator {

public FemaleBMICalculator(double height, double weight) {


super(height, weight);
}

@Override
public String getBMISuggestion() {
if (bmi < 18.5) {
return "You are underweight. It's important to eat a balanced diet and consult with
a healthcare provider.";
} else if (bmi >= 18.5 && bmi < 24.9) {
return "You have a normal weight. Keep up the good work maintaining a healthy
lifestyle!";
} else if (bmi >= 25 && bmi < 29.9) {
return "You are overweight. Consider a balanced diet and regular exercise to
improve your health.";
} else {
return "You are obese. It's important to seek advice from a healthcare provider for
a suitable plan to improve your health.";
}
}

ChildBMICalculator:

Department Of Computer Applications, BMSCE 12


22MCA2PCJP: Java Programming

package bmical;

public class ChildBMICalculator extends BMICalculator{

public ChildBMICalculator(double height, double weight) {


super(height, weight);
}

public String getBMISuggestion() {


if (bmi < 14) {
return "You are underweight for your age. Please consult with a pediatrician for
advice.";
} else if (bmi >= 14 && bmi < 18) {
return "You have a normal weight for your age. Keep maintaining a healthy diet
and active lifestyle!";
} else {
return "You are overweight for your age. It's important to consult with a
pediatrician for a suitable plan to improve your health.";
}
}

BMICalculatorMain:

package bmical;

import java.util.Scanner;

public class BMICalculatorMain {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Input gender, age, height, and weight from user


System.out.print("Enter your gender (M/F): ");
char gender = scanner.next().charAt(0);

System.out.print("Enter your age: ");


int age = scanner.nextInt();

System.out.print("Enter your height in meters: ");


double height = scanner.nextDouble();

Department Of Computer Applications, BMSCE 13


22MCA2PCJP: Java Programming

System.out.print("Enter your weight in kilograms: ");


double weight = scanner.nextDouble();

BMICalculator bmiCalculator;

// Create appropriate BMICalculator instance based on age and gender


if (age < 18) {
bmiCalculator = new ChildBMICalculator(height, weight);
} else if (gender == 'M' || gender == 'm') {
bmiCalculator = new MaleBMICalculator(height, weight);
} else {
bmiCalculator = new FemaleBMICalculator(height, weight);
}

// Calculate and display BMI


double bmi = bmiCalculator.calculateBMI();
System.out.printf("Your BMI is: %.2f\n", bmi);

// Provide suggestions based on BMI, gender, and age


String suggestion = bmiCalculator.getBMISuggestion();
System.out.println(suggestion);

scanner.close();
}

Department Of Computer Applications, BMSCE 14


22MCA2PCJP: Java Programming

Department Of Computer Applications, BMSCE 15


22MCA2PCJP: Java Programming

5. Write a java program to enable unit conversions:


a. Kilometer to Meter
b. Weights to Pounds
c. Kilometer to Miles
d. Celcius to Fahrenheit

UnitConverter
package unitconvertor;

public abstract class UnitConverter {

public abstract double convert(double value);

LengthConverter:
package unitconvertor;

import java.util.Scanner;

public class LengthConverter extends UnitConverter{

private static final double METER_TO_FEET = 3.28084;

@Override
public double convert(double value) {
Scanner scanner = new Scanner(System.in);
System.out.print("Convert (1) Meters to Feet or (2) Feet to Meters: ");
int choice = scanner.nextInt();
scanner.close();
if (choice == 1) {
return value * METER_TO_FEET;
} else {
return value / METER_TO_FEET;
}

Department Of Computer Applications, BMSCE 16


22MCA2PCJP: Java Programming

DistanceConverter:
package unitconvertor;

import java.util.Scanner;

public class DistanceConverter extends UnitConverter {

private static final double KM_TO_MILES = 0.621371;

@Override
public double convert(double value) {
Scanner scanner = new Scanner(System.in);
System.out.print("Convert (1) Kilometers to Miles or (2) Miles to Kilometers: ");
int choice = scanner.nextInt();
scanner.close();
if (choice == 1) {
return value * KM_TO_MILES;
} else {
return value / KM_TO_MILES;
}
}

TemperatureConverter:
package unitconvertor;

import java.util.Scanner;

public class TemperatureConverter extends UnitConverter {

@Override
public double convert(double value) {
Scanner scanner = new Scanner(System.in);
System.out.print("Convert (1) Celsius to Fahrenheit or (2) Fahrenheit to Celsius: ");
int choice = scanner.nextInt();
scanner.close();
if (choice == 1) {
return (value * 9 / 5) + 32;
} else {
return (value - 32) * 5 / 9;
}
}}

Department Of Computer Applications, BMSCE 17


22MCA2PCJP: Java Programming

WeightConverter:
package unitconvertor;

import java.util.Scanner;

public class WeightConverter extends UnitConverter {

private static final double KILOGRAM_TO_POUND = 2.20462;

@Override
public double convert(double value) {
Scanner scanner = new Scanner(System.in);
System.out.print("Convert (1) Kilograms to Pounds or (2) Pounds to Kilograms: ");
int choice = scanner.nextInt();
scanner.close();
if (choice == 1) {
return value * KILOGRAM_TO_POUND;
} else {
return value / KILOGRAM_TO_POUND;
}
}

UnitConversionMain:
package unitconvertor;

import java.util.Scanner;

public class UnitConversionMain {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Choose the type of conversion:");


System.out.println("1. Length (meters to feet and vice versa)");
System.out.println("2. Weight (kilograms to pounds and vice versa)");
System.out.println("3. Temperature (Celsius to Fahrenheit and vice versa)");
System.out.println("4. Distance (kilometers to miles and vice versa)");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

UnitConverter converter;
switch(choice) {
case 1:

Department Of Computer Applications, BMSCE 18


22MCA2PCJP: Java Programming

converter = new LengthConverter();


break;

case 2:
converter = new WeightConverter();
break;

case 3:
converter = new TemperatureConverter();
break;

case 4:
converter = new DistanceConverter();
break;

default:
System.out.println("Invalid Choice");
return;

System.out.print("Enter the value to be converted: ");


double value = scanner.nextDouble();
double convertedValue = converter.convert(value);

System.out.printf("Converted value: %.2f %s\n", convertedValue);

scanner.close();
}

Department Of Computer Applications, BMSCE 19

You might also like