logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    William Li
    Sep 16, 2021

    9/15 Algorithms, Pseudocode, and printf()

    in Introduction to Java

    Today in class, we learned about:

    - Algorithms and pseudo-code. When solving a problem using code, we need steps to solve it, and this list of steps is an algorithm. An example is a morning routine:


    function morningRoutine() {
        getOutOfBed();
        while (hair == messy) {
            combHair();
        }
        takeShower();
        eatBreakfast();
        brushTeeth();
        leaveForSchool();
    }

    Note that this is written in Pseudo-code, which is talked about below:


    Pseudo-code is written in the style of code, but won't run properly in an actual IDE. This is a great way to understand the code you're writing and can be a first step in developing an effective algorithm. In the code I wrote above, each "function" within the morningRoutine function is an activity one would normally do to get ready for school. There is no correct way to write pseudo-code, but it's best to write in a style that you and other people will understand later. You can, however, write it in the style of a programming language, and in the example above it looks similar to Javascript.


    - System.out.printf()

    We learned about System.out.print() and System.out.println() last class, but this week we learned about a new function, printf(). This lets you format the output you want. For example:

    System.out.printf("%.2f", Math.pi);

    Will print out π to 2 decimal places.


    You can find references for the printf() function here: https://docs.oracle.com/javase/tutorial/java/data/numberformat.html


    When using print functions, special characters are reserved. For example, " is reserved because it is used to tell the compiler that the phrase you want to output is finished. In order to actually output a ", you must use an escape character, \. The backslash itself is a reserved character, which means to print it out, you need to use \\. More escape sequences can be found at https://www.tutorialspoint.com/escape-sequences-in-java.


    3 comments
    0
    cloudiiiday
    Sep 17, 2021

    Here is the homework for this week's class: https://docs.google.com/forms/d/e/1FAIpQLSdkhs7YSmO_r0W1Jp94R5O-M2q1G8dFWy3bktr2JEth__w9IQ/viewform?usp=sf_link.


    I will post the solution to the homework before next class starts.

    0
    cloudiiiday
    Sep 22, 2021

    This is the solution for the homework: https://replit.com/@liizz/915-HW-Solution#Main.java

    0
    leosong23
    3d

    website testing

    0
    3 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners