Lab 2 (Year 3 Sem 1)

Lab 2 is the third lab in semester 1 of year 3, and the third lab in the total of year 3. It is a lab for Programming in Java, and is considered to be easy.

Description
Please note the following when submitting your code:
 * The use of  is NOT allowed on Coursemology. Please do not include such statements when submitting solution to Coursemology.
 * Methods that requires to be invoked by Coursemology should have  access so that the Coursemology autograder can call it.

Unlocked after completion

 * OCD (Score at least 60% for Lab 2: Programming in Java)
 * New Kid on the Block (Score at least 80% for Lab 2: Programming in Java)

Files

 * [[Media:Chapter_2_-_Programming_in_Java_%2B_Appendix.pdf|Chapter 2 - Programming in Java + Appendix.pdf]]

Question
Write a Java program named SumDigit which functions as follows:


 * method sumdigit will take in an integer from 0 to 1000 (not inclusive of 0 and 1000) and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 9 + 3 + 2 = 14. You have to use mathematical operators to solve the problem. Return the final output as an integer.


 * print out the output in the main method. For example: The sum of the digits of the number 932 is 14.

Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit.

Solution
Just as the hint states, you can use the modulo (%) operator to extract the last digit of the number and use the division operator to remove the last digit. Combine that with a while loop to check if the number is not yet 0 and the rest should be trivial. Note that integer division in Java always returns an integer; i.e. 7/3 = 2.

Question
Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature.

The formula to compute the energy is where M is the mass of water in kilograms, temperatures are in degrees Celsius, and energy Q is measured in joules.

Your program should:
 * prompt the user to enter the amount of water in kilograms, w, and the initial and final temperatures of the water, i and f respectively.
 * have a method named calculateEnergy which will take in parameters w, i and f, calculate the energy, and return the energy.
 * Finally, format the energy printed to 1 decimal place.

Sample Output: Enter the amount of water in kilograms: 55.5 Enter the initial temperature: 3.5 Enter the final temperature: 10.5 The energy needed is 1625484.0

Solution
Pretty simple, just use the formula given in the question. How to read input should be given in the notes. Do note that the inputs should be read as doubles and not integers. For formatting, use. Refer to the notes to learn how to use it. DON'T FORGET TO IMPORT SCANNER!!

Question
Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays the distance between them. The formula for computing the distance is $$\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$

Your program should have a method named distance which will take in the inputs x1, y1, x2, y2 of double data type, calculate the distance between the two points, and return the distance as a double.

Note that you can use  to compute square root of a.

Your program should:
 * prompt the user to enter two points x1, y1 and x2, y2.
 * have a method named distance which will take in parameters x1, y1, x2, y2, calculate the distance, and return the distance.

Sample Output: Enter x1 and y1: 1.5 -3.4 Enter x2 and y2: 4 5 The distance between the two points is 8.764131445842194

Solution
Also fairly trivial, just use the formula given. Don't forget to take input as double instead of int, and remember to import.

Question
Write a program that prompts the user to enter three points (x1, y1),(x2, y2), (x3, y3) of a triangle and displays its area. The formula for computing the area of a triangle is $$s = (side1 + side2 + side3) / 2$$ $$area = \sqrt{s(s-side1)(s-side2)(s-side3)}$$

Your program should:


 * prompt the user to enter three points x1, y1, x2, y2 and x3, y3.
 * use a try catch block to check that the user inputs valid data. Print out the error message "incorrect input" if an exception is met.
 * have a method named computeArea which will take in parameters x1, y1, x2, y2, x3, y3, calculate the area, and return the area.
 * Finally, format the area printed to 1 decimal place when printing.

Sample Output 1: Enter three points for a triangle: a incorrect input

Sample Output 2: Enter three points for a triangle: 1.5 -3.4 4.6 5 9.5 -3.4 The area of the triangle is 33.6

Solution
Just use the formula given in the question. The distance function from last question should be pretty useful here. Use  to format the answer like in question two.

Question
Develop a cash register program that reads a purchase price (in cents) and an amount tendered (in cents) and then displays the change given ($2, $1, 50cents, 20cents and 10cents).

Sample Output: Purchase Price (in cents): 3760 Amount Tendered (in cents): 4000 Your change is: $2.40 1 $2 note 0 $1 coin 0 50 cents coin 2 20 cents coin 0 10 cents coin Thank you for your business. Come back soon.

Note that:


 * you may assume that the purchase price entered is always less than or equal to amount tendered.
 * we would like to optimize the number of notes and coins given as change (i.e. if the change is 3 dollars, you should tender out 1 $2 note and 1 $1 coin instead of, for example, 3 $1 coin).
 * the change should be formatted to 2 decimal places.

Solution
It is recommended to keep all values in cents. You can use / and % to determine the amount of each denomination to use; for example with 430 cents of change the number of $2 notes is needed is simply 430/200 = 2, and 430%200 = 30 cents will be left for the smaller denominations.