top of page
Writer's pictureAakash Rahsi

Blue J | Java

1. Setting Up Blue J

  • Download Blue J: Blue J can be installed like most other applications using the setup executable file, available from the BlueJ web page.

  • Install JDK: Ensure that the Java Development Kit (JDK) is installed on your system before installing Blue J, as Blue J requires the JDK to run Java programs.

2. Understanding the Blue J Interface

  • Project Window: The workspace where your project files (classes) are displayed.

  • Code Editor: The place where you write your Java code.

  • Object Bench: Allows you to interactively create objects and make calls to methods.

  • Terminal Window: Displays output from your programs.

3. Basic Java Concepts

3.1. Hello World Program

Let us begin with the traditional "Hello World" program.

  1. Create a New Project: Open BlueJ, then go to Project > New Project, and name the project HelloWorldProject.

  2. Create a New Class: Right-click in the Project Window, then select New Class. Name the class HelloWorld.

  3. Write Code: Double-click on the class to open the editor.


    public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

  4. Compile and Run: Click Compile, then right-click the HelloWorld class and select void main(String[] args) to execute the program.

3.2. Variables and Data Types

Understanding variables and data types in Java is essential.

  • Example: A simple program using variables:

    public class VariablesExample { public static void main(String[] args) { int number = 10; // integer variable double price = 9.99; // double variable String name = "BlueJ"; // string variable System.out.println("Number: " + number); System.out.println("Price: " + price); System.out.println("Name: " + name); } }

Concepts Utilized:

  • Data Types: int, double, String.

  • Variable Declaration: int number = 10;

  • Printing Variables: System.out.println("Number: " + number);

3.3. Control Structures

Control structures help manage the flow within your program.

  • Example: A program that checks if a number is positive, negative, or zero:

    public class ControlStructuresExample { public static void main(String[] args) { int number = 5; if (number > 0) { System.out.println("The number is positive."); } else if (number < 0) { System.out.println("The number is negative."); } else { System.out.println("The number is zero."); } } }

Concepts Taught:

  • If-Else Statements: Making decisions in code.

  • Logical Conditions: number > 0, number < 0, etc.

3.4. Loops

Loops are used to iterate an action for a given number of times.

  • Example: A program that prints numbers from 1 to 5:

    java

    Copy code

    public class LoopsExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("Number: " + i); } } }

Concepts:

  • For Loop: for (int i = 1; i <= 5; i++);

  • Loop Control: Starting point, condition, and how much will change.

3.5. Arrays

Arrays are structures that can hold multiple values of a single type.

  • Example: A program that saves and reads an array of numbers:

    public class ArraysExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; for (int i = 0; i < numbers.length; i++) { System.out.println("Number: " + numbers[i]); } } }

Concepts Explained:

  • Array Declaration: int[] numbers = {1, 2, 3, 4, 5};

  • Accessing Array Elements: numbers[i]

  • Traversing an Array: Using a for loop.

4. Object-Oriented Programming (OOP) Concepts

4.1. Classes and Objects

Classes are blueprints for objects.

  • Example: Definition of a simple class and objects with attributes and methods:

    public class Car { String model; int year; void startEngine() { System.out.println("Engine started for " + model); } } public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.model = "Toyota"; myCar.year = 2020; myCar.startEngine(); } }

Concepts:

  • Class Definition: public class Car

  • States: String model; int year;

  • Behaviors: void startEngine()

  • Object: Car myCar = new Car();

4.2. Inheritance

Inheritance enables one class to inherit the attributes and methods of another.

  • Example: A Car class derived from the Vehicle base class:

    public class Vehicle { String brand; void honk() { System.out.println("Honk!"); } } public class Car extends Vehicle { String model; void displayInfo() { System.out.println("Brand: " + brand + ", Model: " + model); } } public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.brand = "Toyota"; myCar.model = "Corolla"; myCar.displayInfo(); myCar.honk(); } }

Covered Concepts:

  • Inheritance: class Car extends Vehicle

  • Method Overriding: Using methods from the parent class.

5. Advanced Topics

5.1. Polymorphism

Polymorphism allows one interface to be used for a general class of actions.

  • Example: Demonstrating polymorphism with a parent class Animal and child classes Dog and Cat:

    class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } class Cat extends Animal { void sound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Dog(); myAnimal.sound(); // Outputs: Dog barks myAnimal = new Cat(); myAnimal.sound(); // Outputs: Cat meows } }

Concepts Covered:

  • Polymorphism: Using parent and child classes interchangeably.

  • Method Overriding: Child classes provide specific implementations of the parent class's methods.

5.2. Encapsulation

Encapsulation is the bundling of data with methods that operate on that data.

  • Example: Creating a Student class where all the attributes are private, and getter and setter functions are public:

    public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class Main { public static void main(String[] args) { Student student = new Student(); student.setName("John"); student.setAge(18); System.out.println("Name: " + student.getName()); System.out.println("Age: " + student.getAge()); } }

Concepts:

  • Encapsulation: Using private variables and public methods.

  • Getters and Setters: Accessing and modifying private data.

6. Concluding Activity and Lab

  • Lab: Ask the students to design their own class and test out the topics learned.

  • Exercise: Experiment more with BlueJ, create interactive objects, and use the debugger.


2 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page