Saif 8
Saif 8
OOP
Submitted By:
Muhammad Saif Ur Rehman
CS-23-156
“D”
Submitted to:
SIR M.ASIF
Dated:
27/04/24
Lab No 1: Write a program that computes the square and cube of the given
number using the
Solution:
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();
}
}
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;
Page 2 of 7
class Marks extends Student {
private int[] marks = new int[3];
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);
}
}
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;
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);
}
}
System.out.println("\nManager details:");
manager.show();
System.out.println("\nScientist details:");
scientist.show();
System.out.println("\nClerk details:");
clerk.show();
}
}
Page 5 of 7
Conclusion:
Page 6 of 7