diff --git a/labs/lab4/Account.class b/labs/lab4/Account.class new file mode 100644 index 0000000..723f8e9 Binary files /dev/null and b/labs/lab4/Account.class differ diff --git a/labs/lab4/Account.java b/labs/lab4/Account.java new file mode 100644 index 0000000..ed50394 --- /dev/null +++ b/labs/lab4/Account.java @@ -0,0 +1,71 @@ +//******************************************************* +// Account.java +// +// A bank account class with methods to deposit to, withdraw from, +// change the name on, charge a fee to, and print a summary of the account. +//******************************************************* +public class Account +{ + private double balance; + private String name; + private long acctNum; + //---------------------------------------------- + //Constructor -- initializes balance, owner, and account number + //---------------------------------------------- + public Account(double initBal, String owner, long number) + { + balance = initBal; + name = owner; + acctNum = number; + } + //---------------------------------------------- + // Checks to see if balance is sufficient for withdrawal. + // If so, decrements balance by amount; if not, prints message. + //---------------------------------------------- + public void withdraw(double amount) + { + if (balance >= amount) + balance -= amount; + else + System.out.println("Insufficient funds"); + } + //---------------------------------------------- + // Adds deposit amount to balance. +//3 +//Chapter 4: Writing Classes + //---------------------------------------------- + public void deposit(double amount) + { + balance += amount; + } + //---------------------------------------------- + // Returns balance. + //---------------------------------------------- + public double getBalance() + { + return balance; + } + //---------------------------------------------- + // Returns a string containing the name, account number, and balance. + //---------------------------------------------- + public String toString() + { + String strbalance = balance+""; + return "Account name: " + name + ", Account num: " + acctNum + ", Account balance: " + strbalance; + } + //---------------------------------------------- + // Deducts $10 service fee + //---------------------------------------------- + public void chargeFee() + { + balance -= 10; + } + //---------------------------------------------- + // Changes the name on the account + //---------------------------------------------- + public void changeName(String newName) + + { + name = newName; + } +} diff --git a/labs/lab4/Grades.java b/labs/lab4/Grades.java new file mode 100644 index 0000000..9ca7da4 --- /dev/null +++ b/labs/lab4/Grades.java @@ -0,0 +1,24 @@ +// **************************************************************** +// Grades.java +// +// Use Student class to get test grades for two students +// and compute averages +// +// **************************************************************** +public class Grades +{ + public static void main(String[] args) + { + Student student1 = new Student("Mary"); + //create student2, "Mike" + Student student2 = new Student("Mike"); + //input grades for Mary + student1.inputGrades(); + //print average for Mary + System.out.println(student1.getAverage()); + //input grades for Mike + student2.inputGrades(); + //print average for Mike + System.out.println(student2.getAverage()); + } +} \ No newline at end of file diff --git a/labs/lab4/ManageAccounts.java b/labs/lab4/ManageAccounts.java new file mode 100644 index 0000000..603fef9 --- /dev/null +++ b/labs/lab4/ManageAccounts.java @@ -0,0 +1,33 @@ +// **************************************************************** +// ManageAccounts.java +// +// Use Account class to create and manage Sally and Joe's +// bank accounts +// **************************************************************** +public class ManageAccounts +{ + public static void main(String[] args) + { + Account acct1, acct2; + //create account1 for Sally with $1000 + acct1 = new Account(1000, "Sally", 1111); + //create account2 for Joe with $500 + acct2 = new Account(500, "Joe", 2222); + //deposit $100 to Joe's account + acct2.deposit(100); + //print Joe's new balance (use getBalance()) + acct2.getBalance(); + //withdraw $50 from Sally's account + acct1.withdraw(50); + //print Sally's new balance (use getBalance()) + acct1.getBalance(); + //charge fees to both accounts + acct1.chargeFee(); + acct2.chargeFee(); + //change the name on Joe's account to Joseph + acct2.changeName("Joseph"); + //print summary for both accounts + System.out.println(acct1); + System.out.println(acct2); + } +} diff --git a/labs/lab4/Student.class b/labs/lab4/Student.class new file mode 100644 index 0000000..05abdac Binary files /dev/null and b/labs/lab4/Student.class differ diff --git a/labs/lab4/Student.java b/labs/lab4/Student.java new file mode 100644 index 0000000..2168af9 --- /dev/null +++ b/labs/lab4/Student.java @@ -0,0 +1,46 @@ +// **************************************************************** +// Student.java +// +// Define a student class that stores name, score on test 1, and +// score on test 2. Methods prompt for and read in grades, +// compute the average, and return a string containing student’s info. +// **************************************************************** +import java.util.Scanner; +public class Student +{ + private String studentName; + private double test1; + private double test2; + public Student(String studentName) + { + this.studentName = studentName; + this.test1 = 0; + this.test2 = 0; + } + //----------------------------------------------- + //inputGrades: prompt for and read in student's grades for test1 and test2. + //Use name in prompts, e.g., "Enter's Joe's score for test1". + //----------------------------------------------- + public void inputGrades() + { + System.out.println("Enter "+studentName+"'s score for test1"); + Scanner scan = new Scanner(System.in); + test1 = scan.nextDouble(); + System.out.println("Enter "+studentName+"'s score for test2"); + test2 = scan.nextDouble(); + } + //----------------------------------------------- + //getAverage: compute and return the student's test average + //----------------------------------------------- + public double getAverage() + { + return (test1+test2)/2; + } + //----------------------------------------------- + //getName: print the student's name + //----------------------------------------------- + public String getName() + { + return studentName; + } +} \ No newline at end of file