Lab 03 - Structured Programming
Lab 03 - Structured Programming
BS(CS)- Semester 02
(Spring 2018)
Introduction to :
1. String
2. Control Structures (if, if else, switch, for, while and do while)
3. Arrays
1. String
Write a Java program that enters a 10-digit string as a typical U.S. telephone number. Extract the
3-digit area code, the 3-digit "exchange," and the remaining 4-digit number as separate strings, prints
them, and then prints the complete telephone number in the usual form atting. A sample run might
look like this:
Enter 10-digit telephone number: 1234567890
You entered 1234567890
The area code is 123
The exchange is 456
The number is 7890
The complete telephone number is (123)456-7890
import java.util.Scanner;
2. Control Structures
Write a Java program that generates a random year between 1800 and 2000 and then reports whether
it is a leap year. A leap year is an integer greater than 1584 that is either divisible by 400 or is
divisible by 4 but not 100. To generate an integer in the range 1800 to 2000, use
The rand.nextInt() method returns the ineger between 0 until 199 (excluding 200). The
transformation 1800 converts a number in the range the range l800 ≤ year < 2000.
import java.util.Random;
public class TestLeapYear {
public static void main(String[] args){
Random rand = new Random();
int year = rand.nextInt(200) + 1800;
System.out.println("The year is "+ year);
if(year%400 == 0 || year%100 != 0 && year%4 == 0)
System.out.print("That is a leap year..");
else
System.out.print("That is not a leap year..");
}
}
3. Arrays
An example of simple array declared and initilized
int array[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
System.out.println("Index Value" );
for ( int counter = 0; counter < array.length; counter++ )
System.out.println( counter+" "+ array[counter] );
}
}
2D Array:
CS Department, BUKC 3/8 Semester Spring 2018
CSL-210: Object-Oriented Programming Lab Lab03: Strucutred Programming with Java
A java program to calculate the sum of each row, colum, left diagonal and right diagonal of a two-
dimensional (2D) array of size MxM. for example:
[0] 1 2 3 4 5
R [1] 6 7 8 9 10
O [2] 11 12 13 14 15
W [3] 16 17 18 19 20
S
[4] 21 22 23 24 25
[0] [1] [2] [3] [4]
COLUMNS
Sum of row 1: 15
Sum of row 2: 40
Sum of row 3: 65
Sum of row 4: 90
Sum of row 5: 115
Sum of Col 1: 55
Sum of Col 2: 60
Sum of Col 3: 65
Sum of Col 4: 70
Sum of Col 5: 75
Exercises
Exercise 1 (StringParser.java)
Write a java program that have this string “Hello! I am string in java. I have several function and I
am very “Important ”# string_is _importnat” and split it as follows.
Exercise 2 (Fibonacci.java)
Write a java program that as asks user to entera valid integer range and print Fibonacci series
between that range. NOTE: Fibonacci series is a series whose next didgit is the sum of previous two
CS Department, BUKC 5/8 Semester Spring 2018
CSL-210: Object-Oriented Programming Lab Lab03: Strucutred Programming with Java
digits e.g. 0 1 1 2 3 5 8 13 ……. So program should work as follow
Exercise 3 (BMICalculator.java)
Write a program that calculates the user’s body mass index (BMI) and categorizes it as underweight,
normal, overweight, or obese, based on the table from the United States Centers for Disease Control:
To calculate BMI based on weight in pounds (lb) and height in inches (in), use this formula (rounded
to tenths):
Exercise 4 (Arithmatic.java)
Write a java program to compute quotient and remainder of a number without using division ('/')
operator and modulo ('%') operator.
CS Department, BUKC 6/8 Semester Spring 2018
CSL-210: Object-Oriented Programming Lab Lab03: Strucutred Programming with Java
Exercise 5 (Occurrances.java)
Write a java program that gets input from a user into an array (sixe defined by user ) then find the
max and min numbers found in array as well as the index at which they are found at. Then calculate
the difference between two values and the difference between index as well. See screen shot for
refernace. HINT:use java.lang.Math.abs package to print absolute value between index differences
to avoid negative value.
Exercise 6 (Histogram.java)
Given the following array, display its data graphically by plotting each numeric value as a bar of
asterisks (*) as shown in the diagram.
int[] array = {10, 19, 5, 1, 7, 14, 0, 7, 5};
CS Department, BUKC 7/8 Semester Spring 2018
CSL-210: Object-Oriented Programming Lab Lab03: Strucutred Programming with Java
Element Value Histogram
0 10 **********
1 19 *******************
2 5 *****
3 1 *
4 7 *******
5 14 **************
6 0
7 7 *******
8 5 *****
Exercise 7 (Transpose.java)
2 0 4 2 6 3
9 9 1 0 4 1
7 1 2 3 7 4
2 2 2 7 1 6
1 5 8 7 4 1
Design then implement a Java program that will produce its transpose and print it along with the
original one.
HINT:
The transpose of matrix A = [aij] is the mxn matrix AT defined by AT = [aji]
So, the transpose of the matrix above is:
2 9 7 2 1
0 9 1 2 5
4 1 2 2 8
2 0 3 7 7
6 4 7 1 4
3 1 4 6 1
Exercise 8 (MatricesMultiplication.java)
Write a Java program that will calculate its product and print the resultant matrix.
CS Department, BUKC 8/8 Semester Spring 2018
CSL-210: Object-Oriented Programming Lab Lab03: Strucutred Programming with Java
Then multiple the resultane matrix with a scalar value 2 the resultant will be as follows