• 5 Posts
  • 8 Comments
Joined 8 months ago
cake
Cake day: October 9th, 2023

help-circle











  • PamasichOPtoPamasich@kbin.socialKES fix-codeblocks tests
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    5 months ago

    Java

    (example provided by Edge Copilot)

    // A generic class to store a pair of values of any type that implements Comparable
    public class Pair<T extends Comparable<T>> {
        // Declare two private fields of type T
        private T first;
        private T second;
    
        // Define a constructor that takes two arguments of type T
        public Pair(T first, T second) {
            // Assign the arguments to the fields
            this.first = first;
            this.second = second;
        }
    
        // Define a getter method for the first field
        public T getFirst() {
            return first;
        }
    
        // Define a getter method for the second field
        public T getSecond() {
            return second;
        }
    
        // Define a method to compare the two values and return the larger one
        public T max() {
            // Use the compareTo method of the Comparable interface
            if (first.compareTo(second) > 0) {
                return first;
            } else {
                return second;
            }
        }
    
        // Define a method to compare the two values and return the smaller one
        public T min() {
            // Use the compareTo method of the Comparable interface
            if (first.compareTo(second) < 0) {
                return first;
            } else {
                return second;
            }
        }
    
        // Define a method to return a string representation of the pair
        public String toString() {
            return "(" + first + ", " + second + ")";
        }
    }
    
    public class Main {
        // Test the Pair class with different types
        public static void main(String[] args) {
            // Create a pair of integers
            Pair<Integer> p1 = new Pair<>(10, 20);
            System.out.println("p1 = " + p1);
            System.out.println("The larger value of p1 is " + p1.max());
            System.out.println("The smaller value of p1 is " + p1.min());
    
            // Create a pair of strings
            Pair<String> p2 = new Pair<>("Hello", "World");
            System.out.println("p2 = " + p2);
            System.out.println("The larger value of p2 is " + p2.max());
            System.out.println("The smaller value of p2 is " + p2.min());
        }
    }
    
    

    (example provided by Edge Copilot)


  • Rust

    (example provided by Edge Copilot)

    // A generic function to swap two values of any type that implements Copy
    fn swap<T: Copy>(x: &mut T, y: &mut T) {
        // Use a temporary variable to store the value of x
        let temp = *x;
        // Assign the value of y to x
        *x = *y;
        // Assign the value of temp to y
        *y = temp;
    }
    
    fn main() {
        // Test the swap function with different types
        let mut a = 10;
        let mut b = 20;
        println!("Before swap: a = {}, b = {}", a, b);
        swap(&mut a, &mut b);
        println!("After swap: a = {}, b = {}", a, b);
    
        let mut c = "Hello";
        let mut d = "World";
        println!("Before swap: c = {}, d = {}", c, d);
        swap(&mut c, &mut d);
        println!("After swap: c = {}, d = {}", c, d);
    }