Software and Programming 1

Similar documents
Software and Programming 1

Software and Programming 1

Object-Oriented Programming in Java

More on Objects and Classes

Software and Programming 1

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

Software and Programming 1

Fundamentos de programação

Software and Programming 1

Software and Programming 1

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Instance Method Development Demo

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Software and Programming 1

CS1004: Intro to CS in Java, Spring 2005

Software and Programming 1

Chapter 6 Lab Classes and Objects

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

MIDTERM REVIEW. midterminformation.htm

Readings for This Lecture

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

Classes. Classes as Code Libraries. Classes as Data Structures

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

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Encapsulation in Java

Programming II (CS300)

CS1083 Week 2: Arrays, ArrayList

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Implementing classes

Programming II (CS300)

CMSC 341 Lecture 7 Lists

CSCI 161 Introduction to Computer Science

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Chapter 6 Lab Classes and Objects

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Principles of Object Oriented Programming. Lecture 4

G52CPP C++ Programming Lecture 9

Lab Exercise 1. Objectives: Part 1. Introduction

CAT.woa/wa/assignments/eclipse

Big Java. Fifth Edition. Chapter 3 Fundamental Data Types. Cay Horstmann

Practice Problems: Instance methods

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

Create a Java project named week10

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

CS 201, Fall 2016 Sep 28th Exam 1

Introduction to Java Unit 1. Using BlueJ to Write Programs

Midterms Save the Dates!

Department of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book

Getter and Setter Methods

What will this print?

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Chapter 1 Lab Algorithms, Errors, and Testing

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

UNDERSTANDING CLASS DEFINITIONS CITS1001

Notes on Chapter Three

Overview of Eclipse Lectures. Module Road Map

Introduction to Software Development (ISD) David Weston and Igor Razgon

Recitation 3 Class and Objects

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

coe318 Lab 1 Introduction to Netbeans and Java

Ch 7 Designing Java Classes & Class structure

Programming II (CS300)

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

Objects as a programming concept

Claremont McKenna College Computer Science

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

Recommended Group Brainstorm (NO computers during this time)

Chapter 4. Defining Classes I

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Slide 1 CS 170 Java Programming 1

Programming with Java

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

CS 302 Week 9. Jim Williams

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Chapter 5: Classes and Objects in Depth. Introduction to methods

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

Midterms Save the Dates!

AP Computer Science Unit 1. Writing Programs Using BlueJ

Class Relationships. Lecture 18. Based on Slides of Dr. Norazah Yusof

ECE 122. Engineering Problem Solving with Java

Programming II (CS300)

This exam is open book. Each question is worth 3 points.

COMP-202: Foundations of Programming. Lecture 9: Classes and Objects Sandeep Manjanna, Summer 2015

CSIS 10A Assignment 9 Solutions

Building custom components IAT351

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Transcription:

Software and Programming 1 Lab 7: Construction of a Simulated Cash Register and a Student Class 28 February 2019 SP1-Lab7-2018-19.ppt Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1

Coursework Plagiarism Plagiarism is claiming the work of others as your own. Plagiarism is a serious offence and can result in your exclusion from all colleges of the University of London. The College policy on Plagiarism can be viewed here: http://www.bbk.ac.uk/mybirkbeck/services/rules/assessment_offences.pdf Copying work, either from each other or the internet, to complete the lab exercises amounts to plagiarism. When your lab exercises are assessed, you will be asked to explain exactly how your programs work and your coding choices before it is accepted. 2

Lab 7 Objectives Implement classes with instance variables, mutator methods, accessor methods and a constructor. Implement test classes in which objects can be created and manipulated. Make changes to classes, without changing the public interface, to reinforce the concept of encapsulation. 3

A quick reminder An object holds instance variables that are accessed by its methods All instances variables should be private Most methods should be public A mutator method changes the object on which it operates An accessor method just queries the object for some information without changing it A constructor initialises the instance variables of an object The name of the constructor is the name of the class (the constructor has no return type, not even void) 4

Exercise 1: CashRegister Implement the class CashRegister that simulates a cash register. The cash register should have the following functionality: 1) Add an item s price 2) Clear the cash register for a new sale 3) Get the (current) number of items in a sale 4) Get the (current) total price of the sale 5

CashRegister Class (from SP1 lecture notes, lecture 6, 21/2/2019) /** A simulated cash register */ public class CashRegister /* private data you will need to declare instance variables to store the values for the cash register s item count and total price */ /* methods (public interface) */ public void additem(double price) /* This mutator (setter) method will be called each time there is a new item added, so you will need to increment the item count by 1, and add the current price to the total price (current price is the value stored in this method s only parameter, price) */ 6

CashRegister class (2) public void clear() /*This mutator method should reset the local variables that store the values for the item count & total price*/ public double gettotal() /* This is a standard accessor (getter) method. It should return the current value stored in the variable you declared for the total price */ public int getcount() /* This accessor method should return the value stored for the item count */ 7

CashRegister class (3) /* constructor */ public CashRegister() /* Within the constructor you need to initialise the instance variables for the total price and item count. The variables were declared earlier so should not include the data type */ // end of class CashRegister Remember: The constructor shares the same name as the class The constructor does not have a return type 8

Exercise 1 (cont): testing CashRegister class The CashRegister on its own will not create objects, therefore, implement a test class to create instances of CashRegister. The CashRegisterTest class from last week s lecture notes (shown on the next slide) can be used to test the CashRegister class as is, but must be modified to include the following in the lab today (see slide 11): The creation of 3 instances of CashRegister, each of which adds at least 2 item prices The item prices for each instance should be read by a Scanner object from the keyboard 9

Testing CashRegister Class (from SP1 lecture notes, lecture 6, 21/2/2019, slide 21) public class CashRegisterTest public static void main(string[] args) // create an instance (register 1) of the CashRegister Object CashRegister reg1 = new CashRegister(); // add items to register 1 reg1.additem(2.95); reg1.additem(1.99); // use the CashRegister instance method getcount() System.out.println(reg1.getCount()); System.out.println((reg1.getCount() == 2)? "OK" : "FAIL"); // use the CashRegister instance method gettotal() System.out.printf("%.2f\n", reg1.gettotal()); System.out.println((reg1.getTotal() ==4.94)? "OK": "FAIL"); 10

Exercise 1: CashRegisterTest class import java.util.scanner; public class CashRegisterTest public static void main(string[] args) /* write code to create Scanner object input*/ CashRegister reg1 = new CashRegister(); // new instance /* write code: use input.nextdouble() to get an items value and store in a new variable price of type double */ reg1.additem(price); // CashRegister instance method /* repeat code to get second item price and add item to object */ System.out.println("Total cost is " + reg1.gettotal()); System.out.println("The number of items bought is " + reg1.getcount()); 11

Exercise 1: CashRegisterTest class (2) /* write additional code to: */ (i) create the remaining two instances of CashRegister (ii) add a number of item prices in each of them // end of class CashRegisterTest 12

Exercise 2: Student Class 1. Implement a class Student. The Student constructor should have two parameters of type String that will receive the values for the student s first and last name. The following instance methods will be required: getname() this accessor method should return a String value, the first and last name concatenated addquizscore(int score) this mutator method should update the instance variable with the value in score gettotalscore() accessor returns the total score getaveragescore() accessor returns the average of all scores as accumulated by the addquizscore method 2. Implement a suitable test class for Student. 13

Structure of Student Class /** Student class */ public class Student // declare private data /* methods (public interface) */ public void addquizscore(int score) // write code to add score to totalscore // increment scorecount public String getname() /* return name */ 14

Structure of Student Class (2) public int gettotalscore() /* return totalscore */ public double getaveragescore() /* calculate average score and return the result */ /* constructor */ public Student(String firstname, String lastname) name = firstname + " " + lastname; /* next initialise the instance variables: total quiz score and score count */ // end of class Student 15

Test the Student Class public class StudentTest public static void main(string[] args) Student student1 = new Student("Sam", "Wise"); /* */ write more code to test the methods: getname(), addquizscore(int score), gettotalscore() and getaveragescore() 16

Marked Exercise 5: CashRegisterFixedSize Duplicate the entire CashRegister project in BlueJ. Then re-implement the class as CashRegisterFixedSize with additional functionality as set out on slide 18. Do not remove any of the code created in exercise 1 within the public interface to the class, only add code to test the additional functionality. The re-implementation, CashRegisterFixedSize, along with the CashRegisterTest class, are the fifth exercise and you are required to complete them and show the working program by the 14 th March. Failure to do so by this date will mean you cannot be entered for the second inclass test. 17

Marked Exercise 5: CashRegisterFixedSize (2) Additional functionality required for CashRegisterFixedSize: Limit items in each instance Allow the last item entered to be deleted The CashRegister class should be modified to limit the number of items that can be added to an instance of the CashRegister. The limit should be declared as a constant in such a way that all instances of the CashRegisterFixedSize have the same limit. The CashRegisterFixedSize class should also have a new instance method, undo(), that removes the last item entered into an instance. 18

Hints: Marked Exercise 5: CashRegisterFixedSize (3) To implement the additional functionality required, it is recommended that you use an array to store each item s price within the CashRegisterFixedSize class. You should adapt the count variable so it can be used to set the index of the array this will be used store each entry, calculate the total price and when removing the last entered item. Remember that array indexes begin at 0! The gettotal() method will need to be modified to calculate the total of all values stored in the array. A loop construct is ideal for this. When implementing the undo()method, the value stored in the variable count should be considered. 19