Programming Problems with Java Solutions

Numbers

  1. Highest Common Factor: For any given integer numbers find the highest common factor, i.e. the biggest integer number that both given numbers can be divided to. (Highest Common Factor)
  2. Rational Numbers: Develop the class Rational to represent fractional value expressed as the quotient of two positive integers. Examples of rational numbers are 1/2, 3/4 and 8/5. A rational number comprises a pair of integers for the numerator and non-zero divisor. For this class implement suitable constructor and accessor operations getNumerator and getDivisor. Implement also addition and multiplication methods. Have in mind reduction of rational numbers. (Solution)

Arrays

  1. Fill an array: Fill in an array of N elements with consecutive integer numbers 1, 2,…, N a) starting from the first array element; b) starting from the last array element and going backwards.
  2. Fill a 2D array: You need to fill in a MxN two dimensional array with consecutive integer numbers starting at 1 and going a) by rows - left to right and top to bottom; b) by columns - top to bottom and left to right.
  3. Dump an array: Create a program that dumps on screen all the elements of an array of length N traversing a) from first to last; b) from last to first.
  4. Dump a 2D array: Create a program that dumps on screen all the elements of a two dimensional array MxN traversing a) by rows - left to right, top to bottom; b) by columns - top to bottom, left to right. c) Extend the program by introducing column (cstep) and row (rstep) steps. The program will dump only elements in every cstep column and rstep row. If the step is negative the columns or rows will be traversed in revers order (right to left or bottom to top).
  5. Fill a 2D array in spiral: You need to fill in a MxN two dimensional array with consecutive integer numbers (1,2,…,M*N) in a spiral way going in a clock-wise direction.
  6. Spiral dump: Create a program that traverses a two dimensional array (NxN) in a spiral way: 1. Elements in first row left to right, 2. Elements in last column top to bottom, 3. Elements in last row right to left, 4. Elements in first column, excluding first and last (already traversed) bottom to top. 5. Elements in second row starting from second etc. (Solution 1)
  7. Shuffle an array: Create a program that randomly shuffles an array. (Solution 1, Solution 2)
  8. Remove duplicates from an array: Create a program that extracts only unique values from a given array.

Dates

  1. Leap Year: A year is a leap year if it is divisible by 4. Years ending with 00 are the exception - they have to be divisible by 400. Write a program that inputs a year from the user end and finds out if it's a leap year. (Solution)

Some ideas

  • Matrix operations: sum, difference, multiplication, spur, transposition, reverse (division), solve linear equations
  • Shuffle an array 1 and 2 dimensional.
  • Find the triangle's center.
  • Position a text in the midle of a triangle.
  • Capitalize string.
 
programming/problems/java.txt · Last modified: 2009/10/31 23:39 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki