Professional Documents
Culture Documents
Hamza Nazir (2872) Oop Lab Report
Hamza Nazir (2872) Oop Lab Report
OOP
Lab reports
Submitted By:
Hamza Nazir
(2872)
Submitted
Sir Bilal
DATE:--/01/2023
Source code
Lab 12
TASK 1
Source code
import javax.swing.*;
import java.awt.event.*;
public TextFieldExample() {
f = new JFrame("Watta Khel no1.");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(l1);
f.add(l2);
f.add(tf1);
f.add(tf2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(270, 350);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String a = tf1.getText();
String b = tf2.getText();
int x = Integer.parseInt(a);
int y = Integer.parseInt(b);
int z = 0;
if(e.getSource() == b1) {
z = x + y;
String show = "Sum of two numbers";
String result = String.valueOf(z);
JOptionPane.showMessageDialog(f, result, show ,
JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource() == b2) {
z = x - y;
String show = "Diff of two numbers";
String result = String.valueOf(z);
JOptionPane.showMessageDialog(f, result, show ,
JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource() == b3) {
z = x * y;
String show = "Product of two numbers";
}
}
out put
TASK 2
Source code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JLabel l,l1,l2,l3,l4,l5,l6,l7,l8;
JTextField tf1,tf2,tf3,tf4;
JRadioButton r,r1;
JTextPane tp6;
JCheckBox c;
JFrame f,f1;
JButton b;
JPanel p;
Signup(){
f = new JFrame("Numl reg.exe");
l = new JLabel("Signup Page");
l.setBounds(135, 10, 85, 30);// x,y
l1 = new JLabel();
l1.setBounds(20, 45, 66, 30);// x,y
l1.setText("First name: ");
tf1 = new JTextField();
tf1.setBounds(85, 50, 200, 25);// x,y
l2 = new JLabel();
l2.setBounds(20, 75, 66, 30);// x,y
l2.setText("Last name: ");
tf2 = new JTextField();
tf2.setBounds(85, 80, 200, 25);// x,y
l3 = new JLabel();
l3.setBounds(20, 106, 66, 30);// x,y
l3.setText("Email: ");
tf3 = new JTextField();
l4 = new JLabel();
l4.setBounds(20, 135, 66, 30);// x,y
l4.setText("Password: ");
tf4 = new JTextField();
tf4.setBounds(85, 140, 200, 25);// x,y
l5 = new JLabel();
l5.setBounds(20, 165, 66, 30);// x,y
l5.setText("Gender: ");
r = new JRadioButton("Male");
r.setBounds(81, 171, 65, 20);
r1 = new JRadioButton("Female");
r1.setBounds(223, 171, 75, 20);
l6 = new JLabel();
l6.setBounds(20, 195, 66, 30);// x,y
l6.setText("Address: ");
tp6 = new JTextPane();
tp6.setBounds(85, 200, 200, 75);// x,y
l7 = new JLabel();
l7.setBounds(85, 280, 217, 20);// x,y
l7.setText("I accept the forms and conditions.");
c = new JCheckBox();
c.setBounds(20, 280, 65, 20);// x,y
b = new JButton("Signup");
b.setBounds(125, 320, 85, 35);
b.addActionListener(this);
f.add(l);f.add(l1);f.add(l2);f.add(l3);f.add(l4);f.add(l5);f.add(l6);f.add(
l7);
f.add(tf1);f.add(tf2);f.add(tf3);f.add(tf4);f.add(tp6);
f.add(r);f.add(r1);
f.add(c);
f.add(b);
f.setSize(330, 400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Signup(String name){
f1 = new JFrame("NUML_Welcome.exe");
l8 = new JLabel("Welcome "+name, JLabel.CENTER);
f1.add(l8);
f1.setSize(300,300);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Out put
Source code
Lab 11
Task 1
Make an interface ADD which contains only one method named Addition. This method takes two
parameters of double type and its return type is void. Make another interface named Sub which also
contains only one method named Subtraction. This method has the same signature and return type
as Add method. Make another interface named MulDiv which contains two methods named
Multiplication and Division. This interface inherits interface Sub. The two methods in this interface
also have the same signature and return type as the above mentioned methods. Now make a class
that implements interfaces Add and MulDiv only. Method Add performs the addition of two
parameters and displays the result. Same is the case with other methods. In main class, call the
methods of the previously mentioned class which implements methods of interfaces and show results.
Source Code
package hamza;
package hamza;
package hamza;
package hamza;
@Override
public void Subtraction(double a,double b) {
// TODO Auto-generated method stub
System.out.println("subtraction :"+ (a-b));
}
@Override
public void Multiplication(double a,double b) {
// TODO Auto-generated method stub
System.out.println("Multiplication :"+ (a*b));
}
@Override
public void Division(double a,double b) {
// TODO Auto-generated method stub
System.out.println("Division :"+ (a/b));
}
@Override
public void Addition(double a, double b) {
// TODO Auto-generated method stub
System.out.println("Addition:"+ (a+b));
}
package hamza;
Out put
Task 2: Marks:3
Make an interface Name one which has a variable x initialized with the value of 12. Similarly, there is
another interface which has another variable y initialized with the value of 10 as well as a method
named display. In main class, define display method that prints values of x, y and summation of both
values and create an object to print the output.
Source Code
package hamza;
package hamza;
@Override
public void display() {
Out put
Task 3: Marks:3
Write a program to create an interface Bank having a method named rateOfInterest. Now create two
classes HBL and ABL that implement this interface and provide their own interest rate of 7% and
7.5%, respectively. You have to achieve 100% abstraction so reference variable must be of interface.
Source Code
package hamza;
package hamza;
@Override
public void interest_rate() {
// TODO Auto-generated method stub
System.out.println("AbL have interest rate of 7.5% ");
}
package hamza;
bank t;
t=new ABl();
t.interest_rate();
t=new HBL();
t.interest_rate();
}
Out put
}
class student extends same
{
String rl,sem;
public teacher(String email, String ph, String name, String dg, String
salery) {
super(email, ph, name);
this.dg = dg;
this.salery = salery;
}
void Display_teacher()
{
System.out.println("..................teacher
data...................");
System.out.println("Name : "+name);
System.out.println("Email adress : "+email);
System.out.println("Contact no : "+ph);
System.out.println("ohda or designation : "+dg);
System.out.println("salary : "+salery);
}
}
class main
{
public static void main(String[] args) {
student st=new student("hamza@xcjn","0882222","hamza","2765","2nd
");
teacher tc=new teacher("tsbb@xcjn","444452222","bilal,", "SE
teacher ","infanite");
st.Display_students();
tc.Display_teacher();
}
}
out put
c
Source code
----------------------------------LAB 8------------------------------------
Task 1
Write a program to print the area and perimeter of a rectangle by creating a class
named 'Rectangle', taking the values of its length and breadth as parameters of its
constructor and having a method named 'Area' and perimeter which return the area
and perimeter, respectively. Length and breadth of rectangle are entered through
keyboard.
Source code
package haMZAaaaaa;
import java.util.Scanner;
System.out.println("Area is :"+A.Area());
System.out.println("paramiter is :"+A.Paramiter());
return length*breadth;
}
public float Paramiter()
{
return 2*(length+breadth);
}
Out put
Source code
----------------------------------LAB 7------------------------------------
Task 1
Define a class addressBook. It has three attributes name, address, and phone. It must
have methods showName (), showAddress (), and showPhone() with return type void.
Show methods will show the values of attributes only. Your name, address, & phone
number to the class attributes can be set directly using syntax
objectname.attributename. Make another class which encapsulates the main ()
method, make object of class adddressBook and print values of name, address and
phone using these methods.
void showAddress ()
{
System.out.println("adress is : "+address);
}
void showPhone()
{
System.out.println("phone no is : "+phno);
}
}
Out put
Task 2:
Make a class Factorial which consists of a public parameterized method named factorial ( ). Factorial
method must take an integer as a parameter and calculate its factorial. (Factorialmethod () can have
return type void or a value returning method.)
Source code
package haMZAaaaaa;
public class Factorial {
package haMZAaaaaa;
Output
Task 3:
Make a class Rectangle which calculates the area and perimeter of rectangle according
to the following algorithm.
Step1: Declare two variables length and width of type double.
Step2: Make a Parameterize method which sets the length and width according to the values
it receives as parameters.
Step 3: Make two methods getLength and getWidth which only return length and width.
Their return type is double.
Step 4: Make a method perimeter which calculates the perimeter according to the formula
2*L+2*W.
Step 5: Make a method area which calculates the area according to the formula L*W.
Step 6: Make a class RectDemo which encapsulates main method. Make an object of
Rectangle class.
Department of Computer Sciences
NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
Step 7: Get and Print the length and width of Rectangle by using appropriate methods.
Step 8: Print the Perimeter and Area of Rectangle.
Source code
package haMZAaaaaa;
package haMZAaaaaa;
class Rectangle {
double width;
double length;
void setvalue(double a,double b)
{
width=a;
length=b;
}
double getwidth()
{
return width;
}
double getlength()
{
return length;
}
double perimeter()
{
return 2*length+2*width;
}
double area()
{
return length*width;
Output
Source code
----------------------------------LAB 6------------------------------------
Task 1:
Write a Java application that uses looping to print the following table of values:
Marks: 3
Source code
package haMZAaaaaa;
public class task_1_lab_6 {
Output
Task 2:
Write a program using while loop which calculate square of every number and then
sum the squares of all numbers. Output should be like as shown below:
Output:
1
4
9
16
25
36
49
64
81
100
Total is 385
Source code
package haMZAaaaaa;
public class task_2_lab_6 {
for(int i=1;i<=10;i++ )
{
int a=i*i;
System.out.println(a);
sum=sum+a;
}
System.out.println("Total is "+sum);
Out put
Task 3:
Write a test program that takes some values from user and stores in an array, and finds
out the minimum value from an array.
Source code
package haMZAaaaaa;
import java.util.Scanner;
//System.out.print(Integer.MAX_VALUE);
//int min=2147483647;
int min=arr[0];
for(int j=0;j<arr.length;j++)
{
if(arr[j]<min)
{
min=arr[j];
}
}
Out put
}
}
Source code
package haMZAaaaaa;
public class task3 {
public static void main(String[] args) {
int count = 1;
while ( count <= 10 ) {
System.out.println( count % 2 == 1 ? "****" : "++++++++" );
++count;
}
}
}
output
Source code
----------------------------------LAB 3------------------------------------
Task 0:
Pass username and password in command line arguments and display them on the console.
Source code
package cs_40;
//import java.util.Scanner;
}
Out put
Task 1:
Take your name and registration number as input from user and generate the following
output. (Note: Output must be similar as shown h
Source code
package cs_40;
import java.util.Scanner;
public class LAB_3TASK_2 {
public static void main(String[] args)
{
java.util.Scanner t=new Scanner(System.in);
System.out.print("Enter your Name : ");
String name=t.next();
System.out.print("Enter your Registration no :
");
int reg=t.nextInt();
System.out.println("
*************** My first java
program**********************");
System.out.println("
My Name is : "+name);
System.out.println(" My
Registration Number is : "+reg);
}
}
Out put
Task 2:
Take radius of circle from user and calculate area and circumference of circle.
//Take radius of circle from user and calculate area and
circumference of circle.
Source code
import java.util.Scanner;
public class lab3_task_3 {
}
Out put
Task 3:
Write a program to convert temperature in Fahrenheit to temperature in Celsius
C=(F-32)*5/9; // temperature in Fahrenheit will be entered by user
Source code
import java.util.Scanner;
public class lab3_task3 {
System.out.println("temperature in Celsius :
"+C);
}
Out put
Task 4:
Take three numbers from user and calculate their average. Output should be like this.
Source code
package cs_40;
import java.util.Scanner;
}
}
Out put
Task 5:
Write a program to convert kilometers into meters where user will input distance in kilometer
and the program will convert distance into meters.
Source code
package cs_40;
import java.util.Scanner;
Out put
Source code
----------------------------------LAB 2------------------------------------
Task 1:
Create a Java program and write the following statements one by one to find the
difference between print() and println() methods. Closely examine how the outputs
differ.
1.
Department of Computer Sciences
NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
2.
System.out.print("java");
System.out.print("java");
3.
System.out.println("java");
Source code
package haMZAaaaaa;
//2.
System.out.println("Which one is your favourite programming
language?");
System.out.print("java");
System.out.print("java");
//3.
System.out.println("Which\n one\n is\n your\n favourite\n programming\n
language?");
System.out.println("java");
Out put
Task 2:
Calculate the average, sum & difference of 876 &767. First store these values in variables
and then calculate the result.
Source code
package haMZAaaaaa;
Output
Task 3:
Calculate the area of circle when radius is 9.8 cm. (A=πr2)
Source code
package haMZAaaaaa;
public class lab2_task3 {
Output
Task 4:
Write a program to declare three variables; a, b, and c. Assign some values to a and b and save
their sum in variable c. Now, print the sum.
Source code
package haMZAaaaaa;
class lab2_taskt04 {
int a=4;
int b=5;
int c;
c=a+b;
System.out.print("sum is : "+ c);
Output
Source code
----------------------------------LAB 1------------------------------------
LAB 1
Task 1:
Source code
{
System.out.println("i am Hamza Ramay"+ " "+ "my roll no is S22 39089" );
}
}
Out put
Task 2
Can we save this code with some other name like Testing.java rather than with the class
name? Try it.
Source code
class FirstProgram
}
}
Out put
Task 3:
Run the following code and see the result.
Run the following programs. Observe the output. Focus on comments to enhance the
understandability.
Source code
class Program2 {
a=2;
b=3;
c=a+b;
Out put
Task 4:
Run the following code by saving it firstly with name abc.java and then with bcd.java
and check the output.
Source code
class abc
{
public static void main(String args[])
{
System.out.println("hello world!");
}
Department of Computer Sciences
NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
}
class bcd
{
public static void main(String args[])
{
System.out.println("This is my First Program!");
}
}
Out put
Abc.java output
Bcd.java output