diff --git a/src/lab/three/one/Account.java b/src/lab/three/one/Account.java new file mode 100644 index 0000000..e57bbd9 --- /dev/null +++ b/src/lab/three/one/Account.java @@ -0,0 +1,34 @@ +package lab.three.one; + + +public class Account +{ + private double balance; + public Account( double initialBalance ) + { + + if ( initialBalance > 0.0 ) + balance = initialBalance; + } + + public void credit( double amount ) + { + balance = balance + amount; + } + + public void debit(double amount ) { + double newBalance = balance - amount; + if (newBalance < 0) { + System.out.println("Debit amount exceeds balance."); + } + else + balance = newBalance; + + } + + public double getBalance() + { + return balance; + } +} + diff --git a/src/lab/three/one/AccountTest.java b/src/lab/three/one/AccountTest.java new file mode 100644 index 0000000..628cc0c --- /dev/null +++ b/src/lab/three/one/AccountTest.java @@ -0,0 +1,36 @@ +package lab.three.one; + +import java.util.Scanner; + +public class AccountTest { + + public static void main(String[] args) { + + Account account1 = new Account( 50.00 ); + Account account2 = new Account( -7.53 ); + + System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); + System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() ); + + Scanner input = new Scanner( System.in ); + double withdrawalAmount; // withdrawal amount read from user + + System.out.print( "Enter withdrawal amount for account1: " ); + withdrawalAmount = input.nextDouble(); // obtain user input + System.out.printf( "\nsubtracting %.2f from account1 balance\n", withdrawalAmount ); + account1.debit(withdrawalAmount); + // display balances + System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); + System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() ); + + System.out.print( "Enter withdrawal amount for account2: " ); + withdrawalAmount = input.nextDouble(); // obtain user input + System.out.printf( "\nsubtracting %.2f from account2 balance\n", withdrawalAmount ); + account2.debit(withdrawalAmount); + // display balances + System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); + System.out.printf( "account2 balance: $%.2f\n", account2.getBalance() ); + + } + +} diff --git a/src/lab/three/two/Student.java b/src/lab/three/two/Student.java new file mode 100644 index 0000000..d19356f --- /dev/null +++ b/src/lab/three/two/Student.java @@ -0,0 +1,62 @@ +package lab.three.two; + +public class Student { + + private String firstName; + private String lastName; + private double grade1, grade2, grade3; + + public Student(String firstName, String lastName, double grade1, double grade2, double grade3) { + this.firstName = firstName; + this.lastName = lastName; + this.grade1 = grade1; + this.grade2 = grade2; + this.grade3 = grade3; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public double getGrade1() { + return grade1; + } + + public void setGrade1(double grade1) { + this.grade1 = grade1; + } + + public double getGrade2() { + return grade2; + } + + public void setGrade2(double grade2) { + this.grade2 = grade2; + } + + public double getGrade3() { + return grade3; + } + + public void setGrade3(double grade3) { + this.grade3 = grade3; + } + + public double getAverage() { + return (grade1 + grade2 + grade3)/3; + } + + +} diff --git a/src/lab/three/two/StudentTest.java b/src/lab/three/two/StudentTest.java new file mode 100644 index 0000000..e19d5c6 --- /dev/null +++ b/src/lab/three/two/StudentTest.java @@ -0,0 +1,11 @@ +package lab.three.two; + +public class StudentTest { + + public static void main(String[] args) { + Student stu = new Student("Ahmad", "Barakat", 95, 90.5, 87); + String message = "The student %s has an average of %.2f"; + System.out.printf(message, stu.getFirstName(), stu.getAverage()); + } + +}