1DV506, Problem Solving and Programming, Autumn 2018


Assignment 1: Getting Started


Problems?

Do not hesitate to ask your teaching assistant at the practical meetings (or Jonas at the lectures) if you have any problems. You can also post a question in the assignment forum in Moodle.

Exercises

    Lecture 1 (Getting Started)

  1. 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.
  2. 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.
  3. Setup Eclipse Workspace
    Before you start programming, do the following.
  4. 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
           }	      
       }
    	      
  5. Lecture 2 - (Input/Output, Operations on Primitive Types)

  6. Printing
    Write a program Print.java, which will print the phrase Knowledge is power!
  7. 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."
    		
  8. 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
    	     
    	       
  9. 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
    		
  10. 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.
  11. 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
    
  12. 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).

  13. 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
    	
  14. Lecture 3 - Using Library Classes

  15. 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.
  16. 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?
  17. 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.
  18. 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.
  19. 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).

  20. 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
    	      



Submission

Only exercises 5-16 should be handed in. (Notice that the VG exercises 11 and 16 are not mandatory.) We are only interested in your .java files. Hence, zip the directory named YourLnuUserName_assign1 (inside directory named src) and submit it using the Moodle submission system.