Sample Solution Assignment 5 Question R3.1 The interface of a class is the part that the user interacts with, not necessarily knowing how it works. An implementation of is the creation of this interface. Question R3.2 Encapsulation is hiding the unnecessary details of implementation of a class from its user. The idea is that the user of the class can use it without worrying how it works, simplifying its job. Question R3.3 The private reserved word provides hiding of information to only the user of the class that doesn't have its source code. This private interface variables can only be modified by the methods or constructors of the class, so they aren't directly modifiable by the user of the class. Question R3.5 Set 1: private integer hours; private integer minutes; Set 2: private string am_pm; private integer hours; private integer minutes; private integer seconds; Question R3.6 Nothing because the details of the implementation are hidden to the user of the class, they only have to use the public interface and as it remains the same they don't have to do anything. Question R3.8 a. The programmer can obtain BankAccount objects by using the blank constructor and then using the deposit method to assign an arbitrary balance to the new account. b. Yes, since a new BankAccount object with zero balance can be obtained by just setting the balance of the account to 0.
Question R3.10 The balance of the the account becomes negative. This is a logic error because you can't retire more money than you have, so the implementation must be changed to avoid this. Question R3.12 The mystery method retires a quantity of money from the object being used (its implicit parameter) specified by the parameter amount and deposits it in the balance of another account specified by the that parameter. BankAccount exampleaccount = new BankAccount(2000); BankAccount targetaccount = new BankAccount(); exampleaccount.mystery(targetaccount, 1500); Question R3.14 The area is meant to be calculated out of the side lengths of a square. If there's a field which holds the value of area, it means that somehow it could be possible (by for example, extending the class) change the value of area arbitrarily to some value that doesn't makes sense. It would be better then to have something like this: public int getarea() { int area = sidelength*sidelength; return area; Question R3.15 sidelength isn't a method, is a field. My fix would be rewriting the grow() method as public void grow() { sidelength = 2 * sidelength; Question R3.18 A House class would be required for drawing a single house. A HouseComponent class would be required to display the houses drawings and a HouseCarViewer class would be required to display frames with houses and cars together.
Question P3.1 public class BankAccountTester { BankAccount testaccount = new BankAccount(); testaccount.deposit(1000); testaccount.withdraw(500); testaccount.withdraw(400); System.out.print("Current balance: "); System.out.print(testAccount.getBalance()); System.out.println("."); System.out.println("Expected Balance: 100"); Question P3.2 //Method to add public void addinterest(double rate) { double current_balance = this.getbalance(); this.deposit((current_balance/rate)); public class BankAccountTester { // TODO Auto-generated method stub BankAccount testaccount = new BankAccount(); testaccount.deposit(1000); testaccount.addinterest(10); System.out.println(testAccount.getBalance()); System.out.println("Expected: 1,100"); Question P3.3 public class SavingsAccount { // Constructors public SavingsAccount(double initialbalance, double initialinterest) { balance = initialbalance; interest = initialinterest;
public SavingsAccount() { balance = 0; public void deposit(double amount) { balance = balance + amount; public void withdraw(double amount) { balance = balance - amount; public double getbalance() { return balance; public void addinterest() { double current_balance = this.getbalance(); this.deposit((current_balance/interest)); private double balance; private double interest; public class SavingsAccountTester { SavingsAccount myaccount = new SavingsAccount(1000,10); myaccount.addinterest(); System.out.println(myAccount.getBalance()); System.out.println("Expected: 1100"); Question P3.6 public class Employee { // Constructors public Employee(string ename, double currentsalary) { name = ename; salary = currentsalary; // Methods public String getname() { return this.name;
public double getsalary() { return this.salary; public void raisesalary(double bypercent) { this.salary = salary + (salary/bypercent); private string name; private double salary; public class EmployeeTester { Employee servant = new Employee("Servant", 100); System.out.println("Employee name: " + servant.getname()); System.out.println("Current Salary: " + servant.getsalary()); servant.raisesalary(-10); System.out.println("Reduced Salary: " + servant.getsalary()); Question P3.7 public class Car { public Car(double fuelefficiency) { this.fuelefficiency = fuelefficiency; fuelintank = 0; public void addfuel(double amount) { fuelintank = fuelintank + amount; /** * Simulate a car drive * @param distance Distance driven by the car should be in distance units public void drive(double distance) { if (distance >= (fuelintank * fuelefficiency)) { System.out.println("Not enough fuel to drive so far."); else { fuelintank = fuelintank - (fuelefficiency/distance);
public double getfuelintank() { return fuelintank; /** * In volume units (liters, gallons...) private double fuelintank; /** * In distance unit per volume unit (miles per gallon, kilometers per liter...) private double fuelefficiency; Question P3.11 public class Bug { public Bug(int initialposition) { currentposition = initialposition; direction = 1; //positive means right direction public void turn() { direction = direction * (-1); public void move() { currentposition = currentposition + direction; public int getposition() { return currentposition; // direction will act as a unit vector private int direction; private int currentposition; public class BugTester {
/** * @param args Bug bugsy = new Bug(10); bugsy.move(); // now the position is 11 System.out.println("Actual: " + bugsy.getposition()); System.out.println("Expected: 11"); bugsy.turn(); bugsy.move(); // now the position is 10 System.out.println("Actual: " + bugsy.getposition()); System.out.println("Expected: 10"); Question P3.15 import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.ellipse2d; import javax.swing.jcomponent; public class BullsEyeComponent extends JComponent { public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double circle1 = new Ellipse2D.Double(50,50,100,100); Ellipse2D.Double circle2 = new Ellipse2D.Double(65,65,70,70); Ellipse2D.Double circle3 = new Ellipse2D.Double(80,80,40,40); Ellipse2D.Double circle4 = new Ellipse2D.Double(95,95,10,10); g2.setcolor(color.black); g2.fill(circle1); g2.draw(circle1); g2.setcolor(color.white); g2.fill(circle2); g2.draw(circle2); g2.setcolor(color.black); g2.fill(circle3); g2.draw(circle3); g2.setcolor(color.white); g2.fill(circle4); g2.draw(circle4);
import javax.swing.jframe; public class BullsEyeViewer { JFrame frame = new JFrame(); frame.setsize(400,400); frame.settitle("bullseye"); frame.setdefaultcloseoperation(jframe.exit_on_close); BullsEyeComponent target = new BullsEyeComponent(); frame.add(target); frame.setvisible(true); Question P3.16 import java.awt.graphics2d; import java.awt.rectangle; import java.awt.geom.line2d; public class House { public House() { public void draw(graphics2d g2) { Rectangle body = new Rectangle(10,50,100,100); Rectangle door = new Rectangle(25,80,30,70); Rectangle window = new Rectangle(65,85,30,30); Line2D.Double roof1 = new Line2D.Double(10,50,60,20); Line2D.Double roof2 = new Line2D.Double(60,20,110,50); g2.draw(body); g2.draw(door); g2.draw(window); g2.draw(roof1); g2.draw(roof2);
import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jcomponent; public class HouseComponent extends JComponent { public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D) g; House h1 = new House(); h1.draw(g2); import javax.swing.jframe; public class HouseViewer { /** * @param args JFrame frame = new JFrame(); frame.setsize(300, 300); frame.add(new HouseComponent()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); Question P3.17 import java.awt.dimension; import java.awt.graphics2d; import java.awt.point; import java.awt.rectangle; import java.awt.geom.line2d; public class House {
public House(Point pos, Dimension size) { housepos = pos; housesize = size; public House(int x1, int y1, int width, int height) { housepos = new Point(x1, y1); housesize = new Dimension(width, height); public void draw(graphics2d g2) { Rectangle body = new Rectangle(housePos.x,(housePos.y + 40),houseSize.width,houseSize.height); Rectangle door = new Rectangle(housePos.x + 15, housepos.y + 40 + housesize.height - 70,30,70); Rectangle window = new Rectangle(housePos.x + (housesize.width - 45), housepos.y + 75,30,30); Line2D.Double roof1 = new Line2D.Double(housePos.x, housepos.y + 40,housePos.x + (housesize.width/2), housepos.y + 10); Line2D.Double roof2 = new Line2D.Double(housePos.x + (housesize.width/2),housepos.y + 10,housePos.x + housesize.width,housepos.y + 40); g2.draw(body); g2.draw(door); g2.draw(window); g2.draw(roof1); g2.draw(roof2); private Point housepos; private Dimension housesize; import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jcomponent; public class HouseComponent extends JComponent { public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D) g; House h1 = new House(10, 10, 100, 100); House h2 = new House(200, 10, 150, 150); House h3 = new House(300, 200, 300, 300);
h1.draw(g2); h2.draw(g2); h3.draw(g2); import javax.swing.jframe; public class HouseViewer { /** * @param args JFrame frame = new JFrame(); frame.setsize(800, 600); frame.add(new HouseComponent()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true);