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

Week 2 Programming Exercises Guide

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)
15 views2 pages

Week 2 Programming Exercises Guide

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

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:


[Link]
In the project, you’ll find a file [Link] for each exercise number N.

Solutions
You can find solutions to all the exercises here:
[Link]
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.

Common questions

Powered by AI

Naming conventions for constants, such as using uppercase letters and underscores (e.g., FINAL_DAYS_IN_YEAR), enhance code readability and maintenance by making it immediately clear which variables are immutable. This clarity helps prevent accidental changes to important fixed values, supports code consistency, and aids in understanding the program's logic, reducing errors in maintenance .

Breaking code into sections for declarations and assignments is beneficial as it promotes clarity and structure, making it easier for beginners to understand the flow and organization of a program. This separation allows students to grasp the concept of variable scope and initialization distinctly, which are foundational to writing more complex code effectively .

String manipulation and variable outputs in beginner exercises offer pedagogical advantages by allowing students to explore how strings are handled in programming, including concatenation and length determination. These tasks promote an understanding of how text data can be dynamically incorporated into programs. Practically, they teach debugging skills, coding precision, and preparing for real-world scenarios where string data is prevalent .

Exercises involving variable manipulation and expression building help beginners grasp fundamental Java programming concepts by providing hands-on experience in distinguishing between data types, understanding variable scope, and applying arithmetic operations. These foundational skills are essential for progressively learning advanced topics and for developing critical thinking and logical reasoning .

Variables should be declared and assigned in a way that uses meaningful names following the camelCase convention. For example, to hold the number of days in a year, a suitable name might be 'daysInAYear', and the variable should be of type 'int' since it is a whole number. For constant values, these should be declared with the 'final' keyword and named using all uppercase letters with underscores, e.g., 'FINAL_DAYS_IN_YEAR' .

In temperature conversion programs, distinguishing between integer and floating-point arithmetic is crucial because it affects the precision of calculations. For example, converting Celsius to Fahrenheit using the formula F = (C × 9/5) + 32, if integer division is used, it will truncate the decimal, leading to inaccurate results. Thus, using floating-point values ensures the correct calculation of 68.9 degrees Fahrenheit from 20.5 degrees Celsius .

Exercises that involve converting variables such as age to days, hours, and minutes develop problem-solving skills by requiring a deeper understanding of both mathematical operations and program structure. These tasks encourage students to apply logical reasoning to real-world scenarios, handle unit conversions correctly, and optimize code for efficiency. This strengthens analytical skills and the ability to implement practical algorithms .

A programmer might choose to use formatted strings in interactive applications to provide clear and user-friendly output that enhances user experience. Formatted strings allow for the integration of variable data in human-readable formats, such as "You are Y years old, which is the same as D days, or H hours, or M minutes." This not only improves communication to the user but also demonstrates how to dynamically incorporate data into responses .

Interactive programs, which require user input handling, enhance cognitive skills by forcing programmers to think about validation, data type conversion, and error handling. This contrasts with static programs where input is hard-coded. Handling diverse inputs from users improves understanding of different data types (e.g., int, String) and encourages the development of robust, user-friendly applications .

Using a Scanner for input in educational exercises promotes a better understanding of data types because it provides practical experience in how different data types are read and processed. For example, methods like nextInt(), nextDouble(), and next() highlight how data is captured differently, reinforcing the importance of knowing the correct data type needed for a variable and improving overall coding competency .

You might also like