Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • null : 16

    • Algorithms and Strings : 15

    • Strings : 14

    • Functions and Algorithms : 13

    • Practice with Functions : 12

    • More About Functions : 11

    • Errors and Debugging : 10

    • Functions : 9

    • Practice with Loops and Algorithms : 8

    • Algorithms I : 7

    • Loops : 6

    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Algorithms and Strings

    String bestCourse(String[] courses) {
    for (String course : courses) {
    if (course.contains("124")) {
    return course;
    }
    }
    return "";
    }
    System.out.println(bestCourse(new String[] {"CS 124", "ECE 110"}));
    System.out.println(bestCourse(new String[] {"STAT 107", "CS 105"}));

    This lesson is a lot of fun. We’ll integrate what we’ve learned recently about Strings, algortihms, and then functions. And then we’ll get some practice approaching a few new problems using Strings.

    Odds and Ends
    Odds and Ends

    But first, we have a few new things to cover.

    Casting
    Casting

    When we started working with variables and Java’s eight primitive types, we observed that there were certain types of assignments that would fail. For example:

    int i = 10.8; // Can't assign a double literal to an int

    But, there are also other kinds of assignments from one type to another that will succeed:

    double i = 10; // Can assign a int literal to a double

    Let’s discuss why that is, and how to can force Java to perform certain type changes when necessary.

    int i = 10.8;
    double i = 10;

    Strings Are Immutable
    Strings Are Immutable

    One of the important things to understand about Java’s Strings is that none of the methods that we can call on them change that String. Instead, they always return a new String that may be modified in some way. Let’s see that in action:

    String first = "I'm a string!";

    Solve: String Reverse (Practice)

    Created By: Geoffrey Challen
    / Version: 2020.9.0

    Write a function called reverse. It should accept a single String argument and return that String, reversed! There are several ways to approach this problem—so have fun!

    String Algorithms
    String Algorithms

    Now let’s have some fun and write a few new algorithms that work on Strings!

    String Character Search
    String Character Search

    First, let’s try and write a function that determines if a String contains a particular character. We’ll sketch our our algorithm first, explore some potentially useful String methods, and construct and test a solution.

    // Write a function that searches a `String` for a character

    Structured String Parsing
    Structured String Parsing

    Frequently when computers work with text data, we are processing data that was itself created by a machine. Or set up for machines to easily process.

    One example is data stored in comma-separated-value format. Let’s say we wanted to track how many people were tested for some random respiratory illness each day. We might save that data in a String that looked like this:

    2020-09-02,5402
    2020-09-03,12042
    2020-09-04,4637
    2020-09-05,89054
    2020-09-06,2033
    2020-09-07,10238
    2020-09-08,76452
    2020-09-09,8902

    (Note that this is not real data!)

    Let’s experiment with how we might work with this kind of data. We’ll use some existing String methods and a few new ones. We’ll also use another unfamiliar method: Integer.parseInt, which allows us to convert a String to an int.

    // This is a new way of enclosing `Strings` that was just added to Java!
    // The walkthrough talks about it
    String data = """
    2020-09-02,5402
    2020-09-03,12042
    2020-09-04,4637
    2020-09-05,89054
    2020-09-06,2033
    2020-09-07,10238
    2020-09-08,76452
    2020-09-09,8902""";
    System.out.println(data);

    Solve: String Flip Halves

    Created By: Geoffrey Challen
    / Version: 2021.8.0

    Write a method name flipHalves that returns a passed String rearranged as follows. You should move the second half of the string to the front, and the first half to the back. So given the String "CS" you would return "SC", and given the String "testme" you would return "tmetes". However, if the length of the String is odd the middle character should remain in place. So given the String "CS124" you would return "241CS". If the passed String is empty, just return the empty String.

    You will definitely want to review the substring String method, and also spend some time experimenting with it before you begin crafting your solution. You also may want to consider odd and even length Strings separately.

    CS People: Joy Buolamwini
    CS People: Joy Buolamwini

    Joy Buolamwini is a self-described poet of code who, through her art, voice, and research, is drawing attention to important social implications of the widespread use of artificial intelligence. Watch her speak for herself, in words both beautiful and powerful:

    If you have a few more minutes, watch her TED Talk on algorithmic bias—what she calls “the coded gaze”.

    More Practice

    Need more practice? Head over to the practice page.