Lecture 1 (Getting Started)
- Install Java
Download and install Java SE JDK: www.oracle.com/technetwork/java/javase/downloads.
We recommend you to use the latest stable version (Java SE 11.0.1).
Also, there are plenty of instruction videos available in YouTube. Just search for
"Install Java X" where X is your operating system.
- Install Eclipse
Download and install Eclipse IDE for Java Developers: www.eclipse.org/downloads/packages.
Once again, there are plenty of instruction videos available in YouTube. Just search
for "Install Eclipse X" where X is your operating system.
- Setup Eclipse Workspace
Before you start programming, do the following.
-
Create an Eclipse workspace (a folder) with the name java_courses on some location in
your home directory.
- Create a Java project with the name 1DV506 inside the workspace.
- Create a package with the name YourLnuUserName_assign1 inside the
project. For example, it might look something like wo222ab_assign1.
- Save all program files from the exercises in this assignment inside the package YourLnuUserName_assign1.
- In the future: create a new package (YourLnuUserName_assignX) for each assignment and a new
project (with the course code as name) for each new course using Java.
- Edit, compile and execute.
Create, compile and execute the following program inside your assignment 1 package:
/* The classical "Hello World!" program. */
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!"); // Print
}
}
Lecture 2 - (Input/Output, Operations on Primitive Types)
- Printing
Write a program Print.java, which will print the phrase Knowledge is power!
- on one line,
- on three lines, one word on each line,
- inside a rectangle made up by the characters = and |.
- Quote
Write a program Quote.java which reads a line of text (using class Scanner )
and then prints the same line as a quote (that is inside " ").
An example of an execution:
Write a line of text: I wish I was a punk rocker with flowers in my hair.
Quote: "I wish I was a punk rocker with flowers in my hair."
- Fahrenheit
Write a program Fahrenheit.java using the class Scanner to read
a real number (The Fahrenheit temperatur F) and then print the corresponding Celsius
temperetaure C. The realtionship between C and F is:
F = (9/5)*C + 32
An example of an execution:
Provide a temperature in Fahrenheit: 100
Corresponding temperature in Celsius is 37.77778
- 5-year Interest
Write a program Interest.java which computes the value of your savings S
after five years given a certain interest rate P (percentage).
You can assume that both S and P are integers.
The value should be an integer correctly rounded off.
An example of an execution:
Initial savings: 1000
Interest rate (in percentages): 9
The value of your savings after 5 years is: 1539
- Time
Write a program Time.java, which reads a number of seconds (an integer) and then prints
the same amount of time given in hours, minutes and seconds. An example of an execution:
Give a number of seconds: 9999
This corresponds to: 2 hours, 46 minutes and 39 seconds.
Hint: Use integer division and the modulus (remainder) operator.
- Sum of Three
Write a program SumOfThree.java which asks the user to provide a three digit number.
The program should then compute the sum of the three digits. For example:
Provide a three digit number: 483
Sum of digits: 15
If Time Permits
Exercise 11 is marked as VG task ==> only mandatory for those of you that aspire for a higher mark (A or B).
- Change (VG-task)
Write a program Change.java that computes the change a customer should receive
when he has paid a certain sum. The program should exactly describe the minimum number
of Swedish bills and coins that should be returned rounded off to nearest krona (kr).
Example:
Price: 372.38
Payment: 1000
Change: 628 kronor
1000kr bills: 0
500kr bills: 1
200kr bills: 0
100kr bills: 1
50kr bills: 0
20kr bills: 1
10kr coins: 0
5kr coins: 1
2kr coins: 1
1kr coins: 1
Lecture 3 - Using Library Classes
- Area
Write a program Area.java which reads a radius (R, a float) and computes
the area A of a circle with radius R. An example of an execution:
Provide radius: 2.5
Corresponding area is 19.6
The result should be presented with a single decimal correctly rounded off.
- Short Name
Write a program ShortName.java, reading a first name and a last name (given name and family name) as two Strings. The output should consist of the first letter of the first name followed by a dot and a space, followed by the first four letters of the last name. An example of an execution:
First name: Anakin
Last name: Skywalker
Short name: A. Skyw
Hint: Use methods of the String class.
What will happen if the last name consists of less than four letters?
- Random Number
Write a program RandomSum.java generating and printing the sum of five random
numbers in the interval [1,100]. An example of an execution:
Five random numbers: 78 13 91 2 36
There sum is 222
Hint: Use the class java.util.Random.
- Square Root
Write a program Distance.java which reads two coordinates in the form (x,y) and then
computes the distance between the points, using the formula
distance = Sqrt( (x1-x2)^2 + (y1-y2)^2 )
Sqrt() means "the square root of" and ^ means "raised to". The answer should be presented with
three decimal digits.
Hint: Use the class java.lang.Math for the computations.
If Time Permits
Exercise 16 is marked as VG task ==> only mandatory for those of you that aspire for a higher mark (A or B).
- Wind Chill (VG-task)
Write a program WindChill.java that asks the user for a temperature (°C)
and the wind speed (measured in m/s) and then computes the so-called wind chill temperature Twc using
the Wind Chill Index formeln:
Twc = 13.12 + 0.6215*T - 11.37*V^{0.16} + 0.3965*T*V^{0.16}
where T is the temperature (Celsius) and V is the wind speed (kilometers per hour). Notice you must convert the wind
speed from meter per second to kilometers per hour before you can apply the formula.
And we expect you to present the result with one decimal (correctly rounded off).
For example:
Temperature (C): -7.8
Wind speed (m/s): 8.4
Wind Chill Temperature (C): -16.7