Lesson: Introduction to Object-Oriented Programming with Java and Machine Learning
Lesson Objectives:
- Understand the concept of Object-Oriented Programming (OOP) in Java.
- Learn how to create and use classes, constructors, and methods.
- Apply OOP concepts to a simple Machine Learning model.
- Explore method overloading and object instantiation.
Specification Sheet
Class: MLModel
Attributes:
- modelName (String): Name of the machine learning model.
- numLayers (int): Number of layers in the model.
- learningRate (double): Learning rate of the model.
Constructors:
1. MLModel(): Initializes with default values.
2. MLModel(String modelName, int numLayers): Initializes with specified name and layers, default learning rate.
3. MLModel(String modelName, int numLayers, double learningRate): Fully customizable initialization.
Methods:
1. train(): Simulates training the model.
2. predict(): Simulates making predictions.
3. evaluate(): Overloaded method to evaluate the model.
4. evaluate(String dataset): Evaluates model using a dataset.
5. saveModel(): Saves the model.
6. loadModel(): Loads a model.
Pseudo Code
BEGIN
DEFINE CLASS MLModel
DEFINE ATTRIBUTES modelName, numLayers, learningRate
DEFINE CONSTRUCTOR MLModel()
DEFINE CONSTRUCTOR MLModel(modelName, numLayers)
DEFINE CONSTRUCTOR MLModel(modelName, numLayers, learningRate)
DEFINE METHOD train()
DEFINE METHOD predict()
DEFINE METHOD evaluate()
DEFINE METHOD evaluate(dataset)
DEFINE METHOD saveModel()
DEFINE METHOD loadModel()
END CLASS
DEFINE CLASS MachineLearningApp
MAIN FUNCTION
CREATE INSTANCE model1 USING MLModel()
CREATE INSTANCE model2 USING MLModel("NeuralNet", 5)
CREATE INSTANCE model3 USING MLModel("DeepLearningModel", 10, 0.005)
CALL model1.train()
CALL model2.train()
CALL model3.train()
CALL model1.predict()
CALL model2.evaluate()
CALL model3.evaluate("Test Dataset")
CALL model1.saveModel()
CALL model2.loadModel()
END MAIN FUNCTION
END CLASS
END
Java Code: MLModel.java
class MLModel {
******************** code your solution ************************
Java Code: MLModelTester.java
public class MLModelTester {
******************** code your solution ************************
You will drop off 4 files into Google Classroom:
• Your files will be: (Remember the python program is dropped off first.)
• PX_lastname_MLModel.java (Java program that is instantiated)
• PX_lastname_MLModelTester.java (Java program that instantiates the object)
• PX_lastname_MLModel.png (Screenshot inside Eclipse)
• PX_lastname_MLModel.mp4 (Video running the program)
If you do not understand this assignment,
ask Mr. Cusack and/or attend tutorials.
Lesson Summary:
- We created a Java class
MLModel
that simulates a simple Machine Learning model.
- We explored constructors to initialize objects with different parameters.
- We learned about method overloading, which allows multiple methods with the same name but different parameters.
- The
MLModelTester
class demonstrates how to instantiate and use objects of the MLModel
class.