0% found this document useful (0 votes)
40 views

Homework Assignments - Week 1 (Intro) : 0. Warm-Up

This document provides instructions and examples for 14 homework assignments involving writing Java methods to perform various tasks like calculating sums, squares, string manipulation, checking triangle properties, converting between units of measurement, comparing values, and more. The homework includes writing methods to validate data, compute areas and perimeters of shapes, find possible knight moves on a chess board, count divisible integers within a range, convert between light frequencies and wavelengths, and solve quadratic equations.

Uploaded by

Cosmin Grosu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Homework Assignments - Week 1 (Intro) : 0. Warm-Up

This document provides instructions and examples for 14 homework assignments involving writing Java methods to perform various tasks like calculating sums, squares, string manipulation, checking triangle properties, converting between units of measurement, comparing values, and more. The homework includes writing methods to validate data, compute areas and perimeters of shapes, find possible knight moves on a chess board, count divisible integers within a range, convert between light frequencies and wavelengths, and solve quadratic equations.

Uploaded by

Cosmin Grosu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Homework Assignments - Week 1 (intro)

0. Warm-up:

Write one method for each of these cases, which should:


a. Receive 2 numbers (integer) and computes/returns their sum
b. Receive a number (double) and returns the square of its value (number to power 2)
c. Receive 2 string values, joins them and returns as a single string

d. Receives a String, computes and returns it's length (number of characters)


Hint: check what methods are available on the String object (type 's.' and wait for
autocomplete by Idea...)

e. Receives a double value (with some fractional part), should return only the integer
part of it (drop the decimals)
Hint: you need to use casting to transform between these types (double -> int)

f. Receives 3 values (x, min, max) and returns true if first one is in interval between the
other 2 values (min <= x <= max), or false otherwise

1. Triangle Checker:
a. Given three values representing the angles (in degrees) of a triangle, print if the
triangle is right-angled or not (check first if the triangle is a valid one - meaning
the sum of all its angles is 180)
Some tests:
10,30,50 => not a valid triangle
60,60,60 => valid triangle, not right angled
30,60,90 => valid triangle, right angled

b. Given three values representing the length of the edges of a triangle, print if the
triangle is right-angled or not (hint: use Pythagoras theorem; should also first
check if the triangle is a valid one - if the sum of any 2 edges is greater than the
3rd)
Some tests:
10,20,40 => not a valid triangle
10,20,20 => valid, not right angled
30,40,50 => valid, right angled
2. Time Converter:
Print the total number of milliseconds contained in H hours, M minutes and S seconds,
where H, M, S are inputs introduced by the user. (check first that each value is in valid
range, for example 0<= S < 60...)

3. Height Converter:
Write a program to:
a. Convert the height of a person from feet and inches (e.g 5 feet 10 inches) to
centimeters (178cm). The printed value should be an integer (no decimal part)
Hint: to convert (truncate) a double value to an integer, you can use the cast
operator: double d = 2.45; int i = ​(int)​ d; //i will be 2
b. Convert the height of a person from centimeters to feet and inches. The printed
values should be two integers.

4. Even-Odd Checker:
Write a program that is given a positive number, not bigger than 1000, and checks if the
number is even or not.
a. Prints 0 if the number is even or 1 if the number is odd.
b. Change your program so it prints “even”/”odd” instead of 0/1 (hint: may use the
ternary operator)

5. Ordered checker:
Write a program that accepts three integers from the user and returns true if they are in
ascending order (first number is less than 2nd which is less than 3rd).

6. Digit remover:
Write a program that is given an integer positive number, of exactly 5 digits, and
computes and prints a new number based on first one from which the middle digit was
removed. Example: 12345 => should print: 1245

7. Common digit:
Write a program that accepts 2 integers values as input between 13 and 89 and prints
true if there is a common digit in both numbers.
Example: x=34, y=48 => should print: true

8. Max value method:


Write a method to print out the maximum value of the 2 values given as parameters.
Method’s signature should be: static void printMax (int a; int b) {...}
Some tests to run on it:
printMax( 2, 3); //should print: 3
printMax(-2, -3); //should print: -2
printMax( 7, 7); //should print: 7
(hint: you may use the ternary operator to decide between values)
9. Absolute value method:
Write a method to compute and return the absolute value of an input value. The absolute
value is defined as: the value itself, if it’s positive, or the value with reversed sign, if it’s
negative. Signature should be: static double abs (double value) {...}
Write some code to test it for a few values.

10. Measuring shapes:


a. Given the value of a circle’s radius, print its area and circumference. Which types
should you use? (hint: area: A = PI * R^2 , circumference: L = 2*PI*R)
b. Given the length of a side of a square, print its area and perimeter.
c. Comparing 2 shapes: given the radius of a circle and the size of a square’s side,
print out which one has the greater area; the same for the perimeter.
i. Hint: for easier testing/clearer code, you may define separate methods for
computing area/perimeter for the 2 shapes, with signatures like:
static double circleArea(double radius){...}
static double squareArea(double side){...} etc..

11. Knight Moves:


Given the position of a knight on a chess board (e.g column = 2, row = 4, print the list of
possible squares that he can move to; for the above examples these would be (1, 6), (1,
2), (3, 6), (3, 2), (4, 3), (4, 5).
a. You should first check that the original position is valid (within the limits of the 8x8
chess table).
b. You should also check that any possible destination is a valid position too.
c. Hints:
i. You could define a method to validate any position from the table, and
then use it to check both original position and any destination position.
What signature (parameters, return type) should this method have?
ii. You could define another method for computing and printing each
position; it would receive as parameters the current position and 2 values
describing the relative movement, and would just print out the destination
position (if valid), or an empty line (if not valid); it may call the validation
method as needed. You could the just call this new method multiple
times, to print out all possible movements from current cell..
EXTRA (optional):

12. Write a program to find the number of integers within the range of two numbers received
as input and that are divisible by another number (also received as input). Example: x=5,
y=20 and p=3, the output should be 5. For x=6, y=22 and p=2, the output should be 9

13. Given the frequency of some photons in hertzs (e.g 4.3MHz = 4300000), print the
corresponding wavelength of the light. If its value is in range of the visible spectrum, also
print the corresponding color (eg “green”, “red”, etc.)

14. Quadratic equation:


a. Print the complex solutions of the quadratic equation, not just the real ones
b. Print the solutions of the quadratic equation with only 2 decimals (3.44 instead of
3.438485435…)

You might also like