Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Constructors : 26

    • Objects, Continued : 25

    • Introduction to Objects : 24

    • Compilation and Type Inference : 23

    • Practice with Collections : 22

    • Maps and Sets : 21

    • Lists and Type Parameters : 20

    • Imports and Libraries : 19

    • Multidimensional Arrays : 18

    • Practice with Strings : 17

    • 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

    Objects, Continued

    class Dimensions {
    double width;
    double height;
    double area() {
    return width * height;
    }
    }
    Dimensions room = new Dimensions();
    room.width = 8.8;
    room.height = 10.0;
    System.out.println(room.area());

    Next we continue our exploration of Java objects. Objects combine state and behavior. In the previous lesson we showed how they can store data like variables. Now we’ll show how they can run algorithms like methods.

    Object Methods
    Object Methods

    Last time we began experimenting with simple Java objects. Consider an object that stores information about a room:

    class Room {
    double width;
    double height;
    String name;
    // And you could add more properties here!
    }

    Our Room class allows us to model a Rooms height, width, and name. Let’s create a few instances!

    class Room {
    double width;
    double height;
    String name;
    }
    Room livingRoom = new Room();
    livingRoom.name = "Living Room";
    livingRoom.width = 10.0;
    livingRoom.height = 8.0;
    Room kitchen = new Room();
    kitchen.name = "Kitchen";
    kitchen.width = 6.0;
    kitchen.height = 12.0;
    System.out.println("The "
    + kitchen.name + " is " + kitchen.width + " by " + kitchen.height);
    System.out.println("The "
    + livingRoom.name + " is " + livingRoom.width + " by " + livingRoom.height);

    Cool! But we said that Java objects combine state and behavior. Where’s the behavior?

    To start, let’s see if we can have each room print out the String that we printed manually in the previous example. We’ll go through how to do that together.

    class Room {
    double width;
    double height;
    String name;
    }
    Room livingRoom = new Room();
    livingRoom.name = "Living Room";
    livingRoom.width = 10.0;
    livingRoom.height = 8.0;

    Solve: Guessing Game (Practice)

    Created By: Geoffrey Challen
    / Version: 2021.9.0

    Let's play a guessing game! Complete a method named getSecretValue which is passed an instance of a Secret. The Secret class provides a single method guess which accepts an int parameter. It returns true if you have guessed the secret value, and false otherwise. assert that the passed Secret is not null.

    Write code to determine the secret value between 0 and 31, inclusive. If the secret does not fall in that range, you should return -1.

    However, note that the Secret class will fail if you guess again after you have already guessed the secret value! So as soon as you find the secret, your code should return it and not guess again. Additional guesses will cause your submission to be marked as incorrect.

    Instance Methods
    Instance Methods

    What we’ve created above is called an instance method. In some ways it’s just like the other methods that we’ve written. But, because it is part of a class definition, it is also different.

    Specifically, instance methods have access to the values of instance variables or properties. We saw that in the walkthrough above, since our print function could access that room’s width, height, and name. Let’s continue exploring this together, and look at how instance methods can both access instance variables and accept parameters.

    class Room {
    double width;
    double height;
    String name;
    }

    Modifying Instance Variables
    Modifying Instance Variables

    Instance methods can both access and modify instance variables. Let’s look at example of how that works.

    class Person {
    String name;
    }

    Solve: Map Has Duplicate Values

    Created By: Geoffrey Challen
    / Version: 2021.9.0

    Write a method hasDuplicateValues that, given a non-null Map<String, String>, returns true if the map contains duplicate values—meaning that two different keys map to the same value—and false otherwise. Recall that a map can never contain duplicate keys, since the second mapping from the same key overwrites the first.

    You should use a Set to solve this problem! You do not need to import Map, Set, or HashSet, since these are already provided for you.

    CS People: Marques Brownlee
    CS People: Marques Brownlee

    If you’ve ever found yourself searching for technology product reviews on YouTube, you may have already run across the work of Marques Brownlee. A YouTube technology reviewer with 16 million subscribers, Marques is also a high-level professional Ultimate frisbee player. In the video below he discusses some of the challenges with being a diverse individual in technology with another fairly well-known Black man, Barack Obama:

    More Practice

    Need more practice? Head over to the practice page.