Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Learning Guide Unit 2

Site: University of the People Printed by: Somtochukwu Onyekwelu


Course: CS 1102-01 Programming 1 - AY2024-T3 Date: Monday, 12 February 2024, 9:58 AM
Book: Learning Guide Unit 2
Description

Learning Guide Unit 2


Table of contents

Overview
Introduction
Reading Assignment
Discussion Forum
Programming Assignment
Self-Quiz
Checklist
Overview

Unit 2: Control Structures

Topics:

Looping Control Structures (While, Do-While, and For)


Advanced control structures (nested loops, break, continue)
Exception handling (try-catch, finally)

Learning objectives:

By the end of this Unit, you will be able to:

1. Differentiate between various control structures, including conditional statements (if-else, switch) and looping structures (for,
while, do-while).
2. Develop and implement solutions incorporating nested control structures for complex scenarios.
3. Apply error-handling techniques using control structures to address anticipated errors and exceptional situations.

Tasks:

Read through the Learning Guide and the Reading Assignment


Complete the Discussion Assignment by posting in the Discussion Forum
Respond to three of your fellow classmates’ posts in the Discussion Forum
Complete and submit the Programming Assignment
Take and submit the Self-Quiz
Introduction

In the realm of Java programming, the effective utilization of looping control structures, such as the while, do-while, and for loops, play a
fundamental role in executing repetitive tasks with ease and efficiency. These structures enable the program to iterate over a set of
instructions until a specified condition is met. Moreover, a more sophisticated control structure, the nested loop, empowers
programmers to construct intricate patterns and perform complex computations. Additionally, the employment of break and continue
statements within loops allows for selective termination or continuation of loop iterations based on specific conditions.

Furthermore, to ensure robust and error-free code execution, the implementation of exception handling mechanisms becomes crucial.
By incorporating try-catch blocks, Java developers can anticipate and gracefully handle exceptional scenarios, preventing abrupt
program termination. The inclusion of the final block ensures that critical cleanup operations are executed, irrespective of whether an
exception occurs or not. Mastery over these control structures and exception handling techniques enhances code efficiency, readability,
and maintainability, making them indispensable skills for Java programmers.
Reading Assignment

As you read through the learning resource consider the following:

How do looping control structures, such as while, do-while, and for loops, contribute to the execution of repetitive
tasks in Java programs?
What is the purpose of break and continue statements in Java loops, and how do they affect the flow of program execution?
How can mastery over control structures and exception handling techniques enhance code efficiency, readability, and
maintainability in Java programming?

Textbook:

Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed under CC 4.0.

In Chapter 3 – Programming in the small II: Control; Read 3.3(The While and do... while statements), 3.4 (The for
Statement), and 3.7 (Introduction to Exceptions and try...catch)

Watch

1. Coding with John. (2020, December 31). Exception handling in Java tutorial [Video]. YouTube.

This video elaborates about Exception Handling in Java and its extensive applications in program coding.

2. Neso Academy. (2020a, March 18). The Do-while loop in Java [Video]. YouTube.

This video gives a brief idea about Do-While loops and its difference between While loop.
3. Neso Academy. (2020b, March 21). Break and continue keywords in Java [Video]. YouTube.

This video explains about the usage of Break and Continue keywords and the benefits of using them.
Discussion Forum

For this discussion assignment, please provide your response to each of the below questions in a minimum of 350 words and a
maximum of 400 words.

1. Discuss the nuances, practical implications, and application scenarios of the three looping control structures in Java: while, do-
while, and for. Explain how each of these control structures can be effectively used to solve various programming challenges.
Highlight situations where one looping structure is more suitable than the others and provide an overview of the advantages and
disadvantages of each.
2. In the context of Java programming, compare and contrast the usage of if-else statements and switch statements. Analyze the
benefits and drawbacks of each approach with regards to factors such as readability, maintainability, performance, and code
organization. Share your personal experiences and provide real-world examples where you have encountered situations where
one approach was more effective or efficient than the other. Furthermore, explore scenarios where a combination of if-else and
switch statements can be employed to achieve the desired outcome.

Following the APA standard, use references and in-text citations for the textbook and any other sources.

You instructor will grade you based on the Discussion Forum Unit 2 Rubric.

NOTE: Your instructor will be assessing your posts/responses for this discussion.
Programming Assignment

You are tasked with implementing a basic library system in Java. The program should allow users to add books to the library, borrow
books, and return books. The system maintains a record of the available quantity of each book.

Question:

Write a Java program that accomplishes the following tasks:

1. Implement the following options:

Add Books
Borrow Books
Return Books
Exit

2. For "Add Books":

Prompt the user to enter the book title, author, and quantity.
If the book already exists in the library, update the quantity.
If the book is new, add it to the library.

3. For "Borrow Books":

Prompt the user to enter the book title and the number of books to borrow.
Check if the requested number of books is available in the library.
If the books are available, update the quantity and display a success message.
If not, display an error message.

4. For "Return Books":

Prompt the user to enter the book title and the number of books to return.
Check if the books being returned belong to the library system.
If they do, update the quantity and display a success message.
If not, display an error message.

5. Handle invalid input and display appropriate error messages.

6. Implement an exit option to allow the user to exit the program.

Remember to use appropriate variable names and follow coding best practices.

Submit the assignment in MS Word or PDF file format. Your submission should contain the program code, explanation, and output
screenshot

This assignment will be assessed by your instructor using the rubric available on the assignment page in course homepage.
Self-Quiz

The Self-Quiz gives you an opportunity to self-assess your knowledge of what you have learned so far.

The results of the Self-Quiz do not count towards your final grade, but the quiz is an important part of the University’s learning process
and it is expected that you will take it to ensure understanding of the materials presented. Reviewing and analyzing your results will help
you perform better on future Graded Quizzes and the Final Exam.

Please access the Self-Quiz on the main course homepage; it will be listed inside the Unit.
Checklist

Read through the Learning Guide and the Reading Assignment


Complete the Discussion Assignment by posting in the Discussion Forum
Respond to three of your fellow classmates’ posts in the Discussion Forum
Complete and submit the Programming Assignment
Take and submit the Self-Quiz

You might also like