Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Lab Report No _08_

OOP

Submitted By:
Muhammad Saif Ur Rehman
CS-23-156
“D”

Submitted to:
SIR M.ASIF

Dated:
27/04/24

Department of Computer Science,


HITEC University, Taxila

Lab No 1: Write a program that computes the square and cube of the given
number using the

Solution:

Brief description (3-5 lines)


The hello class defines methods to calculate the square and cube of a number entered by the
user, displaying the results. It utilizes an inner class cube to perform the cube calculation,
demonstrating encapsulation and object-oriented design.

The code
import java.util.Scanner;
public class hello {
private int a;
private int result;
public void square(){
this.a=a;
Scanner scanner = new Scanner(System.in);
System.out.println("enter a number:");
a = scanner.nextInt();
this.result=result;
result= a*a;
System.out.println("result :" + result);
cube ob1 = new cube();
ob1.cube();
}
public class cube extends hello{
public void cube(){
int a;
int result;
System.out.println("enter a number for cube:");
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
result = a*a*a;
System.out.println("result:"+result);

}
}
public static void main(String []args){
hello ob1 = new hello ();
ob1.square();

}
}

The results (Screenshot)

Page 1 of 7
Lab Example No 2: Create a base class Student that contains :

Solution:
Brief description (3-5 lines)
This Java program defines two classes: Student and Marks. The Student class contains data
members for name, roll number, and address, along with a method to get these details from
the user. The Marks class inherits from the Student class and includes functionality to input
marks in three subjects, calculate the total and average marks, and display the marks details.
Finally, the hello class contains the main method where an object of the Marks class is
created, and its methods are invoked to input marks, calculate, and display the marks details.

The code
import java.util.Scanner;
class Student {
protected String name;
protected int rollNumber;
protected String address;

public void getData() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter student name:");
name = scanner.nextLine();
System.out.println("Enter roll number:");
rollNumber = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter address:");
address = scanner.nextLine();
}
}

Page 2 of 7
class Marks extends Student {
private int[] marks = new int[3];

public void inputMarks() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter marks in three subjects:");
for (int i = 0; i < 3; i++) {
System.out.print("Subject " + (i + 1) + ": ");
marks[i] = scanner.nextInt();
}
}
public void calculate() {
int totalMarks = 0;
for (int mark : marks) {
totalMarks += mark;
}
double averageMarks = (double) totalMarks / marks.length;

System.out.println("\nMarks Detail:");
for (int i = 0; i < 3; i++) {
System.out.println("Subject " + (i + 1) + ": " + marks[i]);
}
System.out.println("Total Marks: " + totalMarks);
System.out.println("Average Marks: " + averageMarks);
}
}

public class hello {


public static void main(String[] args) {
Marks studentMarks = new Marks();
studentMarks.getData();
studentMarks.inputMarks();
studentMarks.calculate(); }
}

The results (Screenshot)

Lab Example No 3: Create a base class Employee that stores name (a string)
and identification number

Page 3 of 7
Solution:
Brief description (3-5 lines)
This Java program defines classes representing different types of employees (Manager,
Scientist, and Clerks) that inherit from a base class (Employee). Each class has methods to
input and display specific employee details. The main function creates instances of each
employee type, prompts the user to input their details, and then displays the entered
information.
The code
import java.util.Scanner;

class Employee {
protected String name;
protected int id;

public void input() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter name:");
name = scanner.nextLine();
System.out.println("Enter ID:");
id = scanner.nextInt();
}
public void show() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
}
}

class Manager extends Employee {


protected String title;
protected String salary;

public void input() {


super.input();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter title:");
title = scanner.nextLine();
System.out.println("Enter salary:");
salary = scanner.nextLine();
}
public void show() {
super.show();
System.out.println("Title: " + title);
System.out.println("Salary: " + salary);
}
}

class Scientist extends Employee {


protected int numArticles;
protected String salary;
public void input() {
super.input();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of articles published:");

Page 4 of 7
numArticles = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter salary:");
salary = scanner.nextLine();
}
public void show() {
super.show();
System.out.println("Number of articles published: " + numArticles);
System.out.println("Salary: " + salary);
}
}
class Clerks extends Employee {
protected int overtime;
public void input() {
super.input();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter overtime:");
overtime = scanner.nextInt();
}
public void show() {
super.show();
System.out.println("Overtime: " + overtime);
}
}

public class hello {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Manager manager = new Manager();
System.out.println("Enter Manager details:");
manager.input();

Scientist scientist = new Scientist();


System.out.println("Enter Scientist details:");
scientist.input();

Clerks clerk = new Clerks();


System.out.println("Enter Clerk details:");
clerk.input();

System.out.println("\nManager details:");
manager.show();
System.out.println("\nScientist details:");
scientist.show();
System.out.println("\nClerk details:");
clerk.show();
}
}

The results (Screenshot)

Page 5 of 7
Conclusion:

In conclusion, the provided lab examples showcase the application of object-oriented


programming principles in Java. Each example demonstrates the use of classes, inheritance, and
methods to model real-world entities and perform specific tasks. By encapsulating data and
behavior within classes, developers can create modular, reusable, and maintainable code.
These examples highlight the versatility of Java for implementing various solutions, from simple
mathematical computations to complex employee management systems.

Page 6 of 7

You might also like