Assignment 2 Reflection:
Project 1: Animals and their Respective Sounds:
Files linked with this project: Main.java and AnimalTest.java.
This challenge question is mapped to the following learning objectives:
This programming challenge question was completed by creating two classes Main.java and AnimalTest.java. Both of these classes were created in two separate files saved in the same folder. As with all Java files created, the both file names are the same as the class names created.
This project demonstrates the concepts of class, inheritance, polymorphism and method overriding.
It required the use of extended classes in the Main class as was directed in the assignment brief. This ultimately allowed for individual animal classes to be created. Examples of classes created includes the pig, sheep, duck and cow classes. These classes take over all attributes, methods and constructors of the main class. This is because they are extended classes and are used to add functionality to these subclasses. In a nutshell, the animal class is a super class with four subclasses.
In this project brief, since Pig (and all other named animal classes i.e. sheep, duck & cow) are subclasses of the class Animal, an object of type Pig contains all the instance variables and instance methods defined in Animal. Variables and methods from the Animal class are inherited by Pig, and they can be used in the definition of Pig just as if they were actually defined in that class—except for any that are declared to be private, which prevents access even by subclasses.
The AnimalTest class is a separate class from the Main class with its own main method and they operate separately, and are intended to be run independently with no direct linkage existing between them. The AnimalTest class allows one to create an instance of an animal based on a command line argument and then invoke the sound method of that animal.
In order to use the Animal hierarchy in the AnimalTest class, I had to create an instance of an animal class by invoking its methods just like was done in the Main class.
In summary, the solution to challenge problem 1 can be outlined thus: In the Main class, instances of each animal are created, and their sound() methods are called to demonstrate different sounds.
In the AnimalTest class, command line arguments are used to determine the type of animal to create, and then thesound() method of the corresponding animal is called. This allows dynamic instantiation and method invocation based on user input. Dynamic instantiation involves creating objects at runtime, and method invocation involves calling the methods associated with objects to perform specific actions or computations. These concepts are fundamental in object-oriented programming paradigms.
This program can only be run via terminal if using Mac.
In order to get the required output for the program, that file called ‘AnimalTest.java should be run by specifying each of the animals in the subclass within the class Animal. Eg. 'java AnimalTest Pig'
Do note that 'AnimalTest.java' will compile without errors ONLY when 'Main.java' has been successfully compiled. This is in other for the interface implementation between both files to occur.
Some key things I learnt while working on this challenge question:
The Main method: I wondered why most examples that I reviewed online had the Main method below their animal subclasses, e.g. Pig, Sheep, Duck, and Cow as subclasses of the class ‘Animal’ and found out that this was so as to ensure readability and logic flow. Personally, I would have preferred to have my Main class at the top of the program if I had not reviewed some examples linked with this question that demonstrates the principles of inheritance, polymorphism, and method overriding in Java.
The sound method: When the sound method is called on each animal instance, polymorphism comes into play, and the overridden sound method of the specific animal type is executed. It should be noted that this project mainly involves polymorphism and method overriding.
In summary, this project demonstrates an example of class, hierarchy, inheritance, and polymorphism through method overriding. The ‘Animal’ class is used as the base class, and the subclasses – pig, sheep, duck and cow show polymorphic behaviour by providing their own implementation of the sound method.
Project Overview:
The Main Class.
The aim of this code is to demonstrate the principles of inheritance, polymorphism & method overriding in Java. In this program, two classes are created in separate files named,'The Main class & 'The AnimalTest Class'.
The 'Main Class stores the 'Animal class which is the base class with a defaultconstructor (a default constructor is on with no parameters e.g. ‘public Animal()’)and a sound method. 'Pig','Sheep','Duck’, & 'Cow' are subclasses of 'Animal'. Each subclass overrides the sound method to provide a specific sound for that animal. Also, in the main method, instances of each animal type (Pig, Sheep, Duck, Cow) are created and assigned to variables of type Animal. This demonstrates polymorphism, where objects of a superclass type (Animal) can refer to objects of its subclasses.
The AnimalTest Class
The aim of this code is to demonstrate the principles of inheritance, polymorphism & method overriding in Java. In this program, two classes are created in separate files named,’ The Main class & 'The AnimalTest class'. The 'Animal Test class, reads the command line user input and creates
the appropriate Animal child class based on the type of animal, it then calls the animal's sound() method.
This program is run via terminal if using a Mac OS or via Command Prompt or PowerShell if using Windows OS. This is done using the 'javac' & 'java ‘keywords to compile and run ie
'javac AnimalTest.java' to compile & 'java AnimalTest Pig' to run. (Pig can be replaced with 'Sheep’, ‘Duck', or 'Cow’) It should be noted that this program works in collaboration with the programentitled Main.java and that required output can be obtained by compiling and running AnimalTest.java
Test Plan
A test plan outlines the testing approach, scope, resources, and schedule for testing. In this case, I'll provide a simplified test plan for both the ‘Main’ & ‘AnimalTest’ classes.
A good test plan typically includes normal, borderline/boundary, and extreme test cases.
Test Plan for Main.java
Since the Main class primarily involves polymorphism and method overriding, I will focus on testing the behavior of the sound method.
Test 1:
Input: None (using default constructor)
Expected Output:
I am an animal.
I am a pig.
I am an animal.
I am a sheep.
I am an animal.
I am a duck.
I am an animal.
I am a cow.
Pig says "oink"
Sheep says "baah"
Duck says "quack"
Cow says "moo"
2. Borderline/Boundary Test: Verifies Polymorphism with Animal Reference of all subclasses. I.e. Pig, Sheep, Duck & Cow. Will use the instantiation of an object of a class named ‘Pig’ as example. Same test applies to all subclasses.
Test 2:
Input: Animal cat = new Pig();//cat could be replaced with any animal
Expected Output:
I am an animal.
I am a pig.
I am an animal.
I am a sheep.
I am an animal.
I am a duck.
I am an animal.
I am a cow.
Pig says "oink"
Sheep says "baah"
Duck says "quack"
Cow says "moo"
Explanation: This test explores polymorphism by assigning an object of the Pig class to a variable of type Animal. It's on the borderline because it tests if the polymorphic behavior is correctly handled, allowing an object of the subclass to be treated as an object of the superclass.
3. Extreme Test Cases:
Test 3: Verify Behavior with Null Object
Input: Large number of instances created within Main class i.e. two additional instances added.
Animal cat = new Cat();
Animal dog = new Dog()
Expected Output:
I am an animal.
I am a pig.
I am an animal.
I am a sheep.
I am an animal.
I am a duck.
I am an animal.
I am a cow.
Pig says "oink"
Sheep says "baah"
Duck says "quack"
Cow says "moo"
Test Plan for AnimalTest.java
Test 1: Program tested with valid animal types: in this case 'cow'. e.g. 'java AnimalTest Cow'
Expected Output:
I am an animal.
I am a cow.
Cow says "moo"
Test 2: Multiple Command-Line Arguments: Test the program with multiple command-line arguments to ensure it only considers the first one. e. g ‘java AnimalTest Pig Sheep Duck Cow’
Expected Output:
I am an animal.
I am a pig.
Pig says "oink"
3. Extreme:
Test 3: Empty Command-Line Arguments: Test the program with an empty array of command-line arguments. e.g. 'java AnimalTest'
Expected Output:
Please provide the type of animal as a command line argument.
References:
Assignment 2 Reflection:
Problem 2: Book Class for Representing and Testing Book Objects.
Files linked with this project: Book.java and TestBook.java.
This challenge question is mapped to the following learning objectives:
articulate the principles of object-oriented problem solving and programming.
outline the essential features and elements of the Java programming language.
explain programming fundamentals, including statement and control flow and recursion.
apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
program with basic data structures using array, list, and linked structures.
explain the object-oriented design process and the concept of software engineering.
program using objects and data abstraction, class, and methods in function abstraction.
analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
analyze problems and implement simple Java applications using an object-oriented software engineering approach.
This project had been created using two classes namely ‘Book.java’ and ‘TestBook.java’.
The Book Class:
This Book class represents a book with various attributes such as title, ISBN number, author, edition, publisher, and year of publication.
The code demonstrates encapsulation by making these attributes private and providing
public getter and setter methods to access and modify them. Additionally, the ‘Book’ class has a constructor that allows the creation of a ‘Book’ object with specified attribute values. This provides a way for the program to initialise the object when created.
The TestBook Class:
This class serves as a testing environment to ensure that the Book class functions as expected, both for manually created instances and for a more extensive set of dynamically generated instances in an extreme testing scenario.
The problem brief does not require that any input validation or immutability checks (i.e. this means that the state of an instance cannot be changed once it has been created) be applied to any of its attributes so this has not been carried out while solving this challenge problem.
In order to have books displayed to console, the file ‘Book.java’ will need to be compiled first before the ‘TestBook.java’ file is compiled and run. This is because the ‘TestBook’ file depends on the ‘Book’ file for creating objects and accessing their attributes. The ‘TestBook’ file uses the functionality provided by the ‘Book’.
In a nutshell, the ‘Book’ class encapsulates data and methods in a single unit called a class. The properties and behaviours related to books, and the ‘TestBook’ class interacts with one another to perform specific operations bearing in mind that encapsulation, is to make sure that "sensitive" data is hidden from users. These sensitive data if needed can be accessed using the ‘getter’ – ‘setter’. methods.
Test Plan for Book & TestBook .java
Normal Test:
Creating a book with standard attributes.
a) I created a new Book object with typical values for title, ISBN, author, edition, publisher, and year.
b) I used getter methods to retrieve each attribute.
c) I verified that the retrieved values match the initially set values.
Expected Output:
javac Book.java
javac TestBook.java
java TestBook
Book 1:
Title: Learn Java in one day
ISBN: 9781790789870
Author: Jamie Chan
Edition: Fourth Edition
Publisher: Amazon
Year: 2018
Book 2:
Title: Python Tricks: The Book
ISBN: 9781775093305
Author: Dan Bader
Edition: First Edition
Publisher: Amazon
Year: 2019
Book 3:
Title: C# Programming
ISBN: 978-1-84078-906-5
Author: Mike McGrath
Edition: 2nd Edition
Publisher: In Easy Steps
Year: 2020
Book 4:
Title: C# For Beginners
ISBN: 9781981070527
Author: Nathan Metzler
Edition: First Edition
Publisher: Amazon
Year: 2019
Book 5:
Title: Python Challenge!
ISBN: 9781910523353
Author: PM Heathcote
Edition: First Edition
Publisher: PG Online
Year: 2021
Borderline Test:
Modify book attributes using setter methods.
Test Steps:
a) The TestBook class already has five new Book objects created with initial values set.
b) To carryout borderline testing on these, I will be using only book 1 for my testing. I used the setter method to modify each attribute of Book 1.
c) I then verified that the retrieved values matched the modified values. See expected output below.
Expected Output:
javac Book.java
javac TestBook.java
java TestBook
Book 1: Original Entry
Title: Learn Java in one day
ISBN: 9781790789870
Author: Jamie Chan
Edition: Fourth Edition
Publisher: Amazon
Year: 2018
Modified Book 1:
Title: Reading and Writing about Literature
ISBN: 978-1-319-03536-5
Author: Janet E. Gardner & Joanne Diaz
Edition: Fourth Edition
Publisher: Macmillan Learning
Year: 2004
Output for book 2 – 5 remained the same as with normal testing upon running program as no modifications was carried out on them.
Extreme Test:
Create and display a large number of books.
Test Steps:
Test to help verify that the code created for the purpose of extreme testing can handle a large number of books without facing errors or performance issues.
Extract from expected output – Data Verification
Since the TestBook.java file is dependent on the Book.java file, all testing carried
out are linked to both files.
References:
Assignment 3 Reflection:
Problem 3: An Introduction to Computer Programming
Elevator Simulation and Testing Challenge.
Files linked with this project: Elevator.java and ElevatorTest.java.
This challenge question is mapped to the following learning objectives:
articulate the principles of object-oriented problem solving and programming.
outline the essential features and elements of the Java programming language.
explain programming fundamentals, including statement and control flow and recursion.
apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
program with basic data structures using array, list, and linked structures.
explain the object-oriented design process and the concept of software engineering.
program using objects and data abstraction, class, and methods in function abstraction.
analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
analyze problems and implement simple Java applications using an object-oriented software engineering approach.
I received the inspiration and necessary guidelines to start off this challenge problem by reading the reflection of another student @ Yuyang Liu's via his comments on the landing - https://landing.athabascau.ca/pages/view/16156140/assignment2. Accessed 28th December 2023.
This program is made up of two files. One of the files defines a class called ‘Elevator’. This class has two constructors, one of these is a default constructor that initializes number of floors to 5. This class also has another constructor that allows one to specify the number of floors when creating an object. Within this class is a cleanup method that sets the elevator to the first floor. The goToFloor method allows the elevator to move to specified floors with appropriate messages printed for different scenarios that are tested.
The second class that this challenge question is made up of is the ElevatorTest class. This class was set up to test various scenarios (up to five in total), including using the default constructor, using a specified number of floors, attempting to go to an invalid floor, testing when the elevator is already on the requested floor, and testing the cleanup method.
One of the challenges that I encountered with this question was that the finalize method did not work with my code. Upon research I found out that this method has been deprecated in recent Java versions (Source: - InfoWorld) So, it has not been explicitly implemented in the solution to this challenge question. Instead, the code uses a cleanup method that is explicitly called in the test scenarios.
In my code I after using each elevator instance, I added a line to set the reference to ‘null’. I however found that that doing this does not directly trigger garbage collection because Java garbage collection will automatically reclaim memory where there are no more references to an object. For the purposes of this project, I still preferred to leave that line of code in my ElevatorTest class.
In summary for this project, the Elevator class encapsulates elevator functionality, including movement between floors and cleanup while the ElevatorTest class provides scenarios to test different aspects of the elevator class, covering constructors, floor movements, error handling, and cleanup procedures.
Test Plan for Elevator & ElevatorTest .java
Normal Test:
Borderline Test:
Extreme Test:
References:
Assignment 4 Reflection:
Problem 4: An Introduction to Computer Programming
Rodent World: Creating and Inheritance Hierarchy in Java
Files linked with this project: RodentTest.java.
This challenge question is mapped to the following learning objectives:
articulate the principles of object-oriented problem solving and programming.
outline the essential features and elements of the Java programming language.
explain programming fundamentals, including statement and control flow and recursion.
apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
program with basic data structures using array, list, and linked structures.
explain the object-oriented design process and the concept of software engineering.
program using objects and data abstraction, class, and methods in function abstraction.
analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
analyze problems and implement simple Java applications using an object-oriented software engineering approach.
In this program I created an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. Methods were then provided in the base class that are common to all rodents based on their various behaviours. Each behaviour prints its action to standard output. The various behaviours used for this challenge problem were accessed from the links provided in the reference section below.
Part of this program involves refining these behaviours in the child classes to perform different behaviours, depending on the specific type of rodent, but only if the behaviour is actually different (e.g., mouse is nibbling on cheese or gerbil is digging tunnel).
Finally the main() class was defined , and instances of every rodent, that displays all the behaviours for each rodent were then created. This concept of creating instances of every rodent created demonstrates polymorphism,where instances of the child classes can be treated as instances of the base class. Finally, Rodent classes were then tested. This process of testing again demonstrated polymorphic behavior where the overridden methods in the child classes are executed based on the actual runtime type of the object.
In summary, this code includes inheritance, polymorphism, and method overriding, and it models a simple hierarchy of rodents with specific behaviors for each type. It also provides a mechanism to test and demonstrate these behaviors in the main method of the RodentTest class.
Test Plan for Rodent World – RodentTest.java
Print Screen of Instances Created.
Expected Output.
Expected Outcome.
Expected Output: A message will be displayed that reads: Cannot test behaviors. Object is not a Rodent.
References:
Assignment 5 Reflection:
Problem 5: An Introduction to Computer Programming
Java Geometry Models Project
Files linked with this project: CustomPoint.java, Shape.java, Rectangle.java, Circle.java Triangle.java & PointMain.java.
This challenge question is mapped to the following learning objectives:
articulate the principles of object-oriented problem solving and programming.
outline the essential features and elements of the Java programming language.
explain programming fundamentals, including statement and control flow and recursion.
apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
program with basic data structures using array, list, and linked structures.
explain the object-oriented design process and the concept of software engineering.
program using objects and data abstraction, class, and methods in function abstraction.
analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
analyze problems and implement simple Java applications using an object-oriented software engineering approach.
In tackling the Java Geometry Models Project, I encountered a profound and multifaceted challenge that not only expanded my grasp of object-oriented problem-solving but also deepened my understanding of Java programming. The project aligns with various learning objectives, covering essential features of the Java language and emphasizing key programming fundamentals.
The initial stages of creating this project was simple enough until it came to the geometric calculations that needed to be done in order to ensure that the program ran the way it was meant to. I will say that by far this is the most challenging project I have encountered so far. The holistic project required six classes in all were created.
The first class was the Point class and this class held x and y values for a point and it had the following methods - show(), add() and subtract(). These classes were created to display the Point x and y values, and add and subtract point coordinates respectively. It served as the foundational element of this project. One of the things I did not realize at the onset of this project is that the word ‘Point’ is actually a reserved word/ a class in java. So, at the onset of this project my file name was called ‘Point.java’. This had to be altered because when it came to testing my final project, some methods such as the show() method from the import statement java.awt.Point could not be accessed because there was a point class conflict of some sorts.
Next a superclass Shape.java was created and this class was used to represent a generic geometric shape with a 'bounding box' defined by two 'CustomPoint' objects x-y.
Afterwards the rectangle class was created. This class was an extension of the shape class.
It adds an array of vertices and a Boolean flag indicating whether the rectangle is a square and
overrides the display method to include information about the rectangles vertices and whether it is a square.
The Triangle class created also extended the shape class to represent a triangle. It used the point class objects as vertices and overrode the display method to include information about the triangle vertices. Additionally, it contained methods for validating a triangle and finding the top-left and bottom-right points for the bounding box.
Next, was the circle class. class extended the shape class to represent a circle. It included
private variables for the center point class and the radius. Override methods were used to calculate the area and circumference of the circle.
The final component created was the main class. This file was used to demonstrate the creation of instances of all shapes created in this project so that their information could be displayed to console as desired.
In conclusion, this project provided a rich learning experience encompassing object-oriented programming, inheritance, polymorphism, encapsulation, exception handling, static methods, composition, method overriding, and conditional statements. It not only enhanced my ability to design and implement solutions but also underscored the significance of these programming concepts in addressing complex problems using Java.
Test Plan for Geometry Project – PointMain.java
Normal:
Expected Output:
Borderline:
relevant exception rule.
Extreme:
I tested application using invalid inputs, such as null values to make sure that
this was handled gracefully by program.
References:
The Landing is a social site for Athabasca University staff, students and invited guests. It is a space where they can share, communicate and connect with anyone or everyone.
Unless you are logged in, you will only be able to see the fraction of posts on the site that have been made public. Right now you are not logged in.
If you have an Athabasca University login ID, use your standard username and password to access this site.
We welcome comments on public posts from members of the public. Please note, however, that all comments made on public posts must be moderated by their owners before they become visible on the site. The owner of the post (and no one else) has to do that.
If you want the full range of features and you have a login ID, log in using the links at the top of the page or at https://landing.athabascau.ca/login (logins are secure and encrypted)
Posts made here are the responsibility of their owners and may not reflect the views of Athabasca University.