Sample Solution Assignment 5. Question R3.1

Similar documents
2/9/2012. Chapter Three: Implementing Classes. Chapter Goals

9/2/2011. Chapter Goals

ICOM 4015: Advanced Programming

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week.

Chapter 2 Using Objects. Copyright 2014 by John Wiley & Sons. All rights reserved. 1

Submit your solution to the Desire2Learn drop box. You may submit as many versions as you like the last one will be graded.

SWC test question #01. Using objects part I

Programming Graphics (P1 2006/2007)

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

8/23/2011. Chapter Goals

2/9/2012. Chapter Two: Using Objects. Chapter Goals

Chapter 2 An Introduction to Objects and Classes. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Chapter 2 Using Objects

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

CP122 Computer Science I. Chapter 2: Using Objects

Implementing Classes (P1 2006/2007)

ICOM 4015 Advanced Programming Laboratory. Chapter 4 Introduction to Object Oriented Programming

Sample Solution A10 R6.2 R6.3 R6.5. for (i = 10; i >= 0; i++)... never ends. for (i = -10; i <= 10; i = i + 3)... 6 times

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Chapter 2 Using Objects. Types. Number Literals. A type defines a set of values and the operations that can be carried out on the values Examples:

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

P 6.3) import java.util.scanner;

Implementing Classes

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Dr. Hikmat A. M. AbdelJaber

Inheritance (P1 2006/2007)

Intro to Computer Science 2. Inheritance

Section Basic graphics

6.092 Introduction to Software Engineering in Java January (IAP) 2009

Final Examination Semester 2 / Year 2011

CHAPTER 10 INHERITANCE

Final Examination Semester 2 / Year 2010

Final Examination Semester 2 / Year 2012

AplusBug dude = new AplusBug(); A+ Computer Science -

Chapter Nine: Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2008 by John Wiley & Sons. All rights reserved.

3/7/2012. Chapter Nine: Interfaces and Polymorphism. Chapter Goals

10/27/2011. Chapter Goals

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

CS Week 13. Jim Williams, PhD

CS 1302 Chapter 9 (Review) Object & Classes

STUDENT LESSON A5 Designing and Using Classes

Lecture 3: Java Graphics & Events

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2)

3/7/2012. Chapter Ten: Inheritance. Chapter Goals

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Lecture 5: Java Graphics

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

The Scanner class reads data entered by the user. Methods: COSC Dr. Ramon Lawrence. Page 3. COSC Dr. Ramon Lawrence A) A = 6, B = 3

Basic Object-Oriented Concepts. 5-Oct-17

Practice problem on defining and using Class types. Part 4.

CSE wi Final Exam 3/12/18 Sample Solution

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Handout 9 OO Inheritance.

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

ICOM 4015 Advanced Programming Laboratory. Chapter 3 Introduction to Graphical Applications in Java using Swing

Handout 8 Classes and Objects Continued: Static Variables and Constants.

Imperative Languages!

Introduction to Inheritance

CS 11 java track: lecture 3

Lecture 06: Classes and Objects

Practice Midterm 1. Problem Points Score TOTAL 50

CSE wi Final Exam 3/12/18. Name UW ID#

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Encapsulation. Mason Vail Boise State University Computer Science

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Running the program (the model) simulates what would

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

int b = 2; for (int a = 0; a < 10 b < 20; a++) { a = a + b; b = b + a; }

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example:

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects

CSSE 220. Intro to Java Graphics. Check out IntroToJavaGraphics and BiggestFan projects from SVN

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Introduction to Classes and Objects

Practice Midterm Examination

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

Lesson 35..Inheritance

Chapter 9 Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

10 Object oriented programming

Building Java Programs

Chapter Goals. T To understand the concept of regression testing. Chapter 6 Arrays and Array Lists. Arrays Array: Sequence of values of the same type

Inheritance and Subclasses

Java Inheritance. Classes implement the concept of ADT:

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Software with a Guarantee

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Data Structures (INE2011)

Practice Midterm 1 Answer Key

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

9Introducing. Concurrency with Threads

H212 Introduction to Software Systems Honors

9/1/2015. Chapter 2 Using Objects. Objects and Classes. Using Objects. Using Objects. Using Objects. Classes. Ch. 1/2 Lab Time

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

Transcription:

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);