You are on page 1of 11

DEPARTMENT OF COMPUTER SCIENCE

UNIVERSITY OF NIGERIA, NSUKKA

LABORATORY EXERCISES FOR:

CSC201, COMPUTER PROGRAMMING I

DEVELOPED BY

DR. OGUIKE, OSONDU EVERESTUS, Ph.D

Introduction:
This laboratory exercise focuses on the fundamental of Java programming language.

Requirements for the Laboratory Sessions:


A good standalone personal computer with Java compiler installed on it or a networked computer
system with Java compiler running on the server.

Lab. Session 1. Fundamental of Java Program


Objectives:
The following are the objectives of this laboratory session:
 To introduce to the students, the main method of a Java program.
 To introduce to the students, the output statement of a Java program.
 To introduce to the students, the Scanner class, and how to use it.
Learning Outcome:
At the end of this laboratory session, the student should be able to:
 Write simple Java program that will contain the main method.
 Use the output statement in Java to display information.
 Use one of the methods in the Scanner class to read data into the program.

Theoretical Overview
The entry point of execution of Java program is the main method.

Consider the following sample Java codes.

import java.util.Scanner;
class eg1
{
public static void main(String args[])
{
int numb = 0;
Scanner myinput = new Scanner(System.in);
System.out.print(“Enter an integer number : “);
numb = myinput.nextInt();
System.out.println(“This is my first Java program.”);
System.out.print(“The number you enter is : “+ numb);
}
}

0 Enter the sample program using an appropriate Java text editor, compile it, test it and
understand what the program does.

1. Modify the sample program, so that it requests for two integer numbers, and it displays
your name as the author and the two integer numbers that you have read. Compile it and
test it.

2. Modify the sample program so that it reads two integer numbers and displays the sum of
the two numbers.

3. Modify the sample program so that it reads two integer numbers and displays the product
of the two numbers.

4. Modify the sample program so that it reads two integer numbers and divides the two
numbers and displays the result of the division.

5. Make screen print of all the modified programs and paste them in one file. Your class rep.
will collate them in one folder and submit to me as soft copy.

Lab. Session 2. Use of Basic Looping Control Structures


Objectives:
The following are the objectives of this laboratory session:
 To introduce to the students, the use of the ‘for statement’ in Java.
 To introduce to the students, the use of the ‘while statement’ in Java.
 To introduce to the students, the use of the ‘do while statement’ in Java.
Learning Outcome:
At the end of this laboratory session, the student should be able to:
 Write simple Java program using ‘for’ statement.
 Write simple Java program using ‘while’ statement.
 Write simple Java program using ‘do while’ statement.

Theoretical Overview
The looping control structures are those control structures that enable the computer to execute
some statements a particular number of times.

Consider the following sample Java codes.

import java.util.Scanner;
class eg2
{
public static void main(String args[])
{
int numb = 0, sum = 0;
Scanner myinput = new Scanner(System.in);
System.out.print(“Enter an integer number : “);
numb = myinput.nextInt();
for (int i = 1; i <= numb; i++)
{
sum = sum + i;
}
System.out.println(“The result is ” + sum);
}
}

0. Enter the sample program using an appropriate Java text editor, compile it, run it and
understand what the program does.

1. Test the sample program with the following integer numbers as number, and write the
corresponding output, sum.

Numb 20 30 40 50 60 70 80
Sum

2. Modify the sample program using ‘while’ statement. Compile it and test it with the
following values of number and write the corresponding output, sum.

Numb 5 10 15 20 25 30 35
Sum

2. Modify the sample program using ‘do while’ statement. Compile it and test it with the
following values of numb, and write the corresponding output, sum.
Numb 3 13 23 33 43 53 63
Sum

3. Modify the sample program so that it request for an integer number n, afterwards, it
calculates n!. Compile it and test it with the following values of n, and write the
corresponding output. Use any looping control structure of your choice.

Numb 2 4 6 8 10 12 14
Output

Make screen print of all the programs and paste them in one file, together with the
tabulated output. The tabulated output should follow its program.

Lab. Session 3. Use of Basic Selective Control Structures


Objectives:
The following are the objectives of this laboratory session:
 To introduce to the students, the use of the ‘if statement’ in Java.
 To introduce to the students, the use of the ‘switch statement’ in Java.
Learning Outcome:
At the end of this laboratory session, the student should be able to:
 Write simple Java program using ‘if’ statement.
 Write simple Java program using ‘switch’ statement.

Theoretical Overview
Selective control structures are those control structures that the computer uses to select the
execution of statements, based on two or three alternatives.

Consider the following sample Java codes.

import java.util.Scanner;
class eg3
{
public static void main(String args[])
{
int first = 0, second = 0;
Scanner myinput = new Scanner(System.in);
System.out.print(“Enter the first integer number : “);
first = myinput.nextInt();
System.out.print(“Enter the second integer number : “);
second = myinput.nextInt();

if (first < second)


{
System.out.print(“The first number is less than the second number”);
}
else
{
System.out.println(“The first number is not less than the second number”);
}
}
}

0. Enter the sample program using appropriate Java text editor, compile it, test it and
understand what the program does.

1. Rewrite the sample program so that it displays any of the messages:


 The first number is equal to the second number.
 The first number is not equal to the second number.

2. Rewrite the sample program so that it displays any of the messages:


 The first number is greater than the second number.
 The first number is not greater than the second number.

3. Rewrite the sample program so that it adds the first and second numbers and determines
if the sum is less than the first number or otherwise.

4. Rewrite the sample program so that it subtracts the second number from the first number
and determines if the difference is less than the first number or otherwise.

5. Consider the following sample program:

import java.util.Scanner;
class eg4
{
public static void main(String args[])
{
int numb = 0;
Scanner myinput = new Scanner(System.in);
System.out.print(“Enter the first integer number : “);
numb = myinput.nextInt();
switch (numb)
{
case 70 : System.out.print(“The number is 70”); break;
case 80 : System.out.print(“The number is 80”); break;
default :
System.out.print(“The number is neither 70 nor 80”); break;
}
}
}

5.0 Enter the sample program in No. 5, using appropriate text editor, compile it, test it and
understand what it does.
5.1 Modify the sample program in No. 5, so that the program displays any of the following
messages:
 The number is 10
 The number is 20
 The number is 30
 The number is 40
 The number is 50
 The number is 60
 The number is 70
 The number is 80
 The number is 90
 The number is 100
 The number is neither 10, 20, 30, 40, 50, 60, 70, 80, 90 nor 100.

5.2 Modify the sample program in No. 5, so that it requests for the month and year as two
integer numbers, and it determines and displays the number of days in that month of the
year.

5.3 Make screen print of all the modified programs, and paste them in one file.

Lab. Session 4. Simple Statistical Computations


Objectives:
The following are the objectives of this laboratory session:
 To write Java program that will compute the total of n different scores.
 To write Java program that will compute the average of n different scores.
 To write Java program that will compute the sum of product of n different scores.
 To write Java program that will compute the sum of squares of n different scores.
 To write Java program that will compute the variance of n different scores.
Learning Outcome:
At the end of this laboratory session, the student should be able to:
 Write simple Java program that will compute the total of n different scores.
 Write simple Java program that will compute the average of n different scores.
 Write simple Java program that will compute the sum of the product of n different scores.
 Write simple Java program that will compute the sum of squares of n different scores.
 Write simple Java program that will compute the variance of n different scores.

Theoretical Overview
Suppose Xi , Yi are the variables for the ith pairs of scores in n collection of
n pairs of scores, the statistical formula that compute the following statistics are:
n
∑ Xi
Total = i=1
n
∑ Xi
i=1

Average = n
n
∑ X iY i
Sum of Product = i=1

n
∑ X i2i
Sum of Squares = i=1

( (∑ ) )
n 2
Xi
2 i=1
∑X i

n
Variance = (n−1)

Consider this sample program that requests for an integer number n, afterwards, it
requests for n different scores, and computes and displays the average and the total
of the n scores.

import java.util.Scanner;
class eg5
{
public static void main(String args[])
{
double numb = 0.0, score = 0.0, total = 0.0, average = 0.0;
Scanner myinput = new Scanner(System.in);
System.out.print(“Enter an integer number :“);
numb = myinput.nextDouble();
for (int i = 1; I <= numb; i++)
{
System.out.print(“Enter a score : “);
score = myinput.nextDouble();
total = total + score;
}
average = total/numb;
System.out.println(“The total of the scores is : “ + total);
System.out.print(“The average of the scores is : “ + average);
}
}
0. Type the sample program above, compile it and test it with the following values of score
and write the corresponding output, average.

Score 20 30 40 50 60 70 80
Average

1. Modify the sample program using while statement, The modified program will compute
the variance of n different scores. Compile it, test it with the following values, and write
the corresponding output as shown below:
Score1 (X) 3 5 9 11 15 17 19
Variance

2 Modify the sample program using do while statement, The modified program will
compute the variance of n different scores. Compile it, test it with the following values,
and write the corresponding output as shown below:
Score1 (X) 3 5 9 11 15 17 19
Variance

3 Modify the sample program using for statement. The modified program will compute the
variance of n different scores. Compile it, test it with the following values, and write the
corresponding output as shown below:
Score1 (X) 3 5 9 11 15 17 19
Variance

Lab. Session 5. Statistical Computations


Objectives:
The following are the objectives of this laboratory session:
 To write Java program that will compute regression parameter, beta.
 To write Java program that will compute regression parameter, alpha.
 To write Java program that will compute the Pearman’s correlation parameter.
Learning Outcome:
At the end of this laboratory session, the student should be able to:
 Write Java program that will compute the regression parameter, beta for n different
scores.
 Write Java program that will compute the regression parameter, alpha.
 Write Java program that will compute the Pearman’s correlation parameter.

Theoretical Overview
Suppose Xi , Yi are the variables for the ith pairs of scores in n collection of
n pairs of scores, the statistical formula that compute the following statistics are:
n
∑ X iY i
Sum of Product = i=1
n
∑ X i2i
Sum of Squares = i=1

( (∑ )(∑ ) )
n n

n
Xi Yi
i=1 i=1
∑ X iY i− n
i=1

( (∑ ) )
n 2

n
Xi
2 i=1
∑ X i− n
i=1
Beta =
n n
∑Yi ∑ Xi
i=1
−Beta∗ i=1
Alpha = n n

(∑ (∑ )(∑ ))
n n n
n X i Y i− Xi Yi
i=1 i =1 i=1
r=

√( (∑ ) )( ∑ (∑ ) )
n n 2 n n 2
n∑ X 2− Xi n Y 2− Yi
i=1 i i=1 i =1 i i =1

Consider this sample program that requests for an integer number n, afterwards, it
requests for n different scores, and computes and displays the average and the total
of the n scores.

import java.util.Scanner;
class eg5
{
public static void main(String args[])
{
double numb = 0.0, scorex = 0.0, scorey = 0.0, sumpro = 0.0, average = 0.0,
sumsqx = 0.0;
Scanner myinput = new Scanner(System.in);
System.out.print(“Enter an integer number :“);
numb = myinput.nextDouble();
for (int i = 1; I <= numb; i++)
{
System.out.print(“Enter a pair of score : “);
scorex = myinput.nextDouble();
scorey = myinput.nextDouble();
sumpro = sumpro + scorex * scorey;
sumsqx = sumsqx + scorex * scorex;
}
System.out.println(“The sum of the product of the scores is : “ + sumpro);
System.out.println(“The sum of squares of the scores is : “ + sumsqx);
}
}

1. Modify the sample program using while statement. It will request for an integer number n
afterwards it will request for n pairs of scores, Xi, Yi and it will compute and display this

( (∑ )(∑ ) )
n n

n
Xi Yi
i=1 i=1
∑ X iY i− n
i=1

( (∑ ) )
n 2

n
Xi
2 i=1
∑ X i− n
i=1
statistics, Beta =
Compile it and test it will the following pairs of scores and write the corresponding
output in the Table below:
Scorex 10 20 25 35 53 64 75
Scorey 2 3 4 5 6 7 8
Beta

2 Modify the sample program using do while statement. It will request for an integer
number n afterwards it will request for n pairs of scores, Xi, Yi and it will compute and
n n
∑Yi ∑ Xi
i=1 i=1
−Beta∗
display this statistics, Alpha = n n Compile it and test it will the
following pairs of scores and write the corresponding output in the Table below:
Scorex 10 20 25 35 53 64 75
Scorey 2 3 4 5 6 7 8
Alpha
3 Modify the sample program using do while statement. It will request for an integer
number n afterwards it will request for n pairs of scores, Xi, Yi and it will compute and

( ( )( ))
n n n
n ∑ X i Y i− ∑ Xi ∑ Y i
i=1 i =1 i=1
r=

√( (∑ ) )( ∑ (∑ ) )
n n 2 n n 2
n ∑ X 2− Xi n Y 2− Yi
i=1 i i=1 i =1 i i =1
display this statistics,
Compile it and test it will the following pairs of scores and write the corresponding
output in the Table below:
Scorex 10 20 25 35 53 64 75
Scorey 2 3 4 5 6 7 8
r

You might also like