IceCreamInfo - Java Hello

This article shows the basic language structures and constructs of Java (aka anatomy). In async order, it is critical to understand these examples and learn vocab for OOP and Creating Objects:

// Define a class
public class IceCreamInfo {
    //  instance variables to store ice cream information
    private String name;  // Stores the name of the ice cream
    private int price;    // Stores the price of the ice cream in cents

    // Initializes instance variables with default values
    public IceCreamInfo() {
        this.name = "";    // Initialize name with an empty string
        this.price = 0;    // Initialize price = 0
    }

    // Initializes instance variables with provided values
    public IceCreamInfo(String name, int price) {
        this.name = name;  // Initialize name with provided value
        this.price = price;  // Initialize price with provided value
    }

    // Setter method to update the ice cream's name
    public void setName(String newName) {
        this.name = newName;  // Update name with the new provided value
    }

    // Setter method to update the ice cream's price
    public void setPrice(int newPrice) {
        this.price = newPrice;  // Update price with the new provided value
    }

    // Getter method to retrieve the ice cream's name
    public String getName() {
        return this.name;  // Return the stored name
    }

    // Getter method to retrieve the ice cream's price
    public int getPrice() {
        return this.price;  // Return the stored price
    }

    public static void main(String[] args) {
        System.out.println("Ice Cream Time");

        // Create instances of the class using constructors
        IceCreamInfo vanilla = new IceCreamInfo();
        vanilla.setName("Vanilla");  // Set the name of the ice cream
        vanilla.setPrice(200);       // Set the price of the ice cream

        IceCreamInfo chocolate = new IceCreamInfo("Chocolate", 200);
        IceCreamInfo strawberry = new IceCreamInfo("Strawberry", 300);
        IceCreamInfo creme = new IceCreamInfo("Creme", 100);
        IceCreamInfo caramel = new IceCreamInfo("Caramel", 200);

        // Print Statements to display ice cream information
        System.out.println("Ice Cream | Price ");
        System.out.println(chocolate.getName() + " | " + chocolate.getPrice());
        System.out.println(strawberry.getName() + " | " + strawberry.getPrice());
        System.out.println(creme.getName() + " | " + creme.getPrice());
        System.out.println(caramel.getName() + " | " + caramel.getPrice());
    }
}
IceCreamInfo.main(null);
Ice Cream Time
Ice Cream | Price 
Chocolate | 200
Strawberry | 300
Creme | 100
Caramel | 200
IceCreamInfo chocolate = new IceCreamInfo();
chocolate.getPrice();
chocolate.setPrice(500);
chocolate.getPrice();
500


IMG_0FD79FA9CD9D-1

Static vs Non-Static

Static

  • Class Variables: shared among all instances of the class. If oe instance changes the value of a static variable, the change is reflected across all instances and the class itself
  • Class Methods: Belong to the class and not to any instance. They can be called using the class name and are often used for utility functions that don’t require instance-specific data.

Instance Elements

  • Attributes/Fields: hold data that varies from instance to instance. Each instance has its own set of instance values
  • Object Methods: Operate on the data of a specific instance. Can acess instance variable and other instance methods

Differences

  • Memory Allocation: Static elements are allocated memory only once when the class is loaded, regardless of how many instances are created. Instance elements are allocated memory separately for each instance.
  • Access: Static elements can be accessed using the class name, e.g., ClassName.staticMethod(). Instance elements are accessed using an instance of the class, e.g., instanceName.instanceMethod().
  • Usage: Static methods are used for tasks that don’t require instance-specific data. Instance methods are used for tasks that involve instance-specific data.
  • Visibility: Static elements can access other static elements directly. Instance elements can access both static and other instance elements directly.

Hacks

Build your own Jupyter Notebook meeting these College Board and CTE competencies. It is critical to understand Static versus Instance Now, this is College Board requirement!!!

  • X Explain Anatomy of a Class in comments of program (Diagram key parts of the class).
  • X Comment in code where there is a definition of a Class and an instance of a Class (ie object)
  • X Comment in code where there are constructors and highlight the signature difference in the signature
  • X Call an object method with parameter (ie setters).

Additional requirements (Pick something)

  1. Go through code progression of understanding Class usage and generating an Instance of a Class (Object). a. Build a purposeful dynamic Class, using an Object, generate multiple instances: - Person: Name and Age - Dessert: Type and Cost - Location: City, State, Zip b. Create a static void main tester method to generate objects of the class. c. In tester method, show how setters/mutators can be used to make the data in the Object dynamically change
  2. Go through progression of understanding a Static Class. Build a purposeful static Class, no Objects.
    • Calculate common operations on a Date field, age since date, older of two dates, number of seconds since date
    • Calculate stats functions on an array of values: mean, median, mode.