KEY TERMS:
-
CLASS: The class always has to be called in order for the code to be run.
-
METHOD: Akin to the CSP procedure which is being called. This is the code being run.
-
SIGNATURE: public static void, string[] args
-
METHOD CALL: How is the code in question being run?
public class HelloStatic {
public static void main(String[] args) {
System.out.println("Welcome to CSA!");
}
}
HelloStatic.main(null);
Welcome to CSA!
// defining the public class called Ager
public class Ager {
// creating the two instance variables, quite basic (player, age)
private String player;
private String age;
// constructor, initializes the instance variables declared above
public Ager(String initplayer, String initage) {
player = initplayer;
age = initage;
}
// Setter method to change the player's name
public void setPlayer(String newPlayer) {
player = newPlayer;
}
// easy print method, formatting
public void print() {
System.out.println("Player: " + player);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
// creates instance of class
Ager p1 = new Ager("LeBron James", "38");
// Using the setter to change the player's name
p1.setPlayer("LeBron Updated Setter Name");
// calling print method
p1.print();
Ager p2 = new Ager("Michael Jordan", "60");
p2.setPlayer("MJ Updated Setter Name");
p2.print();
}
}
// calls main method in order to get an output
Ager.main(null);
Player: LeBron Updated Setter Name
Age: 38
Player: MJ Updated Setter Name
Age: 60
import java.util.Arrays; // array ops
import java.util.Scanner; // allows for user input
// class declaration
public class StatsCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // reads user input
// prompts user for input
System.out.print("Enter a list of comma-separated whole numbers: ");
String input = scanner.nextLine(); // reads input as a string
// makes an array of strings --> int array
int[] data = Arrays.stream(input.split(","))
.mapToInt(Integer::parseInt)
.toArray();
// finds highest value. if there is no max value then it returns the min value.
System.out.println("High: " + Arrays.stream(data).max().orElse(Integer.MIN_VALUE));
// finds lowest value. if there is no low value it returns the max value.
System.out.println("Low: " + Arrays.stream(data).min().orElse(Integer.MAX_VALUE));
// calculates standard deviation of dataset
System.out.println("Standard Deviation: " + calculateStdDev(data));
scanner.close(); // closes scanner
}
// calculcates std of int array
public static double calculateStdDev(int[] values) {
// mean
double mean = Arrays.stream(values).average().orElse(Double.NaN);
// sum of squared diffs
double squaredDiffSum = Arrays.stream(values).mapToDouble(v -> Math.pow(v - mean, 2)).sum();
// variance --> sqrt (variance) = std
double variance = squaredDiffSum / values.length;
return Math.sqrt(variance);
}
}
StatsCalculator.main(null);
Enter a list of comma-separated whole numbers: High: 5
Low: 3
Standard Deviation: 0.816496580927726