Oop Lab Report
Oop Lab Report
1-SIMON GELETAW………………………………………………..3518/16
2-TIZITA SINTAYEHU………………………………………………3675/16
SUBMITTED TO : Mr Daniel T.
SUBMISSON DATE-March 25.2025.
Introduction
This Lab report provides a structured approach to understanding Object-Oriented
Programming (OOP) in Java through hands-on exercises. It covers core
programming concepts such as basic input-output, arithmetic operations, control
structures, variable types, loops, and arrays. Each exercise is designed to enhance
problem-solving skills while applying fundamental Java concepts.
Abstract
The report consists of practical Java programs that introduce fundamental OOP
concepts.
The practices and exercises include-
- Displaying messages
-Performing mathematical operations
- Working with variables and control structures
- Implementing flow control mechanisms
- Using arrays and ArrayLists
- Developing interactive applications like a calculator and a number-guessing game
Objectives
*Develop a solid understanding of Java programming syntax and structure.
*Learn to use arithmetic operations and methods efficiently.
*Implement conditional statements and loops to control program flow.
*Work with local, instance, and class variables.
*Utilize arrays and ArrayLists for data management.
*Create interactive programs using object-oriented principles.
Specific Objectives
1. Basic Output - Display a welcome message on the screen.
2. User Input and Operations - Take user input and perform basic arithmetic operations.
3. Control Structures - Use loops and switch cases for menu-driven programs.
4. Variable Scope - Demonstrate the use of local, instance, and class variables.
5. Flow Control - Implement methods for square root, absolute values, and exponentiation.
6. Conditional Statements - Solve a quadratic equation.
7. Loops and Iteration - Check for prime numbers and print a series of prime numbers.
8. Arrays and ArrayLists - Perform operations like finding total, average, max, and min.
9. Interactive Applications - Implement a number-guessing game and a menu-based calculator.
Practice 1: Write a java program to display a string “Welcome to OOP with java Programming!” on
to the monitor.
Objective: To print a simple welcome message on the console.
Code
public class Welcome {
} Output
Expected Output
Welcome to OOP with Java Programming!
We understood
-The structure of a Java program
- Using System.out.println() to display messages.
Practice 2: Write a java program to add two numbers by accepting the user input from keyboard.
The objective
1. To understand the scanner(input)
2. To differentiate access modifiers particularly private
3. How to create methods
4. How to call methods in the main method
5. How to declare variable in
In order to create simple java program to add two numbers that are accepted from the keyboard or user we
will have to use “import java.util.Scanner” this line of code which will allow as to use scanner class , with
the help of this code will be able to access the scanner class. In main method we used the following line
of code “scanner s =new Scanner(system.in)” we are saying that create new scanner named ”s” , then we
accept two numbers using the “s”.
from practice 2 we understand in access modifier is used to set visibility(access level ) of classes methods
and constructs and variables and used for who can access the data or method so private is most restricted
only with in same class used to hide internal detail .
we used static method static method is belong to the class not to specific object .the we define the return
data type and finally the method name .
in order to declare variable, we used data type then valid variable name then we successfully declare a
variable.
In order to call the function or a method we will use the name of method then the parameter that are
passed to the function.
CODE Output
import java. util.Scanner;
public class Summation
{
private static float add(float a, float b)
{
return (a+b);
}
public static void main(String [] arg)
{
System.out.print("Input 1:");
Scanner s = new Scanner(System.in);
float f1 = s.nextFloat();
System.out.print("Input 2:");
float f2 = s.nextFloat();
float r = add(f1, f2);
System.out.println("Sum ="+r);
}
Practice 3: Add , sub, mul , and div two numbers respectively and display the manipulated result
on to the monitor.
Objective by modify practice 2 and we will understand more about methods and how to scan from
keyboard
import java.util.Scanner;
public class SimpleCalc
{
private static float add (float a, float b)
{
return (a+b);
}
private static float sub (float a, float b)
{
return (a-b);
}
private static float mul(float a, float b)
{
return (a*b);
}
private static float div(float a, float b)
{
return (a/b);
}
public static void main(String [] arg)
{
System.out.print("Input 1:");
Scanner s = new Scanner(System.in);
float f1 = s.nextFloat();
System.out.print("Input 2:");
float f2 = s.nextFloat();
System.out.println(f1 + '+' + f2 + '=' + add(f1, f2));
System.out.println(f1 + '-' + f2 + '=' + sub(f1, f2));
System.out.println(f1 + '*' + f2 + '=' + mul(f1, f2));
System.out.println(f1 + '/' + f2 + '=' + div(f1, f2));
}
}
Error
-To display the output using single quotes
After fixing the error
import java.util.Scanner;
public class SimpleCalc
return (a+b);
return (a-b);
return (a*b);
return (a/b);
System.out.print("Input 1:");
float f1 = s.nextFloat();
System.out.print("Input 2:");
float f2 = s.nextFloat();
we understood:
- The difference and their purpose of single and double quotes
- How to perform and display the results of multiple arithmetic operations.
- The importance of method reusability in Java.
Lab Activity-Temperature conversion
import java.util.Scanner;
public class TempConverter {
scanner.close();
Practice 4 :Write a Java program to demonstrate the local, instance and class variables as follow
Objective
1. To demonstrate local ,instance and class variables
public class LocalVariable
int batchYear=2011;
int year=2;
lv.TellMeMyBatch();
}}
This error is due to the variables are declared only for specific class and not known by the main method so we solve the error
we moved the batchYear and year variables outside of the TellMeMyBatch() method and declared them
as instance variables at the top of the class.
In the main method, I accessed batchYear and year via the lv object (lv.batchYear and lv.year).
public class LocalVariable {
int year = 2;
lv.TellMeMyBatch();
From this practice we understand that if we don’t assign specifically the variable will be
instance. And to create local variable we will use “ LocalVariable lv = new LocalVariable();”
Practice 5 :Modify Practice 3 by adding Control Structure Switch and do … While loop to make it Menu Based Program.
Objective
import java.util.Scanner;
float l, r;
do {
calc.menu();
op = s.next().charAt(0);
switch(op) {
case '+':
l = s.nextFloat(); r = s.nextFloat();
System.out.println(l+"+"+r+" = "+calc.add(l,r));
break;
case '-':
l = s.nextFloat(); r = s.nextFloat();
System.out.println(l+"-"+r+" = "+calc.sub(l,r));
break;
case '*':
l = s.nextFloat(); r = s.nextFloat();
System.out.println(l+"*"+r+" = "+calc.mul(l,r));
break;
case '/':
l = s.nextFloat(); r = s.nextFloat();
System.out.println(l+"/"+r+" = "+calc.div(l,r));
break;
default:
System.out.println("Wrong
Operator!");
break;
yn = s.next().charAt(0);
class Calculator {
if (rop != 0) {
return 0;
System.out.println("....................................:");
System.out.println("....................................:\n");