0% found this document useful (0 votes)
2 views2 pages

Week2TutorialExercises-1 (2)

Uploaded by

Noah Asmerom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Week2TutorialExercises-1 (2)

Uploaded by

Noah Asmerom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Week 2 Tutorial Exercises

These are formative exercises, i.e. they are not assessed but are intended to help you develop
your programming skills. You should try to solve these problems after you watch the relevant
lecture material and before the weekly live session.

Checkout Initial Code from Gitlab


First of all, check out the initial code stubs for these exercises from Gitlab into Eclipse.
If you don’t know how to do this, watch the video guides from Week 1.

The Gitlab project link for these exercises is:


https://gitlab-student.macs.hw.ac.uk/f27sa_2024-25/week2-tutorial-exercises-24-25
In the project, you’ll find a file ExerciseN.java for each exercise number N.

Solutions
You can find solutions to all the exercises here:
https://gitlab-student.macs.hw.ac.uk/f27sa_2024-25/week2-tutorial-solutions-24-25
However, try to do the exercises before you look at the solutions!
Values, Types and Variables
These exercises are based on material covered in this lecture.

1. Create three separate variables to hold: the number of days in a year, the number of hours
in a day, and the number of minutes in an hour. Then, assign appropriate values to them
(you can ignore leap years!). For each variable, the declaration and assignment should be
on the same line. Finally, print out the contents of the variables to the console.
• Make sure you give the variables suitable names, i.e. these should be informative
and use camelCase to join words.
• Choose an appropriate type for each one, bearing in mind that these variables
are only going to contain whole numbers.

2. Change your solution to Exercise 1 so that the variables are all declared before any
assignments are made, i.e. there should be two sections of code, one for declarations, one
for assignments.

Expressions
These exercises are based on material covered in this lecture.

3. Create a variable to hold a temperature value in degrees Celsius, and assign it the value
20.5. Next write an expression to convert this value into degrees Farenheit, saving the
result in a new variable. Then display the result to the user.
• The formula for the conversion is F = (C x 9/5) + 32.
• The answer should be 68.9 degrees Farenheit.
• If you’re not getting the right answer, think about whether each number in the
expression is being treated as an integer or a floating-point value, and re-watch
the Expressions lecture from about 11 minutes onwards.
4. Go back to your solution to Exercise 1 and add a new variable age to hold the user’s age,
assigning it a value of 18. Then:
• Using the existing variable containing the number of days in a year, add code to
convert the value held in age into days, saving it to a new variable called days.
• Then do the same for hours and minutes, using the print line statements to print
out these three values to the user.

Strings, Input and Constants


These exercises are based on material covered in this lecture.

5. Write a program that calculates the cube of a number entered by the user.
• Use a Scanner to read a value from the user and save this to a variable.
• The cube of a number N is N x N x N.
• Display the result using a print line statement, formatting the output in the form:
“The cube of N is CUBE”, where CUBE and N are both replaced by numbers.

6. Go back to your solution to Exercise 4 and change the variables containing the number of
days in a year, the number of hours in a day, and the number of minutes in an hour into
constants. Make sure to follow the naming conventions for constants.

7. Continuing your solution to Exercise 6, add code to make this an interactive program.
• Your program should ask the user for their age, in years.
• Use a Scanner to read a value from the user, and assign this value to the existing
variable age (before the expressions to calculate days, hours and minutes).
• Display the converted age to the user in the form “You are Y years old, which is the
same as D days, or H hours, or M minutes”, where Y, D, H and M are appropriate
values.

8. Write a program that asks the user for the following information, and saves it into
appropriate variables: their name, their height, and how many siblings they have. Then
display this information back to the user in the form “Hello NAME, you are HEIGHT metres
tall, you have SIBLINGS siblings, and your name is LENGTH letters long.”
• Pay attention to both the types used for the variables, and the scanner methods
you use to read in the values from the user, recalling that there are different
scanner methods for different types.
• You can use String’s length() method to find the length of the user’s name. See
examples in the Strings, Input and Constants lecture.

You might also like