Java Foundations with BlueJ
Download Week 1 BlueJ projects
Download Week 1 BlueJ projects (Mac OS)
What is an Object?
An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life.
Objects are key to understanding object-oriented technology. Look around right now and you’ll find many examples of real-world objects:
Real-world objects share two characteristics:
a) State
b) Behavior
Dogs have state:
name, color, breed, hungry
and behavior:
barking, fetching, wagging tail
Bicycles also have state:
current gear, current pedal cadence, current speed
and behavior:
changing gear, changing pedal cadence, applying brakes
Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Read more here.
Java Programs: Object Party
A typical Java program is like a lit squad of objects, all vibing and interacting by calling each other’s methods. Through these object hangouts, a program can do all sorts of epic stuff, like creating a sick app interface, running game graphics that slay, or streaming and sharing the latest viral memes. Once an object has finished its mission, it’s like, “I’m out!” and its resources get recycled for other objects to flex with.
Here’s a small program, called MemeStreamerTester
, that creates two objects: one Meme
object and one MemeStreamer
object. You will need all two source files to compile this program.
public class MemeStreamerTester {
public static void main(String[] args) {
MemeStreamer streamer = new MemeStreamer();
// Adding some memes to the list
streamer.addMeme("Doge", "https://example.com/doge.jpg");
streamer.addMeme("Distracted Boyfriend", "https://example.com/distracted.jpg");
streamer.addMeme("Success Kid", "https://example.com/success.jpg");
// Sharing some memes
streamer.shareMeme();
streamer.shareMeme();
}
}
🚀 Final Thoughts
This example showcases how objects (memes) can interact within a program (meme streamer) to perform a fun and relatable task: sharing memes. Keep exploring Java, and you’ll be able to build even more awesome and complex programs!
Read more here.
What is a Class?
A class is a blueprint or prototype from which objects are created.
Blueprint to Instance: A Gen Z Guide
👟 Sneaker Class - Java Style! 👟
Alright, peeps! Let’s dive into the world of Java programming with a cool example: the Sneaker
class. Think of it as the ultimate blueprint for creating digital sneakers. Check it out:
class Sneaker {
// Fields representing sneaker state
int size = 0;
String color = "White";
String brand = "Unknown";
// Method to change size
void changeSize(int newSize) {
size = newSize;
}
// Method to change color
void changeColor(String newColor) {
color = newColor;
}
// Method to change brand
void changeBrand(String newBrand) {
brand = newBrand;
}
// Method to print the current state of the sneaker
void printDetails() {
System.out.println("size: " + size + " color: " + color + " brand: " + brand);
}
}
👟 What’s Happening Here?
This Sneaker
class is like a recipe card for making sneakers in a video game or app. The fields (size
, color
, and brand
) are like the sneaker’s stats, and the methods (changeSize
, changeColor
, changeBrand
) are the moves or actions you can do with the sneaker.
But, just like a TikTok dance tutorial, this class alone isn’t the whole story. It’s just the blueprint. We need another class to actually create and use these sneaker objects. Let’s roll over to the SneakerDemo
class:
🎬 SneakerDemo Class - Action Time!
Here’s how we put our sneakers into action:
public class SneakerDemo {
public static void main(String[] args) {
// Creating two different Sneaker objects
Sneaker sneaker1 = new Sneaker();
Sneaker sneaker2 = new Sneaker();
// Customizing sneaker1
sneaker1.changeSize(42);
sneaker1.changeColor("Red");
sneaker1.changeBrand("Nike");
sneaker1.printDetails();
// Customizing sneaker2
sneaker2.changeSize(38);
sneaker2.changeColor("Blue");
sneaker2.changeBrand("Adidas");
sneaker2.changeColor("Black"); // Changed mind on the color
sneaker2.changeSize(40); // Realized they needed a bigger size
sneaker2.printDetails();
}
}
📊 Output: Sneaker Stats Time!
When you run this code, you get the stats of your two sneakers at the end:
size: 42 color: Red brand: Nike
size: 40 color: Black brand: Adidas
🛠️ Breaking It Down:
Sneaker sneaker1 = new Sneaker();
Creates a new sneaker called sneaker1.sneaker1.changeSize(42);
Sets the size to 42.sneaker1.changeColor("Red");
Changes the color to Red.sneaker1.changeBrand("Nike");
Sets the brand to Nike.sneaker1.printDetails();
Prints out the current state of the sneaker.
🚀 Final Thoughts
This is just the beginning. The Sneaker class shows how we can define objects in Java. It’s like building your virtual closet of sneakers with different sizes, colors, and brands. Keep customizing through the code, and soon you’ll be styling through more advanced Java concepts!
Read more here.