CS110 Programming Language I

CS110 Programming
Language I
Lab 10 : Arrays
Computer Science Department
Fall 2014
Lab Objectives:
In this lab, the student will practice:




Declaring and creating arrays
Processing arrays elements
Using nested control structures
Declaring and using methods
Lab Exercise 1: Program Output
Problem Description
What is the output of the following code segment?
Your answer:
Page|2
Your answer:
Page|3
Lab Exercise 2: Area
Problem Description
Write a java program that asks the user to input a sequence of floating-point numbers
representing radius of circles. The program should store the values in an array, compute and print
their respective areas.
Your program should define and use a value returning method called circleArea to calculate
and return the area of the circle. It should accept the radius as a parameter.
Hint:
Area of a circle = PI r2, where r is the radius of the circle
Sample output
Enter the radius of a circle ( negative to stop): 5
Enter the radius of a circle ( negative to stop): 3
Enter the radius of a circle ( negative to stop): -5
Radius
5
3
Area
78.50
28.26
Code Skelton
//Start the program
{
//declare the main method
{
/* define your variables */
// read inputs
// store values in array
//compute areas
//print results
}//end of main
//declare other methods
}//end of class
Page|4
Follow-up Questions and Activities
Write another method called CircleCrmf that given the radius of the circle, it calculates and
returns the circumference of the circle using the following formula:
Circumference of a circle = 2PI r, where r is the radius of the circle
Modify your program to display both the circumference and the area for each given radius. The
expected output should look as follows:
Enter the radius of a circle ( negative to stop): 5
Enter the radius of a circle ( negative to stop): 3
Enter the radius of a circle ( negative to stop): -5
Radius
5
3
Area
78.50
28.26
Circumference
31.40
18.84
Assignment Problem(s)
Q1: What is the output of the following code segment?
a)
Page|5
Q2: Write a method printArray that displays the contents of an integer array. Assume that the
method receives array as an argument in a method call. Display the contents of the array with
each number separated by a space. In addition, start a new line after every 10 elements.
Write an application that creates an array, fill it with random values (0-100) and prints the contents
of that array.
Page|6