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

CSL-210: Object-Oriented Programming Lab

BS(IT) 2A
Semester 02(spring 2020)

Enrollment: 02-235192-010
Course instructor: Sir irfan mustafa

Lab01: Getting started with Netbeans

Exercises

Exercise 1 (Elephant.java)

Write a program that prints your first three name initials similar to the following:

Solution
Code:
package ooplab;
import java.util.Scanner;
public class Ooplab {

public static void main(String[] args) {


Scanner input=new Scanner (System.in);
System.out.println("Name Print");
System.out.println(" -------- ---------- ----------- ");
System.out.println(" / | | | ");
System.out.println(" / | | | ");
System.out.println(" / |----------| | ");
System.out.println(" / | | | ");
System.out.println(" ------- | | ---------- ");
}
}
}
}
IT Department, BUKC 2/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans

Output

Exercise 2 (Arithmatic.java)

Type-in the following example, which receives the input of two integer numbers and compute the
sum, difference and product. Compile and run this program.
/*
* Compute the sum, difference, and product of two integer numbers.
*/
import java.util.Scanner;
public class Arithmetic {
/* Main method */
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int number1; // 1st integer variable
int number2; // 2nd integer variable
System.out.print("Enter First Number: ");
number1 = input.nextInt();
System.out.print("Enter Second Number: ");
number2 = input.nextInt();
System.out.println("Sum is: " + (number1 + number2));
System.out.println("Difference is: " + (number1 - number2))
System.out.println("Product is: " + (number1 * number2));
}//Main method ends
} // class ends

Solution
Code:
package ooplab;
import java.util.Scanner;
public class Ooplab {

public static void main(String[] args) {


Scanner input = new Scanner(System.in); int number1; // 1st integer variable
int number2;
IT Department, BUKC 3/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans
System.out.print("Enter First Number: ");
number1 = input.nextInt();
System.out.print("Enter Second Number: ");
number2 = input.nextInt();
System.out.println("Sum is: " + (number1 + number2));
System.out.println("Difference is: " + (number1 - number2));
System.out.println("Product is: " + (number1 * number2));
}
}
}
Output

Exercise 3 (TirePressure.java)

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
Solution
Code:
package ooplab;
import java.util.Scanner;
public class Ooplab {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
float P,V,M,T;
System.out.print("Enter Voulme in Cubic feet: ");
IT Department, BUKC 4/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans
V=input.nextFloat();
System.out.print("Enter Mass of air in pounds: ");
M=input.nextFloat();
System.out.print("Enter Temperature in Fahrenheit: ");
T=input.nextFloat();
P=(float) ((0.37*M)*(T+460)/V);
System.out.println("Automobile Tire Pressure: is: "+P);

}
}
}
Output

Exercise 4 (Cookies.java)

There are 12 cookies per box (sold at $1.14) and 24 boxes per carton. Left over boxes are sold for
57¢. Remaining cookies are given away free. Given the number of cookies produced, determine the
number of boxes, cartons, left over boxes and the total money made.

Hint:
Simple math:
leftover cookies: total_cookies % 12;
total boxes: total_cookies / 12
leftover boxes: total_boxes % 24
cartons: total_boxes / 24

Sample output:
IT Department, BUKC 5/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans

Solution
Code:
package ooplab;
import java.util.Scanner;
public class Ooplab {

public static void main(String[] args) {

Scanner i= new Scanner(System.in);


int cb;
System.out.print("Enter the number of cookies baked: ");
cb=i.nextInt();
System.out.println("\nIf "+ cb+" cookies were baked you would end up
with: ");
int c=cb/24;
System.out.println("\ncartons: "+ c);
int tb= cb/12;
int lb=tb%24;
System.out.println("leftover boxes: "+ lb);
int lc=cb%12;
System.out.println("leftover cookie: "+ lc);
double p;
p= (27.36* c)+(lb*0.0065);
System.out.println("profit: $"+ p);
}

Output
IT Department, BUKC 6/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans

Exercise 5 (PullyFormulas.java)

Pulley formulas
a) calculate the speed of one pulley if there are 2 pulleys connected with a belt:
RPM2 = diameter1/diameter2 * RPM1
b) calculate the amount of weight that can be lifted with a multiple pulley system:
weight lifted = force exerted * number of up ropes

solution
Code:
package ooplab;
import java.util.Scanner;
public class Ooplab {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);
float d1,d2,RPM1,RPM2;
System.out.println("FORMULA : RPM2 = (d1/d2)*RPM1 ");
System.out.print("Enter diameter of first pully : ");
d1=obj.nextFloat();
System.out.print("Enter diameter of second pully : ");
d2=obj.nextFloat();
System.out.print("Enter speed of first pully : ");
RPM1=obj.nextFloat();
RPM2=(d1/d2)*RPM1;
System.out.println("RPM2 = "+RPM2);

}
IT Department, BUKC 7/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans
}
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int weight_Lifted,fore_Exerted,number_of_upropes;
System.out.println("FORMULA : weight lifted = force exrted * number of
upropes");
System.out.print("Enter force exerted : ");
fore_Exerted=obj.nextInt();
System.out.print("Enter diameter of second pully : ");
number_of_upropes=obj.nextInt();
weight_Lifted=fore_Exerted*number_of_upropes;
System.out.println("Weight lifted = "+weight_Lifted+" KG");
}
}
Output

Exercise 6 (BMI.java)

The body mass index (BMI), or Quetelet index, 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,
IT Department, BUKC 8/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans
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
Code:
package ooplab;
import java.util.Scanner;
public class Ooplab {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
float weight,height,BMI;
System.out.print("ENTER YOUR WEIGHT IN POUNDS : ");
weight=sc.nextFloat();
System.out.print("ENTER YOUR HEIGHT IN INCHES : ");
height= sc.nextFloat();
BMI=(weight/(height*height))*703;
System.out.println("YOUR BMI IS : "+BMI);
if(BMI<18.5)
{
System.out.println("YOU ARE UNDERWEIGHT");
}
else if(BMI>=18.5 && BMI<=24.9)
{
System.out.println("YOU ARE NORMAL");
}
else if(BMI>=25.0 && BMI<=29.9)
{
System.out.println("YOU ARE OVERWEIGHT");
}
else if(BMI>=30.0)
{
System.out.println("YOU ARE OBESE");

}
}
IT Department, BUKC 9/9 Semester spring 2020
CSL-210: Object-Oriented Programming Lab Lab01: Getting Started with Netbeans
}
Output

You might also like