0% found this document useful (0 votes)
100 views8 pages

Lab 03 - Structured Programming

This document provides exercises for a lab on structured programming with Java. It introduces strings, control structures like if/else and for loops, and arrays. It includes 6 exercises practicing skills like splitting strings, generating Fibonacci sequences, calculating BMI from user input, arithmetic without division/modulo, finding min/max in arrays, and displaying arrays as histograms. Solutions are to be written in Java.

Uploaded by

Sajid Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
100 views8 pages

Lab 03 - Structured Programming

This document provides exercises for a lab on structured programming with Java. It introduces strings, control structures like if/else and for loops, and arrays. It includes 6 exercises practicing skills like splitting strings, generating Fibonacci sequences, calculating BMI from user input, arithmetic without division/modulo, finding min/max in arrays, and displaying arrays as histograms. Solutions are to be written in Java.

Uploaded by

Sajid Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 8

CSL-210: Object-Oriented Programming Lab

BS(CS)- Semester 02
(Spring 2018)

Lab03: Strucured Programming with Java

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;

public class TelephoneNumbers {

public static void main(String[] args){


Scanner keyboard = new Scanner(System.in);
String telephone, areaCode,exchange,number;
System.out.print("Enter 10-digit telephone number: ");
telephone = keyboard.nextLine();
System.out.println("You entered "+ telephone);
areaCode = telephone.substring(0,3);
System.out.println("The area code is "+ areaCode);
exchange = telephone.substring(3,6);
System.out.println("The exchange is "+ exchange);
number = telephone.substring(6);
System.out.println("The number is " + number);
System.out.println("The complete telephone number is
("+areaCode+")"+exchange+"-"+ number);
}
}
CS Department, BUKC 2/8 Semester Spring 2018
CSL-210: Object-Oriented Programming Lab Lab03: Strucutred Programming with Java

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

Random rand = new Random();

int year = rand.nextInt(200) + 1800;

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

public class TestArray {


public static void main(String[] args){

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

Sum of Left Diagonal: 65


Sum of Right Diagonal: 65

public class MatrixOperations {


final static int ROWS =5;
final static int COLUMNS =5;

public static void main(String [] args){


int [][]m = new int[ROWS][COLUMNS];
int rowSum, colSum, leftDiagonalSum, rightDiagonalSum;
Random rand = new Random();
//Assigning Random values to array elements
for(int i=0; i<ROWS; i++)
for (int j=0; j<COLUMNS; j++)
m[i][j]=rand.nextInt(25)+1;
//Calculating the sum of each row
for(int i=0; i<ROWS; i++){
rowSum=0;
for (int j=0; j<COLUMNS; j++){
rowSum+=m[i][j];
}
System.out.println("Sum of Row "+(i+1)+" : "+rowSum);
}
//Calculating the sum of each column
for(int i=0; i<ROWS; i++){
colSum=0;
for (int j=0; j<COLUMNS; j++){
colSum+=m[j][i];
}
System.out.println("Sum of Col "+(i+1)+" : "+colSum);
CS Department, BUKC 4/8 Semester Spring 2018
CSL-210: Object-Oriented Programming Lab Lab03: Strucutred Programming with Java
}
//Calculating the sum of left diagonal
leftDiagonalSum=0;
for(int i=0; i<ROWS; i++){
leftDiagonalSum+=m[i][i];
}
System.out.println("Sum of left diagonal : "+leftDiagonalSum);

//Calculating the sum of right diagonal


rightDiagonalSum=0;
for(int i=0; i<ROWS; i++){
rightDiagonalSum+=m[i][(ROWS-1)-i];
}
System.out.println("Sum of left diagonal : "+rightDiagonalSum);
}
}

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.

NOTE: Use substring(int beginIndex) OR substring(int beginIndex, int endIndex) and


indexOf(String str) methods of String.

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:

BMI Weight Status


Below 18.5 Underweight
18.5 – 24.9 Normal
25.0-29.9 Overweight
30.0 and above Obese

To calculate BMI based on weight in pounds (lb) and height in inches (in), use this formula (rounded
to tenths):

Prompt the user to enter weight in pounds and height in inches.


IMP: Repeat this proves again unless user chose not to calculate BMI index.
HINT: use do-while
[SAMPLE RUN]
Enter your weight in whole pounds: 110
Enter your height in whole inches: 60
You have a BMI of 21.5, and your weight status is normal.

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)

Suppose you have the following matrix:

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)

Suppose you have the following matrices:

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

You might also like