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

Lab 10 Tasks

Uploaded by

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

Lab 10 Tasks

Uploaded by

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

Object Oriented

Programming
LAB TASK

Omer Shahzad (SP22-BAI-038) 12/12/24 OOP


Graded Tasks:
Task 1:
import java.util.ArrayList;
import java.util.Scanner;

class Contact {
private String firstName;
private String lastName;
private String phoneNumber;
private String email;

public Contact(String firstName, String lastName, String phoneNumber, String


email) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.email = email;
}

public String getFirstName() { return firstName; }


public String getLastName() { return lastName; }
public String getPhoneNumber() { return phoneNumber; }
public String getEmail() { return email; }

public void setFirstName(String firstName) { this.firstName = firstName; }


public void setLastName(String lastName) { this.lastName = lastName; }
public void setPhoneNumber(String phoneNumber) { this.phoneNumber =
phoneNumber; }
public void setEmail(String email) { this.email = email; }

@Override
public String toString() {
return "Contact{" +
"First Name='" + firstName + '\'' +
", Last Name='" + lastName + '\'' +
", Phone Number='" + phoneNumber + '\'' +
", Email='" + email + '\'' +
'}';
}
}

public class ContactDatabase {


private static final Scanner scanner = new Scanner(System.in);
private static final ArrayList<Contact> contacts = new ArrayList<>();

1
public static void main(String[] args) {
while (true) {
System.out.println("\nContact Database Menu:");
System.out.println("1. Add Contact");
System.out.println("2. Display All Contacts");
System.out.println("3. Search and Display Contact");
System.out.println("4. Search and Delete Contact");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline

if (choice == 1) addContact();
else if (choice == 2) displayAllContacts();
else if (choice == 3) searchAndDisplayContact();
else if (choice == 4) searchAndDeleteContact();
else if (choice == 5) {
System.out.println("Exiting program. Goodbye!");
break;
} else {
System.out.println("Invalid choice. Please try again.");
}
}
}

private static void addContact() {


System.out.print("Enter first name: ");
String firstName = scanner.nextLine();
System.out.print("Enter last name: ");
String lastName = scanner.nextLine();
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Enter email address: ");
String email = scanner.nextLine();

contacts.add(new Contact(firstName, lastName, phoneNumber, email));


System.out.println("Contact added successfully!");
}

private static void displayAllContacts() {


if (contacts.isEmpty()) {
System.out.println("No contacts to display.");
} else {

2
System.out.println("\nAll Contacts:");
for (Contact contact : contacts) {
System.out.println(contact);
}
}
}

private static void searchAndDisplayContact() {


System.out.print("Enter search string: ");
String search = scanner.nextLine().toLowerCase();
boolean found = false;

for (Contact contact : contacts) {


if (matches(contact, search)) {
System.out.println(contact);
found = true;
}
}

if (!found) {
System.out.println("No matching contact found.");
}
}

private static void searchAndDeleteContact() {


System.out.print("Enter search string: ");
String search = scanner.nextLine().toLowerCase();
boolean found = false;

for (Contact contact : contacts) {


if (matches(contact, search)) {
System.out.println("Found: " + contact);
System.out.print("Do you want to delete this contact? (yes/no):
");
if (scanner.nextLine().equalsIgnoreCase("yes")) {
contacts.remove(contact);
System.out.println("Contact deleted.");
} else {
System.out.println("Contact not deleted.");
}
found = true;
break;
}
}

3
if (!found) {
System.out.println("No matching contact found.");
}
}

private static boolean matches(Contact contact, String search) {


return contact.getFirstName().toLowerCase().contains(search) ||
contact.getLastName().toLowerCase().contains(search) ||
contact.getPhoneNumber().toLowerCase().contains(search) ||
contact.getEmail().toLowerCase().contains(search);
}
}

OUTPUT

Task 2:
import java.util.*;

class MyMathClass<T extends Number> {

4
public double standardDeviation(ArrayList<T> numbers) {
if (numbers.isEmpty()) return 0.0;

double sum = 0.0;


for (T num : numbers) {
sum += num.doubleValue();
}

double mean = sum / numbers.size();


double variance = 0.0;

for (T num : numbers) {


variance += Math.pow(num.doubleValue() - mean, 2);
}

variance /= numbers.size();
return Math.sqrt(variance);
}
}

public class TestMyMathClass {


public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<>();
intList.add(10);
intList.add(20);
intList.add(30);
intList.add(40);

ArrayList<Double> doubleList = new ArrayList<>();


doubleList.add(1.5);
doubleList.add(2.5);
doubleList.add(3.5);
doubleList.add(4.5);

MyMathClass<Integer> intMath = new MyMathClass<>();


MyMathClass<Double> doubleMath = new MyMathClass<>();

System.out.println("Standard Deviation (Integer): " +


intMath.standardDeviation(intList));
System.out.println("Standard Deviation (Double): " +
doubleMath.standardDeviation(doubleList));
}
}

5
OUTPUT:

Task 3:

import java.util.ArrayList;
import java.util.Random;

class RandomBox<T> {
private final ArrayList<T> items = new ArrayList<>();
private final Random random = new Random();

public void add(T item) {


items.add(item);
}

public boolean isEmpty() {


return items.isEmpty();
}

public T drawItem() {
if (isEmpty()) {
return null;
}
int index = random.nextInt(items.size());
return items.get(index);
}
}

public class TestRandomBox {


public static void main(String[] args) {
RandomBox<String> nameBox = new RandomBox<>();
nameBox.add("Alice");
nameBox.add("Bob");
nameBox.add("Charlie");
nameBox.add("Diana");

6
RandomBox<Integer> lotteryBox = new RandomBox<>();
lotteryBox.add(5);
lotteryBox.add(12);
lotteryBox.add(23);
lotteryBox.add(42);

System.out.println("Drawing from name box:");


for (int i = 0; i < 5; i++) {
System.out.println(nameBox.drawItem());
}

System.out.println("\nDrawing from lottery box:");


for (int i = 0; i < 5; i++) {
System.out.println(lotteryBox.drawItem());
}

System.out.println("\nTesting empty box:");


RandomBox<String> emptyBox = new RandomBox<>();
System.out.println("Draw result: " + emptyBox.drawItem());
}
}

OUTPUT:

7
Task 4:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class DivingScoreCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the degree of difficulty (1.2 to 3.8): ");


double degreeOfDifficulty = scanner.nextDouble();
if (degreeOfDifficulty < 1.2 || degreeOfDifficulty > 3.8) {
System.out.println("Invalid degree of difficulty. Must be between 1.2
and 3.8.");
return;
}

ArrayList<Double> scores = new ArrayList<>();


System.out.println("Enter the scores from 7 judges (0 to 10):");
for (int i = 0; i < 7; i++) {
System.out.print("Judge " + (i + 1) + ": ");
double score = scanner.nextDouble();
if (score < 0 || score > 10) {
System.out.println("Invalid score. Must be between 0 and 10.");
return;
}
scores.add(score);
}

double highest = Collections.max(scores);


double lowest = Collections.min(scores);
scores.remove(highest);
scores.remove(lowest);

double sum = 0;
for (double score : scores) {
sum += score;
}

double finalScore = sum * degreeOfDifficulty * 0.6;


System.out.printf("The diver's final score is: %.2f%n", finalScore);

8
}
}

OUTPUT:

You might also like