Program: A One-sided Conversation

Let’s write a program that asks a series and questions processes the responses in various ways. First, the program will ask for the user’s pet’s name and respond with the number of letters it contains.

import java.util.Scanner;

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

        System.out.println("What is your pet's name?");
        String petName = sc.next();
        System.out.printf("%s is %d letters long.\n", petName, petName.length());
    }
}
[Run]
What is your pet's name?
Fluffles
Fluffles is 8 letters long.

Line 5 sets up a Scanner. Line 7 asks the user for their pet’s name. Line 8 obtains the name and stores it in variable petName. Line 9 prints a response. The %s is replaced with the second argument petName and the %d is replaced with the third argument petName.length(). This length method is one of many String methods that you can call. It simply returns the length of the string (the number of characters). In the sample run above, petName = "Fluffles", which means petName.length() returns 8. Therefore, %s gets replaced with “Fluffles” and %d gets replaced with 8, resulting in the output:

Fluffles is 8 letters long.

Next, the program will ask for the user’s favourite colour.

import java.util.Scanner;

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

        System.out.println("What is your pet's name?");
        String petName = sc.next();
        System.out.printf("%s is %d letters long.\n", petName, petName.length());

        System.out.println("What is your favourite colour?");
        String favColour = sc.next();
        System.out.printf("The colour %s is one of my favourites, too!\n", favColour);
    }
}
[Run]
What is your pet's name?
Fluffles
Fluffles is 8 letters long.
What is your favourite colour?
Turquoise
The colour Turquoise is one of my favourites, too!

Here, lines 11-13 are new. Line 11 asks the user what their favourite colour is. Line 12 obtains the input in variable favColour. In the example, the user entered “Turquoise”, so favColour = “Turquoise”. Line 13 prints

The colour Turquoise is one of my favourites, too!

Although, one thing that bothers me is that the word Turquoise is still capitalised in the response. This also means that if the user enters

RED

then the program will respond with

The colour RED is one of my favourites, too!

We can make the program respond more elegantly by using the toLowerCase method:

import java.util.Scanner;

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

        System.out.println("What is your pet's name?");
        String petName = sc.next();
        System.out.printf("%s is %d letters long.\n", petName, petName.length());

        System.out.println("What is your favourite colour?");
        String favColour = sc.next();
        favColour = favColour.toLowerCase();
        System.out.printf("The colour %s is one of my favourites, too!\n", favColour); 
    }
}

On line 13, a line of code has been inserted that transforms all the letters in favColour to lowercase. To be accurate, favColour.toLowerCase() doesn’t actually alter the string in favColour. Instead, it returns a copy of the string that is all lowercase letters. This is why we have to set favColour equal to it, so that favColour is reassigned to the returned string. Then line 14 prints the response as usual. This is how it looks now:

[Run]
…
What is your favourite colour?
Turquoise
The colour turquoise is one of my favourites, too!

Even an erratic input is transformed correctly:

[Run]
…
What is your favourite colour?
MaGeNtA
The colour magenta is one of my favourites, too!

Finally, let’s inquire about the user’s wealth. The program will ask the user how much money they have on them and reply with the amount of change they need to reach the next whole number.

import java.util.Scanner;

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

        System.out.println("What is your pet's name?");
        String petName = sc.next();
        System.out.printf("%s is %d letters long.\n", petName, petName.length());

        System.out.println("What is your favourite colour?");
        String favColour = sc.next();
        favColour = favColour.toLowerCase();
        System.out.printf("The colour %s is one of my favourites, too!\n", favColour);

        System.out.println("How much money do you have on you?");
        System.out.print("£");
        double money = sc.nextDouble();
        System.out.printf("Just £%.2f more and you'll have a nice round £%.2f.\n", Math.ceil(money) - money, Math.ceil(money));
    }
}
[Run]
What is your pet's name?
Fluffles
Fluffles is 8 letters long.
What is your favourite colour?
Turquoise
The colour turquoise is one of my favourites, too!
How much money do you have on you?
£17.73
Just £0.27 more and you'll have a nice round £18.00.

Lines 16-19 are new. Line 16 asks the user how much money they have on them. Line 17 prints a currency symbol, which makes it look more pleasing/professional for the user to input the amount. Line 18 obtains said input. In the example above, the user enters 17.73. Finally, line 19 prints a response. In the response, we can see the first %.2f gets replaced with the result of

Math.ceil(money) – money

Math.ceil (short for ceiling) takes a double, rounds it up (to the nearest integer), and returns it. Since money = 17.73, that means Math.ceil(money) returns 18.0. Then, money is subtracted to get the difference. Breaking it down:

Math.ceil(money) – money
= Math.ceil(17.73) – 17.73
= 18.0 – 17.73
= 0.27

The second %.2f gets replaced with the result of Math.ceil(money). This simply returns 18.0. Therefore, as shown above, the output is

Just £0.27 more and you'll have a nice round £18.00.

Remember that Math.ceil rounds up, specifically. (Math.floor rounds down and Math.round rounds to the nearest whole number.) So, a number like 14.09 still gives the correct output:

[Run]
…
How much money do you have on you?
£14.09
Just £0.91 more and you'll have a nice round £15.00.

Leave a Reply

Your email address will not be published. Required fields are marked *