Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 53

NATIONAL UNIVERSITY OF MODERN LANGUAGES

DEPARTMENT OF COMPUTER SCIENCES

OOP
Lab reports

Submitted By:
Hamza Nazir
(2872)

Submitted
Sir Bilal

DATE:--/01/2023

CLASS: BSCS-40B (2nd Semester) Evening

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Source code

Lab 12

TASK 1

Source code

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

import javax.swing.*;
import java.awt.event.*;

public class TextFieldExample implements ActionListener {

JTextField tf1, tf2;


JLabel l1,l2;
JButton b1,b2,b3,b4,b5;
JFrame f;

public TextFieldExample() {
f = new JFrame("Watta Khel no1.");

l1 = new JLabel("Enter first number: ");


l1.setBounds(50, 30, 150, 20);
tf1 = new JTextField();
tf1.setBounds(50, 50, 150, 20);
l2 = new JLabel("Enter second number: ");
l2.setBounds(50, 80, 150, 20);
tf2 = new JTextField();
tf2.setBounds(50, 100, 150, 20);
b1 = new JButton("+");
b1.setBounds(50, 150, 50, 30);
b2 = new JButton("-");
b2.setBounds(150, 150, 50, 30);
b3 = new JButton("*");
b3.setBounds(50, 200, 50, 30);
b4 = new JButton("/");
b4.setBounds(150, 200, 50, 30);
b5 = new JButton("Clear");
b5.setBounds(80, 250, 80, 30);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

String result = String.valueOf(z);


JOptionPane.showMessageDialog(f, result, show ,
JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource() == b4) {
z = x / y;
String show = "Division of two numbers";
String result = String.valueOf(z);
JOptionPane.showMessageDialog(f, result, show ,
JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == b5) {
tf1.setText(null);
tf2.setText(null);
}

public static void main(String[] args) {


new TextFieldExample();

}
}

out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

TASK 2

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Source code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Signup implements ActionListener {

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

tf3.setBounds(85, 110, 200, 25);// x,y

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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

public static void main(String[] args) {


new Signup();

public void actionPerformed(ActionEvent e) {


if(e.getSource()==b) {
String name = tf1.getText()+" ";
name += tf2.getText();
f.dispose();
new Signup(name);
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

public interface ADD {


void Addition(double a,double b) ;
}

package hamza;

public interface Sub {


void Subtraction(double a,double b);
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

package hamza;

public interface MulDiv extends Sub {


void Multiplication(double a,double b);
void Division(double a,double b);
}

package hamza;

public class test implements ADD , MulDiv {

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

public class Main {

public static void main(String[] args) {


test b=new test();
b.Subtraction(23332.23,2323.23);
b.Addition(2343.432, 4323.43);
b.Division(3242.43, 234234.432);
b.Multiplication(231321.213,3213.321);
}
}

Out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

public interface one1 {


int x=12;
int y=10;
void display();
}

package hamza;

public class Main1 implements one1 {

@Override
public void display() {

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

// TODO Auto-generated method stub


System.out.println("x = "+x+ "\ny = "+y + "\nsum is : " +(x+y));

public static void main(String[] args) {

one1 obj1=new Main1();


obj1.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;

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

public interface bank {


void interest_rate();
}
package hamza;

public class HBL implements bank {


@Override
public void interest_rate() {
System.out.println("HbL have interest rate of 7% ");

package hamza;

public class ABl implements bank {

@Override
public void interest_rate() {
// TODO Auto-generated method stub
System.out.println("AbL have interest rate of 7.5% ");
}

package hamza;

public class main12 {

public static void main(String[] args) {

bank t;
t=new ABl();
t.interest_rate();
t=new HBL();
t.interest_rate();
}

Out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

----------- Extra LAB inheritence----------------------

public class same {


String email,ph,name;

public same(String email, String ph, String name) {


this.email = email;
this.ph = ph;
this.name = name;
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

}
class student extends same
{
String rl,sem;

public student(String email, String ph, String name,String rl, String


sem) {
//super.email=email;
//super.ph=ph;
//super.name=name;
super(email,ph,name);
this.rl = rl;
this.sem = sem;
}
void Display_students()
{
System.out.println("..................Student
data...................");
System.out.println("Name : "+name);
System.out.println("Email adress : "+email);
System.out.println("Contact no : "+ph);
System.out.println("Roll no : "+rl);
System.out.println("Semester : "+sem);
}
}
class teacher extends same
{
String dg,salery;

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

c
Source code
----------------------------------LAB 8------------------------------------

Task 1

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

public class task_1 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner hamza=new Scanner(System.in);

System.out.print("enter length :");


float l=hamza.nextFloat();

System.out.print("enter breadth :");


float B=hamza.nextFloat();

rectangle A=new rectangle(l, B);

System.out.println("Area is :"+A.Area());
System.out.println("paramiter is :"+A.Paramiter());

public class rectangle {


float length;
float breadth;

rectangle(float length,float breadth)


{
this.length=length;
this.breadth=breadth;
}
public float Area() {

return length*breadth;
}
public float Paramiter()
{
return 2*(length+breadth);
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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.

public class addressbook {


String name;
String address;
String phno;
void setvalue(String a,String b,String c)
{
name=a;
address=b;
phno=c;
}
void showName ()
{
System.out.println("Name is : "+name);
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

void showAddress ()
{
System.out.println("adress is : "+address);
}
void showPhone()
{
System.out.println("phone no is : "+phno);
}
}

public class book {

public static void main(String[] args) {


// TODO Auto-generated method stub
addressbook obj=new addressbook();
obj.setvalue("hamza agerwal", "tabakto afganistan ", "0300345241264");
obj.showName();
obj. showAddress ();
obj. showPhone();
}

Out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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 {

void factorial ( int a)


{
int fact=1;
for(int i=a;i>=1;i--)
{
fact=fact*i;
}
System.out.println("factorial of "+a+" is :"+fact);
}
}

package haMZAaaaaa;

public class task_2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Factorial obj=new Factorial();
obj.factorial(4);
}

Output

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

public class RectDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
Rectangle obj=new Rectangle();
obj.setvalue(343.3,34.4);
System.out.println("length is : "+obj.getlength());
System.out.println("length is : "+obj.getwidth());
System.out.println("area is : "+obj.area());
System.out.println("peramitter is : "+obj.perimeter() );
}

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;

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Output

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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 {

public static void main(String[] args) {

// TODO Auto-generated method stub


System.out.println("N N*10 N*100 N*1000");
System.out.println();
for(int i=1;i<=5;i++ )
{

System.out.println(i+" "+(i*10)+" "+i*100+"


"+i*1000);
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Total is 385

Source code
package haMZAaaaaa;
public class task_2_lab_6 {

public static void main(String[] args) {


// TODO Auto-generated method stub

// TODO Auto-generated method stub


int sum=0;

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

public class task_3_lab_6 {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
// TODO Auto-generated method stub
System.out.println("enter the length of arry");
int l=sc.nextInt();
int []arr=new int[l];
for (int i=0;i<arr.length;i++)
{
System.out.print("enter the value for index "+i+" :");
arr[i]=sc.nextInt();
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

//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];
}
}

System.out.print(" minimum no is : "+min);


}

Out put

Task 4: What does the following program print?


public class task3 {
public static void main(String[] args) {
int count = 1;
while ( count <= 10 ) {
System.out.println( count % 2 == 1 ? "****" : "++++++++" );
++count;
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

}
}

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

public class lab_40B {


public static void main(String[] args)
{
for(int i=0; i<2; i++)
{
System.out.println("Argument " + i + "
= " + args[i]);
}
}

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

}
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 :
");

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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 {

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

public static void main(String[] args) {


java.util.Scanner t=new Scanner(System.in);
System.out.print(" enter radius of circle : ");
int r=t.nextInt();
float pie=3.14f;
double A=pie*(r*r),C=(2*pie)*r;
System.out.println(" Area of circle is : "+A+"\n
circumference of circle is "+C);

}
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 {

public static void main(String[] args) {


Department of Computer Sciences
NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

java.util.Scanner t=new Scanner(System.in);


System.out.print(" enter tempreture in farnhite
: ");
double fw=t.nextDouble();
double C=(fw-32)*5/9;

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.

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Source code
package cs_40;

import java.util.Scanner;

public class lab3_task004 {


public static void main(String[] args) {
Scanner t=new Scanner(System.in);
System.out.print(" enter first no ");
float x=t.nextFloat();
System.out.print(" enter second no ");
float y=t.nextFloat();
System.out.print(" enter third no ");
float z=t.nextFloat();
float p=(x+y+z)/3;

System.out.println(" avarge of "+x+" "+y+" "+z+" is


"+p);

}
}
Out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

public class lab3_task005 {

public static void main(String[] args)


{
Scanner t=new Scanner(System.in);
System.out.print(" enter distance in killo
meter: ");
float r=t.nextFloat();
float p=r*1000;
Department of Computer Sciences
NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

System.out.println(" Distance in meter


"+p);

Out put

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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

System.out.println("Which one is your favourite programming language?");


System.out.print("java");

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

Source code
package haMZAaaaaa;

public class lab2_task1 {

public static void main(String[] args) {


//1.
System.out.println("Which one is your favourite programming
language?");
System.out.print("java");

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

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;

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

public class lab_task2 {

public static void main(String[] args)


{
int a=876;
int b=767,sum,diff,avg;
sum=a+b;
diff=a-b;
avg=(a+b)/2;
//for c++ Cout<<"sum is"<<sum<<endl;
System.out.println("sum is : "+ sum);
System.out.println("difference is : "+ diff);
System.out.println("average is : "+ avg);

Output

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Task 3:
Calculate the area of circle when radius is 9.8 cm. (A=πr2)

Source code
package haMZAaaaaa;
public class lab2_task3 {

public static void main(String[] args)


{
float r=9.8f;
double pie =3.14,A;
A=pie*(r*r);
System.out.print("Area of circle is : "+ A);

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.

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Source code

package haMZAaaaaa;
class lab2_taskt04 {

public static void main(String[] args)


{

int a=4;
int b=5;
int c;
c=a+b;
System.out.print("sum is : "+ c);

Output

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Source code
----------------------------------LAB 1------------------------------------

LAB 1
Task 1:

Write a code to print your name and roll number.

Source code

public class task1


{
public static void main(String args[])

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

{
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

public static void main(String abc[])

System.out.println("My first Java Program");

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

}
}

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 {

public static void main(String args[]) {

int a,b,c; //this statement declares three variables a, b and c.

a=2;

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

b=3;

c=a+b;

System.out.println("Sum of two numbers = "+c); }


}

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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES

You might also like