Assignment 1: Java warm-up
Due: Thursday, September 1
On CMSX, we have made a project available as a zip file named a1.zip. It contains a class A1 in the file A1.java, with has several methods to be completed in accordance with their specs and the instructions given here. In addition, there is a class A1Test that implements a test harness for the class A1. You can run this class to test the method implementations. You are also expected to add a few tests of your own.
Learning Objectives
These methods are meant to be a short and simple introduction to the Java language as well as IntelliJ. Please practice good syntax conventions — having a solid foundation in program writing will help you later on in the course and when programming the future! IntelliJ has helpful editor commands for reformatting your code and fixing indentation.
How to complete CS 2110 Assignments Effectively
The two biggest things to succeed when doing a program assignment are to read carefully and to manage your time well. Make sure that you understand the expectations of each method, as many methods will ask you to write a solution a certain way. For example, if you are asked to find the sum of a range from n to m using a while loop, you are expected to use a while loop and will lose points for not following directions. Unfortunately, while there are many ways to write a program, we are seeing if you can write them as per the specification. Additionally, please start the assignment well ahead of time. If you run into any problems early, the dedicated course staff in Rhodes 405 can help you with any questions you may have. But if you start the assignment on the last day, you may not be able to get the help you need!
Academic Integrity and Collaboration Policy
You may not collaborate with anyone on this assignment — this includes looking at anyone else’s code or showing anyone else your code in any form. While you can talk to others, you should not be communicating code directly but rather general strategies on how to tackle a problem. You may not look at solutions to similar previous assignments in 2110. If you do not know where to start, are stuck, do not understand testing, or are lost, please see someone on course staff immediately. This can be in the form of an instructor, TA, or consultant.
How to set up A1 in IntelliJ
To set up assignment 1 on IntelliJ, follow these steps. First, download the file a1.zip from CMSX and unzip it into a new folder. It contains the two Java source files you need, A1.java and A1Test.java, in the src folder. You can then tell IntelliJ to create a new project “from existing sources”, and point it at the a1 folder you just extracted.
Make sure that your JDK is set to version 17. Once you have created the project, you should be able to run it and see (incorrect) output from the test program. If you are having troubles, please come to office hours or ask on Ed.
How to do this Assignment
Begin by reading the comment at the top of the assignment. It asks you to provide some information including your name, netID, and what you thought of the assignment. Additionally, there is a variable called timeSpent — once you have completed the assignment, please indicate the amount of time spent in hours there.
There are a total of 13 methods in this assignment. Please make sure to carefully review the method specifications at the top of each function. This handout contains further details and instructions on how to complete each method. Additionally, As you finish writing a method, you should be writing test cases in order to ensure that your code is functioning properly. We have provided Some tests in A1Test.java. At minimum, please Complete all the TODOs in this file.
In the specification of some methods, you will
see a Requires
clause. This clause describes the
preconditions that the caller must satisfy
in order to use the method. The method does not need
to check these preconditions; the blame is placed on the
calling code if they are violated.
product
This method should return the product of the
values 7, 11, and 13. This can be done in one
line with a return statement. Note that method
product()
is a function, since it returns
a value. A return statement stops the execution of a
method body and the value of the function product() is
whatever is returned. To learn more about the return
statement, lookup return in the JavaHyperText linked
below.
https://www.cs.cornell.edu/courses/JavaAndDS/definitions.html
theAnswerIs2
This method gives you the answer: 2! If you want,
test the algebra to see that this is the case. This
method was included in order to show you that Java
maintains order of operations. Additionally, inside
this method is a parameter of type int
called x
. A parameter is a value passed
into a function. This value can be manipulated by the
function. Please write this method as a single
expression.
isLiquid
Given a temperature in Celsius, determine if water at that temperature would be liquid or not. Water freezes at 0°C and boils at 100°C. Notice how the function has a return type of boolean. A boolean is a primitive type that can either be true or false. We can write boolean statements as a combination of booleans and logical operators. These boolean statements then simplify into true or false. The four logical operators are == (equals), && (and), || (or), and ! (not). Additionally, Relational operators such as <, >, <= can compare primitive values. For example, 5+3 < 9 && !true evaluates to false. This question can be answered with a one line return statement using logical operators.
magicTrick
This question introduces the remainder operator and
if-statements. The % or remainder operator takes the
remainder of a division operation, for example 5 % 2
evaluates to 1. An if-statement takes a boolean expression
and if it evaluates to true it executes certain code. For example:
If (5 % 2 > 0) { execute some code }
In this example, the boolean statement is 5 % 2 > 0
Since it evaluates to true, the code within the if statement
Executes. For more information reference the JavaHyperText
entry on if statements. You must use an if-statement for this
problem.
theAnswerIs42Conditional
We have previously learned how to set up this
problem using an if-else statement. Another way to
write this would be using the conditional operator. The
conditional operator is used in the following manner:
<boolean expression> ? <exp1> : <exp2>
. If the
boolean expression is true the statement evaluates to
option1 and if it is false the statement evaluates to
option2. Please refer to JavaHyperText for more
information. Write your code in one line following the
return statement using a conditional operator.
hypotenuse
This problem requires you to do a square root. Luckily, java has prebuilt libraries we can import into our programs. These functions make our lives much easier since we do not have to write our own function to execute certain tasks. Please visit the website below to learn more about the math library and find the appropriate function to call. Note, when calling the function, write Math. before the function — we do this so our program knows to look for the function in the Math class. In this method we may assume that legs a and b are valid inputs.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html
isTriangle
For this problem, it is useful to know that any side length of a triangle may not be greater than or equal to the sum of the other two sides.
calculate
This method takes a String object, which stores a sequence
of Java characters (primitive type char
) Please
review the website below to read more about strings and find
out what functions are associated with the object. Note using
== in a boolean statement with two strings does not compare
them. Are there any methods in the documentation that do
this? You may assume that opp only can be "ADD", "SUBTRACT",
"DIVIDE", or "MULTIPLY". Additionally, "DIVIDE" and y = 0 are
not valid inputs.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html
rangeSum
The goal of this method is to get you comfortable with writing for loops. Let's start with an example of a for loop:
for (int i = 0; i<15; i++) { sum = sum + i; }
For loops, initialize some variable (i), have some condition that will make them stop (i<15), increment the previously initialized variable (i++), and execute some code (sum=sum + i;). This code adds 0 through 14 inclusive. For more information, please refer to the JavaHyperText entry on while loops. Now that you know the form of a for loop, how would you write the sum of some range n to m?
rangeSumOdd
The goal of this method is to get you comfortable with writing while loops. Let's start by looking at pseudocode of a while loop:
while (boolean expression) { loop body }
This means while some condition is true, the code in the body of the while loop executes. For more information, please refer to the JavaHyperText entry on while loops.Now that you know the form of a while loop, how would you write the sum of the odd numbers in some range n to m?
isPalindrome
Return whether the string str
is a palindrome. For this
problem, you should use the charAt()
and
length()
methods from the String class. The
expression s.charAt(i)
returns the character at
position i
in the string s
, and
s.length()
is the number of characters in the string.
duplicateVowels
Given some string str
, return the string with vowels
duplicated. For example, Ohio would become OOhiioo. Please note that
only vowels in the English alphabet should be duplicated. We expect you
to use a loop in this method. You may use the substring or charAt methods
from the String class only!
Submission
Please make sure to fill in the time you spent using the
timeSpent
field in the A1 class. Then upload your files
A1.java and A1Test.java to CMSX before the deadline. To locate
these files on your computer, you can right click on A1.java
and select "Open In" and then select your file explorer. For
Windows, that may be "Explorer" and for Mac, that may be
"Finder". Do not submit any files with different extensions
at the end. To check if you submitted the correct files,
download the files from CMSX after you have submitted.