Left fold java

public static A fold(F f, A z, Iterable xs) { A p = z; for (B x : xs) { p = f.f(p).f(x); } return p; }

java age from date

import java.time.LocalDate; import java.time.Period; class TestJava { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); LocalDate birthDay = LocalDate.of(1989, 2, 22); Period period = Period.between(birthDay, currentDate); System.out.println(“Age: “); System.out.println(“Years: ” + period.getYears()); System.out.println(“Months: ” + period.getMonths()); System.out.println(“Days: ” + period.getDays()); } } Output: Age: Years: 32 Months: 11 Days: 2

Java every second

import java.util.Timer; import java.util.TimerTask; class TestJava { public static void main(String[] args) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println(“Print in every second”); } }, 0, 1000);//wait 0 milliseconds before doing the action and do it every 1000ms (1 second) } } Output: Print in every second Print … Read more

Random colors java

import java.awt.Color; import java.util.Random; class TestJava { public static void main(String[] args) { Random rand = new Random(); // Java ‘Color’ class takes 3 floats, from 0 to 1. float red = rand.nextFloat(); float green = rand.nextFloat(); float blue = rand.nextFloat(); Color randomColor = new Color(red, green, blue); System.out.println(randomColor); } } Output: java.awt.Color[r=232,g=230,b=101]

Java create window

import javax.swing.JFrame; public class CreateWindow{ public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(150,150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle(“Test Frame”); frame.setVisible(true); } }