Skip to main content
Logo image

Section 1.18 Coding Practice 1a (1.1-1.6)

Activity 1.18.1.

The following code should print “Mary’s favorite color is blue”. However, the code has errors. Fix the code so that it compiles and runs correctly.
Solution.
Line 5 is missing a starting ". Line 6 is missing a ending ;. Line 7 has Name when it should be name. Remember that variable names start with a lowercase letter.
public class Test1
{
    public static void main(String[] args)
    {
        String name = "Mary";
        String color = "blue";
        System.out.println(name + "'s favorite color is " + color);
    }
}

Activity 1.18.2.

The following code should print “Gabby’s favorite sport is soccer”. However, the code has errors. Fix the code so that it compiles and runs correctly.
Solution.
Line 5 is missing a =. Line 6 is missing the closing ". Line 7 has Name when it should be name. Remember that a variable name starts with a lowercase letter. Line 8 is missing an ending +.
public class Test1
{
    public static void main(String[] args)
    {
        String name = "Gabby";
        String sport = "soccer";
        System.out.println(name +
               "'s favorite sport is " + sport);
    }
}

Activity 1.18.3.

The following code should print Your name is Carly and your favorite color is red. Finish the code so that it prints the output correctly using the variables provided.
Solution.
Add the required strings using the + operator and be sure to include spaces as needed.
public class Test1
{
    public static void main(String[] args)
    {
        String name = "Carly";
        String color = "red";
        System.out.println(
                "Your name is "
                        + name
                        + " and your favorite color is "
                        + color);
    }
}

Activity 1.18.4.

Finish the code below so that it prints ``Your name is Justin and your age is 16`` using the variables provided.
Solution.
Use the + operator to append the strings. Be sure to include spaces as needed.
public class Test1
{
    public static void main(String[] args)
    {
        String name = "Justin";
        int age = 16;
        System.out.println("Your name is " + name +
                           " and your age is " + age);

    }
}

Activity 1.18.5.

Write the code to print Julian's favorite color is green. His favorite food is pizza. using the variables provided.
Solution.
Add the strings together using +. Don’t forget to include spaces and periods at the end of the sentences.
public class Test1
{
    public static void main(String[] args)
    {
        String name = "Julian";
        String color = "green";
        String food = "pizza";
        System.out.println(
                name
                        + "'s favorite color is "
                        + color
                        + ".  His favorite food is "
                        + food
                        + ".");
    }
}

Activity 1.18.6.

Finish the code below to print your favorite animal and food.
Solution.
Use + to add strings together. Add spaces as needed and periods.
public class Test1
{
    public static void main(String[] args)
    {
        String animal = "horse";
        String food = "chicken";
        System.out.println(
                "My favorite animal is a "
                        + animal
                        + ".  "
                        + "My favorite food is "
                        + food
                        + ".");
    }
}

Activity 1.18.7.

Finish the code below to print your favorite movie and book.
Solution.
Add the strings together using +. Don’t forget to include spaces and periods at the end of the sentences.
public class Test1
{
    public static void main(String[] args)
    {
        String movie = "The Princess Bride";
        String book = "Harry Potter";
        System.out.println("My favorite movie is " + movie + ".  " +
                           "My favorite book is " + book + ".");

    }
}

Activity 1.18.8.

The following code should calculate the cost of a trip that is 300 miles if gas is $2.50 a gallon and your car gets 30 miles per gallon. However, the code has syntax errors, like missing semicolons, wrong case on names, or unmatched " or (. Fix the code so that it compiles and runs correctly.
Solution.
Line 5 is missing a semicolon. Line 6 has Double instead of double. Remember that the primitive types all start with a lowercase letter. Line 8 has tripmiles instead of tripMiles. Remember that you should uppercase the first letter of each new word to make the variable name easier to read (use camel case).
public class Test1
{
    public static void main(String[] args)
    {
        int tripMiles = 300;
        double price = 2.50;
        int milesPerGallon = 30;
        double numberOfGallons = tripMiles / milesPerGallon;
        double totalCost = numberOfGallons * price;
        System.out.println(totalCost);
    }
}

Activity 1.18.9.

The following code should calculate the body mass index (BMI) for someone who is 5 feet tall and weighs 110 pounds. However, the code has syntax errors, like missing semicolons, wrong case on names, or unmatched " or (. Fix the code so that it compiles and runs correctly.
Solution.
Line 5 has Height instead of height. Remember that variable names should start with a lowercase letter. Line 6 is missing an equal sign. Line 7 is missing a * to square the height. Line 8 is missing a semicolon at the end of the statement.
public class Test1
{
    public static void main(String[] args)
    {
        double height = 60; // in inches (60 inches is 5 feet)
        double weight = 110; // in pounds
        double heightSquared = height * height;
        double bodyMassIndex = weight / heightSquared;
        double bodyMassIndexMetric = bodyMassIndex * 703;
        System.out.println(bodyMassIndexMetric);
    }
}

Activity 1.18.10.

The following code should calculate the number of miles that you can drive when you have $8.00 and the price of gas is 2.35 and the car gets 40 miles per gallon. However, the code has errors. Fix the code so that it compiles and runs correctly.
Solution.
Line 5 is missing the type double. Line 6 is backwards. It should be double milesPerGallon = 40;. Line 8 is missing a /. Line 10 is missing a ).
public class Test1
{
    public static void main(String[] args)
    {
        double gallonPrice = 2.35;
        double milesPerGallon = 40;
        double totalFunds = 8.0;
        double numGallons = totalFunds / gallonPrice;
        double distance = numGallons * milesPerGallon;
        System.out.println(distance);
    }
}

Activity 1.18.11.

The following code should calculate the cost of an item that is on clearance (70% off) when you also have a coupon for an additional 20% off the clearance price. However, the code has errors. Fix the code so that it compiles and runs correctly.
Solution.
Lines 5, 6, and 7 should all be double versus int so that the decimal portion of the calculation isn’t thrown away.
public class Test1
{
    public static void main(String[] args)
    {
        double originalPrice = 68.00;
        double clearancePrice = originalPrice * 0.3;
        double finalPrice = clearancePrice * 0.8;
        System.out.println(finalPrice);
    }
}

Activity 1.18.12.

The following code should calculate the number of whole days in 320893 seconds. However, the code has errors. Fix the code so that it compiles and runs correctly.
Solution.
Lines 6 and 7 are both missing a /. Line 8 is missing a (. Line 9 is missing a } to close the main method.
public class Test1
{
    public static void main(String[] args)
    {
        int numSecs = 320893;
        int numHours = numSecs / 3600;
        int numDays = numHours / 24;
        System.out.println(numDays);
    }
}

Activity 1.18.13.

Complete the code below to calculate and print how many months it will take to save $200 if you earn $20 a week.
Solution.
Calculate how many weeks it would take to make $200. Next divide the number of weeks by 4 (roughly the number of weeks in a month).
public class Test1
{
    public static void main(String[] args)
    {
        double weeklyRate = 20;
        double goal = 200;
        double numWeeks = goal / weeklyRate;
        double numMonths = numWeeks / 4;
        System.out.println(numMonths);
    }
}

Activity 1.18.14.

Write the code to calculate the number of miles you can drive if you have a 10 gallon gas tank and are down to a quarter of a tank of gas and your car gets 32 miles per gallon.
Solution.
First calculate the number of gallons you have left and then multiply that by the miles per gallon to get the number of miles you can still drive.
public class Test1
{
    public static void main(String[] args)
    {
        double numGallons = 10.0 / 4;
        double milesPerGallon = 32;
        double miles = numGallons * milesPerGallon;
        System.out.println(miles);
    }
}

Activity 1.18.15.

Write the code to calculate the number of seconds in 3 days. Remember that there are 60 seconds in a minute and 60 minutes in an hour and 24 hours in a day.
Solution.
First compute the number of seconds in 1 day and then multiple that by 3 days.
public class Test1
{
    public static void main(String[] args)
    {
        int secondsInMinute = 60;
        int minutesInHour = 60;
        int hoursInDay = 24;
        int secondsInDay = secondsInMinute * minutesInHour * hoursInDay;
        int secondsInThreeDays = secondsInDay * 3;
        System.out.println(secondsInThreeDays);
    }
}

Activity 1.18.16.

Write the code to print the number of chicken wings you can buy if you have $4.50 and they cost $0.75 each. Remember that you can’t buy part of a wing.
Solution.
Divide the amount of money you have by the cost of each wing and set the result to an integer since you can’t buy a part of a wing.
public class Test1
{
    public static void main(String[] args)
    {
        double money = 4.5;
        double pricePerWing = 0.75;
        int numWings = (int) (money / pricePerWing);
        System.out.println(numWings);
    }
}
You have attempted of activities on this page.