Sample Solution Assignment 5. Question R3.1

Size: px
Start display at page:

Download "Sample Solution Assignment 5. Question R3.1"

Transcription

1 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.

2 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.

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

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

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

6 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 {

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

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

9 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 { /** 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 {

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

11 h1.draw(g2); h2.draw(g2); h3.draw(g2); import javax.swing.jframe; public class HouseViewer { /** args JFrame frame = new JFrame(); frame.setsize(800, 600); frame.add(new HouseComponent()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true);

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

2/9/2012. Chapter Three: Implementing Classes. Chapter Goals Chapter Three: Implementing Classes Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand the purpose and use of constructors To

More information

9/2/2011. Chapter Goals

9/2/2011. Chapter Goals Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand the purpose and use of constructors To understand how to access instance fields

More information

ICOM 4015: Advanced Programming

ICOM 4015: Advanced Programming ICOM 4015: Advanced Programming Lecture 3 Reading: Chapter Three: Implementing Classes Copyright 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes Chapter Goals To become

More information

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

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week. 1 CPS109 Lab 7 Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week. Objectives: 1. To practice using one- and two-dimensional arrays 2. To practice using partially

More information

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

Chapter 2 Using Objects. Copyright 2014 by John Wiley & Sons. All rights reserved. 1 Chapter 2 Using Objects Copyright 2014 by John Wiley & Sons. All rights reserved. 1 Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To

More information

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

Submit your solution to the Desire2Learn drop box. You may submit as many versions as you like the last one will be graded. CS46A Exam 2 Exam Rules You may use any books, notes, files, web sites of your choice, except as noted below. It is a good idea to compile and run your programs if you have the time to do it. You may NOT

More information

SWC test question #01. Using objects part I

SWC test question #01. Using objects part I SWC test question #01 Using objects part I Using objects part I Give a presentation of the concept objects, and how you use objects You could describe Types Variables Variable definitions The assignment

More information

Programming Graphics (P1 2006/2007)

Programming Graphics (P1 2006/2007) Programming Graphics (P1 2006/2007) Fernando Brito e Abreu (fba@di.fct.unl.pt) Universidade Nova de Lisboa (http://www.unl.pt) QUASAR Research Group (http://ctp.di.fct.unl.pt/quasar) Chapter Goals To be

More information

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

Agenda: Notes on Chapter 3. Create a class with constructors and methods. Bell Work 9/19/16: How would you call the default constructor for a class called BankAccount? Agenda: Notes on Chapter 3. Create a class with constructors and methods. Objectives: To become familiar with

More information

8/23/2011. Chapter Goals

8/23/2011. Chapter Goals Chapter Goals Chapter Two: Using Objects To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters and return values To implement test

More information

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

2/9/2012. Chapter Two: Using Objects. Chapter Goals Chapter Two: Using Objects Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters and return values To implement test

More information

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

Chapter 2 An Introduction to Objects and Classes. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved. Chapter 2 An Introduction to Objects and Classes Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters and return

More information

Chapter 2 Using Objects

Chapter 2 Using Objects Chapter 2 Using Objects Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters and return values To be able to browse

More information

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

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance; public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance; public double deposit(double amount) { public double withdraw(double amount) {

More information

CP122 Computer Science I. Chapter 2: Using Objects

CP122 Computer Science I. Chapter 2: Using Objects CP122 Computer Science I Chapter 2: Using Objects Tech News! Cyber Monday: $3.3B https://youtu.be/r4rfcay9fiq Tech News! Cyber Monday: $3.3B https://youtu.be/r4rfcay9fiq Small drone warfare https://cdn1.tnwcdn.com/wpcontent/blogs.dir/1/files/2016/11/ezgif.comoptimize-1-1.mp4

More information

Implementing Classes (P1 2006/2007)

Implementing Classes (P1 2006/2007) Implementing Classes (P1 2006/2007) Fernando Brito e Abreu (fba@di.fct.unl.pt) Universidade Nova de Lisboa (http://www.unl.pt) QUASAR Research Group (http://ctp.di.fct.unl.pt/quasar) Chapter Goals To become

More information

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

ICOM 4015 Advanced Programming Laboratory. Chapter 4 Introduction to Object Oriented Programming ICOM 4015 Advanced Programming Laboratory Chapter 4 Introduction to Object Oriented Programming University of Puerto Rico Electrical and Computer Engineering Department by Juan E. Surís 1 Introduction

More information

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

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 Sample Solution A10 R6.2 0000000000 0123456789 0246802468 0369258147 0482604826 0505050505 0628406284 0741852963 0864208642 0987654321 R6.3 for (i = 10; i >= 0; i++)... never ends for (i = -10; i

More information

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

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } } 9.12) public interface Filter boolean accept(object x); Describes any class whose objects can measure other objects. public interface Measurer double measure(object anobject); This program tests the use

More information

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

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved. Chapter 10 Inheritance Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn about protected and package

More information

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:

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: Chapter 2 Using Objects Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters and return values To be able to browse

More information

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

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners GUI (Graphic User Interface) Programming Part 2 (Chapter 8) Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons, text fields, and the

More information

P 6.3) import java.util.scanner;

P 6.3) import java.util.scanner; P 6.3) import java.util.scanner; public class CurrencyConverter public static void main (String[] args) Scanner in = new Scanner(System.in); System.out.print("How many euros is one dollar: "); double rate

More information

Implementing Classes

Implementing Classes Implementing Classes Advanced Programming ICOM 4015 Lecture 3 Reading: Java Concepts Chapter 3 Fall 2006 Slides adapted from Java Concepts companion slides 1 Chapter Goals To become familiar with the process

More information

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

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13 Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13 Fall 2006 Adapted from Java Concepts Companion Slides 1 Chapter Goals To learn about inheritance To understand how

More information

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

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn about protected and package access control To

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber Portion of the Java class hierarchy that include basic graphics classes and Java 2D API classes and interfaces. java.lang.object Java.awt.Color Java.awt.Component Java.awt.Container

More information

Inheritance (P1 2006/2007)

Inheritance (P1 2006/2007) Inheritance (P1 2006/2007) Fernando Brito e Abreu (fba@di.fct.unl.pt) Universidade Nova de Lisboa (http://www.unl.pt) QUASAR Research Group (http://ctp.di.fct.unl.pt/quasar) Chapter Goals To learn about

More information

Intro to Computer Science 2. Inheritance

Intro to Computer Science 2. Inheritance Intro to Computer Science 2 Inheritance Admin Questions? Quizzes Midterm Exam Announcement Inheritance Inheritance Specializing a class Inheritance Just as In science we have inheritance and specialization

More information

Section Basic graphics

Section Basic graphics Chapter 16 - GUI Section 16.1 - Basic graphics Java supports a set of objects for developing graphical applications. A graphical application is a program that displays drawings and other graphical objects.

More information

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

6.092 Introduction to Software Engineering in Java January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.092 Introduction to Software Engineering in Java January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

More information

Final Examination Semester 2 / Year 2011

Final Examination Semester 2 / Year 2011 Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2011 COURSE COURSE CODE TIME DEPARTMENT LECTURER : JAVA PROGRAMMING : PROG1114 : 2 1/2 HOURS : COMPUTER SCIENCE : LIM PEI GEOK Student

More information

CHAPTER 10 INHERITANCE

CHAPTER 10 INHERITANCE CHAPTER 10 INHERITANCE Inheritance Inheritance: extend classes by adding or redefining methods, and adding instance fields Example: Savings account = bank account with interest class SavingsAccount extends

More information

Final Examination Semester 2 / Year 2010

Final Examination Semester 2 / Year 2010 Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2010 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student

More information

Final Examination Semester 2 / Year 2012

Final Examination Semester 2 / Year 2012 Final Examination Semester 2 / Year 2012 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student s ID : Batch No. : Notes to candidates:

More information

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

AplusBug dude = new AplusBug(); A+ Computer Science - AplusBug dude = new AplusBug(); AplusBug dude = new AplusBug(); dude 0x234 AplusBug 0x234 dude is a reference variable that refers to an AplusBug object. A method is a storage location for related program

More information

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

Chapter Nine: Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand the concept of polymorphism To appreciate how

More information

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

3/7/2012. Chapter Nine: Interfaces and Polymorphism. Chapter Goals Chapter Nine: Interfaces and Polymorphism Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand the concept of polymorphism To appreciate how

More information

10/27/2011. Chapter Goals

10/27/2011. Chapter Goals Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand the concept of polymorphism To appreciate how interfaces can be used to decouple classes

More information

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

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM Objectives The objectives of this assignment are: to refresh your Java programming to become familiar with

More information

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET SOLUTION SHEET ENGR 2710U: Object Oriented Programming & Design Midterm Exam October 19, 2012, Duration: 80 Minutes (9 Pages, 14 questions, 100 Marks) Instructor: Dr. Kamran Sartipi Name: Student Number:

More information

CS Week 13. Jim Williams, PhD

CS Week 13. Jim Williams, PhD CS 200 - Week 13 Jim Williams, PhD This Week 1. Team Lab: Instantiable Class 2. BP2 Strategy 3. Lecture: Classes as templates BP2 Strategy 1. M1: 2 of 3 milestone tests didn't require reading a file. 2.

More information

CS 1302 Chapter 9 (Review) Object & Classes

CS 1302 Chapter 9 (Review) Object & Classes CS 1302 Chapter 9 (Review) Object & Classes Reference Sections 9.2-9.5, 9.7-9.14 9.2 Defining Classes for Objects 1. A class is a blueprint (or template) for creating objects. A class defines the state

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

Lecture 3: Java Graphics & Events

Lecture 3: Java Graphics & Events Lecture 3: Java Graphics & Events CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki Text Input Scanner class Constructor: myscanner = new Scanner(System.in); can use file instead of System.in new Scanner(new

More information

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

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes CSEN401 Computer Programming Lab Topics: Introduction and Motivation Recap: Objects and Classes Prof. Dr. Slim Abdennadher 16.2.2014 c S. Abdennadher 1 Course Structure Lectures Presentation of topics

More information

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

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2) WE1 W o r k e d E x a m p l e 2 2.1 Programming a Bank Database In this Worked Example, we will develop a complete database program. We will reimplement the ATM simulation of Chapter 12, storing the customer

More information

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

3/7/2012. Chapter Ten: Inheritance. Chapter Goals Chapter Ten: Inheritance Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn about protected and

More information

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

More information

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

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables Outline CSE 142 Class Implementation in Java Implementing classes in Java Instance variables properties Value-returning methods for queries Void methods for commands Return statement Assignment statement

More information

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

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 COSC 123 Computer Creativity Course Review Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Reading Data from the User The Scanner Class The Scanner class reads data entered

More information

Basic Object-Oriented Concepts. 5-Oct-17

Basic Object-Oriented Concepts. 5-Oct-17 Basic Object-Oriented Concepts 5-Oct-17 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions, which could manipulate any data An object contains

More information

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

Practice problem on defining and using Class types. Part 4. CS180 Programming Fundamentals Practice problem on defining and using Class types. Part 4. Implementing object associations: Applications typically consist of collections of objects of different related

More information

CSE wi Final Exam 3/12/18 Sample Solution

CSE wi Final Exam 3/12/18 Sample Solution Question 1. (8 points, 2 each) Equality. Recall that there are several different notions of object equality that we ve encountered this quarter. In particular, we have the following three: Reference equality:

More information

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

Introduction to Computer Science Unit 4B. Programs: Classes and Objects Introduction to Computer Science Unit 4B. Programs: Classes and Objects This section must be updated to work with repl.it 1. Copy the Box class and compile it. But you won t be able to run it because it

More information

Handout 9 OO Inheritance.

Handout 9 OO Inheritance. Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 1 of 11 Handout 9 OO Inheritance. All classes in Java form a hierarchy. The top of the hierarchy is class Object Example: classicalarchives.com

More information

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

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance

More information

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

ICOM 4015 Advanced Programming Laboratory. Chapter 3 Introduction to Graphical Applications in Java using Swing ICOM 4015 Advanced Programming Laboratory Chapter 3 Introduction to Graphical Applications in Java using Swing University of Puerto Rico Electrical and Computer Engineering Department by Juan E. Surís

More information

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

Handout 8 Classes and Objects Continued: Static Variables and Constants. Handout 8 CS603 Object-Oriented Programming Fall 16 Page 1 of 8 Handout 8 Classes and Objects Continued: Static Variables and Constants. 1. Static variable Declared with keyword static One per class (instead

More information

Imperative Languages!

Imperative Languages! Imperative Languages! Java is an imperative object-oriented language. What is the difference in the organisation of a program in a procedural and an objectoriented language? 30 class BankAccount { private

More information

Introduction to Inheritance

Introduction to Inheritance Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits

More information

CS 11 java track: lecture 3

CS 11 java track: lecture 3 CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces

More information

Lecture 06: Classes and Objects

Lecture 06: Classes and Objects Accelerating Information Technology Innovation http://aiti.mit.edu Lecture 06: Classes and Objects AITI Nigeria Summer 2012 University of Lagos. What do we know so far? Primitives: int, float, double,

More information

Practice Midterm 1. Problem Points Score TOTAL 50

Practice Midterm 1. Problem Points Score TOTAL 50 CS 120 Software Design I Spring 2019 Practice Midterm 1 University of Wisconsin - La Crosse February 25 NAME: Do not turn the page until instructed to do so. This booklet contains 10 pages including the

More information

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

CSE wi Final Exam 3/12/18. Name UW ID# Name UW ID# There are 13 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

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

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms. Java GUI Windows Events Drawing 1 Java GUI Toolkits Toolkit AWT Description Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

More information

Encapsulation. Mason Vail Boise State University Computer Science

Encapsulation. Mason Vail Boise State University Computer Science Encapsulation Mason Vail Boise State University Computer Science Pillars of Object-Oriented Programming Encapsulation Inheritance Polymorphism Abstraction (sometimes) Object Identity Data (variables) make

More information

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

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical

More information

Running the program (the model) simulates what would

Running the program (the model) simulates what would CSE 143 Programming as Modeling Reading: Ch. 1-6 Building Virtual Worlds Much of programming can be viewed as building a model of a real or imaginary world in the computer a banking program models real

More information

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

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I Electrical and Computer Engineering Object-Oriented Topic : Graphics GUI Part I Maj Joel Young Joel.Young@afit.edu 15-Sep-03 Maj Joel Young A Brief History Lesson AWT Abstract Window Toolkit Implemented

More information

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

int b = 2; for (int a = 0; a < 10 b < 20; a++) { a = a + b; b = b + a; } 1. What does mystery(9870) return? public int mystery(int n) { String m = ""; while (n > 0) { m = m + n%10; n = n/10; return m; (a) "9870" (b) "987" (c) "0" (d) "789" (e) "7890" 2. What are the values

More information

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

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example: Packages Package A package is a set of related classes Syntax to put a class into a package: package ; public class { } Two rules: q q A package declaration must always come

More information

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved. 1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 3 Nothing can have value without being an object of utility. Karl Marx Your public servants serve you right. Adlai E. Stevenson Knowing how to answer one who speaks, To reply to one who sends a message.

More information

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

CSSE 220. Intro to Java Graphics. Check out IntroToJavaGraphics and BiggestFan projects from SVN CSSE 220 Intro to Java Graphics Check out IntroToJavaGraphics and BiggestFan projects from SVN Announcement Exam 1 Starts Wednesday the 23 rd We re splitting the exam into written and programming and doing

More information

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

1. Which of the following is the correct expression of character 4? a. 4 b. 4 c. '\0004' d. '4' Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 1 2 Introduction to Classes and Objects You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Practice Midterm Examination

Practice Midterm Examination Steve Cooper Handout #28 CS106A May 1, 2013 Practice Midterm Examination Midterm Time: Tuesday, May 7, 7:00P.M. 9:00P.M. Portions of this handout by Eric Roberts and Patrick Young This handout is intended

More information

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

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t; 1 AppBisect > PrBisect > class Punct public class Punct { double x,y; public Punct(double x, double y) { this.x = x; this.y = y; public void setx(double x) { this.x = x; public double getx() { return x;

More information

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

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]; 1. The source code for a class (a) must explicitly define exactly one constructor. (b) can explicitly define zero, one or more constructors. (c) must explicitly define at least one constructor. (d) can

More information

Lesson 35..Inheritance

Lesson 35..Inheritance Lesson 35..Inheritance 35-1 Within a new project we will create three classes BankAccount, SavingsAccount, and Tester. First, the BankAccount class: public class BankAccount public BankAccount(double amt)

More information

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

Chapter 9 Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved. Chapter 9 Interfaces and Polymorphism Chapter Goals To be able to declare and use interface types To understand the concept of polymorphism To appreciate how interfaces can be used to decouple classes

More information

10 Object oriented programming

10 Object oriented programming 250 Java Programming for A-level Computer Science 10 Object oriented programming Java is an object oriented programming language. The use of objects has a number of important advantages, particularly when

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 15 Lecture 15-1: Implementing ArrayIntList reading: 15.1-15.3 Recall: classes and objects class: A program entity that represents: A complete program or module, or A template

More information

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

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 Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized for loop To study common array algorithms To learn how to use two-dimensional

More information

Inheritance and Subclasses

Inheritance and Subclasses Software and Programming I Inheritance and Subclasses Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Packages Inheritance Polymorphism Sections 9.1 9.4 slides are available at www.dcs.bbk.ac.uk/

More information

Java Inheritance. Classes implement the concept of ADT:

Java Inheritance. Classes implement the concept of ADT: Java Inheritance Rui Moreira Class ADT (Abstract Data Type) Classes implement the concept of ADT: n Provide a coherent representation for the declaration of structured data types and also the code for

More information

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller Inheritance & Abstract Classes 15-121 Margaret Reid-Miller Today Today: Finish circular queues Exercise: Reverse queue values Inheritance Abstract Classes Clone 15-121 (Reid-Miller) 2 Object Oriented Programming

More information

Software with a Guarantee

Software with a Guarantee 1. Unit Testing Software with a Guarantee We ve settled in Objectville for quite some years now. Time for a new and nicer car. But we wouldn t dare buying one without taking it for a test-drive first.

More information

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

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb HAND IN Answers recorded on Examination paper This examination is THREE HOURS

More information

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

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam ( hnam@hanyang.ac.kr ) Lecture 1 1 Data Structures Data? Songs in a smartphone Photos in a camera Files

More information

Practice Midterm 1 Answer Key

Practice Midterm 1 Answer Key CS 120 Software Design I Fall 2018 Practice Midterm 1 Answer Key University of Wisconsin - La Crosse Due Date: October 5 NAME: Do not turn the page until instructed to do so. This booklet contains 10 pages

More information

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

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos. Accelerating Information Technology Innovation http://aiti.mit.edu Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos. Data Field Encapsulation Sometimes we want

More information

9Introducing. Concurrency with Threads

9Introducing. Concurrency with Threads 9Introducing Concurrency with Threads 118 Chapter 9: Introducing Concurrency with Threads Self-review Questions 9.1 What is a thread? A thread is a single path of execution through a program that can be

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14 Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9,

More information

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

9/1/2015. Chapter 2 Using Objects. Objects and Classes. Using Objects. Using Objects. Using Objects. Classes. Ch. 1/2 Lab Time Chapter 2 Using Objects The Plan For Today Chapter 1 Quiz Results Chapter 2 2.1: Objects and Classes 2.2: Variables 2.3: Calling Methods 2.4: Constructing Objects Ch. 1/2 Lab Time Ch. 1 Assignment Due

More information

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

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2 1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring 2010 - Quiz 2 Name: MIT Email: TA: Section: You have 80 minutes to complete this exam. For coding questions, you do not need

More information