0% found this document useful (0 votes)
32 views17 pages

Oop Solved Paper Regular

The document outlines the 2nd semester examination for BS in Computer Science at Sindh University Laar Campus, focusing on Object Oriented Programming. It includes various programming tasks and questions that require students to write Java code for specific functionalities, such as calculating numbers, managing accounts, and determining grades. The exam consists of multiple questions, each carrying equal marks, and emphasizes practical coding skills in Java.

Uploaded by

attiq7306
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)
32 views17 pages

Oop Solved Paper Regular

The document outlines the 2nd semester examination for BS in Computer Science at Sindh University Laar Campus, focusing on Object Oriented Programming. It includes various programming tasks and questions that require students to write Java code for specific functionalities, such as calculating numbers, managing accounts, and determining grades. The exam consists of multiple questions, each carrying equal marks, and emphasizes practical coding skills in Java.

Uploaded by

attiq7306
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/ 17

SINDH UNIVERSITY LAAR CAMPUS @ BADIN

DEPARTMENT OF COMPUTER SCIENCE


2ND SEMESTER EXAMINATION- 2024
BS (Computer Science) P-I [Regular]

OBJECT ORIENTED PROGRAMMING [CSOO:303]

Date: Friday, December 20 , 2024 Duration: 2 Hours Marks: 80

Attempt any Five Questions. Each questions carries equal marks

Q1 A) Write a program that inputs five numbers and determines and prints the number of
negative numbers input, the number of positive numbers input and the number of zeros
input.
import java.util.Scanner;
public class Q1_A
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
int number;
int positive=0;
int negative=0;
int zero=0;

int counter=1;
while(counter<=5)
{
System.out.println("Enter number ");
number = input.nextInt();

if(number>0)
{
positive++;
}
if(number<0)
{
negative++;
}
if(number==0)
{
zero++;
}
counter++;
}
System.out.println("Total positive number entered: "+positive);
System.out.println("Total negative number entered: "+negative);
System.out.println("Total zero number entered: "+zero);
}
}

A) Write an Account and Account Test Driver class application using constructor the
attributes of Account class are name and balance and provide methods of class is
(deposit balance, withdraw balance, set Name, get Name)
import java.util.Scanner;
public class Q1_B{
private String name;
private double balance;

public Q1_B(String name,double balance)


{
this.name = name;
if(balance>0.0)
{
this.balance = balance;
}
}
public void deposit(double deposit)
{
if(deposit>0.0)
{
this.balance = balance+deposit;
}
}
public void withDraw(double withdraw)
{
if(withdraw<balance)
{
this.balance = balance-withdraw;
}
else{
System.out.println("Balance is not available");
}
}
public String getName()
{
return name;
}
public double getBalance()
{
return balance;
}

public static void main(String[]args)


{
Q1_B obj = new Q1_B("Ali",2200);
System.out.println("Account name "+obj.getName()+" \n Balance
"+obj.getBalance());

Scanner input = new Scanner(System.in);


System.out.println("Enter balance deposit");
double deposit = input.nextDouble();
if(deposit>0.0)
{
obj.deposit(deposit);
}
System.out.println("Enter balance withdraw");
double withdraw = input.nextDouble();
if(withdraw>0.0)
{
obj.withDraw(withdraw);
}
System.out.println("Account name "+obj.getName()+" \n Balance
"+obj.getBalance());
}
}

Q2: A) write an application that calculates the squares and cubes of the numbers from 0
to 10 and prints the resulting values in table format using JOptionPane

import javax.swing.JOptionPane;
public class Q2_A
{
public static void main(String[]args)
{

String r="";
for(int i=1;i<=10;i++)
{
r=r+" "+i+" "+(i*i)+" "+(i*i*i)+"\n";

}
JOptionPane.showMessageDialog(null,r);
}
}
B) Create a class Employee that includes three instance variables—a first name (type
String), a last name (type String) and a monthly salary (double). Provide a
constructor that initializes the three instance variables. Provide a set and a get
method for each instance variable. If the monthly salary is not positive, do not set
its value. Write a test app named Employee Test that demonstrates class
Employee’s capabilities. Create two Employee objects and display each object’s
yearly salary. Then give each Employee a 10% raise and display each Employee’s
yearly salary again.

public class Q2_B{


private String firstName;
private String lastName;
private double monthlySalary;
private double yearlySalary;
private double raiseYearlySalary;

public Q2_B(String firstName, String lastName, double monthlySalary){


this.firstName = firstName;
this.lastName = lastName;
if(monthlySalary > 0.00){
this.monthlySalary = monthlySalary;
}
}

public void setFirstName(String sarah){


firstName = sarah;
}

public String getFirstName(){


return firstName;
}

public void setLastName(String akinkunmi){


lastName = akinkunmi;
}

public String getLastName(){


return lastName;
}
public void setMonthlySalary(double monthlySalary){
this.monthlySalary = monthlySalary;
}

public double getMonthlySalary(){


return monthlySalary;
}

public double yearlySalary(){


return yearlySalary = monthlySalary * 12;
}

public double raiseYearlySalary(){


return yearlySalary = yearlySalary + (0.1 * yearlySalary);
}
public static void main(String[] args){
Q2_B sarahEmployee = new Q2_B("Ali", "Ahmed", 400000.57);
Q2_B nnennaEmployee = new Q2_B("Hamza", "Ali", 450000.80);

System.out.printf("The employee first name is %s%n",


sarahEmployee.getFirstName());

System.out.printf("The employee last name is %s%n",


sarahEmployee.getLastName());

System.out.printf("The employee monthly salary is %.2f%n",


sarahEmployee.getMonthlySalary());

System.out.printf("Sarah yearly salary is %.2f%n",


sarahEmployee.yearlySalary());

System.out.printf("Nnenna yearly salary is %.2f%n",


nnennaEmployee.yearlySalary());

System.out.printf("Sarah yearly salary is %.2f%n",


sarahEmployee.raiseYearlySalary());

System.out.printf("Nnenna yearly salary is %.2f%n",


nnennaEmployee.raiseYearlySalary());

}
}
Q3: A) Create a class Student that includes two instance variables—a name (type String), a
average (type double). Provide a constructor that initializes the two instance variables.
Provide a set and a get method for each instance variable. Provide getLetterGrade ()
method which return Student grade if marks >=90 return A grade if marks >=80 return B
grade if marks >=70 return C grade if marks >= 60 return D grade else return F grade. Write
Driver class Student Test that demonstrates class student class capabilities.

public class Q3_A {


private String name;
private double average;

public Q3_A(String name, double average) {


this.name = name;

if (average > 0.0) {


if (average <= 100.0) {
this.average = average;
}
}
}

public void setName(String name) {


this.name = name;
}

public String getName() {


return name;
}

public void setAverage(double average) {

if (average > 0.0) {


if (average <= 100.0) {
this.average = average;
}
}
}
public double getAverage() {
return average;
}

public String getLetterGrade() {


String letterGrade = "";

if (average >= 90.0) {


letterGrade = "A";
}
else if (average >= 80.0) {
letterGrade = "B";
}
else if (average >= 70.0) {
letterGrade = "C";
}
else if (average >= 60.0) {
letterGrade = "D";
}
else {
letterGrade = "F";
}

return letterGrade;
}

public static void main(String[] args) {


Q3_A account1 = new Q3_A("Atiq", 93.5);
Q3_A account2 = new Q3_A("Awis", 72.75);

System.out.printf("%s's letter grade is: %s%n",


account1.getName(), account1.getLetterGrade());
System.out.printf("%s's letter grade is: %s%n",
account2.getName(), account2.getLetterGrade());
}
}

B Create an Even Check class using default constructor provide Is Even method that return
true when number received even else return false on odd number

import java.util.Scanner;
public class Q3_B
{
private int value;

public void isEven(int value)


{
this.value = value;
}
public boolean getIsEven()
{
return value % 2 == 0;
}
public static void main(String[]args)
{
Q3_B obj = new Q3_B();
Scanner input = new Scanner(System.in);
System.out.println("Enter any number");
int number = input.nextInt();
obj.isEven(number);
System.out.println(obj.getIsEven());

}
}

public class Q3_B_B{


public static boolean isEven(int number) {
boolean isEven;
isEven = number % 2 == 0;
return isEven;
}

public static void main(String[] args) {


System.out.println(isEven(4));
System.out.println(isEven(12627));
System.out.println(isEven(701));
}
}

Q4: A) Write a program which display sum and average of any 10 numbers

import java.util.Scanner;

public class Q4_A {


public static void main(String[] args) {

Scanner input = new Scanner(System.in);


int total = 0;
int gradeCounter = 1;
while (gradeCounter <= 10) {
System.out.print("Enter grade: ");
int grade = input.nextInt();
total = total + grade;
gradeCounter = gradeCounter + 1;
}

int average = total / 10;

System.out.printf("%nTotal of all 10 grades is %d%n", total);


System.out.printf("Class average is %d%n", average);
}
}

B) create a Java application that will input the miles driven and gallons used (both as
integers) for each trip. The program should calculate and display the miles per gallon
obtained for each trip and print the combined miles per gallon obtained for all trips up to
this point. All averaging calculations should produce floating-point results. Use class
Scanner and sentinel-controlled repetition to obtain the data from the user.

import java.util.Scanner;

public class Q4_B {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int vehicleOwnerInput;
int tripCounter = 0;
int milesPerGallon = 0;
int totalMilesPerGallon = milesPerGallon;

do {
System.out.println("Enter the miles driven for this trip: ");
int milesDriven = input.nextInt();

System.out.println("Enter the gallons used for this trip: ");


int gallonsUsed = input.nextInt();

milesPerGallon = milesDriven / gallonsUsed;


System.out.println("The miles per gallon used for a trip is " + milesPerGallon);
totalMilesPerGallon += milesPerGallon;

tripCounter++;

System.out.println("Enter 1 to input miles driven and gallons used or press any key to
cancel");
vehicleOwnerInput = input.nextInt();
} while(vehicleOwnerInput == 1);
System.out.println("The total miles per gallon used for the " + tripCounter + " trips is " +
totalMilesPerGallon);
}

}
Q5: A) Write a program which reads 10 integer numbers by user and display largest and
smallest number using while loop.

import java.util.Scanner;

public class Q5_A {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 1;
int largest=0;
int smallest=0;
int number;
while(counter <=10){
System.out.println("Enter another number: ");
number = input.nextInt();

if(number > largest)


{
largest = number;
}
else if(number<largest)
{
smallest = number;
}
counter++;
}

System.out.println("The largest number is " + largest);


System.out.println("The smallest number is " + smallest);
}
}
B) Write an application that prints the diamond shape. You may use output statements
that print a single asterisk (*), a single space or a single newline character. Maximize your
use of repetition
(with nested for statements), and minimize the number of output statements

public class Q5_B {


public static void main(String[]args) {
int spaces = 9;
int stars = 1;
int lines = 9;
int div = lines / 2 + 1;

for(int i = 1; i <= lines; i++){


for(int s = spaces; s > 0; s--){
System.out.print(" ");
}
for(int a = stars; a > 0; a--){
System.out.print("* ");
}
System.out.println();
if(i < div){
spaces -= 2;
stars += 2;
}
else if(i >= div){
spaces += 2;
stars -= 2;
}
}

}
}

Q6: A) Write a program which input three integers’ numbers through user after that pass
three input integers numbers in addition subtraction multiplication methods and provide
display method for result output.
import java.util.Scanner;

public class Q6_A


{
private int r1;
private int r2;
private int r3;

public int Addition(int n1,int n2,int n3)


{
r1 = (n1+n2+n3);
return r1;
}
public int Substraction(int n1,int n2,int n3)
{
r2 = (n1-n2-n3);
return r2;
}
public int Multi(int n1,int n2,int n3)
{
r3 = (n1*n2*n3);
return r3;
}
public void display()
{
System.out.println("Addition is "+r1);
System.out.println("Substraction is "+r2);
System.out.println("Multiplication is "+r3);
}
public static void main(String[]args)
{
Q6_A obj = new Q6_A();
Scanner input = new Scanner(System.in);
System.out.println("Enter first number ");
int n1 = input.nextInt();
System.out.println("Enter Second number ");
int n2 = input.nextInt();
System.out.println("Enter third number ");
int n3 = input.nextInt();
obj.Addition(n1,n2,n3);
obj.Substraction(n1,n2,n3);
obj.Multi(n1,n2,n3);
obj.display();
}
}

B) Write an application that Calculate grades A, B, C, D, F in while loop using switch


statement when user press ctrl z loop exit and display grade report sum average and
students received grades

import java.util.Scanner;

public class Q6_B{


public static void main(String[] args) {
int total = 0;
int gradeCounter = 0;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;

Scanner input = new Scanner(System.in);

System.out.printf("%s%n%s%n %s%n %s%n",


"Enter the integer grades in the range 0-100.",
"Type the end-of-file indicator to terminate input:",
"On UNIX/Linux/macOS type <Ctrl> d then press Enter",
"On Windows type <Ctrl> z then press Enter");

08
while (input.hasNext()) {
int grade = input.nextInt();
total += grade;
++gradeCounter;

switch (grade / 10) {


case 9:
case 10:
++aCount;
break;
case 8:
++bCount;
break;
case 7:
++cCount;
break;
case 6:
++dCount;
break;
default:
++fCount;
break;
}
}

System.out.printf("%nGrade Report:%n");

if (gradeCounter != 0) {

double average = (double) total / gradeCounter;

System.out.printf("Total of the %d grades entered is %d%n",


gradeCounter, total);
System.out.printf("Class average is %.2f%n", average);
System.out.printf("%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n",
"Number of students who received each grade:",
"A: ", aCount,
"B: ", bCount,
"C: ", cCount,
"D: ", dCount,
"F: ", fCount);
}
else {
System.out.println("No grades were entered");
}
}
}
Q7 : A) Write a program which print following output
Number Odd Even
0 1 0
1 3 2
2 5 4
3 7 6
4 9 8
5 11 10
6 13 12
7 15 14
8 17 16
9 19 18
10 21 20

Number: 0 1 2 3 4 5 6 7 8 9 10
Square: 0 1 4 9 16 25 36 49 64 81 100
Cube: 0 1 8 27 64 125 216 343 512 729 1000

public class Q7_A


{
public static void main(String[]args)
{
String r1="";
String r2="";
String r3="";
int odd=1;
int even=0;
System.out.println("Number \t Odd \t Even");
for(int i=0;i<=10;i++)
{
System.out.println(i+"\t "+odd+"\t "+even);
odd = odd+2;
even = even+2;
r1 = r1+" "+i;
r2 = r2+" "+(i*i);
r3 = r3+" "+(i*i*i);

}
System.out.println("Number "+r1);
System.out.println("Square "+r2);
System.out.println("Cube "+r3);
}
}
public class Q7_A_A
{
public static void main(String[]args)
{
System.out.println("Number odd Even");
String s1="";
String s2="";
String s3="";
for(int i=0;i<=10;i++)
{
System.out.println(i+" "+(i*2+1)+" "+(i*2));
s1 =s1+" "+(i);
s2 = s2+" "+(i*i);
s3 = s3+" "+(i*i*i);
}
System.out.println("Number : "+s1+"\n Square "+s2+"\n cube "+s3);
}
}

B) Write a program which display following output


1 2 3 4 5 6 7 8 9 10
1 3 6 10 15 21 28 36 45 55

public class Q7_B


{
public static void main(String[]args)
{
int sum=0;
String r1="";
String r2="";
for(int i=1;i<=10;i++)
{
sum = sum+i;
r1=r1+" "+i;
r2 = r2+" "+sum;

}
System.out.println(r1);
System.out.println(r2);
}
}

You might also like