Java February 1 Java Assignment 1: Interfaces and Object Factories

Assignment Due: 6 pm March 31
How to submit
Put screen shots of your running /executed code into a Word Document.
Save the Word Document as file name : StudentName_StudentID.docx
Put student name / StudentID.
Label each question / for section with your completed screen shots showing the the code and the completed run/output for each section.

Upload your Word Document to :

Section 1: Basics of Running the Simplest Java Program

To understand and execute a basic Java program that prints "Hello, World!" to the console.
**Lecture Notes**: - Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. - Java programs are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.
**Lab Exercise**: 1. **Setting Up Your Environment**: - Install Java Development Kit (JDK) from the official Oracle website. - Set up an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA.
2. **Writing the Program**: - Create a new Java project and a class named `HelloWorld`. ​
3. **Compiling and Running Your Program**: - Use the IDE to compile and run the program. - Observe "Hello, World!" printed on the console.
**Assignment**: Write a reflective piece on why "Hello, World!" is a common first program in many programming languages.

Section 2: Variables, Loops, and Conditional Statements

**Objective**: To understand and use basic Java constructs like variables, loops, and conditional statements.
**Lecture Notes**: - Variables are containers for storing data values. - Loops are used for executing a set of statements repeatedly. - Conditional statements (if-then) are used for decision-making processes.
**Lab Exercise**:

1. **Working with Variables**:

Create a program that declares variables of different types (int, String, double) and prints their values.

2. **Using Loops**:

- Write a for loop that prints numbers from 1 to 10. - Modify the loop to print only even numbers.

3. **Conditional Statements**:

- Write an if-then statement that checks if a number is odd or even and prints a message to the console.
**Assignment**: Create a program that calculates and prints the factorial of a number using a loop.

Section 3: Creating Classes and Objects**

**Objective**: To understand the concept of classes and objects in Java and how to create and use them.
**Lecture Notes**: - A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or properties), and implementations of behavior (member functions or methods). - An object is an instance of a class.
Learner activity: 1. **Defining a Class**: - Create a class `Car` with attributes like `brand`, `model`, and `year`.
2. **Creating an Object**: - In your main method, create an object of the `Car` class and assign values to its attributes.
3. **Accessing Object Attributes**: - Print the attributes of the `Car` object to the console.
Learner Activity: Design and implement a class `Book` with attributes and create multiple objects of it.

Section 4: Method Calls Between Objects

**Objective**: To understand how objects interact with each other using methods and convey information.
**Lecture Notes**: - Methods in a class can be called by objects of that class to perform actions. - Objects can interact by calling each other's methods.
**Lab Exercise**: 1. **Writing Methods**: - Add a method in the `Car` class that displays information about the car.
2. **Calling Methods Between Objects**: - Create two `Car` objects and call their display methods.
3. **Simple Calculator**: - Create a `Calculator` class with methods for basic operations like add, subtract, multiply, and divide. - Take input from the command line and use these methods to perform calculations and display the results.
**Assignment**: Enhance the Calculator to handle error conditions like division by zero.

Section 5: Arrays and Method Calls

**Objective**: To understand arrays in Java and how they can be used in method calls between objects.
**Lecture Notes**: - An array is a container object that holds a fixed number of values of a single type. - Arrays can be passed to methods and can also be returned from them.
**Lab Exercise**: 1. **Creating and Using Arrays**: - Write a program that creates
an array of integers and prints its contents.
2. **Passing Arrays to Methods**: - Create a method that takes an array as a parameter and prints the sum of its elements.
3. **Returning Arrays from Methods**: - Write a method that returns an array containing the first n even numbers.

Student Activity:

Create a program that sorts an array using a method and prints the sorted array.

This concludes the first part of the workbook.

This second part before is only for advanced students intent on getting a job as a Programmer:

The following sections will further explore advanced concepts in Java programming.
Students are encouraged to experiment with the code and understand the underlying principles of object-oriented programming as it was understood in early 21st-century Earth technology.

Detailed code examples for Sections 3, 4, and 5 of the workbook.

public class Main { public static void main(String[] args) { Car myCar = new Car("Tesla", "Model S", 2020); myCar.displayInfo(); } } ```
Section 5: Arrays and Method Calls**
**Objective**: To understand arrays and their usage in method calls.
#### **Using Arrays in Methods**
```java public class ArrayOperations { // Method to sum elements of an array public int sumArray(int[] array) { int sum = 0; for (int i : array) { sum += i; } return sum; }
// Method to return an array of the first n even numbers public int[] firstNEvenNumbers(int n) { int[] evenNumbers = new int[n]; for (int i = 0, num = 2; i < n; i++, num += 2) { evenNumbers[i] = num; } return evenNumbers; } }
These examples illustrate the basic principles of object-oriented programming in Java, including class creation, object instantiation, method invocation, and array handling. Students are encouraged to modify and experiment with these examples to deepen their understanding.

Watch this video to learn about Java Interfaces:

Assignment Section 2: This is optional:

For advanced students planning to work as professional software developers:


Epoch 2: Advanced Java Concepts

In this phase, we delve into more complex topics:
Factory method for object creation and Interfaces
Handling data structures
Moving data structures between objects, and
Shared field composition.
This will provide Star Fleet engineers a deeper understanding of object-oriented programming and data handling in Java.

Section 6: Factory Methods for Object Creation**

To understand and implement the Factory Method design pattern for object creation.
- The Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. - This pattern is particularly useful when the exact type of objects to be created is not known until runtime.
**Lab Exercise**: 1. **Defining a Factory Interface**: - Create an interface `VehicleFactory` with a method `createVehicle`.
2. **Implementing the Factory**: - Create classes `CarFactory` and `BikeFactory` that implement `VehicleFactory`.
3. **Creating Objects Using the Factory**: - In the main method, use these factories to create different vehicle objects.
Implement a factory method for a Star Fleet equipment class, allowing for the creation of different types of equipment.
Section 7: Data Structures in Java
**Objective**: To explore and utilize different data structures like Lists, Sets, and Maps in Java.
**Lecture Notes**: - Data structures are used to store and organize data efficiently. - Java Collections Framework provides several data structures like ArrayList, HashSet, and HashMap.
**Lab Exercise**: 1. **Working with Lists**: - Create an `ArrayList` and perform basic operations like add, remove, and iterate over it.
2. **Using Sets**: - Demonstrate the use of a `HashSet` and explain its property of storing unique elements.
3. **Exploring Maps**: - Create a `HashMap` and demonstrate how to store and retrieve key-value pairs.
**Assignment**: Create a program that maps Star Fleet officers to their respective departments using a HashMap.

Section 8: Moving Data Structures Between Objects**

**Objective**: To learn how to pass data structures between objects using method calls.
**Lecture Notes**: - Data structures can be passed as parameters to methods of other objects. - This is useful for transferring a collection of data from one object to another.
**Lab Exercise**: 1. **Creating a Data Structure**: - Create an `ArrayList` of `String` objects representing Star Fleet equipment.
2. **Passing Data Structure to Methods**: - Create a method in a class that takes the ArrayList as a parameter and prints its contents.
**Assignment**: Write a program where one object creates a data structure and another object processes the data in it.

Section 9: Shared Field Composition

To understand and implement shared field composition in Java.
**Lecture Notes**: - Shared field composition involves creating objects that share references to common objects. - This is a way to create complex relationships between objects.
**Lab Exercise**: 1. **Defining Shared Fields**: - Create a class `Engine` and use it as a shared field in the `Car` class.
2. **Implementing Shared Composition**: - Demonstrate how multiple cars can share the same engine object.
**Assignment**: Design a shared composition scenario for a Star Fleet vessel's navigation system.

Section 10: Consumer Objects and Data Structures

To create and utilize consumer objects that unpack and use data encoded in data structures.
**Lecture Notes**: - Consumer objects are designed to process collections of data. - These objects can extract, modify, or use data stored in data structures.
**Lab Exercise**: 1. **Creating a Consumer Object**: - Design a class `EquipmentManager` that processes an ArrayList of equipment.
2. **Unpacking Data Structures**: - Implement methods in `EquipmentManager` to display and modify equipment details.
**Assignment**: Develop a consumer object that manages a fleet of Star Fleet vehicles, updating their status and deployment.
These sections provide an advanced exploration into object-oriented programming, design patterns, and data handling in Java. Star Fleet engineers should be encouraged to experiment with these concepts, tailoring them to the specific needs of Star Fleet operations and the unique challenges they may face in their engineering roles.

Read this Workbook on Java Interfaces:

Let's dive into the detailed code solutions for Sections 6 to 10, covering advanced Java concepts.

These code examples demonstrate advanced Java concepts, including design patterns, data structure manipulation, and complex object relationships.
They're designed to deepen the understanding of Java programming and its application in diverse scenarios, particularly useful building Service Oriented Architectures.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.