Landing : Athabascau University

Reflections on Assignment 1 - Covers Units 1 - 4 of Course Text: Revision

 

Assignment 1 Reflection:

Project 1: Multiplication Table:

This challenge question can be mapped to the two of the learning objectives outlined below:

★   Outline the essential features and elements of the Java programming language.

★   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.

The first stage of completing this challenge question was quite easy as the basic principles of doing this task was introduced in this course’s recommended text written by Eck, D. Jon on nested ‘for loops’(section 3.4.3)

My first approach to this question was to create the inner and outer loops and to populate the table by calculating the values of the row and the column.  My main challenge with this question was in inserting the column headings.  The row headings were simple enough. Output for the row headings was done using format specifiers with five spaces %5d.  One or two additional spaces were added manually to this printf statement  using the space bars on the keyboard.   The question of inserting the column heading was another matter.  This took some time to figure out.  The video clip by Wulf Tom helped a great deal however trying to get the column headings to align the way they ought to was another matter.  I toggled between using the printf method and the nested if statement to get all the bars and numbers aligned. I eventually succeeded with using the nested loop control structure as suggested in the assignment brief.  Table heading was inserted using println statement.  This program can only be run via terminal if using Mac as I am currently struggling with installing script to my preferred coding platform – Atom.

References: 

Eck, D. J. (2019, July). Introduction to Programming Using Java (Version 8.1). Accessed 9th October 2023

 

Wulf Tom: Java Nested Loops: https://www.youtube.com/watch?v=1C8BYqHhZ0A:Accessed 9th October 2023

Vidal Jose: Java Nested For Loops Tutorial: https://www.youtube.com/watch?v=WOB9J6WHynA Accessed 9thOctober 2023

 

Assignment 2 Reflection:

Problem 2: An Introduction to Computer Programming

This program converts temperature from Fahrenheit to Celsius and vice versa.

This challenge question is mapped to the following learning objectives:

★   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 – partly covered.

★   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.

I approached this challenge by first declaring all the variables needed in order for the task to be completed.  I created a draft outline of how the program might look like using the outline of the sample run that was provided in the assignment brief.  The formula used for conversion was also provided in the brief therefore this was mapped onto the outline of the program before an attempt to solve the problem.

This program was written within a do…while loop because it had to be condition controlled based on user response to questions as directed in the project brief.  The next stage of solving this task was to import the Scanner class that allows the user input data into the console.  All variables were initialized as using type double as the project brief specified that the program use real numbers and not integers for both input and output.  After the framework of the script was put in place, I then attempted to  use the if…else nested loop to check that the users input is converted to lowercase regardless of what case format they entered to console using Java’s  toLowerrCase() String  method - Java String Reference (w3schools.com) however this method did not work with my program.  I did a bit of research online and realized that as opposed to this String method, I could use the equalsIgnoreCase() method that compares two strings, ignoring case considerations.  It took me some time to work out how to write this line of code using Java’s OR || logical operator to compare between lowercase and uppercase entry by user.  Eventually this worked with some online research java - Converting from Celsius to Fahrenheit and vice versa - Code Review Stack Exchange.

One of the main skills I learnt from this task is the difference between Java’s next() and nextline() methods and how they are read in Java.  Difference between next() and nextLine() methods in Java - GeeksforGeeks.  This next() method was used when my Scanner object was created to get user input for desired temperature values that were converted to Fahrenheit and Celsius and vice versa.

One of the major challenges that I encountered with this exercise was at the point when I had to introduce the try…catch block to the Scanner object so that  any entries other than a value accepted for user entry.  After introducing this block, my script would not compile.  An error message that indicated that the variable named userInput used to store user input of type double could not be located was displayed.  After a couple of online searches, I found out that this error message occurred because this variable was declared outside the scope of the try-catch block and that it had to be defined locally right before this block. Reference source of research. Stack Overflow.  java - Try catch and user input - Stack Overflow

I also realized that the variable also had to be assigned an initial value, in this case of 0 before the program could run…error message before assigning value to variable: The local variable userInput may not have been initialized

I got the resource to support this error message from the link below: Variable Might Not Have Been Initialized: Grab the Quick Fix (positioniseverything.net) .  I used this YouTube link Java - Fahrenheit to Celsius (and vice versa) Tutorial! Temperature Calculator in Java and Eclipse! - YouTube to guide me with rounding final output down.  It was also important to insert the code keyboardInput.nextLine() just before the user was able to enter their response to whether or not they wanted to continue with the program.  This served to consume the extra new line created. Explanation for this obtained from StackOverflow java - Scanner is skipping nextLine() after using next() or nextFoo()? - Stack Overflow.  “It's because when you enter a number then press Enter, input.nextDouble() consumes only the number, not the "end of line". When input.nextLine() executes, it consumes the "end of line" still in the buffer from the first input.”  Therefore it was important to use Instead, use input.nextLine() immediately after input.nextDouble()

Overall this task was a bit challenging for me however I learnt a couple of valuable skills from completing it. 

References:

Eck, D. J. (2019, July). Introduction to Programming Using Java (Version 8.1). Accessed 13th  October 2023

Converting from Celsius to Fahrenheit and vice versa  https://codereview.stackexchange.com/questions/119423/converting-from-celsius-to-fahrenheit-and-vice-versa Accessed 16th October 2023

Java - Fahrenheit to Celsius (and vice versa) Tutorial! Temperature Calculator in Java and Eclipse!:

Java - Fahrenheit to Celsius (and vice versa) Tutorial! Temperature Calculator in Java and Eclipse!: Accessed 18th October 2023         

Java String Methods: Java String Reference (w3schools.com) Accessed 18th October 2023

Difference between next() and nextLine() methods in Java: Difference between next() and nextLine() methods in Java - GeeksforGeeks: Accessed 14th October 2023

Try catch and user input: java - Try catch and user input - Stack Overflow: Accessed 13th October 2023

Variable Might Not Have Been Initialized: Grab the Quick Fix

Variable Might Not Have Been Initialized: Grab the Quick Fix (positioniseverything.net): Accessed 17th October 2023

Scanner is skipping nextLine() after using next() or nextFoo()? java - Scanner is skipping nextLine() after using next() or nextFoo()? - Stack Overflow: Accessed 17th October 2023.

Assignment 3 Reflection:

Problem 3: An Introduction to Computer Programming

This program converts temperature from Fahrenheit to Celsius and vice versa.

 

This challenge question is mapped to the following learning objectives:

★   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 – partly covered.

★   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.

This program uses static methods to perform the actual conversion. It uses two static methods. One to convert F to C, and one to convert C of F. The main method uses the for loop to iterate over the range of Fahrenheit temperatures and prints out corresponding Celsius temperatures and vice versa. The printf method is used to format the output with 10 spaces per temperature, and it is displays three digits after the decimal point for both Fahrenheit and Celsius and vice versa. The program does not require any user input.

I approached this challenge by first declaring all the variables needed in order for the task to be completed.  I created a draft outline of how the program might look like using the outline of the sample run that was provided in the assignment brief.  The formula used for conversion was also provided in the brief in problem 1, therefore this was mapped onto the outline of the program before an attempt to solve the problem.

I encountered two main challenges while completing this task.  One of these was the fact that I had to properly articulate how to use static methods, the section of the program that actually produces the results.  I in addition to reading the course text had to go online to view quite a couple of examples on how the static method works in other to be able to get my program to work. The websites I visited are outlined in the reference section below. 

Another challenge I encountered here was to fully articulate how to use the System.out.format to display formatted numerical output in the format that was required by the project brief (printf method). Again, this required a lot of code modification in the main method section to get this done.

References:

  1.  Eck, D. J. (2019, July). Introduction to Programming Using Java (Version 8.1). Accessed 13th  October 2023.

 

  1. Ellis Danielle: What is Static Method in Java [+Examples]?: https://blog.hubspot.com/website/static-method-java#:~:text=Static%20methods%20can%20be%20used,in%20which%20they%20are%20defined.Accessed4th November 2023.
  2.  Lee Alex: Static Method In Java Tutorial #76 https://www.youtube.com/watch?v=xKZ3sMimQYM :Accessed 4th November 2023.
  3. Neso Academy: Static Variables and Static Methods in Java: Static Variables and Static Methods in Java - YouTube Accessed 4th November 2023
  4. Smith James: Temperature Conversion Example: Creating a Class and Calling it from your Main Method in Java: https://youtu.be/5sZOLDBfPZ0 Accessed 4th November 2023.
  5. Static Class Methods | Java | Tutorial 33: https://www.youtube.com/watch?v=bvNU24rvhgA Giraffe Academy: Accessed 4th November 2023.
  6. thenewboston Java Programming Tutorial - 46 – Static: https://www.youtube.com/watch?v=Mhxp5dZOy78: Accessed 4th November 2023

 

Assignment 4 Reflection:

Problem 4: An Introduction to Computer Programming

This program creates a basic help menu for some Java fundamental control flow structures.

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 – partly covered.

★   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.

The five main control flow structures defined in this program include the following

  • If Statement
  • Switch Statement: 
  • While Loop: 
  • For Loop: 
  • Do-While Loop: 

They allow developers to control the flow of execution based on conditions and repetitions.

The system will incorporate a menu system that interacts with the user, and the appropriate help topic will be displayed, based on an input selection.

The program includes a looping structure to both receive user input and process it until the user wishes to quit. A switch structure is used to handle the input and display the appropriate help. When the program is run, the user is able to select between options 1 to 5 for a brief definition of any of the control flow structures outlined above. To exit the program, the user selects the letter ‘x’.

As mentioned above, a switch structure is the control flow method used for the program and a ‘while’ loop is the condition control structure used to ensure that as long as an appropriate option is selected by the user, the loop keeps going until there is a request to terminate program.  

 

In this program, because I was going to deal with menu selections that involve both the use of numbers and letters, I decided to use the String data type to declare my menu selection variable.  This allowed me to handle a combination of numeric and alphanumeric input without any issues. 

Writing this program using a ‘switch statement’ gave me a better understanding of the importance of the use of the ‘break statement’.  I now understand what a ‘fall-through behaviour’ is as it relates to a switch statement. (occurs when the control flow continues to the next case statement even after a matching case has been found) and also now understand that the break statement in a switch statement is essential for controlling the flow of execution, preventing fall-through behavior, ensuring exclusive execution of the correct case, and improving code readability. 

In my program, I used a ‘public modifier’ so that my class, method or field was accessible from other classes. The exit key assigned the value of ‘x’ was declared as a constant so that its value cannot be changed after it is assigned.  

In the draft version of my program, one of the errors that I encountered was that when a selection was made and output displayed, the selection menu would display in addition to the required output.  This challenge was solved by using the new line syntax at the beginning of each ‘switch statement’. I also encountered a bit of a challenge with the incorrect usage of double quotes while writing the definition of one of the control loops and after some research now understand that in Java if a double quote is to be included within a string, that they should be escaped using the backslash \ character. E.g. \"do\"

All in all, this program provides a simple text-based menu system for obtaining help information about various control structures in Java. Users can make selections, and the program responds by displaying relevant information or exiting. 

References:

1. Eck, D. J. (2019, July). Introduction to Programming Using Java (Version 8.1). Accessed 15th November 2023.

2. Lee Alex: Switch Case in Java #35: https://youtu.be/O4KGYGQvHmw:Accessed 14th November 2023.

3. w3Schools: https://www.w3schools.com/java/java_ref_string.asp: Accessed November 15th 2023.

4. Zhang Cuihua: Chapter 6: switch Statement Inside a While loop https://youtu.be/HIusvX5JSzg: Accessed November 14th 2023.

Assignment 5 Reflection:

Problem 5: An Introduction to Computer Programming

This program uses two nested FOR loops and a modulus function to print out PRIME numbers from a range of given numbers: 1 – 10,000.

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 – partly covered.

★   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.

My program was created to use two methods. The main method is the core entry point of the program and is used to define the first FOR loop that declares and iterates through the range of numbers given in the assignment brief.  This method then calls the second method and print out all the prime numbers within the given range if the criteria set in the isPrime method (my second method in the program) is met.

I choose to use the private modifier for my second method, the isPrime method because it aligns with the principles of encapsulation and information hiding. It also prevents unintended external usage of internal methods.

This program uses two nested for loops and a modulus operator (%) to detect and print all prime numbers from 1 to 10,000.  No user input is required in this program.

For me the main challenge I encountered with this program was the reasoning behind the check for divisibility in the second FOR loop however the YouTube video clip by Coding with John: https://youtu.be/I9-3drnfyp0  was very useful in explaining how this works. 

After all checks were done, all required prime numbers found between given range are then displayed to console using the Println method.

Key points to note:

  • A prime number is a whole number that is only divisible by 1 and itself.  They are defined as positive integers greater than 1
  • Some examples of prime numbers include: 2, 3, 5, 7, 11, 13, 17 etc.

For example:

  • 2 is a prime number because its only divisors are 1 and 2.
  • 3 is a prime number because its only divisors are 1 and 3.
  • 4 is not a prime number because it is divisible by 1, 2, and 4.
  • 5 is a prime number because its only divisors are 1 and 5.
  • 6 is not a prime number because it is divisible by 1, 2, 3, and 6.

Reference/s:

  1. Coding with John: Find Prime Numbers In Java - Full Walkthrough with Source https://youtu.be/I9-3drnfyp0 : Accessed 20th November 2023.
  2. Eck, D. J. (2019, July). Introduction to Programming Using Java (Version 8.1). Accessed 15th November 2023.

Some key skills learnt in assignment 1.

 While working on this assignment, I learnt a lot of skills however two of these are outlined below:

Switch

While working on assignment 1 which I literarily just completed, I was curious as to how to decide when the various access modifiers can be used while writing script and therefore looked into the concept of encapsulation a bit.  Although I am still learning about it, some of the key things that I was able to pick up as it relates with this subject matter is that encapsulation is one of the four fundamental Object-Oriented Programming (OOP) concepts, others include inheritance, polymorphism, and abstraction.

It refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit known as a class. The class acts as a blueprint for creating objects, which are instances of the class.

It promotes the principle of information hiding, making it easier to manage and maintain code by restricting access to the internal implementation details of a class and exposing only what is necessary for the outside world to interact with the object.

Break

After completing assignment 1, another key concept that I now have a better understanding of is the 'break' statement, when used with the 'switch' statement. I now know that this statement is next to none which coding with the switch statement because it is essential for:

  1. controlling the flow of execution,
  2. preventing fall-through behavior,
  3. ensuring exclusive execution of the correct case, and
  4. improving code readability in a switch statement.

For example, once a once a case is matched and its code block is executed, the break statement is used to exit the switch statement, preventing the subsequent case blocks from being executed.

It is also worth noting that in the context of a switch statement in programming, "fall through" refers to the behavior where the control flow continues from one case to the next without stopping, even if the subsequent case doesn't explicitly match the evaluated expression. This happens when there is no break statement after a case block.