lab 3
This commit is contained in:
parent
4abc213b6e
commit
16bce4463d
34
src/lab/three/one/Account.java
Normal file
34
src/lab/three/one/Account.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
36
src/lab/three/one/AccountTest.java
Normal file
36
src/lab/three/one/AccountTest.java
Normal file
@ -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() );
|
||||
|
||||
}
|
||||
|
||||
}
|
62
src/lab/three/two/Student.java
Normal file
62
src/lab/three/two/Student.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
11
src/lab/three/two/StudentTest.java
Normal file
11
src/lab/three/two/StudentTest.java
Normal file
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user