# 1.8. Unit 1 Practice Coding¶

## 1.8.1. Mixed Up Code Practice¶

Try to solve each of the following. Click the Check Me button to check each solution. You will be told if your solution is too short, has a block in the wrong order, or you are using the wrong block. Some of the problems may have an extra block that isn’t needed in the correct solution. Try to solve these on your phone or other mobile device!

```        1-8-1: The following program segment should figure out the cost for each shirt if they are buy 2 and get the third free and they are originally \$45 each.  But, the blocks have been mixed up.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.double price = 45;
---
double totalCost = price * 2;
---
double pricePerShirt = totalCost / 3;
---
System.out.println(pricePerShirt);
```
```        1-8-2: The following program segment should figure out the cost per person for a dinner including the tip. But the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.double bill = 89.23;
---
double tip = bill * 0.20;
---
double total = bill + tip;
---
int numPeople = 3;
double perPersonCost = total / numPeople;
---
System.out.println(perPersonCost);
---
System.out.println(perpersoncost); #distractor
```
```        1-8-3: The main method in the following class should print the number of seconds in 5 days. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.public class Test1
{
---
public static void main(String[] args)
{
---
int sInMin = 60;
int mInHour = 60;
int hInDay = 24;
---
int sInDay = sInMin * mInHour * hInDay;
---
int total = sInDay * 5;
---
System.out.println(total);
---
} // end main method
---
} // end class
---
public Class Test1
{ #distractor
```
```        1-8-4: The main method in the following class should calculate the number of months it would take you to save 500 if you make 50 a week. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.public class Test1
{
---
public static void main(String[] args)
{
---
double weeklyRate = 50;
double goal = 500;
---
double numWeeks = goal / weeklyRate;
---
double numMonths = numWeeks / 4;
---
System.out.println(numMonths);
---
} // end main method
} // end class
---
public void main(String[] args)
{ #distractor
```
```        1-8-5: The main method in the following class should calculate the cost of a trip that is 200 miles when the price of gas is 2.20 and the miles per gallon is 42. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.public class Test1
{
---
public static void main(String[] args)
---
{
---
int miles = 200;
double price = 2.20;
int mpg = 42;
---
double numGalls = miles / mpg;
---
double totalCost = numGalls * price;
---
System.out.println(totalCost);
---
}
---
}
---
System.println(totalCost); #distractor
```
```        1-8-6: The main method in the following class should calculate how many miles you can go on half a tank of gas if the miles per gallon is 26 and your tank holds 15 gallons. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.public class Test1
{
---
public static void main(String[] args)
---
{
---
int mpg = 26;
int tankHolds = 15;
---
double numGalls = tankHolds / 2;
---
double miles = numGalls * mpg;
---
System.out.println(miles);
---
}
---
}
---
public static main(String[] args) #distractor
```
```        1-8-7: The main method in the following class should calculate how many chicken wings you can buy with \$3.50 if the wings are \$.60 each. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.public class Test1
{
---
public static void main(String[] args)
---
{
---
double cost = 0.6;
double money = 3.5;
---
int numWings = (int) (money / cost);
---
System.out.println(numWings);
---
}
---
}
---
int cost = 0.6;
int money = 3.5; #distractor
```
```        1-8-8: The main method in the following class should calculate how much you will have to pay for an item that is 60% off the original price of \$52.99. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.public class Test1
{
---
public static void main(String[] args)
---
{
---
double price = 52.99;
double discount = 0.6;
---
double savings = price * discount;
---
double finalPrice = price - savings;
---
System.out.println(finalPrice);
---
}
---
}
---
int price = 52.99;
int discount = 0.6; #distractor
```
```        1-8-9: The main method in the following class should calculate how much the per item costs is for shorts that are buy 2 and get the third free.  The shorts are \$39.99 each. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.public class Test1
{
---
public static void main(String[] args)
---
{
---
double price = 39.99;
---
double priceForTwo = price * 2;
---
double itemPrice = priceForTwo / 3;
---
System.out.println(itemPrice);
---
}
---
}
---
double priceForTwo = price * 2 #distractor
```

## 1.8.2. Code Practice with Variables¶

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 36 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.

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).

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.

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.

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.

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 `)`.

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.

Lines 5, 6, and 7 should all be `double` versus `int` so that the decimal portion of the calculation isn’t thrown away.

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.

Lines 6 and 7 are both missing a `/`. Line 8 is missing a `(`. Line 9 is missing a `}` to close the `main` method.

Write the code below to calculate and print how many months it will take to save \$200 if you earn \$20 a week.

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).

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.

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.

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.

First compute the number of seconds in 1 day and then multiple that by 3 days.