1
1
Task 1.............................................................................................................................................................2
Part 1..........................................................................................................................................................2
Identifying Variables and Datatypes......................................................................................................2
Pseudocode............................................................................................................................................3
Flowchart...............................................................................................................................................4
Input Validation and Logical Errors......................................................................................................5
Part2...........................................................................................................................................................6
Java Program Code................................................................................................................................6
Program Execution and Testing.............................................................................................................9
Task2............................................................................................................................................................11
Part 1........................................................................................................................................................11
Pseudocode..........................................................................................................................................11
Flowchart.............................................................................................................................................12
Input Validation and Logical Errors....................................................................................................13
Part 2........................................................................................................................................................14
Java Program Code..............................................................................................................................14
Program Execution and Testing...........................................................................................................19
Task3............................................................................................................................................................20
Part 1........................................................................................................................................................20
Pseudocode..........................................................................................................................................20
Flowchart.............................................................................................................................................21
Input Validation and Logical Errors....................................................................................................22
Part 2........................................................................................................................................................23
Java Program Code..............................................................................................................................23
Program Execution and Testing...........................................................................................................32
Task 1
Part 1
Identifying Variables and Datatypes
To proceed with design and development of java program, the first was to identify variables and
datatypes, these are listed in the table below(Alhefdhi et al., 2018).
Below is the Pseudocode according to the task 1 scenario and above identified variables and
datatypes(Alokla et al., 2022a).
START
DO
READ choice
IF choice == 1 THEN
CALL orderDetailsAndPriceCalculation()
CALL summaryStatistics()
ELSE
END IF
WHILE choice != 3
END
The pseudocode starts with displaying the menu options. It then enters a loop where it prompts the user to
enter their choice(Alokla et al., 2022b). Based on the user's input, it calls the appropriate method:
If the user selects option 1, it calls orderDetailsAndPriceCalculation().
If the user selects option 2, it calls summaryStatistics().
If the user selects option 3, it displays an exit message and breaks out of the loop.
If the user enters an invalid choice, it displays an error message and prompts the user again.
Flowchart
Shown Below is a flowchart based on pseudocode that shows proper flow of the program-code.
The flowchart starts at the "Start" node and immediately displays the menu options. It then prompts the
user for their choice. Depending on the user's input, the flowchart follows different paths:
For choice 1, it calls the orderDetailsAndPriceCalculation() function and then goes back to
displaying the menu.
For choice 2, it calls the summaryStatistics() function and then goes back to displaying the menu.
For choice 3, it displays an exit message and ends the program.
For any other choice, it displays an error message and goes back to displaying the menu.
Input Validation and Logical Errors
User Input Validation: User inputs for the menu options are validated by ensuring the input
matches one of the predefined choices (1, 2, or 3). This is done through a conditional check (if-
else statements) that re-prompts the user if an invalid choice is entered. The int data type is used
for choice as it effectively represents numeric menu options and allows for easy comparison.
Logical Errors Handling: Logical errors are handled by maintaining clear and concise control
flow. Each valid menu option directly leads to the execution of a specific method
(orderDetailsAndPriceCalculation() or summaryStatistics()), and invalid inputs trigger an error
message without affecting the overall program state. The use of loops ensures the menu is
continuously displayed until the user opts to exit, preventing premature termination or infinite
loops due to invalid inputs.
Part2
Java Program Code
import java.util.Scanner;
do {
displayMenu();
switch (choice) {
case 1:
orderDetailsAndPriceCalculation();
break;
case 2:
summaryStatistics();
break;
case 3:
System.out.println("Exiting program...");
break;
default:
System.out.println("3. Exit.");
}
The Java program is designed to display a menu for a flower shop and execute corresponding methods
based on user input(Pascarella et al., 2019). Here's a brief explanation of the program:
1. Main Method:
o The main method uses a Scanner object to read user input.
3. Placeholder Methods:
o orderDetailsAndPriceCalculation() and summaryStatistics() are placeholder methods that
currently only print a message to the console. These methods will be fully implemented
in later tasks.
Program Execution and Testing.
Here are the screenshots of the program execution showing the correct execution of all menu options:
1. Initial Menu Display:When the program starts, it displays the menu and waits for the user's
input.
3. Option 2 Selected:When the user selects option 2, the program calls summaryStatistics() and
displays "Summary statistics provided."
4. Option 3 Selected:When the user selects option 3, the program displays "Exiting program..." and
terminates(Compton et al., 2020).
Task2
Part 1
Pseudocode
START
PROMPT "Enter flower type (1. Rose, 2. Lily, 3. Carnations, 4. Daffodil, 5. Gerbera, 6.
Chrysanthemum, 7. Assorted): "
READ flowerType
PROMPT "Enter flower color (1. White, 2. Red, 3. Pink, 4. Yellow, 5. Blue, 6. Mixed): "
READ flowerColor
READ bouquetSize
RETURN to menu
END
Explanation of Pseudocode
Prompts and Reads: The program prompts the user to enter the flower type, flower color, and
bouquet size, then reads these inputs(Cok et al., 2022).
Calculate Price: It calculates the bouquet price using the formula provided in the assignment.
Store Order Details: The order details, including the price, are stored in a suitable data structure.
Display Price: The program displays the calculated price of the bouquet.
Return to Menu: The program returns to the main menu.
Flowchart
The flowchart for Task 2 demonstrates the process of collecting user input for the flower type, flower
color, and bouquet size, calculating the bouquet price, storing the order details, and displaying the
calculated price(Andrzejewska and Stolińska, 2022).
Input Validation and Logical Errors
User Input Validation: Each input is validated to ensure it falls within the acceptable range of
values (e.g., 1 to 7 for flower type). If an invalid input is detected, the program should prompt the
user to enter a valid value. This prevents logical errors and ensures the program behaves as
expected(Amarasingha et al., 2018).
Order Details Array: A fixed array is used to store order details. Each order is stored as an array
of strings, with each element representing a different attribute (flower type, flower color, bouquet
size, bouquet price)
Part 2
Java Program Code
import java.util.Scanner;
private static final double[] FLOWER_PRICES = {1.2, 1.3, 1.0, 1.0, 1.1, 1.1, 0.8};
private static final String[] FLOWER_TYPES = {"Rose", "Lily", "Carnations", "Daffodil", "Gerbera",
"Chrysanthemum", "Assorted"};
private static final double[] COLOR_PRICES = {1.3, 1.2, 1.1, 1.1, 1.2, 1.0};
private static final String[] COLOR_TYPES = {"White", "Red", "Pink", "Yellow", "Blue", "Mixed"};
int choice;
do {
displayMenu();
choice = scanner.nextInt();
switch (choice) {
case 1:
orderDetailsAndPriceCalculation(scanner);
break;
case 2:
summaryStatistics();
break;
case 3:
System.out.println("Exiting program...");
break;
default:
scanner.close();
System.out.println("3. Exit.");
}
// Method for ordering details and price calculation
System.out.print("Enter flower color (1. White, 2. Red, 3. Pink, 4. Yellow, 5. Blue, 6. Mixed): ");
orderDetails[orderCount][0] = FLOWER_TYPES[flowerType];
orderDetails[orderCount][1] = COLOR_TYPES[flowerColor];
orderDetails[orderCount][2] = SIZE_TYPES[bouquetSize];
orderCount++;
while (true) {
input = scanner.nextInt();
break;
} else {
System.out.print("Invalid input. Please enter a number between " + min + " and " + max + ": ");
return input;
5. Input Validation: The validateInput method ensures that user inputs are within the valid range,
prompting the user again if they are not(Gad et al., 2022).
Program Execution and Testing.
Here are the outputs when the program is executed:
1. Initial Menu Display:
DISPLAY statistics
END
Flowchart
Input Validation and Logical Errors
User Input Validation
1. Menu Choice Selection:
Validation: The user is prompted to enter a choice from the menu options (1, 2, or 3).
Input validation ensures that the user's choice falls within the valid range(Kaur and
Nayyar, 2020).
Implementation: The choice variable is an int and is validated using a while loop that
keeps prompting the user until a valid choice is entered.
Justification: Using int for choice is appropriate because the menu options are numeric
and can be easily compared using integer comparison.
2. Bouquet Order Details:
Validation: When ordering a bouquet, the user must select the type of flower, flower
color, and bouquet size. Each input is validated to ensure it falls within the valid range.
Implementation: The validateInput method is used to ensure that the user input for
flower type, color, and size is within the specified range(Pierce, 2024).
Justification: Using int for user inputs related to flower type, color, and size is
appropriate because these are categorical choices mapped to numeric codes.
3. Logical Error Checking
a) Ensuring Valid Array Indices:
Validation: When accessing the orderDetails array, indices are validated to ensure they
are within bounds.
Justification: Ensuring valid indices prevents ArrayIndexOutOfBoundsException and
ensures that the correct data is being accessed and modified.
b) Handling Empty Orders:
Validation: Before performing summary statistics, the program checks if there are any
orders to process(Kouzapas et al., 2018).
Justification: This prevents division by zero errors when calculating the average price
and ensures meaningful statistics are displayed.
c) Accumulating Correct Data:
Validation: During the loop that calculates summary statistics, the program ensures that
each order's data is correctly accumulated into the statistics variables(Kulal et al., 2019).
Justification: Ensuring correct accumulation prevents logical errors in the final statistics
output.
Part 2
Java Program Code
import java.util.Scanner;
private static final double[] FLOWER_PRICES = {1.2, 1.3, 1.0, 1.0, 1.1, 1.1, 0.8};
private static final String[] FLOWER_TYPES = {"Rose", "Lily", "Carnations", "Daffodil", "Gerbera",
"Chrysanthemum", "Assorted"};
private static final double[] COLOR_PRICES = {1.3, 1.2, 1.1, 1.1, 1.2, 1.0};
private static final String[] COLOR_TYPES = {"White", "Red", "Pink", "Yellow", "Blue", "Mixed"};
int choice;
addSampleData();
do {
displayMenu();
choice = scanner.nextInt();
switch (choice) {
case 1:
orderDetailsAndPriceCalculation(scanner);
break;
case 2:
summaryStatistics();
break;
case 3:
System.out.println("Exiting program...");
break;
default:
scanner.close();
System.out.println("3. Exit.");
System.out.print("Enter your choice: ");
System.out.print("Enter flower color (1. White, 2. Red, 3. Pink, 4. Yellow, 5. Blue, 6. Mixed): ");
orderDetails[orderCount][0] = FLOWER_TYPES[flowerType];
orderDetails[orderCount][1] = COLOR_TYPES[flowerColor];
orderDetails[orderCount][2] = SIZE_TYPES[bouquetSize];
orderCount++;
}
// Validate input method
int input;
while (true) {
input = scanner.nextInt();
break;
} else {
System.out.print("Invalid input. Please enter a number between " + min + " and " + max + ": ");
return input;
if (orderCount == 0) {
return;
// Calculate statistics
totalPrice += price;
minPrice = price;
maxPrice = price;
if (orderDetails[i][2].equals(SIZE_TYPES[j])) {
sizeIndex = j;
break;
if (orderDetails[i][0].equals(FLOWER_TYPES[j])) {
flowerIndex = j;
break;
}
if (orderDetails[i][1].equals(COLOR_TYPES[j])) {
colorIndex = j;
break;
sizeFrequency[sizeIndex]++;
flowerFrequency[flowerIndex]++;
colorFrequency[colorIndex]++;
// Display statistics
System.out.println("Summary Statistics:");
}
System.out.println("Frequency of each flower type:");
orderCount = 7; // Set the initial order count to 7 based on the sample data
Class Variables:
FLOWER_PRICES, FLOWER_TYPES, COLOR_PRICES, COLOR_TYPES,
SIZE_MULTIPLIERS, and SIZE_TYPES are defined as constants to store flower prices, types,
color prices, types, size multipliers, and sizes(Li et al., 2019).
orderDetails is a 2D array used to store the details of each bouquet order.
orderCount keeps track of the number of orders.
Main Method:
Initializes the Scanner for user input.
Adds sample data to orderDetails array.
Displays the menu and processes user input until the user decides to exit.
displayMenu Method:
Displays the menu options to the user.
orderDetailsAndPriceCalculation Method:
Prompts the user to enter the flower type, color, and size.
Validates user input using the validateInput method.
Calculates the price of the bouquet based on the input.
Stores the order details in the orderDetails array.
Displays the price of the bouquet.
validateInput Method:
Ensures the user input is within the valid range and prompts the user again if it is not.
summaryStatistics Method:
Initializes variables to store statistics.
Loops through each order to calculate the minimum price, maximum price, total price, average
price, and the frequency of each bouquet size, flower type, and flower color(Michail et al., 2020).
Displays the calculated statistics.
addSampleData Method:
Adds sample data to the orderDetails array for testing purposes.
Program Execution and Testing.
Initial Menu Display:
Summary Statistics:
Exiting Program:
References
ALHEFDHI, A., DAM, H. K., HATA, H. & GHOSE, A. Generating pseudo-code from source code using
deep learning. 2018 25th Australasian Software Engineering Conference (ASWEC), 2018. IEEE,
21-25.
ALOKLA, A., GAD, W., NAZIH, W., AREF, M. & SALEM, A.-B. 2022a. Pseudocode generation from
source code using the bart model. Mathematics, 10, 3967.
ALOKLA, A., GAD, W., NAZIH, W., AREF, M. & SALEM, A.-B. 2022b. Retrieval-based transformer
pseudocode generation. Mathematics, 10, 604.
AMARASINGHA, T., PITIPANA, H., LANKA, S. & RANAWEERA, R. PseudoJ: A Pseudo-code
interpreter for transforming Pseudo-code into JAVA. 1st INTERNATIONAL CONFERENCE
ON BUSINESS INNOVATION, 2018. 7.
ANDRZEJEWSKA, M. & STOLIŃSKA, A. 2022. Do structured flowcharts outperform pseudocode?
Evidence from eye movements. IEEE Access, 10, 132965-132975.
COK, D. R., LEAVENS, G. T. & ULBRICH, M. 2022. Java Modeling Language (JML) Reference
Manual.
COMPTON, R., FRANK, E., PATROS, P. & KOAY, A. Embedding java classes with code2vec:
Improvements from variable obfuscation. Proceedings of the 17th International Conference on
Mining Software Repositories, 2020. 243-253.
FERENC, R., TÓTH, Z., LADÁNYI, G., SIKET, I. & GYIMÓTHY, T. A public unified bug dataset for
java. Proceedings of the 14th international conference on predictive models and data analytics in
software engineering, 2018. 12-21.
GAD, W., ALOKLA, A., NAZIH, W., AREF, M. & SALEM, A.-B. 2022. DLBT: Deep Learning-Based
Transformer to Generate Pseudo-Code from Source Code. Computers, Materials & Continua, 70.
KAUR, A. & NAYYAR, R. 2020. A comparative study of static code analysis tools for vulnerability
detection in c/c++ and java source code. Procedia Computer Science, 171, 2023-2029.
KOUZAPAS, D., DARDHA, O., PERERA, R. & GAY, S. J. 2018. Typechecking protocols with Mungo
and StMungo: A session type toolchain for Java. Science of Computer Programming, 155, 52-75.
KULAL, S., PASUPAT, P., CHANDRA, K., LEE, M., PADON, O., AIKEN, A. & LIANG, P. S. 2019.
Spoc: Search-based pseudocode to code. Advances in Neural Information Processing Systems, 32.
LI, Y., TAN, T. & XUE, J. 2019. Understanding and analyzing java reflection. ACM Transactions on
Software Engineering and Methodology (TOSEM), 28, 1-50.
MICHAIL, D., KINABLE, J., NAVEH, B. & SICHI, J. V. 2020. JGraphT—A Java library for graph data
structures and algorithms. ACM Transactions on Mathematical Software (TOMS), 46, 1-29.
ORTEGA, F., ZHU, B., BOBADILLA, J. & HERNANDO, A. 2018. CF4J: Collaborative filtering for
Java. Knowledge-Based Systems, 152, 94-99.
PASCARELLA, L., BRUNTINK, M. & BACCHELLI, A. 2019. Classifying code comments in Java
software systems. Empirical Software Engineering, 24, 1499-1537.
PIERCE, B. C. 2024. Advanced topics in types and programming languages, MIT press.