This repository contains my solutions while taking CSC205 Logic and Design (using Pseudocode, Java, and Affinity Designer) from Colorado State University, Global Campus. I took this required course out of sequence, which is unfortunate. I am two CS courses away from graduation as I am taking this course.
Write pseudocode that performs the following: Ask a user to enter a letter. If the letter is between A and E, write the word sunny. If the letter is between J and P, write the word cloudy. if the letter is between Q and X, write the word rainy. If the input is any other letter, return a statement that it is not a valid option. Then, create a flowchart that correlates to your pseudocode. [Java version] [Java Version Test]
Design a loop (pick one you would like to use) that lets a user enter a number. Have the program multiply the number by 10. Then, store the result in a variable named ‘sales’. The loop should iterate if the input contains a value less than 100. The program should terminate with a response to the user if the value entered is 100 or greater. Be sure to consider infinite loops to put proper precautions in place. Then, create a flowchart that correlates to your pseudocode. [Java Version]
Write pseudocode of an insertion sort algorithm. Illustrate the execution of the algorithm on the array X = {2, 11, 98, 23, 48, 33, 97, 61, 3}, writing the intermediate values of X at each iteration of the algorithm. Then, create a flowchart that correlates to your algorithm. [Java Version] [Java Version Test]
Review the below algorithm that uses sequence, selection, and iteration (repetition) to add all the even numbers in a list and print out the sum. Notice how indentation and curly brackets are used to clarify the different parts of the algorithm.
1 total ← 0 (set total to 0)
2 FOR EACH number IN list
3 {
4 IF(EVEN(number))
5 {
6 total ← total + number (Set total to the current total + number)
7 }
8 }
9 DISPLAY(total)
This algorithm contains examples of all three types of control structures (sequence, selection, and repetition). The lines are numbered for convenience. For your portfolio project, you will be answering the following questions and creating your own pseudocode algorithm and flowchart.
- Which line(s) of the algorithm contains a repetition control structure? Remember a control structure can consist of multiple statements.
- Which line(s) of the algorithm contains a selection control structure?
- If you run this algorithm on the list of numbers (5, 10, -2, -3, 7, 8, 12), what would it print? When tracing through an algorithm, write down the variables (total and number) and act as if you are the computer executing each line of code and change the values of the variables as needed.
- Suppose you had a list of positive numbers such as 5, 10, 12, 13, 6, 7, 1, 3, 2, 1. And suppose for each of the numbers in the list you added the number to a running total if it is even and subtracted it if it is odd, starting the total at 0. What result would you get for this list of numbers?
- Write a pseudocode algorithm that implements the algorithm you used to calculate this total.
- Create a flowchart that correlates to your pseudocode algorithm.
Using the techniques you learned in this course, redesign your Critical Thinking Assignment from Module 2 to make it more complex. Be sure to create a new flowchart. Discuss the changes you made to the project and why you made these changes.
Looking up a word in a dictionary can be a complicated process. For example, assume that you want to look up logic. You might open the dictionary to a random page and see juice. You know this word comes alphabetically before logic, so you flip forward and see lamb. That is still not far enough, so you flip forward and see monkey. You have gone too far, so you flip back, and so on. Draw a structured flowchart or write code [Java] [Python] that describes the process of looking up a word in a dictionary. Describe the thought process that went into creating this flowchart. [Java Test]
Andy Ramlatchan (Teacher) Sep 26 12:49am Reply from Andy Ramlatchan
Hi Stephan, How would your code compare in python to another programming language?
Stephan Peters Edited Sep 26 11:19pm Reply from Stephan Peters
Dr. Ramlatchan,
I rewrote the code in Python, and it looks very similar to the Java version.
It is probably easier to understand as it is more linear, closer to Pseudocode.
Discuss one or two key constructs required to create loops in programs. Identify a scenario that may require two different types of loops. Create a flowchart for each loop and discuss why you used that structure. [Java]
Andy Ramlatchan (Teacher) Oct 3 1:43pm Reply from Andy Ramlatchan
Hi Stephan,
How easy would your program be to program in a traditional object oriented programming language?
Stephan Peters Edited Oct 3 10:45pm Reply from Stephan Peters
Dr. Ramlatchan,
Not really hard at all.
Here is an example with a quick "Number" class I whipped up that acts like a simple calculator. I am still using Java, as I believe Java is the most thought of as an object oriented language.
number in the new main program is a Number object. An object is pretty much just a container. a Number object (created by the Number Class I wrote) is about as simple as it gets, a container that stores one value, an integer, that has some really simple methods that can be performed on the integer stored in the object. The Number object accepts both an int, or a String parseable to an int to store because the constructors are overloaded. If it is instantiated with a String and the String is not parseable, the object will store 0.
Design a class named Cake. Data fields include two string fields for cake flavor and icing flavor and numeric fields for diameter in inches and price. Include methods to get and set values for each of these fields. Create the class diagram and write the pseudo-code that defines the class. What components and attributes will need to be included in your cake class? Why?
Class,
I decided to bake a cake. Instead of using Strings and a number for the fields in the class, I decided to embed enums which can return the strings for the cake and icing, and an int for the diameter.
Enums are freaking awesome. It's like Java's version of a drop-down, and each item in the dropdown can have associated values and methods. This is more the way a real-world scenario would work, except there would be more options, certain cake flavors would go with certain icings, etc. So the user would select the cake from a web interface, then the selection would be crunched in the Java back end in this scenario.
I wish I had been introduced to enums earlier when I was learning Java, and not just as simple lists. As powerful tools that can return values dependent on the selections.
You can play with the program on JDoodle here if you want to.
This is a PlantUML diagram of the class. It shows the embedded enums separately because Java is treating them as separate, even though they are contained in the same source document.
And here is the code:
/** * Represents a cake with different properties such as flavor, icing, and diameter. */ public class Cake { // Define internal enums for flavor, icing, and diameter public enum Flavor { LEMON, CHOCOLATE, PINEAPPLE_UPSIDE_DOWN, CUSTOM; /** * Returns a user-friendly description of the flavor. * @return the formatted string of the flavor name with each first letter capitalized and underscores removed. */ public String getDescription() { return Cake.getDescription(name().toLowerCase().replace('_', ' ')); } } public enum Icing { LEMON_GLAZE, CHOCOLATE, WHIPPED_CREAM_FROSTING, CUSTOM; /** * Returns a user-friendly description of the icing. * @return the formatted string of the icing name with each first letter capitalized and underscores removed. */ public String getDescription() { return Cake.getDescription(name().toLowerCase().replace('_', ' ')); } } public enum Diameter { SIX(6), EIGHT(8), TEN(10), TWELVE(12); private final int value; Diameter(int value) { this.value = value; } /** * Returns the value of the diameter. * @return the integer value of the diameter. */ public int getValue() { return value; } } // Instance variables for flavor, icing, and diameter private Flavor flavor; private Icing icing; private Diameter diameter; /** * Constructs a new Cake object with the given flavor, icing, and diameter. * @param flavor the flavor of the cake. * @param icing the icing of the cake. * @param diameter the diameter of the cake. */ public Cake(Flavor flavor, Icing icing, Diameter diameter) { this.flavor = flavor; this.icing = icing; this.diameter = diameter; } /** * Capitalizes the first letter of each word in the given enum name * and removes underscores to be more user-friendly. * @param name the enum name to be capitalized and formatted * @return the formatted string of the enum name with each first letter capitalized and underscores removed. */ private static String getDescription(String name) { char[] chars = name.toCharArray(); boolean capitalize = false; for (int i = 0; i < chars.length; i++) { if (!capitalize && Character.isLetter(chars[i])) { chars[i] = Character.toUpperCase(chars[i]); capitalize = true; } else if (Character.isWhitespace(chars[i])) { capitalize = false; } } return String.valueOf(chars); } // Gets the flavor of the cake. public Flavor getFlavor() { return flavor; } // Gets the icing of the cake. public Icing getIcing() { return icing; } // Gets the diameter of the cake. public Diameter getDiameter() { return diameter; } /** * Returns a string representation of the Cake object in a user-friendly format. * Overrides the Java Object.toString() method, which would return an unreadable reference to the object. * @return the string representation of the Cake object. */ @Override public String toString() { return String.format("Cake: %nFlavor: %s%nFrosting: %s%nDiameter: %d%n", flavor.getDescription(), icing.getDescription(), diameter.getValue(), "\""); } /** * Main method to demonstrate the usage of the Cake class. * @param args the command line arguments (not used). */ public static void main(String[] args) { Cake cake = new Cake(Flavor.PINEAPPLE_UPSIDE_DOWN, Icing.WHIPPED_CREAM_FROSTING, Cake.Diameter.TEN); System.out.println(cake); } }And this is the output from running the Main example method:
Cake: Flavor: Pineapple Upside Down Frosting: Whipped Cream Frosting Diameter: 10 Process finished with exit code 0-Have Fun!
-Stephan Peters
