Uploaded April 2025 | Updated September 2026, 1 week ago
#coding #programming #cprogramming
// calloc() = Contiguous Allocation.
// Allocates memory dynamically and sets all allocated bytes to 0.
// malloc() is faster, but calloc() leads to less bugs
// calloc(#, size)
#coding #programming #cprogramming
// calloc() = Contiguous Allocation.
// Allocates memory dynamically and sets all allocated bytes to 0.
// malloc() is faster, but calloc() leads to less bugs
// calloc(#, size)
![Lets code an alarm clock with Java! ⏰
#java #javatutorial #javacourse
This is a beginners project to help us learn and understand Object Oriented Programming and Threading. We will only be using topics we have discussed in past videos in this series.
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// JAVA ALARM CLOCK
Scanner scanner = new Scanner(System.in);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(HH:mm:ss);
LocalTime alarmTime = null;
String filePath = A Caring Friend.wav;
while(alarmTime null){
try{
System.out.print(Enter an alarm time (HH:MM:SS): );
String inputTime = scanner.nextLine();
alarmTime = LocalTime.parse(inputTime, formatter);
System.out.println(Alarm set for + alarmTime);
}
catch(DateTimeParseException e){
System.out.println(Invalid format. Please use HH:MM:SS);
}
}
AlarmClock alarmClock = new AlarmClock(alarmTime, filePath, scanner);
Thread alarmThread = new Thread(alarmClock);
alarmThread.start();
}
}
import javax.sound.sampled.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.time.LocalTime;
import java.util.Scanner;
public class AlarmClock implements Runnable{
private final LocalTime alarmTime;
private final String filePath;
private final Scanner scanner;
AlarmClock(LocalTime alarmTime, String filePath, Scanner scanner){
this.alarmTime = alarmTime;
this.filePath = filePath;
this.scanner = scanner;
}
@Override
public void run(){
while(LocalTime.now().isBefore(alarmTime)){
try {
Thread.sleep(1000);
LocalTime now = LocalTime.now();
System.out.printf(r%02d:%02d:%02d,
now.getHour(),
now.getMinute(),
now.getSecond());
}
catch (InterruptedException e) {
System.out.println(Thread was interrupted);
}
}
System.out.println(n*ALARM NOISES*);
playSound(filePath);
}
private void playSound(String filePath){
File audioFile = new File(filePath);
try(AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile)){
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
clip.start();
System.out.print(Press *Enter* to stop the alarm: );
scanner.nextLine();
clip.stop();
scanner.close();
}
catch(UnsupportedAudioFileException e){
System.out.println(Audio file format is not supported);
}
catch(LineUnavailableException e){
System.out.println(Audio is unavailable);
}
catch(IOException e){
System.out.println(Error reading audio file);
}
}
} Lets code an alarm clock with Java! ⏰](https://i.ytimg.com/vi/lGG0LAJf0uE/mqdefault.jpg)




![Java printf() is really useful! 🖨️
#java #javatutorial #javacourse
public class Main {
public static void main(String[] args) {
// printf() is a method used to format output
// % [flags] [width] [.precision] [specifier-character]
// [specifier-character]
String name = Spongebob;
char firstLetter = S;
int age = 30;
double height = 60.5;
boolean isEmployed = true;
System.out.printf(Hello %sn, name);
System.out.printf(Your name starts with a %cn, firstLetter);
System.out.printf(You are %d years oldn, age);
System.out.printf(You are %f inches talln, height);
System.out.printf(Employed: %bn, isEmployed);
System.out.printf(%s is %d years old, name, age);
// [.precision]
double price1 = 9.99;
double price2 = 100.15;
double price3 = -54.01;
System.out.printf(%.3fn, price1);
System.out.printf(%.3fn, price2);
System.out.printf(%.3fn, price3);
// [flags]
// + = output a plus
// , = comma grouping separator
// ( = negative numbers are enclosed in ()
// space = display a minus if negative, space if positive
System.out.printf(%fn, price1);
System.out.printf(%fn, price2);
System.out.printf(%fn, price3);
// [width]
// 0 = zero padding
// number = right justified padding
// negative number = left justified padding
int id1 = 1;
int id2 = 23;
int id3 = 456;
int id4 = 7890;
System.out.printf(id: %04dn, id1);
System.out.printf(id: %04dn, id2);
System.out.printf(id: %04dn, id3);
System.out.printf(id: %04dn, id4);
}
} Java printf() is really useful! 🖨️](https://i.ytimg.com/vi/nTBDjxWAcoE/mqdefault.jpg)

![Learn Java overloaded methods in 6 minutes! 🍕
#java #javatutorial #javacourse
public class Main {
public static void main(String[] args){
// overloaded methods = methods that share the same name,
// but different parameters
// signature = name + parameters
String pizza = bakePizza(flat-bread, mozzarella, pepperoni);
System.out.println(pizza);
}
static String bakePizza(String bread){
return bread + pizza;
}
static String bakePizza(String bread, String cheese){
return cheese + + bread + pizza;
}
static String bakePizza(String bread, String cheese, String topping){
return topping + + cheese + + bread + pizza;
}
} Learn Java overloaded methods in 6 minutes! 🍕](https://i.ytimg.com/vi/nhnAx79gxCM/mqdefault.jpg)
![The Java Math class + exercises! 📐
#java #javatutorial #javacourse
00:00:00 intro
00:00:10 constants
00:00:57 methods
00:04:24 exercise1
00:08:27 exercise2
00:13:25 printf
public class Main {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.E);
double result;
result = Math.pow(3, 4);
result = Math.abs(-5);
result = Math.sqrt(16);
result = Math.round(3.14);
result = Math.ceil(3.14);
result = Math.floor(3.14);
result = Math.max(10, 20);
result = Math.min(10, 20);
System.out.println(result);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a;
double b;
double c;
System.out.print(Enter the length of side A: );
a = scanner.nextDouble();
System.out.print(Enter the length of side B: );
b = scanner.nextDouble();
c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
System.out.println(The hypotenuse is: + c + cm);
scanner.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// circumference = 2 * Math.PI * radius;
// area = Math.PI * Math.pow(radius, 2);
// volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3)
Scanner scanner = new Scanner(System.in);
double radius;
double circumference;
double area;
double volume;
System.out.print(Enter the radius: );
radius = scanner.nextDouble();
circumference = 2 * Math.PI * radius;
area = Math.PI * Math.pow(radius, 2);
volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
System.out.printf(The circumference is: %.1fcmn, circumference);
System.out.printf(The area is: %.1fcm²n, area);
System.out.printf(The volume is: %.1fcm³n, volume);
scanner.close();
}
} The Java Math class + exercises! 📐](https://i.ytimg.com/vi/nle8CQXYhl4/mqdefault.jpg)

