Homework 2: Variables and Operators

Objectives

  • Combine numbers and strings in an output message.
  • Perform a simple calculation with multiple variables.
  • Produce code that complies with the CS 149 style guide. See the Checkstyle lab for the guidelines

Exercise 2.1   Mad Lib

JMU's home page sometimes includes messages like "#2 public school in the South by U.S. News and World Report". It's fun to turn these messages into Mad Libs. Write a program named MadLib.java that begins with the following lines:

int count = 149;
String noun = "cats";
String phrase = "tap dance";

Then write a series of print statements that use these variables to output the following message:

Over 149 JMU students do research, take care of cats, or tap dance on campus.

You don't need to print with the color formatting; we're just emphasizing the location of the variables. Make sure your sentence ends with a newline character. (Hint: Use a combination of printand println statements).

Autolab will change the values of count, noun, and phrase before compiling and running your program. Your output must correspond to the values chosen by Autolab. For example, your program could be made to display this message instead:

Over 4321 JMU students do research, take care of a pile of rocks, or look for jobs on campus.

Exercise 2.2   Greeting

Write a program named Greeting.java that introduces yourself to the instructor. Declare and initialize the following variables: name (string), major (string), classOf (integer), and sleep (double). Then use these variables to display the following output (replacing the color text):

Hi! My name is Chris Mayfield, and I'm a Computer Science major. I plan to graduate with the class of 2005, but I only get about 7.5 hours of sleep per night. Go Dukes!

 

[Text repeated so you have it all due to formatting:

Hi! My name is Chris Mayfield, and I'm a Computer Science major. 
I plan to graduate with the class of 2005, but I only get about 7.5
hours of sleep per night. Go Dukes!

Notice that there is only one line of text. Be careful not to introduce additional newlines or spaces. Autolab will reject submissions that do not match the expected output 100%.

 

Exercise 2.3   Number Fun

Write a program named NumberFun.java that declares and initializes a single integer variable named myNumber. Then use the variable myNumber to calculate and print the following results. Do NOT use the value 74 in any calculations or print statements (use the variable myNumber). For example, if the variable myNumber is assigned the value of 74, your program would output:

74 squared is 5476
74 cubed is 405224
74 mod 5 is 4
74 mod 2 is 0
74 div by 10 is 7 
74 div by 10 has remainder 4
74 minus 456 is -382

Autolab will change the value of myNumber before compiling and running your program. Your output must be correct for any integer chosen.

The point of this exercise is to combine print statements with arithmetic expressions. Do NOT declare any other variables besides myNumber.

Your solution must NOT use the Math class (Chapter 4). Autolab will reject submissions that have the word "Math" anywhere in the file.

Exercise 2.4   Baking Bread

The holidays are approaching, and you need to buy ingredients for baking (possibly many) loaves of bread. Write a program named Baking.java that declares and initializes the following variables: breadWeight (integer), servingSize (double), and guests (integer). The output will look like this:

For 25 people, you will need 3.90625 loaves of bread:

  5.859375 teaspoons instant yeast
  5.859375 teaspoons salt
  5.859375 teaspoons sugar
  9.765625 cups all-purpose flour
  7.8125 cups sourdough starter
  1.953125 cups lukewarm water

In the above example, breadWeight is 16 (ounces), servingSize is 2.5 (ounces), and guests is 25 (people). Can you figure out how to compute the total number of loaves needed? If not, ask for help -- but don't think too hard!

According to King Arthur Flour, you will need the following ingredients for each loaf: 1 1/2 teaspoons instant yeast, 1 1/2 teaspoons salt, 1 1/2 teaspoons sugar, 2 1/2 cups all-purpose flour, 2 cups sourdough starter, and 1/2 cup lukewarm water.

Make sure your output matches the above example exactly. Notice that there are two spaces at the beginning of each line of the recipe. Autolab will change the initial value of each variable before compiling and running your program.

Exercise 2.5   Calculate BMR

Write a program named Calories.java that calculates basal metabolic rate (BMR), which is the amount of energy you use while at rest. Declare and initialize the following integer variables: years, feet, inches, pounds, and ounces. Your program should not change these variables once they are assigned (in other words, declare new work variables if you need them for your calculations). Your program should output two lines:

Male BMR is 1717.847685 calories/day.
Female BMR is 1551.847685 calories/day.

The above example is for a 25-year-old who is 5'10" and 160 pounds, 3 ounces. Here are the formulas for BMR:

  • Male BMR = 10 × weight(kg) + 6.25 × height(cm) - 5 × age(y) + 5
  • Female BMR = 10 × weight(kg) + 6.25 × height(cm) - 5 × age(y) - 161

Before you can output the results, you will need to convert height and weight to the metric system. To convert inches to centimeters, multiply by 2.54. To convert from ounces to kilograms, multiply by 0.0283495. Remember also that one foot is 12 inches, and one pound is 16 ounces.

Notice that the BMR formulas are the same except for the last term. Avoid making the same calculation twice in your source code.

Back to Top