Nothing Special   »   [go: up one dir, main page]

Assignment 01

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

The name: Khaled Wael Hania Number No: 120211958

Assignment #1
EX 9_02
class Stock{
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;

public Stock(String symbol , String name){


this.symbol = symbol;
this.name = name;
}

public double getChangePercent() {


return (currentPrice - previousClosingPrice) / currentPrice;
}

public void setPreviousClosingPrice(double previousClosingPrice) {


this.previousClosingPrice = previousClosingPrice;
}

public void setCurrentPrice(double currentPrice) {


this.currentPrice = currentPrice;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getSymbol() {


return symbol;
}

public void setSymbol(String Symbol) {


this.symbol = symbol;
}
}

public class Stockmain {


public static void main(String[] args) {
Stock testStock = new Stock("ORCL", "Oracle Corporation");
testStock.setPreviousClosingPrice(34.5);
testStock.setCurrentPrice(34.35);

System.out.println("The price change for " + testStock.getSymbol() + " " +


testStock.getName() + " is " + Math.round(testStock.getChangePercent()
* 10000.00) / 100.00 + "%");
}
}
EX 9_04
import java.util.Random;
public class Ex9_04 {
private static final int seed = 1000;
private static final int n = 100;

public static void main(String[] args) {


Random random = new Random(seed);
for (int i = 0; i < 50; i++) {
if (i % 10 == 0) System.out.println();

System.out.print(random.nextInt(n) + " ");


}
}
}

EX 9_06
class StopWatch{
private int startTime;
private int endTime;

public int getStartTime(){


return startTime;
}

public int getEndTime(){


return endTime;
}

public StopWatch (){


startTime = (int) System.currentTimeMillis();
}

public void start(){


startTime = (int) System.currentTimeMillis();
}

public void stop(){


endTime = (int) System.currentTimeMillis();
}

public long getElapsedTime() {


return (endTime - startTime);
}
}
import java.util.Random;
public class StopWatchMain {
public static void main(String[] args) {
int[] testNums = new int[100000];
Random random = new Random();

for (int i = 0; i < 100000; i++) {


testNums[i] = random.nextInt(10000);
}
StopWatch stopWatch = new StopWatch();
stopWatch.start();
System.out.println("Start time: " + stopWatch.getStartTime() + " ms");

selectionSort(testNums);
stopWatch.stop();

System.out.println("End time: " + stopWatch.getEndTime() + " ms");

System.out.println("Elapsed time for selection sort: " +


stopWatch.getElapsedTime() + " ms");
}

public static void selectionSort(int[] nums) {


for (int n = 0; n < nums.length - 1; n++) {
int min = nums[n];
int minIdx = n;
for (int k = n + 1; k < nums.length; k++) {
if (nums[k] < min) {
min = nums[k];
minIdx = k;
}
}
// Swap
if (minIdx != n) {
nums[minIdx] = nums[n];
nums[n] = min;
}
}
}
}

EX 9_07
import java.util.*;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;

public Account() {
id = 0;
balance = 0;
annualInterestRate = 0;
}

public Account(int newId, double newBalance) {

this.id = newId;
this.balance = newBalance;
dateCreated = new Date();
annualInterestRate = 0;
}

public int getID() {

return id;
}

public void setID(int newID) {

this.id = newID;
}

public double getBalance() {

return balance;
}

public void setBalance(double newBalance) {

this.balance = newBalance;
}

public double getAnnualInterestRate() {


return annualInterestRate;
}

public void setAnnualInterestRate(double newInterestRate) {

this.annualInterestRate = newInterestRate;
}

public Date getDateCreated() {

Date startDate = dateCreated;

return startDate;
}

public double getMonthlyInterestRate() {

double InterestRatePerMonth = (annualInterestRate / 12);

return InterestRatePerMonth;
}

public double getMonthlyInterest() {

double monthlyInterest = balance * (this.getMonthlyInterestRate() / 100);

return monthlyInterest;
}

public void withdraw(double amount) {

balance -= amount;
}
public void deposit(double amountDeposit) {

balance += amountDeposit;

public String toString() {

return "\nAccount ID: " + getID() + "\nBalance: " + getBalance() +


"\nDate created " + getDateCreated();
}

public class AccountMain {

public static void main(String[] args) {


Account obj = new Account(1122, 20_000);
obj.setAnnualInterestRate(4.5);
obj.withdraw(2500);
obj.deposit(3000);

System.out.println("The balance in account " + obj.getID() + " is now: $" +


obj.getBalance());
System.out.println("The total interest, at a rate of " +
obj.getMonthlyInterestRate() + "%"
+ " per month is " + obj.getMonthlyInterest());
System.out.println("And your account was created on " +
obj.getDateCreated());
}
}

EX 9_11
public class LinearEquation {
private double a;
private double b;
private double c;
private double d;
private double e;
private double f;

public LinearEquation(double a, double b, double c, double d, double e, double


f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;

public double getA() {


return a;
}

public double getB() {


return b;
}

public double getC() {


return c;
}

public double getD() {


return d;
}

public double getE() {

return e;
}

public double getF() {


return f;
}

public boolean isSolvable() {

return a * d - b * c != 0;
}

public double getX() {

double x = (this.getE() * this.getD() - this.getB() * this.getF()) /


(this.getA() * this.getD() - this.getB() * this.getC());

return x;
}

public double getY() {


double y = (this.getA() * this.getF() - this.getE() * this.getC()) /
(this.getA() * this.getD() - this.getB() * this.getC());
return y;
}
}

import java.util.*;
public class LinearEquationMain {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

double a, b, c, d, e, f;

System.out.println("Enter values for a, b, c, d, e, and f now:");

a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
d = input.nextDouble();
e = input.nextDouble();
f = input.nextDouble();

LinearEquation tester = new LinearEquation(a, b, c, d, e, f);

if (tester.isSolvable()) {

System.out.println("x is " + tester.getX());


System.out.println("y is " + tester.getY());
} else {
System.out.println("The equation has no solution");
}
input.close();
}
}

You might also like