// Define Class
public class HelloDynamic { // name the first letter of class as capitalized, note camel case
// instance variable have access modifier (private is most common), data type, and name
private String hello;
// constructor signature 1, public and zero arguments, constructors do not have return type
public HelloDynamic() { // 0 argument constructor
this.setHello("Hello, World!"); // using setter with static string
}
// constructor signature, public and one argument
public HelloDynamic(String hello) { // 1 argument constructor
this.setHello(hello); // using setter with local variable passed into constructor
}
// setter/mutator, setter have void return type and a parameter
public void setHello(String hello) { // setter
this.hello = hello; // instance variable on the left, local variable on the right
}
// getter/accessor, getter used to return private instance variable (encapsulated), return type is String
public String getHello() { // getter
return this.hello;
}
// public static void main(String[] args) is signature for main/drivers/tester method
// a driver/tester method is singular or called a class method, it is never part of an object
public static void main(String[] args) {
HelloDynamic hd1 = new HelloDynamic(); // no argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!"); // one argument constructor
System.out.println(hd1.getHello()); // accessing getter
System.out.println(hd2.getHello());
}
}
// IJava activation
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
// Method to calculate the mean
double mean(int[] numbers) {
double sum = 0;
for (int num : numbers) {
sum += num;
}
return sum / numbers.length;
}
// Method to calculate the median
double median(int[] numbers) {
Arrays.sort(numbers);
int middle = numbers.length / 2;
if (numbers.length % 2 == 0) {
return (numbers[middle - 1] + numbers[middle]) / 2.0;
} else {
return numbers[middle];
}
}
// Method to calculate the mode
int mode(int[] numbers) {
Map<Integer, Integer> countMap = new HashMap<>();
for (int num : numbers) {
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
}
int maxCount = 0;
int mode = -1;
for (Entry<Integer, Integer> entry : countMap.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
mode = entry.getKey();
}
}
return mode;
}
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a comma-separated list of numbers:");
String input = scanner.nextLine();
String[] numberStrings = input.split(",");
int[] numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i].trim());
}
System.out.println("Mean: " + mean(numbers));
System.out.println("Median: " + median(numbers));
System.out.println("Mode: " + mode(numbers));
Enter a comma-separated list of numbers:
Mean: 4.571428571428571
Median: 5.0
Mode: 5