Skip to main content
Logo image

Section 1.22 Coding Practice 1b (1.7-1.15)

Activity 1.22.1.

Write the code to print a random number from 1 to 100. You can use Math.random() to get a value between 0 and not quite 1.
Solution.
First multiply the output from Math.random() times 100 and then cast it to an integer. This will result in a random number from 0 to 99. Add one to make it from 1 to 100.
public class Test1
{
    public static void main(String[] args)
    {
        System.out.println(((int) (Math.random() * 100)) + 1);
    }
}

Activity 1.22.2.

Given the Circle class below, use its constructor to create a circle of radius 5.0 and then print out its area using its getArea method.
Solution.
Create a new Circle object with a radius of 5.0 and then print out its area using the getArea method.
public static void main(String[] args)
{
    Circle c1 = new Circle(5.0);
    System.out.println(c1.getArea());
}

Activity 1.22.3.

Given the Television class below, use its constructor to create a Television object with the brand Sony and price 499.99. Then print out its brand and price. Finally, set the number of pixels to 1080 and print it out.
Solution.
Create a new Television object with a brand of "Sony" and a price of 499.99. Print out its brand and price using the methods in the class. Set the number of pixels to 1080 using the setHighDef method.
public static void main(String[] args)
{
    // 1. Create a Television object with the brand "Sony" and price 499.99
    Television tv = new Television("Sony", 499.99);
    // 2. Print out its brand 
    tv.printBrand();
    // 3. Print out its price
    System.out.println("Price: " + tv.getPrice()); 
    // 4. Set the number of pixels to 1080 
    tv.setHighDef(1080);
}

Activity 1.22.4.

The following code should get the first letter of the first name, middle name, and last name and append (concatenate) them together and then return them all in lowercase. However, the code has errors. Fix the code so that it compiles and runs correctly.
Solution.
Line 5 has an ending ’ instead of ". Line 7 is missing a =. Line 8 has firstname, but it should be firstName. Remember that you should uppercase the first letter of each new word, after the first word, to make the variable name easier to read (use camel case). Line 9 has subString, but the method name is substring. Line 11 is missing a ).
public class Test1
{
    public static void main(String[] args)
    {
        String firstName = "Sofia";
        String middleName = "Maria";
        String lastName = "Hernandez";
        String initials =
                firstName.substring(0, 1)
                        + middleName.substring(0, 1)
                        + lastName.substring(0, 1);
        System.out.println(initials.toLowerCase());
    }
}

Activity 1.22.5.

The following code should print the first 3 letters of the string message all in lowercase letters. However, the code has errors. Fix the errors so that the code runs as intended.
Solution.
Line 5 ends with : when it should be ;. Line 6 should be substring(0,3). Line 7 should be part not message. Line 8 should be System.out.println.
public class Test1
{
    public static void main(String[] args)
    {
        String message = "Meet me by the bridge";
        String part = message.substring(0, 3);
        String lower = part.toLowerCase();
        System.out.println(lower);
    }
}

Activity 1.22.6.

The following code starts with String name1 = ALEX; and should print Alex. Use the toLowerCase and substring methods to do this task.
Solution.
Create a string that is all lowercase. Create a new string from a substring of the original string (first letter) and a substring of the rest of the string that is all lowercase (all except the first letter). Print that string.
public class Test1
{
    public static void main(String[] args)
    {
        String name1 = "ALEX";
        String nameLower = name1.toLowerCase();
        String finalName = name1.substring(0, 1) + nameLower.substring(1);
        System.out.println(finalName);
    }
}

Activity 1.22.7.

The following code should remove the word β€œvery ” (and following space) from the message and print the new message. You can use indexOf to find the position of a substring in your string. You can use substring to create a new string removing the word.
Solution.
Use indexOf to find the position and then create a new message up to the pos and again after the target string.
public class Test1
{
    public static void main(String[] args)
    {
        String message = "I am very happy!";
        String target = "very ";
        int pos = message.indexOf(target);
        String newMessage =
                message.substring(0, pos)
                        + message.substring(pos + target.length());
        System.out.println(newMessage);
    }
}

Activity 1.22.8.

The following code should replace lol in the message with laugh out loud and print the new message using indexOf and substring.
Solution.
Use indexOf to find the position of the β€œlol” then create a new string from up to that position and append the β€œlaugh out loud” and the substring after it.
public class Test1
{
    public static void main(String[] args)
    {
        String message = "That was great - lol.";
        String target = "lol";
        int pos = message.indexOf(target);
        String newMessage =
                message.substring(0, pos)
                        + "laugh out loud"
                        + message.substring(pos + target.length());
        System.out.println(newMessage);
    }
}
For more practice with Strings see problems at http://codingbat.com/java/String-1
 1 
http://codingbat.com/java/String-1
.
Here are some practice coding problems for Turtles.

Activity 1.22.9.

Finish the code below to have t1 draw a triangle where all of the sides are length 50.

Activity 1.22.10.

Finish the code below to have t1 draw a rectangle. The vertical sides should be length 50 and the horizontal length 100.

Activity 1.22.11.

Finish the code below to have t1 draw the number seven.

Activity 1.22.12.

Finish the code below to have t1 draw the number four.

Activity 1.22.13.

Finish the code below to have t1 draw something interesting.
You have attempted of activities on this page.