Software and Programming 1

Similar documents
Software and Programming 1

Software and Programming 1

Software and Programming 1

Create a Java project named week10

Object-Oriented Programming in Java

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

Week 5-1: ADT Design

Software and Programming 1

Instance Method Development Demo

Software and Programming 1

Informatik II (D-ITET) Tutorial 6

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Distributed Systems Recitation 1. Tamim Jabban

An example from Zoology

Informatik II Tutorial 6. Subho Shankar Basu

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

AP Computer Science. Interactions with superclass

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

Chapter 6 Lab Classes and Objects

Lecture 15: Inheritance II

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Distributed Systems Recitation 1. Tamim Jabban

Abstract class & Interface

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

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

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

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Programming II (CS300)

C08: Inheritance and Polymorphism

CS 302 Week 9. Jim Williams

Programming II (CS300)

Software and Programming 1

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

Lesson 14: Abstract Classes and Interfaces March 6, Object-Oriented S/W Development with Java CSCI 3381

Overview of Eclipse Lectures. Module Road Map

CS1083 Week 2: Arrays, ArrayList

Classes. Classes as Code Libraries. Classes as Data Structures

What is Inheritance?

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

CS 112 Introduction to Programming

Admin. q Admin and recap q Class inheritance

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

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

EXAMINATION FOR THE BSC (HONS) INFORMATION TECHNOLOGY; BSC (HONS) INFORMATION SYSTEMS & BSC (HONS) COMPUTER SCIENCE; YEAR 1

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS1004: Intro to CS in Java, Spring 2005

Chapter 6 Lab Classes and Objects

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

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

CS 201, Fall 2016 Sep 28th Exam 1

Object-Oriented Programming (Java)

Encapsulation in Java

Objects as a programming concept

Final Examination Semester 3 / Year 2010

Inheritance and Subclasses

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

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

C08: Inheritance and Polymorphism

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Fundamentals of Programming Data Types & Methods

Exam Duration: 2hrs and 30min Software Design

Classes and Inheritance Extending Classes, Chapter 5.2

CMSC 132: Object-Oriented Programming II. Inheritance

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Static, Final & Memory Management

COE 211/COE 212 Computer Programming/Engineering Programming. Welcome to The Comprehensive Final Exam Monday June

Notes on Chapter Three

Programming Exercise 14: Inheritance and Polymorphism

Robots. Byron Weber Becker. chapter 6

Readings for This Lecture

1.00 Lecture 13. Inheritance

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2

Final Exam Practice. Partial credit will be awarded.

CSCE 145 Exam 2 Review No Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Polymorphism. return a.doublevalue() + b.doublevalue();

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming. Java-Lecture 1

CO Java SE 8: Fundamentals

MIDTERM REVIEW. midterminformation.htm

Lab 05 Step-by-step Guide to ArrayList, Inheritance, Singleton Q1 (15 min) => Q2 (25 min) => Q3 (10 min) => Q4 (10 min) => Q5 (10 min) => Q6,7(?

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

Course Supervisor: Dr. Humera Tariq Hands on Lab Sessions: Ms. Sanya Yousuf

Chapter 13 Abstract Classes and Interfaces

Assignment 8B SOLUTIONS

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

Lab Exercise 1. Objectives: Part 1. Introduction

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

Exploring the Java API, Packages & Collections

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Chapter 6 Class and Method

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Transcription:

Software and Programming 1 Week 9 Lab - Use of Classes and Inheritance 8th March 2018 SP1-Lab9-2018.ppt Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1

Lab 9: Objectives Exercise 1 Student & StudentTest classes 1. Reinforce Code Writing i. Class structure ii. Accessor (getter) / mutator (setter) methods. 2. Override a method 3. Use of array to create multiple objects 4. Use of Enhanced For Loop / String comparison Exercise 2 Person, Tutor, Student & Test classes 1. Class Inheritance 2. Code Revision / Refactoring 2

1. RomanNumerals Lab 2 Marked Ex. 1 2. Powers Lab 3 Marked Ex.2 Coursework You need to have completed, and have shown to a lab demonstrator the first five marked exercises to be entered for the in-class-test in Week 11, 22 nd March. The exercises are as follows: 3. IntSequence Lab 3 Marked Ex.3 4. ArrayExercises Lab 4 Marked Ex. 4 5. CashRegister / CashRegisterTest Lab 7 Marked Ex. 5 In addition, to receive the 5% awarded for coursework on the module, you must also show the Bicycle/BicycleTest marked exercises 6.1, 6.2 and 6.3 from last week s lab 8 session. 3

Exercise 1: Classes Student and StudentTest Implement a class Student The class requires the following attributes: name, year of birth and programme of study 1. Write the Class declaration 2. Declare variables 3. Create constructor 4. Write three new methods: String getname(), int getyear() and String getprogramme() 5. Override the method String tostring() from the class Object Note: as there are no mutator methods, the data for instances must be supplied on creation (through the constructor). 4

Exercise 1: Classes Student and StudentTest (2) Implement a test class StudentTest The class StudentTest is required to: 1. Create an array of 10 Students. 2. Create instances and receive user input to provide data to each Student object Hint: Use a loop and a Scanner object 3. Once data input is complete, print out the names of students of the programme "BSc ISM" only. Use the enhanced for loop for this. 5

Structure of Student Class /** Student class **/ public class Student /* private data */ private String name; private int year; private String programme; /* methods (public interface) */ public String getname( ) /* To Do: write the code to return name */ 6

Structure of Student Class (2) public int getyear() /* To Do: write the code to return year */ public String getprogramme() /* To Do: write the code to return programme */ public String tostring() return "Student " + name + ", programme " + programme; 7

Structure of Student Class (3) /* Constructor */ public Student(String name, int year, String programme) /* assign values to instance variables on object instantiation */ // end of class Student 8

Testing Student Class import java.util.scanner; public class StudentTest public static void main(string[ ] args) Scanner input = new Scanner(System.in); /* To Do: Write code to */ 1. Declare and create an array of 10 Students. 2. Fill up the array by creating 10 instances of students where the name, year of birth and programme of study are entered at the keyboard. 9

Testing Student Class (2) /* To Do: Write more code to use an enhanced for loop to print out names of students of the programme BSc ISM only. */ // end of method main // end of class StudentTest Note: an example of the enhanced for loop can be found on slide 17 of the Inheritance & Subclasses presentation in week 8. 10

Exercise 2: Classes Person, Tutor, Student (revised) and Test Implement a class Person (a person has a name and a year of birth). Change the class Student so that it extends Person. Then implement another subclass, Tutor of Person (a tutor has a salary). For the classes, write the class declarations, the constructors and the methods tostring() for all classes. Implement a program that tests these classes & methods. 11

Inheritance hierarchy Person name: String year: int getname(): String getyear(): int tostring(): String Student programme: String getprogramme(): String tostring(): String Tutor salary: double getsalary(): double tostring(): String 12

Revised Student Class /** Revised Student class */ public class Student extends Person private String programme; public String getprogramme( ) /* To Do: write the code to return programme */ public String tostring() return "Student " + getname() + ", programme " + programme; 13

Revised Student Class (2) public Student(String name, int year, String programme) super(name, year); // calls the constructor of Person this.programme = programme; // end of class Student 14