Loops

  1. Make a “Hello world” program.
  2. Working with command line arguments…
    1. print how many of them there are.
    2. print them in order, one on each line.
      1. use a loop with a counter
      2. use a foreach loop
      3. print the sequence number in front of each argument.
    3. print them in reverse order.
    4. print their lengths.
    5. print only those that are numbers. (extra)
    6. print only those of which there are multiple copies. (extra)
    7. print them in alphabetical order. (extra)
  3. Supposing that the command line arguments contain only numbers…
    1. print their sum.
    2. print their average.
    3. print only the even numbers.
    4. print the maximum.
    5. print all of the divisors of the first argument.
    6. print them in numerical order. (extra)
  4. For each of the above exercises, take the data from the console, too.

Functions

  1. Print the nth Fibonacci number, where n is the first command line argument.
    1. use the recursive definition of the Fibonacci numbers
      • experiment: by what n value does it get too slow?
      • experiment: by what n value do we get negative numbers? (Why?)
    2. use iteration
      1. store the adjacent values in two variables
      2. store the numbers in an array
    3. use the formula with exponentiation (extra)
    4. turn the above solutions into functions
      • the functions should contain only computation
      • the printout should be separate
    5. print the first n Fibonacci numbers
      1. make a function that returns the first n numbers in an array
  2. Take the square root a number.
    1. There is such a function in the standard library. Find it.
    2. Use Newton’s method. (extra)
  3. Compute Pascal’s triangle.
    1. Print the nth row.
      1. use binomial coefficients
      2. iterate: compute the nth row from the previous row I. use a different array for each row I. allocate an array for the final row, and reuse it in each step
    2. Make a function that returns the nth row (in an array).
    3. Make a function that returns all rows up to the nth row (in an array of arrays).
  4. The command line arguments contains an expression in reverse Polish notation, which consists of the four basic operations and integers. Calculate the value of the expression. (extra)

Classes: fields, methods, constructors

  1. Create the class Person that contains the data of a person: given name, family name, age.
  2. Make getter/setter methods for each field.
  3. Make a constructor for the class. This constructor simply takes values for all fields.
  4. Make a toString method that will make a textual representation of a person like this: (John, Smith, 66)
  5. Make some Person instances and print their data.
  6. Constructors
    1. Make a constructor that takes a string argument: John,Smith,66
      • note that it is slightly different from what toString produces
      • let us assume that the text is good, no checks are necessary
      • you can use the split method of String to take it apart
    2. The command line arguments now contain the data of persons (each argument contains one string). Use them to make an array of Persons.
    3. A Person can have a friend, another Person.
      • if the Person doesn’t have a friend, let the field contain null
      1. Make a setter for the friend.
      2. for the previous constructors, add another constructor with an additional parameter that takes the friend
        • don’t make duplicates in the code!
      3. Supposing we have an even number of command line arguments, make the pairs of people each other’s friends.
        • is the above constructor applicable for this task?
      4. Let toString print the name of the friend, too (if the person has a friend).
        • can the friend himself (as a Person instance) be printed here? what’s the catch?
    4. Make the Persons have bank accounts. The constructor that takes a text will now get the accounts in the text, too: John,Smith,66,5000,20000,8000
      • store them in an array of int values
      • there can be any number of accounts, even zero
      1. Let toString print the accounts as well.
    5. Make a copy constructor for Person.
      • can encapsulation be broken?
  7. Methods
    1. Make getMoney, a method that returns how much money is on the person’s accounts altogether.
    2. Make desposit, a method that takes two parameters: how much money to put on which account.
      • bank accounts are numbered starting with 1
      • let us assume that the account number is valid, no checking is needed
    3. Make withdraw with two parameters: how much money to take from which account.
      • if the account holds less money, only withdraw that much
      • let the method return the withdrawn amount
    4. Make interest that takes an integer. Change each account by this many percents.
      • e.g. if the parameter is 5, every account has to change to 105% of its previous value, if it is -5, then to 95%
      • if the result is not an integer (e.g. 50% of 3), it will be rounded by removing the fraction (in this case, the result is 1)
      • there is a typical mistake in which the code looks good, but it doesn’t change the accounts if the parameter is 5, and removes all money if the parameter is -5. What is the reason for this, and how can it be fixed?
  8. Static data fields
    1. Make the totalMoney static data field that always shows the sum of the money on all people’s accounts.
      • note: a person’s money is counted in even if the person is inaccessible, and possibly garbage collected
      1. Modify the constructors and methods so that they update the value of this data field.
    2. Make a richestEver and a poorestEver static data field. They are null in the beginning, and once there are Persons, they point to the richest and poorest person.
      1. Modify the constructors and methods so that they update the value of this data field.
    3. Make getters for the static fields.

File handling

  1. A file name is given in the command line arguments. Print the number of lines it contains.
    1. If the file does not exist, print an error message.
  2. A file contains numbers. Print their sum.
  3. Copy every second line from a file to the other.
  4. A file contains a number entryCount on its first line. Then entryCount lines follow, each of them contains the comma separated data of a Person. Load the Persons into an array, then…
    1. Write the Persons into another file, in a similar structure.
    2. The user enters commands on the console, each one on a separate line. Print the data of the Persons between commands.
      1. friend Jack Smith Jill Stone: make Jill Stone the friend of Jack Smith.
      2. friend Jack Smith: remove the friend of Jack Smith (if present).
      3. deposit Jack Smith 3 -2000: deposit or withdraw money from an account of the person. In this case, deposit 2000 units from the account #3 of Jack Smith.
      4. save persons.txt: Print the Persons into the given file.
      5. load persons.txt: Load the Persons from the given file.
      6. acquaintance Jack Smith Jill Stone: print true if Jack Stone’s friend is Jill Stone, or his friend’s friend etc., otherwise print false. Note: if the friends contain a cycle, the program will loop infinitely, that’s OK for now.

Data structures

List

  1. Make a list out of the first ten million numbers.
  2. Reverse the list, then print its 1st, 3rd, 6th, 10th etc. elements.
    1. Use the get method to access an element.
    2. Use an iterator.
    3. Make methods for the two exercises above. The methods should take a List parameter so that they can be called with both an ArrayList and a LinkedList parameter. How much time do they take?
  3. Make a lottery game. Shuffle a list that contains the numbers 1..90, and take the first five elements as winners.
    1. Each line of a file contains five comma-separated numbers. For each line, print how many winning numbers are in it.

Inheritance

  1. Create the class Moneylender, a child class for the Person class.
  2. Create the class Stockholder, another child class for Person.