CS114 - Introduction To Programming For Engineers: Lab 02 - B - October 9, 2015
CS114 - Introduction To Programming For Engineers: Lab 02 - B - October 9, 2015
ATTENTION:
Using JCreator, from the "File" menu, click "New" --> "File", select either "Empty Java File" or "Main
Class". Do not create a project. Save the file in in "H:" drive or "D:\Temp" directory. Do not save on
the Desktop!
Java source file names will be Lab02B_Q1_yourLastName.java, Lab02B_Q2_yourLastName.java
and Lab02B_Q3_yourLastName.java. Do not use special Turkish characters in the file name.
Upload the Java program source file (.java file) to UNILICA.
Do not put the file in a zip or other compressed file.
LAB RULES:
1. You are allowed to refer to your lecture notes and textbooks during lab sessions. But, you are not
allowed to talk or exchange any form of information with your classmates. Any form of collaboration
is forbidden. You shall not discuss with your classmates how to solve the problem (either actual code
or the pseudo code). If you have any questions please consult the TAs and tutors.
2. Use of cellular phones during lab session is forbidden.
3. When you are done ask your TA to come by and s/he will ask you questions on the assignment and
have you sign the attendance sheet.
4. When you are finally done, please leave the class in a silent and prompt manner and do not disturb
others who are working on the assignment.
GRADING:
1. Points will be taken off if you do not name your files as specified
2. Part of the points for each question will be based on good style: correct indentation, good use of white
space, aligning of parenthesis, and statement blocks, consistent use of naming conventions.
3. Part of the points for each question will be based on comments
Examine the sample runs carefully. Your program should produce exactly the same outputs.
[Q1 30 points] Write a Java program (Lab02B_Q1_lastname.java) that inputs a number(real) from the user. Based
on the number input, calculate the result according to the following:
Inputs: Formula:
Number is greater than or equal to 8 √𝑛𝑢𝑚2 /3
Number is between 4 and 8 (exclusive) 𝑒 𝑛𝑢𝑚 − 8
Number is less than or equal to 4 𝑛𝑢𝑚
𝑛𝑢𝑚 + 20
Input: 9
Result: 5.20
Input: 7
Result: 1088.63
Input: -5
Result: -0.33
Note: you can use Math.sqrt(num) to calculate the square root of a number, Math.exp(num) returns enum and
Math.pow(num, power) to calculate a number to a give power.
[Q2 30 points] Write a Java program(Lab02B_Q2_lastname.java) that does the following. Given three integer
values x,y and z, result is true if one of y or z is "far" (differing from a by at least 2), while the other is "near", differing
from both other values by no more than 1. Otherwise the result is false. Note: use Math.abs (num) to compute the
absolute value of a number.
Sample Runs:
Enter 3 numbers: 2 5 3
The numbers meet the near/far criteria: false
Enter 3 numbers: 7 8 9
The numbers meet the near/far criteria: true
Enter 3 numbers: 9 8 7
The numbers meet the near/far criteria: true
Enter 3 numbers: 9 8 9
The numbers meet the near/far criteria: false
[Q3 40 points] Write a Java program (Lab02B_Q3_lastname.java) to meet the following requirements. You are
trying to determine the most efficient way to package chocolate bars, by weight. There are two sized bars, small (1kg)
and large (7kg). The user will input the size of the package required, and the number of small and large bars available.
Your program must determine how many of the small bars will be included in the package. The rules are as follows:
Run your program several times, using the inputs below. Check carefully to make sure you see the expected output.
Sample Runs: