Loops
- Make a “Hello world” program.
- Working with command line arguments…
- print how many of them there are.
- print them in order, one on each line.
- use a loop with a counter
- use a foreach loop
- print the sequence number in front of each argument.
- print them in reverse order.
- print their lengths.
- print only those that are numbers. (extra)
- print only those of which there are multiple copies. (extra)
- print them in alphabetical order. (extra)
- Supposing that the command line arguments contain only numbers…
- print their sum.
- print their average.
- print only the even numbers.
- print the maximum.
- print all of the divisors of the first argument.
- print them in numerical order. (extra)
- For each of the above exercises, take the data from the console, too.
- read a line from the console:
System.console().readLine()
- convert them to an array similar to
args
: split(" ")
Functions
- Print the
n
th Fibonacci number, where n
is the first command line argument.
- 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?)
- use iteration
- store the adjacent values in two variables
- store the numbers in an array
- use the formula with exponentiation (extra)
- turn the above solutions into functions
- the functions should contain only computation
- the printout should be separate
- print the first
n
Fibonacci numbers
- make a function that returns the first
n
numbers in an array
- Take the square root a number.
- There is such a function in the standard library. Find it.
- Use Newton’s method. (extra)
- Compute Pascal’s triangle.
- Print the
n
th row.
- use binomial coefficients
- iterate: compute the
n
th 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
- Make a function that returns the
n
th row (in an array).
- Make a function that returns all rows up to the
n
th row (in an array of arrays).
- 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)
- eg. the result of
1 2 3 * +
is (3 * 2) + 1
, that is, 7
- note that
*
has a special meaning in a DOS command line
Classes: fields, methods, constructors
- Create the class
Person
that contains the data of a person: given name, family name, age.
- Make getter/setter methods for each field.
- the fields should not be directly accessible from the outside, only through the getters/setters
- Make a constructor for the class. This constructor simply takes values for all fields.
- Make a
toString
method that will make a textual representation of a person like this: (John, Smith, 66)
- Make some
Person
instances and print their data.
- Constructors
- 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
- The command line arguments now contain the data of persons (each argument contains one string). Use them to make an array of
Person
s.
- A
Person
can have a friend, another Person
.
- if the
Person
doesn’t have a friend, let the field contain null
- Make a setter for the friend.
- for the previous constructors, add another constructor with an additional parameter that takes the friend
- don’t make duplicates in the code!
- 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?
- 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?
- Make the
Person
s 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
- Let
toString
print the accounts as well.
- Make a copy constructor for
Person
.
- can encapsulation be broken?
- Methods
- Make
getMoney
, a method that returns how much money is on the person’s accounts altogether.
- 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
- 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
- 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?
- Static data fields
- 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
- Modify the constructors and methods so that they update the value of this data field.
- Make a
richestEver
and a poorestEver
static data field. They are null
in the beginning, and once there are Person
s, they point to the richest and poorest person.
- Modify the constructors and methods so that they update the value of this data field.
- Make getters for the static fields.
File handling
- A file name is given in the command line arguments. Print the number of lines it contains.
- If the file does not exist, print an error message.
- A file contains numbers. Print their sum.
- Copy every second line from a file to the other.
- 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 Person
s into an array, then…
- Write the
Person
s into another file, in a similar structure.
- The user enters commands on the console, each one on a separate line. Print the data of the
Person
s between commands.
friend Jack Smith Jill Stone
: make Jill Stone the friend of Jack Smith.
friend Jack Smith
: remove the friend of Jack Smith (if present).
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.
save persons.txt
: Print the Person
s into the given file.
load persons.txt
: Load the Person
s from the given file.
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
- Make a list out of the first ten million numbers.
- Compare how fast
ArrayList
and LinkedList
are.
- Reverse the list, then print its 1st, 3rd, 6th, 10th etc. elements.
- Use the
get
method to access an element.
- Use an iterator.
- 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?
- Make a lottery game. Shuffle a list that contains the numbers 1..90, and take the first five elements as winners.
- Each line of a file contains five comma-separated numbers. For each line, print how many winning numbers are in it.
Inheritance
- Create the class
Moneylender
, a child class for the Person
class.
- A
Moneylender
instance will have a moneyRecipients
field, which is the List
of Persons
who have received money from this Moneylender
.
- Create a constructor for
Moneylender
that gets Person
s for this field, and the given name
, family name
, and age
of the Moneylender
. Use super
to call the appropriate constructor in Person
.
- You can make a constructor that also takes the
account
s of the Moneylender
. This constructor will call a different constructor in Person
.
- Override
getMoney
in Moneylender
: beside the money on his own accounts, add the sum on the recipients’ first accounts (that’s where they store the money received from the Moneylender
).
- Override
toString
, equals
, and hashCode
in a meaningful way.
- Create the class
Stockholder
, another child class for Person
.
- First, make two simple classes.
Bond
represents some stock that the Stockholder
has bought: it contains the amount
(an integer), and the name
of the stock.
Price
represents the actual rate of the stock: it contains its price
and its name
.
- The
Stockholder
has a field stocks
, a List
of Stock
s.
- Make a constructor that gets its initial value. (Make sure you have proper encapsulation for the
stock
field.)
- The constructor also receives a
List
of Price
s. Store it in the field prices
. For this field, have no encapsulation: this data can be changed from outside the class, and it represents the fluctuations of the stock market.
- Of course, this constructor will also have to use
super
to call one of the parent’s constructors.
- Override
getMoney
in Stockholder
: add the current value of the Stockholder
’s bonds.
- Override
toString
, equals
, and hashCode
in a meaningful way.