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

Fundamentos de programação

Software and Programming 1

Software and Programming 1

Instance Method Development Demo

Software and Programming 1

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

Software and Programming 1

Software and Programming 1

CS1004: Intro to CS in Java, Spring 2005

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

Introduction to Java Unit 1. Using BlueJ to Write Programs

MIDTERM REVIEW. midterminformation.htm

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

Programming II (CS300)

Programming II (CS300)

Programming with Java

Programming II (CS300)

CS1083 Week 2: Arrays, ArrayList

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

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

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

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

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

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

Readings for This Lecture

Encapsulation in Java

Classes. Classes as Code Libraries. Classes as Data Structures

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

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

Practice Problems: Instance methods

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

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

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

Chapter 6 Lab Classes and Objects

CSCI 161 Introduction to Computer Science

Programming II (CS300)

CS 302 Week 9. Jim Williams

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

Lab Exercise 1. Objectives: Part 1. Introduction

Notes on Chapter Three

Principles of Object Oriented Programming. Lecture 4

Overview of Eclipse Lectures. Module Road Map

CAT.woa/wa/assignments/eclipse

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

Data Structure. Lecture#2: Data Structures and Algorithms. U Kang Seoul National University. U Kang (2016) 1

A very simple program. Week 2: variables & expressions. Declaring variables. Assignments: examples. Initialising variables. Assignments: pattern

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

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

Midterms Save the Dates!

Getter and Setter Methods

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

What will this print?

Software Practice 1 Basic Grammar

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

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

Implementing classes

AP Computer Science Unit 1. Programs

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

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

Supplementary Test 1

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

Chapter 1 Lab Algorithms, Errors, and Testing

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Chapter 6 Lab Classes and Objects

UNDERSTANDING CLASS DEFINITIONS CITS1001

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

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

Recitation 3 Class and Objects

Create a Java project named week10

Object Oriented Programming. Java-Lecture 1

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

Midterms Save the Dates!

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

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

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

Week 7 - More Java! this stands for the calling object:

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

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

Ch 7 Designing Java Classes & Class structure

Topic 4 Expressions and variables

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

CS 201, Fall 2016 Sep 28th Exam 1

Welcome to the Primitives and Expressions Lab!

1.00 Lecture 4. Promotion

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Over and Over Again GEEN163

Objects as a programming concept

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Robots. Byron Weber Becker. chapter 6

Elementary Programming

Transcription:

Software and Programming 1 Lab 7: Construction of a Simulated Cash Register and a Student Class 22 February 2018 SP1-Lab7-2018.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 15-2-2018 lecture notes) /** 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 (included in slide 11) should be modified to include the following: 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 15-2-2018 Slide 21) public class CashRegisterTest public static void main(string[] args) CashRegister reg1 = new CashRegister(); reg1.additem(2.95); reg1.additem(1.99); System.out.println(reg1.getCount()); System.out.println((reg1.getCount() == 2)? "OK" : "FAIL"); 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 price */ 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() + " and 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: CashRegister re-implementation Duplicate the entire CashRegister project in BlueJ. Then reimplement the CashRegister class so that it keeps track of the total price as an integer. For example, within the CashRegister class, instead of storing a floating-point 17.29, store the integer 1729. Do not change the public interface to the class it must still use the exact same code as exercise 1. The re-implementation of CashRegister, along with the CashRegisterTest class, are the fifth exercise and you are required to complete them and show the working program by the 8 th March. Failure to do so by this date will mean you cannot be entered for the second in-class test. 17

Marked Exercise 5: CashRegister re-implementation (2) Further explanation of the exercise: Calculating using floating point numbers (such as those stored in float & double data types) do not give exact results! For example in the following code: double poundsval = 5.15, poundsval2 = 1.25; System.out.println("the result of 5.15 minus 1.25 using double is: " + (poundsval - poundsval2)); The following is output to the terminal: the result of 5.15 minus 1.25 using double is: 3.9000000000000004 18

Marked Exercise 5: CashRegister re-implementation (3) In order to accurately calculate currency, one option is to convert the values to integers. Using the previous example, the first value, 5.15 can first be converted into the equivalent in whole pence 515p. The second value from 1.25 to 125p. We then calculate the difference of these integer values: int penceval = 515, penceval2 = 125; System.out.println("the result of 515 minus 125 using int is: " + (penceval - penceval2)); The following is output to the terminal: the result of 515 minus 125 using int is: 390 Finally, in order to view the result in standard UK currency format, we convert the result back to pounds 19