Homework 1: Basics¶
This assignment will introduce you to printing output on the screen, using variables and methods, and performing arithmetic. You will also gain experience using VS Code, reading error messages from the Java compiler and Checkstyle, and submitting to Gradescope.
Important
Starter code is provided for this homework.
Download and open (extract) hw1.zip.
Move the hw1
folder into your CS159/src/hws
folder.
Please review the Grading Criteria at the end of this page before you start programming.
Learning Objectives¶
After completing this homework, you should be able to:
- Display output using
print()
andprintln()
. - Declare and assign variables (
int
anddouble
). - Perform basic arithmetic using
+
,-
,*
, and/
. - Define and call methods that have parameters.
This assignment must be completed individually. Your work must comply with the JMU Honor Code. Authorized help is limited to general discussion on Piazza, the lab assistants assigned to CS 159, and the instructor. Copying code from someone else, including the use of generative AI, is prohibited and will be grounds for a reduced or failing grade in the course.
Exercise 1.1 Fight Song¶
We are the Dukes of JMU!
Write a program named FightSong.java
that prints the lyrics of the
JMU Fight Song:
Madison, James Madison,
We are the Dukes of JMU
Madison, James Madison,
The fighting Dukes of JMU
Fight for Glory-- Honors Won
Brighten the Lights of Madison
Madison, James Madison,
Show your Colors
Proud and True
We are the Dukes of JMU
Each line should be a separate println()
statement in the code.
Your output must match the above text exactly, character for character.
Exercise 1.2 Madison Quotes¶
Write a program named MadisonQuotes.java
that displays the Top 10
James Madison Quotes
using this format:
Top 10 James Madison Quotes (according to brainyquote.com)
----------------------------------------------------------
[10] The essence of Government is power; and power, lodged as it must be in human hands, will ever be liable to abuse.
[9] The truth is that all men having power ought to be mistrusted.
[8] The advancement and diffusion of knowledge is the only guardian of true liberty.
[7] What is government itself but the greatest of all reflections on human nature?
[6] The people are the only legitimate fountain of power, and it is from them that the constitutional charter, under which the several branches of government hold their power, is derived.
[5] America was indebted to immigration for her settlement and prosperity. That part of America which had encouraged them most had advanced most rapidly in population, agriculture and the arts.
[4] Knowledge will forever govern ignorance; and a people who mean to be their own governors must arm themselves with the power which knowledge gives.
[3] Liberty may be endangered by the abuse of liberty, but also by the abuse of power.
[2] The happy Union of these States is a wonder; their Constitution a miracle; their example the hope of Liberty throughout the world.
[1] If men were angels, no government would be necessary.
The point of this exercise is to learn how to format your source code without exceeding 80 characters per line. Notice that each quote ends with a newline character. There are no line breaks in the middle of a quote.
Warning
Your solution must not use the +
operator.
For this exercise, Gradescope will reject submissions that have a +
character anywhere in the file.
Exercise 1.3 Baking Bread¶
The holidays are approaching, and you need to buy ingredients for baking (possibly many) loaves of bread. 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
- 1/2 cup lukewarm water
Write a program named BakingBread.java
that begins with the following variables:
int breadWeight = 16;
double servingSize = 2.5;
int guests = 25;
The program should (1) calculate how many loaves of bread are needed, and (2) calculate the ingredients for that many loaves of bread. Based on the variables above, the output should be:
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
Before compiling and running your program, Gradescope will replace the numbers 16
, 2.5
, and 25
with different numbers.
Your program must calculate the correct values in the output, based on the variables.
Make sure your output matches the above example exactly. Notice that there are two spaces at the beginning of each line of the recipe.
Exercise 1.4 Unit Conversion¶
When solving physics problems, you often need to convert from one unit to another.
Define a class named UnitConversion
that has the following four methods.
Each method should be public
and static
, return a double
value, and have a double
parameter.
fromCelsiusToFahrenheit
fromFahrenheitToCelsius
fromMetersPerSecondToMilesPerHour
fromMilesPerHourToMetersPerSecond
For example, the first method is written as follows:
/**
* Convert from Celsius to Fahrenheit.
*
* @param c temperature in Celsius
* @return temperature in Fahrenheit
*/
public static double fromCelsiusToFahrenheit(double c) {
return 1.8 * c + 32;
}
The formula for converting from Celsius to Fahrenheit is \(f = 1.8c + 32\). Use algebra to figure out how to convert from Fahrenheit to Celsius (the second method). For the last two methods, note that there are 1.60934 kilometers per mile, 1000 meters per kilometer, and 3600 seconds per hour. Don't just look for formulas online; use algebra to derive the conversions yourself.
Testing with JUnit (optional)¶
In contrast to the previous exercises, UnitConversion
should not have a main method.
Instead, you should create a class named UnitConversionTest
that calls the UnitConversion
methods with different arguments.
For example:
public static final double DELTA = 0.001;
@Test
public void testFromCelsiusToFahrenheit() {
assertEquals(32.0, UnitConversion.fromCelsiusToFahrenheit(0.0), DELTA);
assertEquals(50.0, UnitConversion.fromCelsiusToFahrenheit(10.0), DELTA);
}
The assertEquals()
method takes three arguments: the "expected" return value,
the "actual" value returned by the method, and a "delta" for floating-point precision.
In the above code, the expected and actual values must be within 0.001 of each other to be considered equal.
You do not need to submit UnitConversionTest.java
to Gradescope.
Grading Criteria¶
Your code will first be graded by Gradescope and then by the professor. The grade you receive from Gradescope is the maximum grade that you can receive on the assignment.
After the due date, the professor may manually review your code. At that time, points may be deducted for inelegant code, inappropriate variable names, bad comments, etc.
Your code must compile with the official tests for you to receive any points. For full credit, your code must also pass a Checkstyle audit.
Gradescope will provide you with hints but might not completely identify the defects in your submission. You are expected to test your own code before submitting.
There is no limit on the number of submissions and no penalty for excessive submissions. Points will be allocated as follows:
Criterion | Points | Details |
---|---|---|
Compile | 0 pts | Success Required |
CompileOfficialTests | 0 pts | Success Required |
Style | 20 pts | Partial Credit Possible |
OfficialTests | 80 pts | Partial Credit Possible |