DEPARTMENT OF COMPUTER
SOFTWARE ENGINEERING
MILITARY COLLEGE OF SIGNALS,
NUST, RAWALPINDI
Object Oriented Programming
Assignment no: 02
STUDENT DETAILS:
NAME SARA AMIN
DEPARTMENT SE-29 C
CMS ID 464417
INSTRUCTOR Col Khawir
DATE 1 May, 2024
ASSIGNMENT NO: 02
Question n0 :1
CODE:
Package rectangleClass;
class Rectangle
{
double width;
double height;
public Rectangle(){
width=1;
height=1;
}
public Rectangle(double width,double height)
{
this.width=width;
this.height=height;
}
public double getArea ()
{
return width*height;
}
public double getPerimetrer()
{
return 2*(width+height);
}
}
public class RectangleClass {
public static void main(String[] args) {
Rectangle r =new Rectangle(4,40);
Rectangle r1 =new Rectangle(3.5,35.9);
System.out.println("First Rectangle ");
System.out.println("Width: "+r.width+" Height: "+r.height+" Area: "+
r.getArea()+" Perimeter: "+r.getPerimetrer());
System.out.println("Second Rectangle ");
System.out.println("Width: "+r1.width+" Height: "+r1.height+" Area:
"+r1.getArea()+" Perimeter: "+r1.getPerimetrer());
}
UML DIAGRAM:
Rectangle
+ width : double
+ height : double
+area: double
+perimeter: double
+ Rectangle()
+ Rectangle( double , double )
+ getArea() : double
+ getPerimeter() : double
+display() : void
r : Rectangle R1 : Rectangle
width : 4 width : 3.5
height : 40 height : 35.9
QUESTION NO : 02
CODE:
package stockClass;
class Stock
{
String symbol;
String name;
double previousClosingPrice;
double currentPrice;
Stock(String symbol,String name)
{
this.symbol=symbol;
this.name=name;
}
public double getChangePercent()
{
return (currentPrice-previousClosingPrice)/(previousClosingPrice) *
100;
}
}
public class StockClass {
public static void main(String[] args) {
Stock s1=new Stock("ORCL","Oracle");
s1.previousClosingPrice=50;
s1.currentPrice=55;
System.out.println(" The change percentage is"
+s1.getChangePercent());
Stock
+ symbol : String
+ name : String
+ previousClosingPrice : double
+ currentPrice : double
+ Stock( String , String )
+display() :void
+ getpercentage() : double
s1 : Stock
symbol : ORCL
name : Oracle
previousClosingPrice : 34.5
currentPrice : 34.35
QUESTION NO: 03
CODE:
package accountClass;
import java.util.*;
class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated = new Date();
Account() {
id = 0;
balance = 0;
annualInterestRate = 0.0;
}
Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
return (annualInterestRate) / 12;
}
public double getMonthlyInterest() {
return (this.getMonthlyInterestRate() / 100) * balance;
}
public void withdraw(double amount) {
if (amount < balance)
balance = balance - amount;
else
System.out.println("Balance not enough");
}
public void deposit(double amount) {
balance = balance + amount;
}
}
public class AccountClass {
public static void main(String[] args) {
Account A1 = new Account(1122, 20000);
A1.setAnnualInterestRate(4.5);
A1.withdraw(2500);
A1.deposit(3000);
System.out.println(A1.getBalance());
System.out.println(A1.getMonthlyInterest());
System.out.println(A1.getDateCreated());
}
}
UML DAIGRAM:
Account
-id: int
-balance: double
-AnnualInterestRate: double
-dateCreated: Date
Account()
Account(int,double)
+setId( int) : void
+setBalance(double) : void
+setAnnualRate(double):void
+get id:int
+getBalance: double
+getAnnualRate:double
+getdate: Date
+getMonthlyinterestrate(): double
+montlhyInterest() : double
+withdraw():void
+deposit():void
a1: Account
setannualrate(double) :void
withdraw(int): void
deposit(int):void
QUESTION NO: 04
CODE:
package stopwatchClass;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Random;
class StopWatch
{
private Date starttime;
private Date endtime;
StopWatch()
{
starttime= new Date();
}
public String getstarttime()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String time = dateFormat.format(this.starttime);
return time;
}
public String getendtime()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String time = dateFormat.format(this.endtime);
return time;
}
public void start()
{
starttime=new Date ();
}
public void stop()
{
endtime=new Date();
}
public long getElapsedTime()
{
return endtime.getTime()-starttime.getTime() ;
}
}
public class StopwatchClass {
public static void main(String[] args) {
Random no = new Random();
int [] numbers=new int[100000];
for (int i=0;i<100000;i++)
{
numbers[i]=no.nextInt(100);
}
StopWatch time = new StopWatch();
time.start();
for (int i = 0; i < 100000; i++) {
int minIndex = i;
for (int j = i + 1; j < (100000-1); j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the current element
int temp = numbers[minIndex];
numbers[minIndex] = numbers[i];
numbers[i]= temp;}
time.stop();
System.out.println(: Elapsed Time is " + time.getElapsedTime());
UML DIAGRAM:
StopWatch
- startTime : Date
- endTime : Date
+ StopWatch()
+ start() : void
+ stop() : void
+ ElapsedTime() : long
+ getstarttime() :String
+ getendtime() : String
watch : StopWatch
start() : void
stop() : void
getstarttime(): String
getendtime(): String
Elapsedtime(): Long
QUESTION NO: 05
CODE:
package locationClass ;
import java.util.Scanner;
class Location
{
int rows;
int column;
double maxvalue;
Location(int rows,int column)
{
this.rows=rows;
this.column=column;
public static Location locatelargest(double [][]arr)
{
Location l1=new Location(0,0);
for (int i=1;i<arr.length;i++)
{
for (int j=1;j<arr[0].length;j++)
{
if (arr[i][j]>arr[l1.rows][l1.column])
{
l1.rows=i;
l1.column=j;
l1.maxvalue=arr[i][j];
}
}
}
return l1;
}
}
public class LocationClass {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int rows ;
int column;
System.out.println("Enter rows :");
rows=in.nextInt();
System.out.println("Enter Columns :");
column=in.nextInt();
double[][] arr = new double[rows][column];
System.out.println("Enter array: ");
for (int i=0;i<rows;i++)
{
for (int j=0;j<column;j++)
{
arr[i][j]=in.nextDouble();
}
Location l2=Location.locatelargest(arr);
System.out.println("max value "+l2.maxvalue+" is found at
location["+l2.rows+"]["+l2.column+"]");
UML DAIGRAM:
Location
+row: int
+column: int
+maxvalue: double
Location(int,int)
+locatelargest(double[][]): Location
l1 : Location
Maxvalue
Row
Column
Locatelargest(double [][]):Location