Académique Documents
Professionnel Documents
Culture Documents
12 SOUTH ASIAN INSTITUTE OF TECHNOLOGY AND MANAGEMENT (SAITM) FACULTY OF ENGINEERING DEPARTMENT OF MECHATRONICS ENGINEERING
INSTRUCTED BY: DR. MANODHA GAMAGE NAME: LEWIS.S.G STUDENT ID: EN11ME2039 FIELD: MECHATRONICS DATE OF SUB.: 2011-06-21
RectangleMover Class
import import import import public public { java.awt.Graphics; java.awt.Graphics2D; java.awt.Rectangle; javax.swing.JComponent; class RectangleComponent extends JComponent{ RectangleComponent() topBox = new Rectangle (210, 0, BOX_WIDTH, BOX_HEIGHT); bottomBox = new Rectangle (210, 400, BOX_WIDTH, BOX_HEIGHT); leftBox = new Rectangle (0, 200, BOX_WIDTH, BOX_HEIGHT); rightBox = new Rectangle (420, 200, BOX_WIDTH, BOX_HEIGHT); } public void paintComponent (Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.draw(topBox); g2.draw(bottomBox); g2.draw(leftBox); g2.draw(rightBox); } public void moveBy(int i, int j) { if (topBox.getY() != bottomBox.getY()) { topBox.translate(0, 1); bottomBox.translate(0, -1); } if (leftBox.getX() != rightBox.getX()) { leftBox.translate(1, 0); rightBox.translate(-1, 0); } repaint(); } private Rectangle topBox; private Rectangle bottomBox; private Rectangle leftBox; private Rectangle rightBox; private static final int BOX_WIDTH = 40; private static final int BOX_HEIGHT = 40; }
Manager Classpublic
public Manager (String aName, double aSalary, String aDepartment) {super (aName, aSalary); this.department = aDepartment; } public String toString() { return super.toString() + "[department = " + department + "]"; } private String department; }
Executive Class
public class Executive extends Manager{ public Executive (String aName, double aSalary, String aDepartment) { super (aName, aSalary, aDepartment); } }
Tester Class
public class Tester { public static void main(String[] args) { Employee a = new Employee("Ashan", 50000); Manager b = new Manager("Akhila", 40000, "Mechatronics"); Executive c = new Executive("Hasan", 75000, "Media"); System.out.println(a); System.out.println("Expected:Employee[name=Ashan,salary=50000.0]"); System.out.println(b); System.out.println("Expected: Manager[super=Employee[name=Akhila,salary=40000.0],department=Mechatronics]") ; System.out.println(c); System.out.println("Expected: Executive[super=Manager[super=Employee[name=Hasan,salary=75000.0],department= Media]]"); } }
Exercise P10.4 Person Class /** This class represents a person's name and the year of birth */ public class Person { /** Constructs a Person object. @param aName the name of the person @param aYear the year of the person's birth */ public Person(String aName, double aYear) { this.name = aName; this.year = aYear; } /** Gets the name of the person. @return name the person's name */ public String getName() { return this.name; } /** Gets the year of the person's birth. @return year the person's birth */ public double getYear() { return this.year; } public String toString() { return getClass().getName() + "[name=" + name + "] [ year =" + year + "]"; } private String name; private double year; }
Instructor Class public class Instructor extends Person{ private double salary; public Instructor(String aName, double aYear, double aSalary) { super (aName, aYear); this.salary = aSalary; } public String toString() { return super.toString() + "[salary = " + salary + "]"; } }
Student Class public class Student extends Person{ private String major; public Student(String aName, double aYear , String aMajor) { super (aName, aYear); this.major= aMajor; } public String toString() { return super.toString() + "[major= " + major + "]"; } }
PersonTester Class /** This class tests the Person, Student, and Instructor classes. */ public class PersonTester { public static void main(String[] args) { Person p = new Person("Perry", 1959); Student s = new Student("Sylvia", 1979, "Computer Science"); Instructor e = new Instructor("Edgar", 1969, 65000); System.out.println(p); System.out.println("Expected:Person[name=Perry,birthYear=1959]"); System.out.println(s); System.out.println("Expected: Student[super=Person[name=Sylvia,birthYear=1979],major=Computer Science]"); System.out.println(e); System.out.println("Expected: Instructor[super=Person[name=Edgar,birthYear=1969],salary=65000.0]"); } }
3 Home Work Exercises 3.1 HW Exercise 1. BankAccount Class public class BankAccount { private double balance; /** Constructs a bank account with a zero balance. */ public BankAccount() { balance = 0; } /** Constructs a bank account with a given balance. @param initialBalance the initial balance */ public BankAccount(double initialBalance) { balance = initialBalance; } public BankAccount(int i, int j) { // TODO Auto-generated constructor stub} /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { balance = balance + amount;} /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { balance = balance - amount;} /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; }
/** Transfers money from the bank account to another account @param amount the amount to transfer @param other the other account */ public void transfer(double amount, BankAccount other){ withdraw(amount); other.deposit(amount); } public void addInterest() { // TODO Auto-generated method stub }}
CheckingAccount Class public class CheckingAccount extends BankAccount { public CheckingAccount(int initialBalance) { // construct superclass super(initialBalance); // initialize transaction count transactionCount = 0; } public void deposit(double amount) { transactionCount++; // now add amount to balance super.deposit(amount); } public void withdraw(double amount) { transactionCount++; // now subtract amount from balance super.withdraw(amount); } public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE *(transactionCount FREE_TRANSACTIONS); super.withdraw(fees);} transactionCount = 0; } private int transactionCount; private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2.0; }
SavingsAccount Class
public class SavingsAccount extends BankAccount{ private double interestRate; /** Constructs a bank account with a given interest rate. @param rate the interest rate */ public SavingsAccount(double rate) { interestRate = rate; } /** Adds the earned interest to the account balance. */ public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } }
TimeDepositTester Class /** This program tests the BankAccount class and its subclasses. */ public class TimeDepositTester { public static void main(String[] args) { SavingsAccount momsSavings = new SavingsAccount(5); CheckingAccount harrysChecking = new CheckingAccount(100); BankAccount collegeFund = new BankAccount(10, 3); momsSavings.deposit(10000); momsSavings.transfer(2000, harrysChecking); harrysChecking.withdraw(1500); harrysChecking.withdraw(80); momsSavings.transfer(1000, harrysChecking); harrysChecking.withdraw(400);
momsSavings.transfer(3000, collegeFund); collegeFund.withdraw(800); // simulate end of month momsSavings.addInterest(); collegeFund.addInterest(); harrysChecking.deductFees(); System.out.println("Mom's savings balance: "+ momsSavings.getBalance()); System.out.println("Expected: 4200"); System.out.println("Harry's checking balance: " + harrysChecking.getBalance()); System.out.println("Expected: 1116"); System.out.println("College fund's time deposit balance: "+ collegeFund.getBalance()); System.out.println("Expected: 2398"); } }
3.2 HW Exercise 2
Worker Class
public class Worker { private String name; private double rate; private double totalPay; public Worker(String aName, double aRate){ this.name = aName; this.rate = aRate;} public String getName(){ return this.name;} public double getRate(){ return this.rate;} public String toString(){ return getClass().getName() + "[name=" + name + "] [ salary =" + totalPay + "]";} public double computePay(int hours) { return (rate * hours); }}
HourlyPaidWorker Class
public class HourlyPaidWorker extends Worker { private double wage; private double hours; public HourlyPaidWorker(String aName, double aRate) { super (aName, aRate);} public double computePay(int hours) { double totalPay = 0; if(hours >= 0 && hours <= 40 ) totalPay = this.getRate()*hours; else totalPay = this.getRate() * ((hours - 40) * 1.5 + 40); return totalPay;} public String toString() {return super.toString() ;}}
MonthlyPaidWorker Class
public class MonthlyPaidWorker extends Worker { public MonthlyPaidWorker(String aName, double aRate) { super (aName, aRate);} public double computePay(int hours){ double totalPay = 0; totalPay = this.getRate() * 40; return totalPay;} public String toString(){ return super.toString() ;}}
Tester Class
public class Tester { public static void main(String[] args) { Worker s = new MonthlyPaidWorker("Akhila", 50); Worker h = new HourlyPaidWorker("Hasan", 60); System.out.println(s.computePay(30)); System.out.println("Expected: 2000"); System.out.println(h.computePay(30)); System.out.println("Expected: 1800"); System.out.println(s.computePay(50)); System.out.println("Expected: 2000"); System.out.println(h.computePay(50)); System.out.println("Expected: 3300"); } }