Module 3, Part 1: Lab Day#
This first part of Module 3 does not introduce new contents: instead, it provides a series of assignments that focus on console I/O and conditionals, to improve your Java programming skills. All the following assignments can be solved with the Java programming notions introduced in Module 2. (New contents are introduced in Module 3, Part 2: Loops.)
Important
For each assessment, you can download the corresponding handout and submit your solution on DTU Autolab: https://autolab.compute.dtu.dk/courses/02312-E24.
For details on how to use Autolab and the assessment handouts, and how to submit your solutions, please read these instructions.
If you have troubles, you can get help from the teacher and TAs.
01 - Guess the Animal#
Note
Your solution to 02 - Guess the Animal 2 below can also be submitted as a solution to this assignment. However, 02 - Guess the Animal 2 is more complex — so it may be easier for you to first solve this assignment, and then improve your solution to solve the next one.
Edit the file Animal.java
provided in the handout, and write a program that
guesses what animal the user is thinking of. The animal can be either penguin,
sparrow, camel, or kangaroo. The program must work as follows.
When the program starts, it prints “Is the animal a bird?” and reads an answer from the console (as a
String
).If the answer is “Yes”, then the program prints “Does it fly?” and reads an answer from the console (as a
String
).If the answer is “Yes”, then the program prints “The animal is a sparrow”;
Otherwise, the program prints “The animal is a penguin”.
Otherwise, the program prints “Does it jump?” and reads an answer from the console (as a
String
).If the answer is “Yes”, then the program prints “The animal is a kangaroo”;
Otherwise, the program prints “The animal is a camel”.
When you are done, submit the modified file Animal.java
on DTU Autolab.
Hint
Remember that, to check whether two strings s1
and s2
are equal to each other,
you will need to use s1.equals(s2)
(see how to compare strings).
02 - Guess the Animal 2#
This is a follow-up to 01 - Guess the Animal: the task is the same, but
the program Animal.java
must now perform more accurate checks on the user’s
answers. More specifically:
After each questions, the user is allowed to answer either “yes” or “no” using uppercase or lowercase letters. For instance, the program must accept both “YeS” and “nO” as valid answers.
If the user answers something else, then the program must print “Invalid answer!” and end. For instance, here is a possible execution of the program: (the highlighted lines are the user’s answers)
Is the animal a bird? yES Does it fly? Dunno Invalid answer!
When you are done, submit the modified file Animal.java
on DTU Autolab.
Hint
To solve this assignment, you could reuse and improve your solution to 01 - Guess the Animal.
Before comparing the user’s answer with “yes” or “no”, it may be handy to convert the answer to uppercase or lowercase…
03 - Leap Year#
Edit the file LeapYear.java
provided in the handout, and write a program that
reads an int
eger value (representing a year) and prints whether the given year
is a leap year or not.
For instance, here are two possible executions of the program: (in both cases, the highlighted line is the user’s input)
2024
The year 2024 is a leap year
1900
The year 1900 is not a leap year
In order to determine whether a given year \(n\) is a leap year, you can follow this flowchart.
When you are done, submit the modified file LeapYear.java
on DTU Autolab.
Tip
To determine whether a number \(n\) is divisible by \(m\), you can use the
modulo (a.k.a. remainder) operator ‘%’ in Java: e.g.,
15 % 3
gives the result 0, hence 15 is divisible by 3; instead, 15 % 2
gives a result that is not equal to 0, hence 15 is not divisible by 2.
(Try it on the Java shell!)
Warning
The automatic grading on DTU Autolab includes some additional secret checks that test your submission with various years. After you submit, double-check your grading result on DTU Autolab: if the secret checks fail, then your solution is not correct, and you should fix it and resubmit.
04 - Days in a Month#
Edit the file MonthDays.java
provided in the handout, and write a program that
reads a String
(representing the name of a month) followed by an int
eger
value (representing a year), and prints on the console how many days are in that
month, in that year.
For instance, here is a possible execution of the program: (the highlighted lines are the user’s input)
aPRil
2000
The month of aPRil 2000 has 30 days
Observe that the user could write the name of the month using any combination of uppercase and lowercase letters; also, the message printed by the program must show the name of the month exactly as it was written by the user.
When you are done, submit the modified file MonthDays.java
on DTU Autolab.
Tip
You will need to compare the name of the month provided by the user with the possible month names. To this purpose, it may be handy to convert the name of the month to uppercase or lowercase…
To correctly compute the number of days for the month of February, you will need to check whether the user wrote a leap year: to this purpose, you can reuse and adapt the code from your solution to 03 - Leap Year.
You could write a more compact solution by using a Switch Expression and a Conditional Expression.
Warning
The automatic grading on DTU Autolab includes some additional secret checks that test your submission with various combinations of months and years. After you submit, double-check your grading result on DTU Autolab: if the secret checks fail, then your solution is not correct, and you should fix it and resubmit.
05 - Sales Discount#
Your task is to write a program that computes discounted prices for a shop. Edit
the file Discount.java
provided in the handout, and write a program that reads
a double
value from the console (representing a price) and computes and prints
a corresponding discounted price, with the applied discount percentage.
The program must apply the following rules:
if the price is 1000 or more, then the applied discount must be 20%;
otherwise, if the price 500 or more, then the applied discount must be 10%;
otherwise, if the price 250 or more, then the applied discount must be 5%;
otherwise, the applied discount must be 0% (i.e., no discount).
For instance, here are two possible executions of the program: (in both cases, the highlighted line is the user’s input)
2500.10
The discounted price is 2000.08 (discount: 20%)
300
The discounted price is 285.0 (discount: 5%)
When you are done, submit the modified file Discount.java
on DTU Autolab.
Hint
After you create a
Scanner
object, remember to set its localisation to English.Remember that numerical operations in Java maintain the type of the operands: e.g., a division between
int
eger values gives anint
eger value as a result. Therefore, if your program needs e.g. to represent the discount percentage of 20%, and it tries to achieve it by computing20 / 100
, then the result of the division will be0
(try it on the Java shell!): this might not be what you want! Instead, you may want to take advantage of type promotion, i.e., make sure that at least one of the operands in a numerical operation has typedouble
, to perform the computation in the realm ofdouble
s and return a result of typedouble
(thus including fractional parts). For instance, the division20 / 100.0
gives the result0.2
, as you might expect (try it on the Java shell!).
Warning
The automatic grading on DTU Autolab includes some additional secret checks that test your submission with various prices. After you submit, double-check your grading result on DTU Autolab: if the secret checks fail, then your solution is not correct, and you should fix it and resubmit.
06 - Quadratic Equation#
Edit the file Quadratic.java
provided in the handout, and write a program that
computes and displays the real roots (i.e. the real solutions) of the quadratic
equation \(a x^2 + bx + c = 0\).
The program must read from the console three consecutive values for the
coefficients \(a\), \(b\), and \(c\) (of type double
). Then:
if the equation has two real roots, the program must print them with a space in between — e.g.
1.5 6.7
if the equation has only one real root, the program must print it only once — e.g.
1.9
if the equation has no real roots, the program must print the text
No roots
if the equation has infinitely many roots, the program must print the text
Infinitely many roots
When you are done, submit the modified file Quadratic.java
on DTU Autolab.
Hint
You will need to compute a square root using
Math.sqrt(...)
(see the hints for 06 - Line-Point Distance).Remember to take into account that \(a\), \(b\), and \(c\) can be zero!
If \(a = 0\), the quadratic equation becomes a linear equation: how is its intersection with the \(x\)-axis computed? What happens if you try to compute the discriminant?
What happens when both \(a\) and \(b\) are zero, but \(c\) is not zero? Does the equation have any real roots?
What happens when \(a\), \(b\), and \(c\) are all zero? How many roots does the equation have?