Explain Anatomy of a Class in comments of program (Diagram key parts of the class):
public class HelloDynamic {
// Instance variable to hold the greeting message
private String hello;
// Constructor with no arguments
public HelloDynamic() {
this.setHello("Hello, World!");
}
// Constructor with a greeting message argument
public HelloDynamic(String hello) {
this.setHello(hello);
}
// Setter method to set the greeting message
public void setHello(String hello) {
this.hello = hello;
}
// Getter method to retrieve the greeting message
public String getHello() {
return this.hello;
}
// Main method, entry point of the program
public static void main(String[] args) {
// Creating instances of HelloDynamic using different constructors
HelloDynamic hd1 = new HelloDynamic(); // Using the no argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!"); // Using the one argument constructor
// Displaying the greeting messages of the instances
System.out.println(hd1.getHello());
System.out.println(hd2.getHello());
}
}
// Activating the main method
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!
Comment in code where there is a definition of a Class and an instance of a Class (ie object)
public class HelloDynamic {
private String hello;
public HelloDynamic() {
this.setHello("Hello, World!");
}
public HelloDynamic(String hello) {
this.setHello(hello);
}
public void setHello(String hello) {
this.hello = hello;
}
public String getHello() {
return this.hello;
}
public static void main(String[] args) {
// Creating an instance of HelloDynamic using the no argument constructor
HelloDynamic hd1 = new HelloDynamic();
// Creating an instance of HelloDynamic using the one argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!");
// Displaying the greeting messages of the instances
System.out.println(hd1.getHello());
System.out.println(hd2.getHello());
}
}
// Activating the main method
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!
Comment in code where there are constructors and highlight the signature difference in the signature:
public class HelloDynamic {
private String hello;
// Constructor with no arguments
public HelloDynamic() {
this.setHello("Hello, World!");
}
// Constructor with a greeting message argument
public HelloDynamic(String hello) {
this.setHello(hello);
}
public void setHello(String hello) {
this.hello = hello;
}
public String getHello() {
return this.hello;
}
public static void main(String[] args) {
// Creating an instance of HelloDynamic using the no argument constructor
HelloDynamic hd1 = new HelloDynamic();
// Creating an instance of HelloDynamic using the one argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!");
// Displaying the greeting messages of the instances
System.out.println(hd1.getHello());
System.out.println(hd2.getHello());
}
}
// Activating the main method
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!
public class HelloDynamic {
private String hello;
// Constructor with no arguments
public HelloDynamic() {
this.setHello("Hello, World!");
}
// Constructor with a greeting message argument
public HelloDynamic(String hello) {
this.setHello(hello);
}
public void setHello(String hello) {
this.hello = hello;
}
public String getHello() {
return this.hello;
}
public static void main(String[] args) {
// Creating an instance of HelloDynamic using the no argument constructor
HelloDynamic hd1 = new HelloDynamic();
// Creating an instance of HelloDynamic using the one argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!");
// Displaying the greeting messages of the instances
System.out.println(hd1.getHello());
System.out.println(hd2.getHello());
}
}
// Activating the main method
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!
Create a static void main tester method to generate objects of the class.
// Define the Student class
class Student {
String name;
int age;
// Constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// Method to display student information
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
// Creating objects of the Student class using the constructor
Student student1 = new Student("Alice", 20);
Student student2 = new Student("Bob", 22);
// Calling the displayInfo method to show student information
System.out.println("Student 1:");
student1.displayInfo();
System.out.println("\nStudent 2:");
student2.displayInfo();
}
}
Go through progression of understanding a Static Class. Build a purposeful static Class, no Objects.
public class MathHelper {
// Private constructor to prevent instantiation
private MathHelper() {
// Empty private constructor
}
// Static method to calculate the square of a number
public static double square(double num) {
return num * num;
}
// Static method to calculate the cube of a number
public static double cube(double num) {
return num * num * num;
}
// Static method to calculate the factorial of a positive integer
public static int factorial(int n) {
if (n < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers");
}
if (n == 0 || n == 1) {
return 1;
}
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}
Calculate common operations on a Date field, age since date, older of two dates, number of seconds since date
import java.util.Date;
public class DateUtils {
// Private constructor to prevent instantiation
private DateUtils() {
// Empty private constructor
}
// Calculate age since a given date
public static int calculateAge(Date birthDate) {
Date currentDate = new Date();
long diffInMillis = currentDate.getTime() - birthDate.getTime();
long ageInMillis = diffInMillis;
return (int) (ageInMillis / (1000L * 60 * 60 * 24 * 365)); // Approximation
}
// Find the older of two dates
public static Date findOlderDate(Date date1, Date date2) {
return date1.before(date2) ? date1 : date2;
}
// Calculate the number of seconds since a given date
public static long calculateSecondsSince(Date startDate) {
Date currentDate = new Date();
long diffInMillis = currentDate.getTime() - startDate.getTime();
return diffInMillis / 1000L;
}
public static void main(String[] args) {
// Example usage
Date birthDate = new Date(95, 0, 1); // January 1, 1995
System.out.println("Age: " + calculateAge(birthDate) + " years");
Date date1 = new Date(121, 5, 15); // June 15, 2021
Date date2 = new Date(120, 8, 20); // September 20, 2020
System.out.println("Older date: " + findOlderDate(date1, date2));
Date startDate = new Date(122, 1, 1); // February 1, 2022
System.out.println("Seconds since start date: " + calculateSecondsSince(startDate) + " seconds");
}
}
Calculate stats functions on an array of values: mean, median, mode.
import java.util.*;
public class StatisticsUtils {
// Private constructor to prevent instantiation
private StatisticsUtils() {
// Empty private constructor
}
// Calculate the mean of an array of values
public static double calculateMean(double[] values) {
double sum = 0;
for (double value : values) {
sum += value;
}
return sum / values.length;
}
// Calculate the median of an array of values
public static double calculateMedian(double[] values) {
Arrays.sort(values);
int length = values.length;
if (length % 2 == 0) {
int middleIndex = length / 2;
return (values[middleIndex - 1] + values[middleIndex]) / 2.0;
} else {
return values[length / 2];
}
}
// Calculate the mode of an array of values
public static double calculateMode(double[] values) {
Map<Double, Integer> frequencyMap = new HashMap<>();
for (double value : values) {
frequencyMap.put(value, frequencyMap.getOrDefault(value, 0) + 1);
}
double mode = 0;
int maxFrequency = 0;
for (Map.Entry<Double, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() > maxFrequency) {
mode = entry.getKey();
maxFrequency = entry.getValue();
}
}
return mode;
}
public static void main(String[] args) {
double[] values = { 4, 2, 8, 6, 3, 8, 10, 8 };
System.out.println("Mean: " + calculateMean(values));
System.out.println("Median: " + calculateMedian(values));
System.out.println("Mode: " + calculateMode(values));
}
}