Software and Programming 1

Similar documents
Software and Programming 1

Software and Programming 1

Software and Programming 1

Software and Programming 1

Software and Programming 1

Software and Programming 1

Software and Programming 1

Software and Programming 1

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Chapter 6 Lab Classes and Objects

Object-Oriented Programming in Java

Chapter 6 Lab Classes and Objects

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

Fundamentals of Programming Data Types & Methods

Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002

Introduction to Programming Using Java (98-388)

STUDENT LESSON A5 Designing and Using Classes

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Java and OOP. Part 2 Classes and objects

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

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

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Java Identifiers, Data Types & Variables

Imperative Languages!

Creating Java Programs with Greenfoot

Introduction to Computer Science I

Guessing Game with Objects

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Object Oriented Programming

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Encapsulation in Java

Array. Prepared By - Rifat Shahriyar

CS 101 Fall 2005 Midterm 2 Name: ID:

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Lecture 2: Java & Javadoc

PROGRAMMING FUNDAMENTALS

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

Advanced Programming - CS239

Object Oriented Programming. Java-Lecture 6 - Arrays

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 302 Week 9. Jim Williams

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Recitation 3 Class and Objects

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CS171:Introduction to Computer Science II

University of Palestine. Mid Exam Total Grade: 100

The Java language has a wide variety of modifiers, including the following:

To keep track of this new wrinkle we need some new variables at the Class level:

CHAPTER 7 OBJECTS AND CLASSES

DM550 Introduction to Programming part 2. Jan Baumbach.

H212 Introduction to Software Systems Honors

CSE 113 A. Announcements - Lab

10. Java Classes. Classes - Technical. Example: Earthquake catalog. Classes - Conceptual

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

Variables and Java vs C++

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

WES-CS GROUP MEETING #9

Lecture 5: Methods CS2301

Class 9: Static Methods and Data Members

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Programming Basics. Digital Urban Visualization. People as Flows. ia

Java Classes & Primitive Types

coe318 Lab 1 Introduction to Netbeans and Java

St. Edmund Preparatory High School Brooklyn, NY

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

Following is the general form of a typical decision making structure found in most of the programming languages:

Lab Exercise 1. Objectives: Part 1. Introduction

Java Classes & Primitive Types

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

AP CS Unit 3: Control Structures Notes

AP Computer Science A Summer Assignment

Principles of Object Oriented Programming. Lecture 4

Lec 3. Compilers, Debugging, Hello World, and Variables

Object Oriented Design: Identifying Objects

public class Q1 { public int x; public static void main(string[] args) { Q1 a = new Q1(17); Q1 b = new Q1(39); public Q1(int x) { this.

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

*Java has included a feature that simplifies the creation of

User-built data types Mutable and immutable data

Object Oriented Programming

Topic 10: Introduction to OO analysis and design

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

APCS Semester #1 Final Exam Practice Problems

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

In this lab we will practice creating, throwing and handling exceptions.

C# Programming for Developers Course Labs Contents

Introduction Basic elements of Java

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

Check the entries in the home directory again with an ls command and then change to the java directory:

1.00 Lecture 7. Classes

Transcription:

Software and Programming 1 Lab 8: Use of classes, static class variables and methods 1st March 2018 SP1-Lab8-2018.pdf Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1

Lab 8 Objectives Understanding the encapsulation of objects. Use of the debugger to follow changes in objects. Understanding the difference between static (belongs to the class) and non-static (belongs to the object) variables and methods. Scan program descriptions and break them down into the correct code. Note: Marked exercises 6.1, 6.2 & 6.3 (Bicycle/BicycleTest) are a single marked exercise and you are required to complete them, and show the finished program, explaining how they work together, by the 15 th of March. Failure to complete ALL exercises by this date will mean you will not receive the full coursework marks. 2

Exercise 1: T09 To complete this exercise you will need to download the code from the following URL: http://www.dcs.bbk.ac.uk/~roman/sp1/extra/t09.java Once the code is downloaded, right click on the file and choose edit with notepad++ from the contextual menu. Copy this code into a newly created class T09 in BlueJ. Do not run the code! 3

Exercise 1: T09 (2) Do not run the code! First, analyse the code and write down below what you think will be printed out: Compile the code, and then choose breakpoints that you can use with the BlueJ debugger to see when values change within the objects/class. Now, run the main method and check the results. 4

Marked Exercise 6.1: Bicycle/BicycleTest Write a program that enters bicycles into a system designed in a similar way to Boris Bikes. When new bicycles are entered, they will automatically be assigned a unique ID, but will not be at a pick up point (docking station). You will have to assign a docking station ID to each new bicycle entered. Enter three bicycles into your program. Print out each bicycle s ID number and location. If the location is set at zero, it is presumed the bicycle is out for hire. Finally, print out the number of bicycles in the system. 5

Marked Exercise 6.1: Bicycle/BicycleTest (2) Break down the previous instructions! new bicycles are entered [new objects] automatically be assigned a unique ID [unique ID linked to a static class variable?] will not be at a pick up point [initialise dockingstationid with 0 in constructor]. assign a docking station ID to each new bicycle [an object method?]. three bicycles into your program [three objects] and so on 6

Marked Exercise 6.1: Bicycle/BicycleTest (3) The terminal window running the BicycleTest main method should look similar to the following: 7

Marked Exercise 6.1: Bicycle Class (1) The Bicycle class should have the following variables: Private data: Two instance variables ID to represent a unique ID of the bicycle. dockingstationid to represent an ID of the docking station if the bicycle is parked or 0 if it is in use. One class variable lastassignednumber to store the last number assigned to a newly created instance of Bicycle. 8

Marked Exercise 6.1: Bicycle Class (2) The Bicycle class should have the following four instance methods: 1. getid should return bicycle's ID. 2. pickup, with no parameters, should check whether the bicycle is parked and, if it is available for hire, mark it as being used and return true; otherwise, it should return false. 3. park, with a docking station ID as its parameter, should check whether the bicycle is currently hired and if so, mark it as docked at the provided station ID and return true; otherwise, it should return false. Note: This method will be used to assign a docking station for each new Bicycle object. 9

Marked Exercise 6.1: Bicycle Class (3) 4. getdockingstationno, with no parameters, should check the dockingstationid, and return a String with the value: " is in use " if the dockingstationid is 0 or " is at " + dockingstationid otherwise. The Bicycle class should also have the following class method: getnumberofbicycles with no parameters, which should return the number of bicycles in the system 10

Bicycle Class Skeleton public class Bicycle { /* To Do: declare private data, i.e. instance variables (ID and dockingstationid) and declare and initialise class variable (lastassignednumber) */ /* Instance methods (public interface) */ public int getid() { /* To Do */ } public boolean pickup() { /* To Do */ } public boolean park(int docksid) { /* To Do */ } public String getdockingstationno() { /* To Do */ } 11

Bicycle Class Skeleton (2) /* Class method */ public static int getnumberofbicycles() { /* To Do */ } /* Constructor */ public Bicycle() { } // Assign an ID to created instance // Initialise dockingstationid } // end of class Bicycle 12

BicycleTest Class Skeleton //Scanner import public class BicycleTest { public static void main(string[ ] args) { // create new Scanner object: in // declare int variable to use to assign values from in // create instance of Bicycle: bicycle1 //get user input for docking station //call park method to assign docking station // Repeat creation and assignment for 2 more objects 13

BicycleTest Class Skeleton (2) //print bicycle id and status for bicycle1 using instance //methods getid() & getdockingstationno() // repeat print code above for other objects //print number of bicycles in system using the method // getnumberofbicycles() } //end method } //end BicycleTest 14

Marked Exercise 6.2: BicycleTest continued Modify the BicycleTest class to include the other instance method implemented in the Bicycle Class: pickup() The pickup() method should check whether a bicycle is docked, and if it is, allow the bicycle to be removed from the dock to be used. Call this method, and the park() method at least twice in the BicycleTest class and add extra prints to the terminal to indicate the docked and undocked status. 15

Marked Exercise 6.2: Bicycle/BicycleTest The terminal window running the BicycleTest main method should now look similar to the following: 16

Marked Exercise 6.3: BicycleTest continued Create an additional reusable method getrandomnumber() that creates a random number that replaces the user input. Use this for the argument sent to the park() instance method after each instance of Bicycle is created. getrandomnumber() should have two parameters, int min and int max, and should return a random int value within the min/max range. For the purposes of the Bicycle program values between 0 and 10 will suffice. Hint: Consider searching on the Internet how one can generate random numbers with the Java libraries. 17

Marked Exercise 6.3: Bicycle/BicycleTest The terminal window running the BicycleTest main method should now look similar to the following: 18