Great, Gammachu will prepare a full combined
lab report for all 6 exercises in ONE clean,
professional chunk — perfectly suitable for
converting into a PDF.
Here is your complete report:
---
AASTU – Electrical and Computer Engineering
Department
Object-Oriented Programming (ECEg3109)
Chapter 2 – Java Lab Report
Student: Mulusew Wasie Sisay
Date: __
---
Java Lab Report – Chapter 2
This report contains six Java programming
exercises covering fundamental programming
concepts, including decision-making, loops,
data types, arrays, and string manipulation.
---
2. Objective
The objective of this lab is to develop
foundational Java programming skills by writing
programs that demonstrate input handling,
conditional statements, loops, string
operations, and multidimensional arrays. Each
exercise strengthens understanding of core
Java concepts through practical
implementation.
---
3. Problem Statement
This lab consists of six programming tasks:
1. Write a Java program to compute the roots
of a quadratic equation.
2. Write a Java program to prompt the user for
all primitive data types and display them.
3. Analyze and determine the outputs of string
operations in a given Java program.
4. Write a Java program that demonstrates all
selection (conditional) statements.
5. Write a Java program that demonstrates the
three main loop structures.
6. Write a Java program that inputs an n × m
matrix and displays its elements.
Each task requires clear understanding of Java
syntax, program flow, user input, and output
formatting.
---
4. Procedures
Exercise 1: Quadratic Equation Solver
1. Start program and ask the user for
coefficients a, b, and c.
2. Compute the discriminant .
3. Check if roots are real, repeated, or
complex.
4. Calculate the roots based on the
discriminant.
5. Display the results.
---
Exercise 2: Primitive Data Type Input
1. Create a Scanner object for user input.
2. Prompt for each primitive type: byte, short,
int, long, float, double, char, boolean.
3. Store and display all values.
4. Close the Scanner.
---
Exercise 3: String Method Outputs
1. Define multiple strings.
2. Apply inspection, comparison, modification,
splitting, searching, and conversion methods.
3. Print the output of every method call.
4. Observe how each method behaves.
---
Exercise 4: Selection Statements
1. Input an integer from the user.
2. Use if, if-else, if-else-if, and nested if to
analyze the number.
3. Use a switch statement to interpret a grade
value.
4. Display the results.
---
Exercise 5: Loop Demonstration
1. Implement a for loop from 1 to 5.
2. Implement a while loop from 1 to 5.
3. Implement a do-while loop from 1 to 5.
4. Print loop counters.
---
Exercise 6: n × m Matrix Input
1. Read number of rows and columns.
2. Create a 2D array.
3. Input all elements row by row.
4. Display the matrix in tabular form.
---
5. Steps Taken to Solve the Problems
Identified required Java concepts for each
problem.
Used the Scanner class for user input across
multiple exercises.
Applied conditional statements (if, else if,
switch) to control program flow.
Used loop structures (for, while, do-while) to
repeat tasks.
Applied string methods to understand Java’s
String API.
Used nested loops for matrix input and display.
Ensured all programs compiled and executed
without errors.
---
6. Program Source Code
---
Exercise 1: Quadratic Equation Solver
import [Link];
public class QuadraticEquationSolver {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter coefficient a: ");
double a = [Link]();
[Link]("Enter coefficient b: ");
double b = [Link]();
[Link]("Enter coefficient c: ");
double c = [Link]();
double D = b * b - 4 * a * c;
if (D > 0) {
double r1 = (-b + [Link](D)) / (2 * a);
double r2 = (-b - [Link](D)) / (2 * a);
[Link]("Two real and
distinct roots:");
[Link]("Root 1 = " + r1);
[Link]("Root 2 = " + r2);
} else if (D == 0) {
double r = -b / (2 * a);
[Link]("One real repeated
root:");
[Link]("Root = " + r);
} else {
double real = -b / (2 * a);
double imag = [Link](-D) / (2 * a);
[Link]("Two complex
roots:");
[Link](real + " + " + imag +
"i");
[Link](real + " - " + imag +
"i");
}
[Link]();
}
}
---
Exercise 2: Primitive Data Types Demo
import [Link];
public class PrimitiveDataTypesDemo {
public static void main(String[] args) {
Scanner scanner = new
Scanner([Link]);
[Link]("Enter a byte value: ");
byte b = [Link]();
[Link]("Enter a short value: ");
short s = [Link]();
[Link]("Enter an int value: ");
int i = [Link]();
[Link]("Enter a long value: ");
long l = [Link]();
[Link]("Enter a float value: ");
float f = [Link]();
[Link]("Enter a double value: ");
double d = [Link]();
[Link]("Enter a char value: ");
char c = [Link]().charAt(0);
[Link]("Enter a boolean value:
");
boolean bool = [Link]();
[Link]("\nYou entered:");
[Link]("byte: " + b);
[Link]("short: " + s);
[Link]("int: " + i);
[Link]("long: " + l);
[Link]("float: " + f);
[Link]("double: " + d);
[Link]("char: " + c);
[Link]("boolean: " + bool);
[Link]();
}
}
---
Exercise 3: String Methods Program
public class StringMethod {
public static void main(String[] args) {
String str = " Addis Ababa Science and
Technology University ";
String str2 = "College of Engineering";
String str3 = "Department,of,Electrical,
and, Computer, Engineering";
[Link]("Length: " +
[Link]());
[Link]("Char at index 5: " +
[Link](5));
[Link]("Is Blank: " +
[Link]());
[Link]("Contains 'Ababa': " +
[Link]("Ababa"));
[Link]("To Upper Case: " +
[Link]());
[Link]("Trim: '" + [Link]() +
"'");
[Link]("Replace 'a' with '@': "
+ [Link]('a', '@'));
String[] parts = [Link](",");
[Link]("Split:");
for (String p : parts) [Link](" -
" + p);
[Link]("Repeat: " +
"AASTU".repeat(3));
}
}
---
Exercise 4: Selection Statements Demo
import [Link];
public class SelectionDemo {
public static void main(String[] args) {
Scanner scanner = new
Scanner([Link]);
[Link]("Enter an integer: ");
int num = [Link]();
if (num > 0) [Link]("Positive
number.");
else if (num < 0)
[Link]("Negative number.");
else [Link]("Zero.");
if (num % 2 == 0)
[Link]("Even number.");
else [Link]("Odd number.");
[Link]("Enter grade (A-F): ");
char grade = [Link]().charAt(0);
switch (grade) {
case 'A':
[Link]("Excellent!"); break;
case 'B': [Link]("Good
job."); break;
case 'C': [Link]("Fair.");
break;
case 'D': [Link]("Needs
improvement.");
break;
case 'F': [Link]("Failed.");
break;
default: [Link]("Invalid
grade.");
}
[Link]();
}
}
---
Exercise 5: Loop Demonstration
public class LoopDemo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++)
[Link]("For loop count: " +
i);
int j = 1;
while (j <= 5) {
[Link]("While loop count: "
+ j);
j++;
}
int k = 1;
do {
[Link]("Do-while count: " +
k);
k++;
} while (k <= 5);
}
}
---
Exercise 6: Matrix Input and Display
import [Link];
public class MultiDimensionalArrayDemo {
public static void main(String[] args) {
Scanner scanner = new
Scanner([Link]);
[Link]("Rows: ");
int rows = [Link]();
[Link]("Columns: ");
int cols = [Link]();
int[][] matrix = new int[rows][cols];
[Link]("Enter matrix values:");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
[Link]("Element [" + i + "]["
+ j + "]: ");
matrix[i][j] = [Link]();
}
[Link]("\nMatrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
[Link](matrix[i][j] + "\t");
[Link]();
}
[Link]();
}
}
---
8. Results
All six programs compiled and executed
successfully.
The quadratic solver correctly identified and
calculated different types of roots.
Primitive data type program accepted and
displayed all values.
String method program demonstrated string
inspection, comparison, modification, and
splitting.
Selection demo showed correct conditional
outputs.
Loop demo correctly displayed counting
sequences.
Matrix program accepted user input and
displayed the matrix in tabular form.
---
9. Conclusion
This lab strengthened understanding of
essential Java concepts, including variables,
primitive data types, conditional statements,
loops, and arrays. Through practical exercises,
I gained hands-on experience in writing
modular programs, handling user input,
implementing logic structures, and
manipulating strings and multidimensional
data. These foundational skills are crucial for
advanced Java and Object-Oriented
Programming topics.
---