Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 170: Introduction to Computer Science I
Emory University, Math/CS Dept. January 27, 2009
Second Lab: Simple Java programs
Lab Goals
In this lab we will write few simple Java programs. You will have a chance to get more
experience with writing and running Java code. You will also learn how to use an input and
functions from Java Math library in your programs.
Getting started
First, go to your cs170 directory and create a new subdirectory called lab2. Just as a
reminder, you should invoke the following commands:
cd ~/cs170
mkdir lab2
cd lab2
Each program, which we will write, have two sides (aspects): concept and its imple-
mentation. In this lab our concept will be expressed in mathematical language by formulas.
Understanding a concept of each program is crucial. It allows you to know what is the goal
for implementation. Implementation is the way how we program the concept, in our case
we use arithmetic functions available in Java.
1 Loan payments
Write a program LoanPayments (in file LoanPayments.java) that calculates the monthly
payments you would have to make over a given number of years to pay off a loan at a given
interest rate compounded continuously. The program should read three values from the
input: the number of years t, the principal P and the annual interest rate r. The desired
value (or future value) is given by the following formula:
FV =
Pert
12t
(1)
To start, type gedit LoanPayments.java in terminal. Please make sure you are in lab2
directory when you code (command pwd).
Your program will need some data from the user. It will request the user during the run
by using Scanner. Scanner will scan the input to get the expected number. A scanning
operation will block the program execution waiting for input. Thus we need to tell the user
what input program is expecting. An example code snippet1, which initiate Scanner and
read an integer may look like this:
// Create a Scanner
Scanner input = new Scanner(System.in);
// Enter number of years
System.out.print("Enter number of years: ");
int numberOfYears = input.nextInt();
1snippet – a small region of re-usable source code or text (source: Wikipedia)
1
CS 170: Introduction to Computer Science I
Emory University, Math/CS Dept. January 27, 2009
Scanner initiation should be done only once, then you can use it many times to read from
the input. To read fractional number (double) you need to use method nextDouble()
instead of nextInt().
In your program, you have to calculate value of e raised to some power. Use the function
exp() from class Math to do that:
double epow = Math.exp(val);
Now, the variable named epow contains the value of e raised to the power of val.
To print out calculated final value of monthly payments you need to use the same com-
mand as in the previous lab:
System.out.println(FV);
Example program run:
% java LoanPayments
Enter number of years: 20
Enter the principal amount: 80000
Enter the annual interest rate: 0.05
Monthly payments: 906.0939428196818
Note:
1. Please, add import java.util.*; as a very first line in the program,
2. Please, request from the user number of years, the principal amount and the annual
interest rate in the same order which is used in the example above.
2 Day of the week
Write a program DayOfWeek (in file DayOfWeek.java) that takes a date from the input, and
prints a day of the week that date falls on. The program should take three input numbers
(ints): a month m, a day d and a year y. For months, use 1 for January, 2 for February
and so on. Use 4 digit year format. The following formulas should be used to compute the
day of week number (0 stands for Sunday, 1 for Monday, 2 for Tuesday and so on):
y0 = y − (14 −m)/12 (2)
x = y0 + y0/4 − y0/100 + y0/400 (3)
m0 = m + 12 · ((14 −m)/12) − 2 (4)
week′s day = (d + x + (31 ·m0)/12)%7 (5)
You will have to access the input in the same way as we did it in the LoanPayments program.
Your program will expect three integers. Example program run:
% java DayOfWeek
Enter month number: 2
Enter day number: 14
Enter year number (4 digits): 2000
1
2
CS 170: Introduction to Computer Science I
Emory University, Math/CS Dept. January 27, 2009
Note: Please, request from the user month, day and year numbers in the same order which
is used in the example above.
Turning in the lab
The deadline for completing this lab assignment is 11:59PM on Friday, January 30, 2009.
Your programs should have names LoanPayments and DayOfWeek and should be placed in
files LoanPayments.java and DayOfWeek.java, respectively. For this assignment, please
make sure your programs are placed in ~/cs170/lab2 directory as we will copy those for
grading purposes.
3