public class Student { //Class, where the code is run (a template for creating objects structure and behavior of objects)
public static void main(String[] args) { //Main method, where the code is executed and calls other functions
Student student1 = new Student("Alice", 20); //Create object using Student constructor
student1.introduce();
student1.changeName("Bobert");
student1.introduce();
}
public String name; //instance variables
public int age;
public Student(String name, int age) //Student constructor with 2 parameters
{
this.name = name;
this.age = age;
}
public void changeName(String newName) //Setter that changes the name of the student
{
this.name = newName;
}
public void introduce() { //Function/Method that prints the name and age of the person
System.out.println("Hello, my name is " + getName() + " and I am " + getAge() + " years old.");
}
public String getName() {//Getter for name
return name;
}
// Getter for age
public int getAge() {
return age;
}
}
public class Teacher {
public static void main(String[] args) {
Teacher teacher1 = new Teacher("Mr. Bortenson", "A999");
teacher1.introduceTeacher();
}
public String teachername;
public String classroom;
// Constructor for the Teacher class
public Teacher(String name, String classroom) //Teacher constructor with 2 parameters
{
this.teachername = name;
this.classroom = classroom;
}
public void introduceTeacher() {
System.out.println("Hello, my teacher's name is " + teachername + " and they are in classroom " + classroom + ".");
}
}
Student.main(null);
Teacher.main(null);
Hello, my name is Alice and I am 20 years old.
Hello, my name is Bobert and I am 20 years old.
Hello, my teacher's name is Mr. Bortenson and they are in classroom A999.
Explaining Static vs Dynamic
You cannot create objects of a static class, which differs from a dynamic one.
The following code demonstrates a series of static classes.
Date functions using static
import java.util.*;
import java.time.*;
public class DateUtils {
// Calculate age since a given date
public static void calculateAge(LocalDate birthDate, LocalDate currentDate) {
System.out.println(Period.between(birthDate, currentDate).getYears());
}
// Calculate the older of two dates
public static void findOlderDate(LocalDate date1, LocalDate date2) {
if (date1.isBefore(date2)) {
System.out.println(date1);
}
else
{
System.out.println(date2);
}
}
// Calculate the number of time since a given date
public static void calculateSecondsSince(LocalDate startDate) {
LocalDate now = LocalDate.now();
Period period = Period.between(startDate, now);
System.out.println(period);
}
public static void main(String[] args)
{
LocalDate birthDate = LocalDate.parse("2006-09-08");
LocalDate currentDate = LocalDate.now();
calculateAge(birthDate, currentDate);
LocalDate date1 = LocalDate.parse("2006-09-08");
LocalDate date2 = LocalDate.parse("2006-09-09");
findOlderDate(date1, date2);
LocalDate startDate = LocalDate.parse("2006-09-08");
calculateSecondsSince(startDate);
}
}
DateUtils.main(null);
16
2006-09-08
P16Y11M24D
Statistic functions using static
import java.util.Arrays;
public class StatsUtils {
// Calculate the mean
public static void calculateMean(int[] values) {
int sum = 0;
for (int i= 0; i < values.length; i++) {
sum += values[i];
}
System.out.println(sum / values.length);
}
// Calculate the median
public static void calculateMedian(int[] values) {
Arrays.sort(values);
int middle = values.length / 2;
System.out.println(values[middle]);
}
// Calculate the mode
// public static int calculateMode(int[] values) {
// Map<Integer, Integer> valueFrequency = new HashMap<>();
// int mode = -1;
// int maxFrequency = 0;
// for (int i = 0; i < values.length; i++) {
// int frequency = valueFrequency.getOrDefault(value, 0) + 1;
// valueFrequency.put(value, frequency);
// if (frequency > maxFrequency) {
// mode = value;
// maxFrequency = frequency;
// }
// }
// System.out.println(mode);
// }
public static void main(String[] args) {
int[] values = {42, 18, 7, 56, 91, 32, 5, 67, 23, 10};
calculateMean(values);
calculateMedian(values);
// calculateMode(values);
}
}
StatsUtils.main(null);
35
32