Professional Documents
Culture Documents
DEVELOPED BY
Introduction:
This laboratory exercise focuses on the fundamental of Java programming language.
Theoretical Overview
The entry point of execution of Java program is the main method.
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.
Theoretical Overview
The looping control structures are those control structures that enable the computer to execute
some statements a particular number of times.
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.
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.
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();
0. Enter the sample program using appropriate Java text editor, compile it, test it and
understand what the program does.
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.
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.
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
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