slips
November 30, 2023

SLIP22

Q.1 Write a Java Program to implement undo command to test Ceiling fan.

import java.util.Scanner;

// Ceiling Fan class
class CeilingFan {
    private boolean isOn;

    public CeilingFan() {
        isOn = false;
    }

    public void turnOn() {
        isOn = true;
        System.out.println("Ceiling fan is turned on.");
    }

    public void turnOff() {
        isOn = false;
        System.out.println("Ceiling fan is turned off.");
    }

    public boolean isOn() {
        return isOn;
    }
}

// Remote Control class
class RemoteControl {
    private CeilingFan ceilingFan;
    private boolean previousState;

    public RemoteControl(CeilingFan fan) {
        ceilingFan = fan;
        previousState = false;
    }

    public void pressOn() {
        previousState = ceilingFan.isOn();
        ceilingFan.turnOn();
    }

    public void pressOff() {
        previousState = ceilingFan.isOn();
        ceilingFan.turnOff();
    }

    public void pressUndo() {
        if (previousState) {
            ceilingFan.turnOn();
        } else {
            ceilingFan.turnOff();
        }
    }
}

public class CeilingFanTests {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        CeilingFan fan = new CeilingFan();
        RemoteControl remote = new RemoteControl(fan);

        System.out.println("Ceiling Fan Test");
        while (true) {
            System.out.println("1. Turn On\n2. Turn Off\n3. Undo\n4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    remote.pressOn();
                    break;
                case 2:
                    remote.pressOff();
                    break;
                case 3:
                    remote.pressUndo();
                    break;
                case 4:
                    System.out.println("Exiting the program.");
                    System.exit(0);
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }
        }
    }
}

Q.2 Write a python program to implement logistic Regression for predicting whether a person will buy the insurance or not. Use insurance_data.csv.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
data= pd.read_csv("./csv/insurance_data.csv")
X = data[['age']]
y = data['bought_insurance']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")
conf_matrix = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=['Not Bought', 'Bought'], yticklabels=['Not Bought', 'Bought'])
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()

Q.3 Print below array elements using map: a. let fruits1 = ["apple", "banana"]; b. let fruits2 = ["cherry", "orange"]; c. Merge both fruits array and print it

Paste the FruitsList.jsx In src Folder

import React from 'react';

const FruitsList = () => {
  let fruits1 = ["apple", "banana"];
  let fruits2 = ["cherry", "orange"];

  // Merge both arrays
  let allFruits = [...fruits1, ...fruits2];

  return (
    <div>
      <h1>Merged Fruits List</h1>
      <ul>
        {allFruits.map((fruit, index) => (
          <li key={index}>{fruit}</li>
        ))}
      </ul>
    </div>
  );
};

export default FruitsList;