TAAFT
Free mode
100% free
Freemium
Free Trial
Deals
Create tool
  • Code Humanizer
    Transform AI code into human-like programming
    Open
    2,698
    849
    3.4
    1,137
    Released 3mo ago
    100% Free
    package eecs2030.lab3; import java.util.Objects; public final class Bird implements Comparable<Bird> { private final String species; private final double weightInGrams; public Bird(String species, double weightInGrams) { this.species = species; this.weightInGrams = weightInGrams; } public Bird() { this("Unknown", 300.0); } public String getSpecies() { return species; } public double getWeightInGrams() { return weightInGrams; } private double weightRoundedTo20Grams() { return Math.round(weightInGrams / 20.0) * 20.0; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Bird)) return false; Bird other = (Bird) obj; return Objects.equals(species, other.species) && Double.compare(weightRoundedTo20Grams(), other.weightRoundedTo20Grams()) == 0; } @Override public int hashCode() { long rBits = Double.doubleToLongBits(weightRoundedTo20Grams()); int weightHash = (int) (rBits ^ (rBits >>> 32)); return Objects.hash(species, weightHash); } @Override public String toString() { return String.format("Bird[species=%s, weightInGrams=%.3f]", species, weightInGrams); } @Override public int compareTo(Bird other) { double thisRounded = weightRoundedTo20Grams(); double otherRounded = other.weightRoundedTo20Grams(); int cmp = Double.compare(thisRounded, otherRounded); return cmp != 0 ? cmp : species.compareTo(other.species); } }

Other tools

Ask the community(1)
# Part (a): Add a Student def add_student(student_list, student_id, name, age, course): # Check for unique student ID for student in student_list: if student['student_id'] == student_id: print(f"Error: Student ID {student_id} already exists!") return # Add the new student student_list.append({ 'student_id': student_id, 'name': name, 'age': age, 'course': course }) print(f"Student {name} added successfully.") # Part (b1): Find a Student by ID def find_student_by_id(student_list, student_id): for student in student_list: if student['student_id'] == student_id: return student print("Student not found!") return None # Part (b2): Remove a Student by ID def remove_student_by_id(student_list, student_id): for student in student_list: if student['student_id'] == student_id: student_list.remove(student) print(f"Student ID {student_id} removed successfully.") return print("Student not found!") # Part (c): Class Definitions class Person: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f"Name: {self.name}, Age: {self.age}" class Student(Person): def __init__(self, name, age, course): super().__init__(name, age) self.course = course def study(self): print(f"Student is studying {self.course}") class Instructor(Person): def __init__(self, name, age, subject): super().__init__(name, age) self.subject = subject def teach(self): print(f"Instructor is teaching {self.subject}") # Demonstration of polymorphism student1 = Student("Alice", 20, "Mathematics") instructor1 = Instructor("Bob", 40, "Physics") print(student1) # Uses __str__ from Person student1.study() # Calls study method from Student print(instructor1) # Uses __str__ from Person instructor1.teach() # Calls teach method from Instructor # Part (d): Higher-order function for sorting students def sort_students(student_list, key_function): return sorted(student_list, key=key_function) # Sample student list students = [ {"student_id": 1, "name": "Alice", "age": 20, "course": "Mathematics"}, {"student_id": 2, "name": "Bob", "age": 22, "course": "Physics"}, {"student_id": 3, "name": "Charlie", "age": 19, "course": "Chemistry"} ] # Demonstrate sorting by age sorted_by_age = sort_students(students, key_function=lambda s: s["age"]) print("Students sorted by age:", sorted_by_age) # Demonstrate sorting by name sorted_by_name = sort_students(students, key_function=lambda s: s["name"]) print("Students sorted by name:", sorted_by_name)
Post
0 AIs selected
Clear selection
#
Name
Task