﻿ Java Programming Exercises - Objects

## Design and implement a class for wind speed

degree of difficulty: 2

Wind speed often is measured in kilometer per hours (km/h) or knots (nautic miles). Another measure is the Beaufort scale consisting of integer numbers from 1 to 12.

Design and implement a Java class, for creating wind speeds. Implement a constructor to create wind speed for a given velocity in kilometers per hour. The class must contain methods for getting the speed as knots or on the Beaufort scale. Furthermore, it must be possible to check whether the wind is calm or a European windstorm (Orcan).

Wind speeds less than 2 km/h are calm. More than 120 km/h is a European windstorm. A nautic mile is 1,852 Kilometer. The Beaufort scale is defined by `v = 3,01 * B3/2`, with `v` as the wind velocity in km/h. The Beaufort value `B` is rounded to the nearest integer. There are no Beaufort values larger than 12.

Hint: You can calculate ab with Math.pow(a,b).

Solution

## Design and implement polynomials

degree of difficulty: 2

Design and implement a class Polynomial, that represents a polynomial with real coefficients. The coefficients of the polynomial should be passed as an array parameter with array type double in the constructor of your class. Implement methods to add two polynomials, multiplies a double value to a polynomial, to return the degree n of a polynomial, to return the first derivative, and to return the value f(x) of a polynomial for a given a given value x (with running time O(n) in the worst case).

Carefully test your methods. In particular with the polynomial p(x) = 0 and with sums of polynomials that decrease the degree.

If you already solved this Java programming exercise, then let Polynomial implement your interface for a continous function.

## Design and implement rational numbers

degree of difficulty: 2

Rational numbers are numbers that can be represented as a fraction `p / q ` where `p` is an integer number and `q` is a positive integer (`q != 0`).

Design and implement a Java class `RationalNumber` for representing such numbers. Implement methods to add und mutliply rational numbers. Implement a method for return the value of a rational number as a double value. Make sure that the numerator `p` and denominator `q` do not have common divisors in your implementation. Use the algorithm for calculation the greatest common divisor to ensure this property.

Mind, that zero has a unique interal represention in your implementation.

## Design and implement chemical elements

degree of difficulty: 2

The periodic table of chemical elements classifies and displays all chemical elements. Each chemical element has a unique symbolic name and atomic number (number of protons). Chemcial elements are grouped together by common characteristics (alkali metal, poor metal, ...) called the chemical series.

Examples of chemical elements:

• H (hydrogenium): Hydrogen with atomic number 1.
• O (oxygenium): Oxygen with atomic number 8.
• K: Potassium with atomic number 19. It is an alkali metal.
• Zn: Zinc (from german Zink) with atomic number 30. It is a transition metal.
• Ga: Gallium with atomic number 31. It is a metal.

We consider the following chemical series:

• Alkali metals are all chemical element with atomic number 3, 11, 19, 37, 55, or 87
• Transition metals are all chemical elements with atomic number from 21 to 31, 39 to 48, 72 to 80, and 104 to 112.
• Metalsare all chemical elements with atomic number 13, 49, 50, 81, 82, 83, 113, 114, 115, or 116.

Design and implement a class ChemicalElement. The class should contain methods to retrieve for a chemical element its name, symbolic name, atomic number, and which type of metal it is (three different methods for each metal property). Implement these three methods without if or else, but

• one method with a switch
• one method with a single boolean expression
• one method with a static boolean-array where the index is the atomic number. You can initialize the array in the static initializer of the class ( `static { ... }` ).

Make sure that you choose for each of these three methods the best of the above implementations. How do these variants differ with respect to the maintainability and performance of the program?

Add three constants for the above five examples of chemical elements to the class.

The values of a chemical element must be unmutable: once a chemical element is constructed, its must not be possible to change its internal state (like String objects).

Have a look at the additional programming exercises based on this class for reading in chemical elements from a text file and implementing the abstract data type Comparable to compare two chemical elements.

Solution