Exercises: Data Types

Getting Started

Work on these exercises in the IntelliJ java-web-dev-projects project. Create a new class for each numbered exercise. You may name the classes whatever you like, but use proper Java Naming Conventions and make sure that the file name matches the class name.

Creating a Package and Classes

Here is how to create a new module to store these exercises, and how to create new classes within this package:

  1. Click on the folder java-web-dev-projects in the Project pane.

  2. Open the package datatypes-exercises.

  3. Within the datatypes-exercises directory create the following directory structure: src/main/java.

  4. Right-click (or control-click for some Mac users) on the java folder and select New and then select Package.

    Note

    You may have to right click on the java directory and mark the directory as “Sources root” in order to create a new package.

Select New then Package to create a new package
  1. Name the new package org.launchcode. Name the new package 'org.launchcode`

  2. Right click on the package and select New and then Java Class.

  3. Name this what you will name your class (for example, in the 1st exercise below, you might name the class InputOutput or Hello). Naming the new class InputOutput to match the theme of the exercise

  4. Repeat steps 7 and 8 to create new classes.

Warning

You will be asked whether you want to add this file to Git. Press the ā€œYesā€ or “Add” button. This is similar to git init and will allow you to stage your work later.

  1. You created the new class! You should see it in the file tree. You can proceed to write code within it. (Donā€™t forget to write the main method!)
File tree now contains the new class named InputOutput

The Exercises

A. Input/Output

Write a new ā€œHello, Worldā€ program to prompt the user for their name and greet them by name.

  1. Follow steps 7 and 8 above to create a new HelloWorld Class inside of your org.launchcode folder.

  2. Add an import statement at the the top of the file to include Scanner:

    import java.util.Scanner;
  3. Declare a variable of type Scanner called input:

    Scanner input = new Scanner(System.in);
  4. Add a question to ask the user:

    System.out.println("Hello, what is your name:");
  5. Create a variable to store the userā€™s response using the Scannerā€™s .nextLine() method

    String name = input.nextLine();
  6. Use concatenation to print the greeting:

    System.out.println("Hello " + name);

Right-click/Control-click the arrow next to your class and run the program.

package org.launchcode;

import java.util.Scanner;

public class HelloWorld {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.println("Hello, what is your name:");

      String name = input.nextLine();
      System.out.println("Hello " + name);
   }
}

B. Numeric Types

Write a program to calculate the area of a rectangle and print the answer to the console. You should prompt the user for the dimensions. (What data types should the dimensions be?)

  1. Follow steps 7 and 8 above to create a new Class inside of your exercises.

  2. Add an import statement at the top of your file to use Scanner.

  3. Add a Scanner object to handle the userā€™s input.

  4. Add a print line to prompt the user for the length of the rectangle.

  5. Define a variable to handle the userā€™s response. Now is the time to know what type the dimension will be.

    Tip

    Youā€™ll need to use a different Scanner method than what we used in Exercise 1 above.

  6. Repeat the previous two steps to ask for and store the rectangleā€™s width.

  7. Use the length and width values to calculate the rectangleā€™s area.

  8. Print a statement using concatenation to communicate to the user what the area of their rectangle is.

  9. Run the program to verify your code.

C. Numeric Types

Write a program that asks a user for the number of miles they have driven and the amount of gas theyā€™ve consumed (in gallons), and print their miles-per-gallon.

   import java.util.Scanner;

   public class Miles {
      public static void main(String[] args) {
         Scanner input = new Scanner(System.in);

         System.out.println("How many miles have you driven?");
         Double numMiles = input.nextDouble();

         System.out.println("How much gas did you use? In gallons.");
         Double numGallons = input.nextDouble();

         Double mpg = numMiles / numGallons;
         System.out.println("You are running on " + mpg + " mpg.");
      }
   }

D. Strings

The first sentence of Aliceā€™s Adventures in Wonderland is below. Store this sentence in a string, and then prompt the user for a term to search for within this string. Print whether or not the search term was found. Make the search case-insensitive, so that searching for ā€œaliceā€, for example, prints true.

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, ‘and what is the use of a book,’ thought Alice ‘without pictures or conversation?’

Note

You may want to write the string above on more than one line in your solution. Java 17 and IntelliJ give you a few options to do so. The easiest, thanks to your IDE, is to press Enter as you type the string. IntelliJ will close the string and concatenate it with the next line to create one longer string.

E. Strings

Extend the previous exercise. Assume the user enters a word that is in the sentence. Print out its index within the string and its length. Next, remove the word from the string and print the sentence again to confirm your code. Remember that strings are immutable, so you will need to reassign the old sentence variable or create a new one to store the updated phrase.

   Integer index = firstSentence.indexOf(searchTerm);
   Integer length = searchTerm.length();
   System.out.println("Your search term first appears at index " + index + ". Your term is " + length + " characters long.");
   String modifiedSentence = firstSentence.replace(searchTerm, "");
   System.out.println(modifiedSentence);