Skip to content

Instantly share code, notes, and snippets.

View SiAust's full-sized avatar
🏠
Open to new opportunities

Simon Aust SiAust

🏠
Open to new opportunities
View GitHub Profile
@SiAust
SiAust / interactive_loop.py
Last active July 22, 2018 10:20
An interactive loop in python. Takes user input to exit the loop.
"""A short program using user input in an interactive loop"""
q = False
while not q:
user_quit = input('Quit?(y/n): ')
if user_quit == 'y':
q = True
@SiAust
SiAust / IsLeapYear.java
Created March 23, 2019 15:31
Takes an input year and returns whether the year is a leap year.
public class NumberOfDaysInMonth {
public static boolean isLeapYear(int year){
boolean isLeapYear = false;
if (year < 1 || year > 9999){
return false;
}
if (year % 4 == 0){
@SiAust
SiAust / PointCalculator.java
Created March 25, 2019 14:54
Calculates the distance to a point, with no parameters ( distance to origin 0,0), two parameters x and y or a variable of the type Point.
public class Point {
private int x;
private int y;
public Point(){
//empty constructor (no argument constructor)
}
public Point(int x, int y){
this.x = x;
@SiAust
SiAust / HttpUtils.java
Last active March 1, 2021 04:32
Http request java.io.IOException
public class HttpUtils {
private static HttpServer server;
private static final String CLIENT_ID = "6edb9b1ac21042abacc6daaf0fbc4c4d";
private static final String CLIENT_SECRET = ""; // todo: remove before committing to Github.
private static final String REDIRECT_ID = "http://localhost:8080";
public static void startHttpServer() {
try {
server = HttpServer.create();
server.bind(new InetSocketAddress(8080), 0);
@SiAust
SiAust / FastCar.java
Last active May 7, 2020 14:09
Exampale of using Comparable<> interface to sort objects.
class CarsTest {
public static void main(String[] args) {
Ferrari ferrari = new Ferrari(200);
Jaguar jaguar = new Jaguar(190);
Lotus lotus = new Lotus(170);
Lamborghini lamborghini = new Lamborghini(210);
ArrayList<FastCar> fastCars = new ArrayList<>();
fastCars.add(ferrari);
@SiAust
SiAust / SportsTeams.java
Created May 8, 2020 09:27
Using generics to restrict object initialisation and commparable interface to sort.
import java.util.ArrayList;
import java.util.Collections;
public class Udemy {
public static void main(String[] args) {
League<FootballTeam> premierLeague = new League<>("Premier League");
FootballTeam tottenham = new FootballTeam("Tottenham Hotspur",38,0, 0);
FootballTeam arsenal = new FootballTeam("Arsenal", 0,0, 38);
@SiAust
SiAust / AwtExample.java
Created May 8, 2020 10:37
Creating a simple window in Java.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AwtExample {
/*Abstract Window Toolkit Example */
public static void main(String[] args) {
Awt window = new Awt("Test");
window.setVisible(true);
}
@SiAust
SiAust / Decorator.java
Last active May 17, 2020 09:13
A example decorator design pattern
public class Test {
public static void main(String[] args) {
JavaTeamLead javaTeamLead = new JavaTeamLead
(new SeniorJavaDeveloper(
new JavaDeveloper()));
System.out.println(javaTeamLead.makeJob());
/* Write Java code. Make code review. Send emails to clients. */
}
@SiAust
SiAust / Strategy.java
Last active May 25, 2020 13:28
Strategy design pattern.
public class Main {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final String[] elements = scanner.nextLine().split("\\s+");
int[] numbers = null;
if (elements[0].equals("EMPTY")) {
@SiAust
SiAust / StaticFactory.java
Created May 26, 2020 09:54
Example of a static factory.
class MotorStaticFactory {
/**
* It returns an initialized motor according to the specified type by the first character:
* 'P' or 'p' - pneumatic, 'H' or 'h' - hydraulic, 'E' or 'e' - electric, 'W' or 'w' - warp.
*/
public static Motor make(char type, String model, long power) {
type = Character.toLowerCase(type);
switch (type) {
case 'p':