0% found this document useful (0 votes)
2 views11 pages

Oop Lab Report

This lab report from Hawassa University focuses on Object-Oriented Programming (OOP) in Java, detailing hands-on exercises that cover core concepts such as input-output, arithmetic operations, control structures, and data management. The report outlines specific objectives and practical exercises, including creating interactive applications like a calculator and a number-guessing game. It emphasizes the development of programming skills through structured practice and understanding of Java syntax and principles.

Uploaded by

Simon Geletaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Oop Lab Report

This lab report from Hawassa University focuses on Object-Oriented Programming (OOP) in Java, detailing hands-on exercises that cover core concepts such as input-output, arithmetic operations, control structures, and data management. The report outlines specific objectives and practical exercises, including creating interactive applications like a calculator and a number-guessing game. It emphasizes the development of programming skills through structured practice and understanding of Java syntax and principles.

Uploaded by

Simon Geletaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

HAWASSA UNIVERSITY

IOT- FACULTY OF INFORMATICS

Department of Computer Science

Lab Report- Object-Oriented Programming (OOP)


SECTION I
NAME ID

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 {

public static void main (String[] args)

System.out.print(“Welcome to OOP With Java Programming!”);

} 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

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));

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 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter temperature in Celsius: ");

double celsius = scanner.nextDouble();

System.out.println("Temperature in Fahrenheit: " + c2f(celsius));

System.out.print("Enter temperature in Fahrenheit: ");

double fahrenheit = scanner.nextDouble();

System.out.println("Temperature in Celsius: " + f2c(fahrenheit));

scanner.close();

public static double f2c(double fahrenheit) {

return (fahrenheit - 32) * 5.0 / 9.0;

public static double c2f(double celsius) {

return celsius * 9.0 / 5.0 + 32;

We learned from this lab activity

- The technique of using static methods for temperature conversion.

-Applying mathematical formulas in Java.

- How to handle user input for different types of data.

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

public void TellMeMyBatch(){

int batchYear=2011;

int year=2;

System.out.println("Your Batch is " +batchYear+ " year="+year);

public static void main (String [] args)

LocalVariable lv=new LocalVariable();

lv.TellMeMyBatch();

System.out.println("Your Batch is " + batchYear + " year="+year);

}}

During the practice 4 we encounter the following error

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 {

// Declare batchYear and year as instance variables

int batchYear = 2011;

int year = 2;

public void TellMeMyBatch() {

System.out.println("Your Batch is " + batchYear + " year=" + year);

public static void main(String[] args) {

LocalVariable lv = new LocalVariable();

lv.TellMeMyBatch();

System.out.println("Your Batch is " + lv.batchYear + " year=" +


lv.year);}}

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

 To demonstrate the use of conditional statements (switch and if


statements) for choosing different operations.
 To implement a loop to allow users to perform multiple calculations
without restarting the program

import java.util.Scanner;

public class SimpleCalcDemo {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

Calculator calc = new Calculator();

float l, r;

char op, yn;

do {

calc.menu();

op = s.next().charAt(0);

switch(op) {

case '+':

System.out.print("Enter Left and Right Operands: ");

l = s.nextFloat(); r = s.nextFloat();

System.out.println(l+"+"+r+" = "+calc.add(l,r));

break;

case '-':

System.out.print("Enter Left and Right Operands: ");

l = s.nextFloat(); r = s.nextFloat();

System.out.println(l+"-"+r+" = "+calc.sub(l,r));

break;
case '*':

System.out.print("Enter Left and Right Operands: ");

l = s.nextFloat(); r = s.nextFloat();

System.out.println(l+"*"+r+" = "+calc.mul(l,r));

break;

case '/':

System.out.print("Enter Left and Right Operands: ");

l = s.nextFloat(); r = s.nextFloat();

System.out.println(l+"/"+r+" = "+calc.div(l,r));

break;

default:

System.out.println("Wrong
Operator!");

break;

System.out.print("If you want to


continue, press Y/y: ");

yn = s.next().charAt(0);

} while(yn == 'y' || yn == 'Y');

class Calculator {

public float add(float lop, float rop) { return lop + rop; }

public float mul(float lop, float rop) { return lop * rop; }

public float sub(float lop, float rop) { return lop - rop; }

public float div(float lop, float rop) {

if (rop != 0) {

return lop / rop;


} else {

System.out.println("Error: Division by zero");

return 0;

public void menu() {

System.out.println("....................................:");

System.out.println("...........Enter + for Add..........:");

System.out.println("...........Enter - for Sub..........:");

System.out.println("...........Enter * for Mul..........:");

System.out.println("...........Enter / for Div..........:");

System.out.println("....................................:\n");

System.out.println("What do you want to do?");

You might also like