Description
Purpose:
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.
Task1:
Suppose 𝑚 is 2*( last-two digits of the PantherID + 5).
● Write a program that sums 30 even numbers after 𝑚 (including 𝑚) [Using For-loop]. Call
this method and print out the sum from the For-loop.
● The above arithmetic sequence will be: m, m+2, m+4, … m+(n-1)*2, where n is 30. The
sum of this sequence can be represented as S=n/2*(2m+(n-1)*2), where n is30. Use the
equation to calculate and print out the sum of this sequence.
Expected Output of Task 1:
Task2:
Suppose 𝑛 is the last digit of you PantherID; 𝑛 = 𝑛 + 5.
● Draw a Square whose side length is 𝑛 with two methods. The first method is called
FLside(𝑖𝑛𝑡 𝑛) with an integer parameter that indicates the number of “x” to draw this
side. The second method is called Mside(𝑖𝑛𝑡 𝑛) with an integer parameter; in this method,
only the first and last position will draw “x”, the other positions are fulfilled by “a”. The
example below shows the example of a square whose length is 26.
● Calculate the area of the square by writing a method called Area(𝑖𝑛𝑡 𝑛) with an integer
parameter.
Expected Output of Task 2:
Template for Task1:
//Name: Danyang Zheng
//PantherID:000-000-026
//Due Date: June 12
//=======================================
public class Lab_01{
//Method for calculating the sum of Arithmetic Sequence
public static int Arithmetic_sequence(int n, int number){
}
public static void main(String[] args){
//Last-two digits of the Panther ID
int n = 26;
//Initialize a temporary value to save the sum
int sum =;
//For-loop to calculate the sum
for(){
}
//Print the result of the for-loop
System.out.println(“The result of for-loop is: ” + sum);
//Print the result of the method
System.out.println(“The result of the method is: ” + Arithmetic_sequence(n, 30));
}
}
Template for Task2:
//Name: Danyang Zheng
//PantherID:000-000-026
//Due Date: June 12
//=======================================
public class Lab_02{
//The method to draw the first and last side
public static void LFside(int n){
}
//The method to draw the intermediate side
public static void Mside(int n){
}
//The method to calculate the area of the square
public static void Area(int n){
}
public static void main(String[] args){
//Initialize the value of n
int n = 26 + 5;
//Draw first side
LFside(n);
//Draw intermediate sides
for(){
Mside(n);
}
//Draw last side
LFside(n);
//Calculate and print the area of the square
Area(n);
}
}
Criteria:
1. Upload all of the .java and the .class files to the CSc1302 dropbox on http://
icollege.gsu.edu.
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%
penalty.
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.