0% found this document useful (0 votes)
20 views13 pages

Fiza Oop 1

The document outlines a lab report for the Object Oriented Programming course at Bahria University, detailing tasks assigned to students, including programming exercises in Java. It includes specific tasks such as creating patterns, performing arithmetic operations, calculating discounts based on conditions, and implementing various formulas. The report also contains sample code solutions for each task along with expected outputs.

Uploaded by

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

Fiza Oop 1

The document outlines a lab report for the Object Oriented Programming course at Bahria University, detailing tasks assigned to students, including programming exercises in Java. It includes specific tasks such as creating patterns, performing arithmetic operations, calculating discounts based on conditions, and implementing various formulas. The report also contains sample code solutions for each task along with expected outputs.

Uploaded by

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

Bahria University,

Karachi Campus

Course: CSC-210 - Object Oriented Programming


Term: Spring 2024, Class: BSE- 2(C)

Submitted By:

Fiza Khan 89219


(Name) (Reg. No.)

Submitted To:

Engr. Mahawish/Engr. Saniya Sarim

Signed Remarks: Score:


INDEX
SNO DATE LAB LAB OBJECTIVE SIGN
NO
1 16/2/2024 1 Introduction To Java Programming Language
Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


1
LIST OF TASKS
TASK NO OBJECTIVE
1 Write a program that prints any pattern, similar to the following.

2 Write a JAVA program, which receives the input of two integer numbers, operation
(+,-,*,/,%, power, square-root and factorial) and compute arithmetic operations.
Generate a menu for operations and ask user after every operation if they want to
do another. (Hint use switch case)

3 Make a program in JAVA in which take no. of items, price of items and name of
items as input from the user and give the discount according to the following
conditions:
• If from Bahria University give discount of 30%.
• Else if the total amount is greater then 50,000 and less than 100,000 give
discount of 20%.
• Else if the total amount is greater then 100,000 give discount of 30%.

4 Write a JAVA program which will implement the following formulae using
methods.

Submitted On:
20/2/2024
(Date: DD/MM/YY)
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

LAB # 01
Task # 01:
Write a program that prints any pattern, similar to the following.

Solution:
package fiza.lab.pkg1;

public class FizaLab1 {

public static void main(String[] args) {

System.out.println(" ( (");
System.out.println(" ) )");
System.out.println(" ................");
System.out.println("| |-");
System.out.println("|~~~~~~~~~~~~~~~| ]");
System.out.println(" \\-------------/ ]");
System.out.println(" \\-----------/ -");
System.out.println(" '---------'");
}
}

Output:

FIZA KHAN 1
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

Task # 02:
Write a JAVA program, which receives the input of two integer numbers,
operation (+,-,*,/,%, power, square-root and factorial) and compute arithmetic
operations. Generate a menu for operations and ask user after every operation
if they want to do another. (Hint use switch case)

Solution:
package fiza.lab.pkg1;

import java.util.Scanner;
public class FizaLab1 {

public static void main(String[] args) {

System.out.println(" '---------'");
Scanner input=new Scanner(System.in);
double num1,num2,res;
char ans1;
do{
System.out.println("_____MENU_____");
System.out.println("1) Addition \n2) Subtraction \n3)
Multiplication \n4) Division \n5) Modulus \n6) Power \n7) Square root
\n8) Factorial");
System.out.println("Which operation do you want to
perform?");
int ans=input.nextInt();
switch(ans){
case 1:
System.out.println("Enter 1st number:");
num1=input.nextDouble();
System.out.println("Enter 2nd number:");
num2=input.nextDouble();
res=num1+num2;
System.out.println("The sum of two numbers is
"+res);
break;
case 2:
System.out.println("Enter 1st number:");
num1 = input.nextDouble();
System.out.println("Enter 2nd number:");
num2 = input.nextDouble();
res = num1 - num2;

FIZA KHAN 2
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

System.out.println("The difference of two numbers


is " + res);
break;
case 3:
System.out.println("Enter 1st number:");
num1 = input.nextDouble();
System.out.println("Enter 2nd number:");
num2 = input.nextDouble();
res = num1 * num2;
System.out.println("The product of two numbers is
" + res);
break;
case 4:
System.out.println("Enter 1st number:");
num1=input.nextDouble();
System.out.println("Enter 2nd number:");
num2=input.nextDouble();
res=num1/num2;
System.out.println("The quotient of two numbers is
"+res);
break;
case 5:
System.out.println("Enter 1st number:");
num1 = input.nextDouble();
System.out.println("Enter 2nd number:");
num2 = input.nextDouble();
res = num1 % num2;
System.out.println("The mod of two numbers is " +
res);
break;
case 6:
System.out.println("Enter the base:");
num1 = input.nextDouble();
System.out.println("Enter the power:");
num2 = input.nextDouble();
res = Math.pow(num1, num2);
System.out.println("The result of " +num1+ " power
" +num2+ " is " + res);
break;
case 7:
System.out.println("Enter the number:");
num1 = input.nextDouble();
res = Math.sqrt(num1);
System.out.println("The square root of the number
is " + res);
break;
case 8:

FIZA KHAN 3
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

System.out.println("Enter the number:");


num1 = input.nextDouble();
long fac = 1;
for (int i = 1; i <= num1; i++)
{
fac *= i;
}
System.out.println("The factoral of " + num1 + "
is " + fac);
break;
default:
System.out.println("Invalid option selected");
break;

}
System.out.println("Do you want to continue? Y: Yes N:
NO");
ans1=Character.toUpperCase(input.next().charAt(0));
}
while(ans1=='Y');
System.out.println("Program ended!");
}
}

Output:

FIZA KHAN 4
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

Task # 03:
Make a program in JAVA in which take no. of items, price of items and name
of items as input from the user and give the discount according to the
following conditions:
• If from Bahria University give discount of 30%.
• Else if the total amount is greater then 50,000 and less than 100,000
give discount of 20%.
• Else if the total amount is greater then 100,000 give discount of 30%.

Solution:
package fiza.lab.pkg1;

import java.util.Scanner;
public class FizaLab1 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);


double total=0,dis=0,payAm;
System.out.println("How many items do you want to purchase?");
int n=input.nextInt();
String [] name=new String [n];
double [] price=new double[n];
for(int i=0;i<n;i++){
System.out.println("Enter the name of item " +(i+1)+ ":");
name[i]=input.next();
System.out.println("Enter the price of item " +(i+1)+":");
price[i]=input.nextDouble();
total+=price[i];
}
System.out.println("Are you from Bahria University? Y/N");
char ans=input.next().charAt(0);
if(ans=='y' || ans=='Y'){
dis=total*0.3;
}
else if(total>50000 && total<100000){
dis=total*0.2;
}
else if(total>100000){
dis=total*0.3;
}
payAm=total-dis;

FIZA KHAN 5
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

System.out.println("____________________BILL_____________________");
System.out.println("No. of items \tName of items \tPrice of
items");
for(int i=0;i<n;i++){
System.out.println(i+1+"\t\t"+name[i]+"\t\t"+price[i]);
}
System.out.println("Total Amount: "+total);
System.out.println("Discount: "+dis);
System.out.println("Payable Amount: "+payAm);
}
}

Output:

FIZA KHAN 6
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

Task # 04:
Write a JAVA program which will implement the following formulae using
methods.
• Automobile Tire Pressure: P = 0.37m(T + 460)/V P = pressure in psi.
V = volume in cubic feet
m = mass of air in pounds
T = temperature in Fahrenheit

• Pulley formulas
• calculate the speed of one pulley if there are 2 pulleys connected
with a belt:
RPM2 = diameter1/diameter2 * RPM1

• calculate the amount of weight that can be lifted with a


multiple pulley system:
weight lifted = force exerted * number of up ropes

• The body mass index (BMI), is a heuristic proxy for human body fat
based on an individual's weight and height. BMI does not actually
measure the percentage of body fat. We will be building a BMI
calculator method. Body mass index (BMI) is computed using the the
formula,

Where mass is the subject's weight in pounds (lb) and height is the height in
inches (in). The value 703 is a factor to convert BMI to a value that matches the
original BMI calculations done in metric units (i.e. kilograms-meters).

Solution:
package fiza.lab.pkg1;

import java.util.Scanner;
public class FizaLab1 {
static Scanner input=new Scanner(System.in);
public static void main(String[] args) {

char rep;

FIZA KHAN 7
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

do{
System.out.println("Which calculation do you want to
perform?");
System.out.println("1) Automobile Tire Pressure \n2)
Pulley formulas \n3) BMI Calculator");
int ans=input.nextInt();
switch(ans){
case 1:
ATM();
break;
case 2:
Pulley();
break;
case 3:
BMI();
break;
default:
System.out.println("Invalid choice");
break;
}
System.out.println("Do you want to continue? Y/N");
rep=input.next().charAt(0);
}
while(rep=='Y' || rep=='y');
}
public static void ATM(){
System.out.println("Enter the vaue of volume in cubic feet:");
double V=input.nextDouble();
System.out.println("Enter the value of mass of air in
pounds:");
double m=input.nextDouble();
System.out.println("Enter the value of temperature in
farhenheit:");
double T=input.nextDouble();
double P=0.37*m*(T + 460)/V;
System.out.println("The Automobile Tire Pressure is: "+P+"
psi");
}
public static void Pulley(){
System.out.println("Which calculation do you want to
perform?");
System.out.println("S: Speed of one pulley \nW: Weight that
can be lifted with a multiple pulley system");
char ans=Character.toUpperCase(input.next().charAt(0));
switch(ans){
case 'S':

FIZA KHAN 8
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

System.out.println("Enter the value of diameter of


pulley 1:");
double d1=input.nextDouble();
System.out.println("Enter the value of diameter of
pulley 2:");
double d2=input.nextDouble();
System.out.println("Enter the value of RPM 1:");
double rpm1=input.nextDouble();
double rpm2=d1/d2*rpm1;
System.out.println("The speed of pulley 2 is "+rpm2);
break;
case 'W':
System.out.println("Enter the force exterted:");
double f=input.nextDouble();
System.out.println("Enter the number of ropes:");
double ropes=input.nextDouble();
double W=f*ropes;
System.out.println("The amount of weight that can be
lifted with a multiple pulley system is "+W);
break;
default:
System.out.println("Invalid choice");
break;
}
}
public static void BMI(){
System.out.println("Enter your weight in pounds:");
double m=input.nextDouble();
System.out.println("Enter your height in inches:");
double h=input.nextDouble();
double bmi=m/Math.pow(h, 2)*703;
System.out.println("The body mass index of your body is
"+bmi+" kg/m^2");
}
}

FIZA KHAN 9
16/02/2024 Object Oriented Programming
[Introduction To Java Programming Language]

Output:

FIZA KHAN 10

You might also like