Home
Time Box
Calculator
Snake
Blogs
Hacks

First java notebook • 6 min read

Description

starter code for javascript


Hello world

// Define Static Method within a Class
public class HelloStatic {
    // Java standard runtime entry point
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
// A method call allows us to execute code that is wrapped in Class
HelloStatic.main(null);   // Class prefix allows reference of Static Method
Hello World!

1st class

// Addition class
public class Addition {
    // var for result
    private int result;
    
    // static version
    public Addition() {
        this.setAdd(3, 5); 
    }

    // argument constructor version
    public Addition(int A, int B) { 
        this.setAdd(A, B);
    }

    // setter
    public void setAdd(int A, int B) {
        this.result = A + B;
    }

    public int getAdd() {
        return this.result;
    }

    public static void main(String[] args) {
        Addition hd1 = new Addition(); // no-argument constructor
        Addition hd2 = new Addition(10, 5); // constructor with arguments
        System.out.println(hd1.getAdd());
        System.out.println(hd2.getAdd());
    }
}


Addition.main(null);
8
15

Mean, Median, Mode

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ListStats {
    public static void main(String[] args) {
        // Create an ArrayList and populate it
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(5);
        numbers.add(7);
        numbers.add(9);
        numbers.add(5);

        // Calculate sum for mean and median
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }

        // Calculate mean
        double mean = (double) sum / numbers.size();

        // Calculate median
        Collections.sort(numbers);
        double median; // Double is a way to store floating point values
        if (numbers.size() % 2 == 0) {
            int middle1 = numbers.get(numbers.size() / 2 - 1);
            int middle2 = numbers.get(numbers.size() / 2);
            median = (double) (middle1 + middle2) / 2;
        } else {
            median = numbers.get(numbers.size() / 2);
        }

        // Calculate mode
        Map<Integer, Integer> frequencyMap = new HashMap<>();
        for (int num : numbers) { // same thing as a for each but java uses a :
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
        }
        
        int mode = -1;
        int maxFrequency = 0;
        for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            if (entry.getValue() > maxFrequency) {
                mode = entry.getKey();
                maxFrequency = entry.getValue();
            }
        }

        // Print results
        System.out.println("Mean: " + mean);
        System.out.println("Median: " + median);
        System.out.println("Mode: " + mode);
        System.out.println("Sum: " + sum);
    }
}

ListStats.main(null);
Mean: 6.2
Median: 5.0
Mode: 5
Sum: 31