CSC 1302 Lab 2: Loop, Conditional and Method Practice solution




5/5 - (10 votes)

CSC 1302 Principles of Computer Science II
A Java method is a collection of statements that perform some specific tasks. A method may be
created to return the (task) results to the caller or return nothing to the caller. Methods allow us
to reuse the codes. A for-loop is a common tool used in programming to achieve certain
purpose that needs the specific operation in multiple iterations. In this assignment, you will
practice how to define a method with parameters in Java and how to call the method you
defined by supplying necessary arguments. When defining your methods, you will use loops
and operations on numbers/strings to complete some specific tasks.
Suppose 𝑛 is the last-two digits of the PantherID, 𝑚 = 𝑛 + 5.
● When 𝑚 is odd, please calculate:
1) 1/1 + 1/3 + 1/5 + … + 1/𝑚 = ?
2) 1/3 + 1/7 + 1/11 + … + 1/(2 * 𝑚 + 1) = ?
● When 𝑚 is even, please calculate:
1) 1/2 + 1/4 + 1/6 + … + 1/𝑚 = ?
2) 1/5 + 1/11 + 1/17 + … + 1/(3 * 𝑚 − 1) = ?
Expected Output of Task 1:
Write a java program named
(i) print out your name and your pantherID
(ii) Ask user to type in his/her weight and height; if an illegal input (such as a letter) is typed by the
user, give the user one more chance to input correctly.
(iii) Create a method to compute the body mass index (BMI) accordingly. BMI=weight/(height*height)
* 703
(iv) Print out the weight class according to the right table.
BMI Weight class
below 18.5 underweight
18.5 – 24.9 normal
25.0 – 29.9 overweight
30.0 and up obese
1. Upload all of the .java and the .class files to the CSc1302 dropbox on http://
2. Your assignment will be graded based on the following criteria: (a) Are your programs
runnable without errors? (b) Do your programs complete the tasks with specified outputs?
(c) Do you follow the specified rules to define your methods and programs? (d) Do you
provide necessary comments include the programmer information, date, title of the
program and brief description of the program.
3. Please comment the important lines in the .java file as shown in the template. The
important lines including but not limited to i) variables, ii) for-loop, iii) while-loop, iv)
if-else statement, iv) methods. Please use your own words to describe what is your
purpose to write this line. A .java file without comment will be graded under a 40%
4. Make sure that both the .java and .class files are named and uploaded to icollege
correctly. If any special package is used in the program, be sure to upload the package
too. Should you use any other subdirectory (whatsoever) your program would not be
graded, and you will receive a 0 (zero).
5. No copying allowed. If it is found that students copy from each other, all of these
programs will get 0.