Sie sind auf Seite 1von 5

1

Practical & Homework Week 3


K

Tip
Make sure you have read carefully the slides of the previous lecture, and you have installed
Eclipse and Java according to the instructions provided on Nestor.

3.1

Print a predefined text

The program in project 04-01 is only displaying a predefined message in the console (about
things Barry Burd may like). You will have to create another program that displays the
message Students are working hard!!! in the same manner. The book describes how to
get started with this task from page 52, we will repeat the essentials here.
1. Create a new project. This will be a collection of folders and files containing everything
that it is necessary to execute a program. Here is how you create a new Java project:
(a) From Eclipses menu bar, choose File New Java Project.
(b) A New Java Project dialogue appears. Type a name for your project (e.g. 04-01myVersion, no blank spaces in this name) and click Finish.
(c) On the workbench, in the Package Explorer, you will have a new project level
entry with this name.
2. We now have the project, but there arent any files yet. Lets make a class file containing our main-method.
(a) Select your newly created project in the Package Explorer.
(b) Choose File New Class.
(c) A New Java Class dialog box appears. Check the box for public static void main(
String args[]). Fill in the Name field, with the MyFirstEverClassInJava name (for
example, you may choose another funny name if you want, but it has to start with
a capital letter and cannot have blank spaces). Ignore the warning that appears
on the top of the dialog box (The use of the default package is discouraged,
very true, but for the beginners it does not matter). Click Finish.
(d) In the editor area of Eclipse, you will have generated some essential code for an
empty class with the name you have given.
3. Compare your code with the book example code in 04-01 (source file ThingsILike.java).
Adapt the code in your newly created class to display at execution the message Students are working hard!!!. Run and test your first program.

Table 3.1: The variables for your program


GDP
DC
GS
NX

3.2

the gross domestic product (of a country; in millions of dollars)


domestic consumption
government spending
net exports

Manipulate text input

The program in 05-01 echoes a line from the input. Make a new project (05-01-myVersion
for example) that echoes (in the console) two consecutive lines in reverse order. See for
example the input/output in listing 3.2 between < and > is the user input, the rest is
automatically generated output:
1
2
3
4

< First , it was darkness >


< After , it was light >
After , it was light
First , it was darkness

Tip
Declare two String variables and assign the value of these variables by using the result of
the nextLine() method call of the Scanner predefined class.

3.3

Calculate investment

The programs in 06-01 and 06-02 are adding an amount to a value which is either read
(scanned) from the console input or is assigned directly in the program. Based on these
programs you should be able to write a program that prompts the user to input the four
integer values from table 3.1.
The program has to compute and display the level of potential investments, given the
formula 3.1, which has to be printed as well.
INV = GDP DC GS NX

(3.1)

After the execution of the program, the content of the console should look like this:
1

What is value of the Gross Domestic Product ( GDP - in million


dollars ) ? <32900000 >
What is the value of the Domestic Consumption ( DC - in million
dollars ) ? <15100000 >
What is the value of the Government Spending ( GS - in million
dollars ) ? <6200000 >
What is the value of the Net Exports ( NX - in million dollars ) ?
< -1900000 >
The potential investment INV = GDP - DC - GS - NX is 13500000
million dollars

3.4 Extend the SimpleBarter program

3.4

Extend the SimpleBarter program

Extend the application for SimpleBarter presented during the lecture. Import the existing
project into Eclipse (after you have downloaded the archived file from Nestor). Your expanded application has to have three households instead of two, one producing cheese,
one bread (these two existed already), and new one tomato.
1. Enumerations can be declared as a special kind of class. Delete the enumeration
declaration from the SimpleBarter class, and create an enum class with three symbolic
values, via the File New Enum menu. This will look like:
1

package deusExMachina;

2
3
4
5

public enum FoodStuff {


TOMATO, CHEESE, BREAD,
}
The following line in the code of the Household class is not necessary anymore and you can
remove it.
import deusExMachina.SimpleBarter.FoodStuff;
The new application will have to keep the stock for all three products, for all three households. To achieve that, the Household class will have to have three attributes (member
variables), which will be initialized to zero:
public int cheeseStock = 0;
public int breadStock = 0;
public int tomatoStock = 0;
instead of:
int surplusProduce;
int stockOfTheOtherProduce;
2. A new attribute of the Household class will be the number of family members (you
are free to name it), initialized to a value that is stored in a static defined attribute in
the Experimental_Parameters class, named averageFamilySize. Initially, you will set
its value to 3.
3. You will have to change the eatFor ( int mouths ) method from the Household class.
The new version will deplete all stocks (for all three products). You will have to change
the name of the method to eat(), with no parameters. The reportStocks() method will
display the stocks for all three products.
4. To make the application work, you will have to update the SimpleBarter class in a
way that creates three objects of the Household class (instead of two), and realizes
the barter exchange in a way that ensures that all households get each product. Make
sure that each household consumes all the products with the given depletion rate.
5. Play with the values from ExperimentalParameters in a way to achieve a situation
when all reported stocks are positive. Find two sets of parameters, one which yields
results which are just negative, and another one that yields only positive results.

3.5

Interest

Interest is the fee paid for borrowing money. There are two common types of interest charged
by lenders: simple interest and compound interest. To calculate interest of either type you
need three pieces of information:
P The original amount of money borrowed. (the principal)
r The percentage of the principle that must be paid per time period to compensate the
lender. (the interest rate)
n The length of time the money will be borrowed for, expressed as a number of time
periods, generally years or months. (time duration)

Simple Interest
Simple interest is interest that is charged only on the principal and is calculated by:
P r n = interest

(3.2)

So, if you borrow $1000 at 5% simple interest per year for 5 years your calculation will
be:
1000 .05 5 = $250 interest

(3.3)

The total cost of your loan will be principal plus total interest - in this example $1250.

Compound Interest
Compound interest, by far the more prevalent form of interest charged in financial markets,
is interest that is charged on the principal and on any accrued interest.
For example, if you borrowed $100 for two years at 5% compound interest per year, then
at the end of the first year, assuming no repayments are made, you would be charged $5
interest (5% of $100). At the end of the second year you would be charged 5% interest on
the principal again plus 5% on the interest you accrued during the first year i.e. on $105.
Interest for the second year, therefore, would be 5% of $105 = $5.25. Therefore, the total
interest will be $10.25 and the total amount owed $110.25.
Calculating the total amount that youll have to repay on a loan subject to compound
interest is best done using the formula:
P (1 + r)n

(3.4)

So, for a loan of $1000 at 5% compound interest for 5 years your calculation would be:
T otal = 1000 (1.05)5 = 1000 (1.27628) = 1276.28

(3.5)

In this example the total cost of the loan is $1276.28.


To calculate total interest by itself, simply subtract the principal from the total cost of the
loan: $1276.28 $1000 = $276.28.

3.5.1

Simple interest

Write a Java application that reads from input the values of the principal, the interest per
year, the number of months the loan is for, and computes and displays the total cost of the
loan and the total interest - computed via the simple interest rule.

3.6 The discount instalment loan

3.5.2

Compound interest

Write a Java application that reads from input the values of the principal, the interest, the
number of periods the loan is for, and computes and displays the total cost of the loan and
the total interest - computed via the compound interest rule.

Tip
Spy in Barry Burds project 03-01 to find out the method call that computes the power of
a number.

3.6

The discount instalment loan

Negotiating a consumer loan is not always straightforward. One form of loan is the discount
instalment loan, which uses the simple interest rule that works as follows: Suppose a loan
has a face value of $1000, the interest rate is 15% per year, and the duration is 18 months.
The interest is computed by multiplying the face value of 1000 by 0.15 and by 1.5 (years)
to yield 225 as the total interest owed. That amount is immediately deducted from the face
value, leaving the customer with $775. Repayment is made in equal monthly instalments
based on the initial face value. Thus, the monthly loan payment will be $1000
18 = $55.56. This
method of calculation is not bad if the customer wants $775 on the spot, but the calculation
is more complicated if the customer needs for example $1000 on the spot.
1. Write a Java application that reads three inputs: the amount the customer needs on
the spot, the interest rate per year, and the duration of the loan in months. The
application should compute and display the face value of the required loan, and also
the monthly payment.

3.7

optional Improve the extended example of the barter


economy

You can apply what you have learned so far to further improve the example of the barter
economy with the following changes:
1. Extend the application that simulates the barter between three households - with a
fourth household.
2. Introduce a new produce (OLIVE_OIL), and a new household making it, and ensure
that all products are bartered between all the households.
3. Find two sets of parameters for which there is near zero equilibrium between consumption and production (one set which yields near zero but negative stocks, and one set
which yields near zero but positive stocks).
4. Introduce new parameters in the ExperimentalParameters class, to differentiate between the production and depletion rates for each product (up to now, all products
had the same rates).

Tip
The exercises 3.7.1 and 3.7.2 are essentially the same.
also 3.7.3 is very similar to 3.4.5.

Das könnte Ihnen auch gefallen