0% found this document useful (0 votes)
79 views7 pages

Object Oriented Programming: Objective 1

This document provides instructions and code examples for 5 programming questions/exercises for a lab on control and iteration statements and math classes in Java. The questions cover: 1) solving a quadratic equation, 2) generating the Fibonacci sequence, 3) designating an employee's salary level based on salary range, 4) determining if a number is positive or negative, and 5) finding the greatest and least numbers among 3 integers. The document includes sample input/output and source code for each question.

Uploaded by

Wali Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
79 views7 pages

Object Oriented Programming: Objective 1

This document provides instructions and code examples for 5 programming questions/exercises for a lab on control and iteration statements and math classes in Java. The questions cover: 1) solving a quadratic equation, 2) generating the Fibonacci sequence, 3) designating an employee's salary level based on salary range, 4) determining if a number is positive or negative, and 5) finding the greatest and least numbers among 3 integers. The document includes sample input/output and source code for each question.

Uploaded by

Wali Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Object Oriented Programming Lab#

02

LAB # 2

OBJECTIVE
To Study Control and Iteration Statements and Math Classes.
Question:
1. Write a program to solve quadratic equation using( if, else-if and else) .
Example:
Input a: 1
Input b: 5
Input c: 1
Expected Output :
The roots are -0.20871215252208009 and -4.7912878474779195

Source Code:
import java.util.Scanner;
public class LoopsLogic {
public static void main(String[] args) {

double secondRoot = 0, firstRoot = 0;


Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of a ::");
double a = sc.nextDouble();

System.out.println("Enter the value of b ::");


double b = sc.nextDouble();

System.out.println("Enter the value of c ::");

1 ROLL NO: 2020-SE-216


Object Oriented Programming Lab#
02
double c = sc.nextDouble();

double determinant = (b*b)-(4*a*c);


double sqrt = Math.sqrt(determinant);

if(determinant>0){
firstRoot = (-b + sqrt)/(2*a);
secondRoot = (-b - sqrt)/(2*a);
System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);
}
else if(determinant == 0){
System.out.println("Root is :: "+(-b + sqrt)/(2*a));
}
}
}
Output:

Question:
2. Write a program to generate Fibonacci hypothesis for 19 generations given below.

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765.

Source Code:
import java.util.Scanner;

2 ROLL NO: 2020-SE-216


Object Oriented Programming Lab#
02
public class LoopsLogic {
public static void main(String[] args) {
int n, a = 0, b = 0, c = 1;
System.out.print("Fibonacci Series:");
for(int i = 1; i <= 21; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
}
}
}
Output:

Question:
3. Write a program to read salary of an employee and designate according to their
salary using if-else ladder.

Note: 25k-35k Research Assistant


36k-50k Junior Lecturer
51k-65k Lecturer
66k-80k Assistant Professor
Source Code:
import java.util.Scanner;
public class LoopsLogic {

3 ROLL NO: 2020-SE-216


Object Oriented Programming Lab#
02
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter Salary b/w 25 - 80 ");


double salary = input.nextDouble();

if(salary>=25 && salary<35)


{
System.out.println("Reasearch Assistant");
}
else if(salary>=36 && salary<50)
{
System.out.println("Junior Lecturer");
}
else if(salary>=51 && salary<65)
{
System.out.println("Lecturer");
}
else if(salary>=66 && salary<80)
{
System.out.println("Assistant Professor");
}
}
}

4 ROLL NO: 2020-SE-216


Object Oriented Programming Lab#
02

Output:

Question:

4. Write a Java program to read a number from the user and print whether it is positive
or negative.

Example:
Input number: 35

Source Code:
import java.util.Scanner;
public class LoopsLogic {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number you want to check:");
int number = s.nextInt();
s.close();
if(number > 0)
{
System.out.println(number+" is positive number");
}
else if(number < 0)
{

5 ROLL NO: 2020-SE-216


Object Oriented Programming Lab#
02
System.out.println(number+" is negative number");
}
else
{
System.out.println(number+" is neither positive nor negative");
}
}
}
Output:

Question:

5. Write a program to read 3 integer values and print the greater and lesser number respectively.

Example:
A=29
B=25
C=72
Output:
Greater number=72
Lesser number=29

Source Code:

import java.util.Scanner;

public class LoopsLogic {

public static void main(String[] args) {

//Giving 3 integer values


int n1 = -4, n2 = 7, n3 = 2;

6 ROLL NO: 2020-SE-216


Object Oriented Programming Lab#
02
//Loop for greater number
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the greater number.");

else if (n2 >= n1 && n2 >= n3)


System.out.println(n2 + " is the greater number.");

else
System.out.println(n3 + " is the greater number.");

//Loop for lesser values


if( n1 <= n2 && n1 <= n3)
System.out.println(n1 + " is the lesser number.");

else if (n2 <= n1 && n2 <= n3)


System.out.println(n2 + " is the lesser number.");

else
System.out.println(n3 + " is the lesser number.");
}
}
Output:

7 ROLL NO: 2020-SE-216

You might also like