Object oriented programming
Course name:
Object oriented programming
Course code:
SE-103
Assignment #1
Submitted to:
Ms. Iqra Munawar
Submitted by:
Muhammad Awais
25021598-083
Semester:
Second
Department:
Software Engineering Section AD
Table of Contents
Program no 01:..........................................................................................................................................1
Code:.......................................................................................................................................................1
Output:...................................................................................................................................................1
Program no 02:..........................................................................................................................................2
Code:.......................................................................................................................................................2
Output:...................................................................................................................................................3
Program no 03:..........................................................................................................................................3
Code:.......................................................................................................................................................3
Output:...................................................................................................................................................4
Program no 04:..........................................................................................................................................4
Code:.......................................................................................................................................................4
Output:...................................................................................................................................................5
Program no 05:..........................................................................................................................................5
Code:.......................................................................................................................................................5
Output:....................................................................................................................................................6
Program no 06:..........................................................................................................................................6
Code:.......................................................................................................................................................6
Output:...................................................................................................................................................8
Program no 07:..........................................................................................................................................8
Code:.......................................................................................................................................................8
Output:...................................................................................................................................................9
Program no 08:..........................................................................................................................................9
Code:.....................................................................................................................................................10
Output:.................................................................................................................................................10
Program # 01:
What does the following program do?
Problem-code:
public class Counting {
public static void main ( String[] args )
Scanner s = new Scanner([Link]);
for (int i = 1; i < 3; i++)
for (int j = 1; j < 5; j++)
[Link]('*');
[Link]("\n#####");
Output:
Program no 02:
Write an application that finds the minimum and maximum amongst several integers and then
computes the sum of the two extremes. The user will be prompted to input how many values the
application should ask the user to input.
Problem-code:
package loops_Java;
import [Link];
public class Extremes {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("How many numbers do you want to enter: ");
int n = [Link]();
[Link]("Enter number 1: ");
int num = [Link]();
int min = num;
int max = num;
for (int i = 2; i <= n; i++) {
[Link]("Enter number " + i + ": ");
num = [Link]();
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
}
int sum = min + max;
[Link]("Minimum = " + min);
[Link]("Maximum = " + max);
[Link]("Sum of extremes = " + sum);
}
}
Output:
Program no 03:
Write an application that calculates the sum of those integers between 1 and 30 that are divisible
by 3.
Problem-code:
package loops_Java;
public class Sum{
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 30; i++) {
if (i % 3 == 0) {
sum += i;
}
}
[Link]("Sum of numbers divisible by 3 from 1 to 30
is: " + sum);
}
}
Output:
Program no 04:
Find the summation of the sequence of numbers 1, 2, 3 … n, where n ranges from 1 to 100. Use
type long. Display the results in a tabular format that shows n and the core responding sum. If
this were a product instead of a sum, what difficulty might you encounter with the variable that
accumulates the product?
Problem-code:
package loops_Java;
public class SumSeries{
public static void main(String[] args) {
[Link](" n\tSum");
[Link]("----------------");
long sum = 0;
for (int n = 1; n <= 100; n++) {
sum += n;
[Link]("%3d\t%6d%n", n, sum);
}
}
Output:
Program no 05:
Modify the compound-interest application of Fig. 4.6 to repeat its steps for interest rates of 5%,
6%, 7%, 8%, 9% and 10%. Use a for loop to vary the interest rate.
Problem-code:
package loops_Java;
public class CompoundInterest {
public static void main(String[] args) {
double principal = 1000;
int years = 10;
for (int rate = 5; rate <= 10; rate++) {
[Link]("Interest Rate: %d%%\n", rate);
for (int year = 1; year <= years; year++) {
double amount = principal * [Link](1 + rate / 100.0,
year);
[Link]("Year %2d: %.2f\n", year, amount);
}
[Link]();
}
}}
Output:
Program no 06:
A group of five students earned the following grades: Student 1, ‘A’; student 2, ‘C’; student 3,
‘B’; student 4, ‘A’ and student 5, ‘B’. Write an application that reads a series of pairs of numbers
as follows:
a) student name
b) student letter grade
Your program should use a switch statement to determine how many students got a grade of ‘A’,
how many got a grade of ‘B’, how many got a grade of ‘C’, and how many got a grade of ‘D’.
Use a loop as needed to input the five student grades, and then finally display the results.
Problem-code:
package loops_Java;
import [Link];
public class StudentGrades{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int countA = 0, countB = 0, countC = 0, countD = 0;
for (int i = 1; i <= 5; i++) {
[Link]("Enter name of student " + i + ": ");
String name = [Link]();
[Link]("Enter grade of " + name + " (A/B/C/D): ");
char grade = [Link]().toUpperCase().charAt(0);
switch (grade) {
case 'A':
countA++;
break;
case 'B':
countB++;
break;
case 'C':
countC++;
break;
case 'D':
countD++;
break;
default:
[Link]("Invalid grade entered!");
i--;
continue;
}
}
[Link]("\nGrade Summary:");
[Link]("Number of A's: " + countA);
[Link]("Number of B's: " + countB);
[Link]("Number of C's: " + countC);
[Link]("Number of D's: " + countD);
[Link]();
}
}
Output:
Program no 07:
Assume that i = 2, j = 3, k = 2 and m = 2. What does each of the following statements print?
a) [Link](i == 2);
b) [Link](j == 5);
c) [Link]((i >= 0) && (j <= 3));
d) [Link]((m <= 100) & (k <= m));
e) [Link]((j >= i) || (k != m));
f) [Link]((k + i < j) | (4 - j >= k));
g) [Link](!(k > j));
Problem-code:
package loops_Java;
public class BooleanExpressions {
public static void main(String[] args) {
int i = 2, j = 3, k = 2, m = 2;
[Link]("a) i == 2: " + (i == 2));
[Link]("b) j == 5: " + (j == 5));
[Link]("c) (i >= 0) && (j <= 3): " + ((i >= 0) && (j
<= 3)));
[Link]("d) (m <= 100) & (k <= m): " + ((m <= 100) &
(k <= m)));
[Link]("e) (j >= i) || (k != m): " + ((j >= i) || (k
!= m)));
[Link]("f) (k + i < j) | (4 - j >= k): " + ((k + i <
j) | (4 - j >= k)));
[Link]("g) !(k > j): " + (!(k > j)));
}
}
Output:
Program no 08:
In this chapter, we discussed the logical operators &&, &, ||, |, ^ and !. De Morgan’s laws can
sometimes make it more convenient for us to express a logical expression. These laws state that
the expression !(condition1 && condition2) is logically equivalent to the expression (!condition1
|| !condition2). Also, the expression!(condition1 || condition2) is logically equivalent to the
expression (!condition1 && !condition2). Use De Morgan’s laws to write equivalent expressions
for each of the following, then write an application to show that both the original expression and
the new expression in each case produce the same value:
a) !(x < 5) && !(y >= 7)
b) !(a == b) || !(g != 5)
c) !((x <= 8) && (y > 4))
d) !((i > 4) || (j <= 6))
Problem-code:
package loops_Java;
public class DemorgansLaw {
public static void main(String[] args) {
int x = 6, y = 5, a = 2, b = 3, g = 5, i = 4, j = 7;
boolean originalA = !(x < 5) && !(y >= 7);
boolean equivalentA = (x >= 5) && (y < 7);
[Link]("a) Original: " + originalA + ", Equivalent: "
+ equivalentA);
boolean originalB = !(a == b) || !(g != 5);
boolean equivalentB = (a != b) || (g == 5);
[Link]("b) Original: " + originalB + ", Equivalent: "
+ equivalentB);
boolean originalC = !((x <= 8) && (y > 4));
boolean equivalentC = (x > 8) || (y <= 4);
[Link]("c) Original: " + originalC + ", Equivalent: "
+ equivalentC);
boolean originalD = !((i > 4) || (j <= 6));
boolean equivalentD = (i <= 4) && (j > 6);
[Link]("d) Original: " + originalD + ", Equivalent: "
+ equivalentD);
}
}
Output: