Inf1-OOP Tutorial: Exercises on Object-Oriented Programming, Schemes and Mind Maps of Design

Tutorial exercises on Object-Oriented Programming (OOP) for students, covering topics such as loops, arrays, variable scope, and database population. The exercises include coding challenges and design tasks related to OOP concepts, with a focus on Java programming language.

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 08/05/2022

nguyen_99
nguyen_99 🇻🇳

4.2

(80)

1K documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Inf1-OOP Tutorial Exercises
Release 0.3
Daniel Powell, Ewan Klein & Phil Scott
February 23, 2010
Contents
1 Inf1-OOP Tutorial: Week 1 ii
1.1 Introduction ......................................... ii
Exercise 1: Variable Scope ................................. ii
Exercise 2: Tracing a Loop ................................. ii
Exercise 3: Squares Loop Again .............................. ii
Exercise 4: Forwards and Backwards ............................ iii
Exercise 5: Division Revisited ............................... iii
2 Inf1-OOP Tutorial 2 (Week 4) iii
2.1 OO Design of Home Automation System ......................... iii
2.2 Scenario ........................................... iii
2.3 Issues ............................................ iv
2.4 Procedure .......................................... iv
3 Inf1-OOP Tutorial 3: Week 5 iv
3.1 Introduction ......................................... iv
Exercise 1: Wonky Code .................................. iv
Exercise 2: Populating a Database ............................. v
Exercise 3: Testing a Banking System, 1 .......................... v
Exercise 4: Testing a Banking System, 2 .......................... vi
4 Inf1-OOP Tutorial 5: Week 7 vi
4.1 Introduction ......................................... vi
Exercise 1: More Wonky Code ............................... vi
Exercise 2: Array Manipulation .............................. vii
Exercise 3: Common Members ............................... vii
Exercise 4: Abstract Syntax Tree .............................. ix
5 Indices and tables x
Contents:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Inf1-OOP Tutorial: Exercises on Object-Oriented Programming and more Schemes and Mind Maps Design in PDF only on Docsity!

Inf1-OOP Tutorial Exercises

Release 0.

Daniel Powell, Ewan Klein & Phil Scott

February 23, 2010

Contents

1 Inf1-OOP Tutorial: Week 1 ii

1.1 Introduction......................................... ii

Exercise 1: Variable Scope................................. ii

Exercise 2: Tracing a Loop................................. ii

Exercise 3: Squares Loop Again.............................. ii

Exercise 4: Forwards and Backwards............................ iii

Exercise 5: Division Revisited............................... iii

2 Inf1-OOP Tutorial 2 (Week 4) iii

2.1 OO Design of Home Automation System......................... iii

2.2 Scenario........................................... iii

2.3 Issues............................................ iv

2.4 Procedure.......................................... iv

3 Inf1-OOP Tutorial 3: Week 5 iv

3.1 Introduction......................................... iv

Exercise 1: Wonky Code.................................. iv

Exercise 2: Populating a Database............................. v

Exercise 3: Testing a Banking System, 1.......................... v

Exercise 4: Testing a Banking System, 2.......................... vi

4 Inf1-OOP Tutorial 5: Week 7 vi

4.1 Introduction......................................... vi

Exercise 1: More Wonky Code............................... vi

Exercise 2: Array Manipulation.............................. vii

Exercise 3: Common Members............................... vii

Exercise 4: Abstract Syntax Tree.............................. ix

5 Indices and tables x

Contents:

1 Inf1-OOP Tutorial: Week 1

1.1 Introduction

The following exercises build on the work you have carried out in the OOP lab sessions, and are designed

to be carried out within the tutorial sesssion. In other words, you are not expected to try to work out the

answers in advance of your tutorial.

Exercise 1: Variable Scope

Consider the following program:

public class Repeat {

public static void main(String[] args) { int num = 0; for ( int count = 0; count < 20; count++) { num = count; System.out.print(num); } System.out.println(num); System.out.println(count); } }

Will this compile? Explain your answer.

Exercise 2: Tracing a Loop

What will this code print out to the terminal?

public class TraceLoop {

public static void main( String [] args ) { int j = 0; for ( int i = 1; i < 5; i++) { j = j + i; System.out.println("i is: " + i); System.out.println("j is: " + j); System.out.println(); } } }

Exercise 3: Squares Loop Again

In Lab 2, Exercise 3, you were asked to use a for loop as part of a program to display the square of the

numbers from 1 to 10, as shown below:

different temperature than a “hot shower” for Melinda. The system can play appropriate music in the

shower, and knows what are Melinda’s current hot favourites on her Zune MP3 Player.

You can extend or modify this scenario as you think appropriate.

2.3 Issues

Here are some initial questions:

  • what are the important classes that the home automation system needs to know about?
  • what methods will objects of those classes need to make available to the system?
  • what state will objects of those classes need to contain?

2.4 Procedure

First, try to agree a very high-level overall design within the whole group. Second, assign subgroups to

work on the details of particular classes; each group should try to end up with more detailed specification

of methods, and how these relate to the state of the object. Finally, come together as a group to present

and compare results.

3 Inf1-OOP Tutorial 3: Week 5

3.1 Introduction

The following exercises build on the work you have carried out in the OOP lab sessions, and are designed

to be carried out within the tutorial sesssion. In other words, you are not expected to try to work out the

answers in advance of your tutorial.

Exercise 1: Wonky Code

The following piece of code contains a number of errors. What are they?

public class Wonky {

int n1; double n2; String s;

public int foo(String[] aString) { int n3; String s2; aString[42] = s + n3; if (aString.length <= 3) { return aString[3]; } else { n1 = Integer.parseInt(s); return n2; } return n1 + n3;

public static void main(String[] args) { int [] anArray = { 3, 4, 5 }; anArray[0] = ( double ) 5; System.out.println("The value of foo is " + foo(anArray, anArray[n3]); } }

Exercise 2: Populating a Database

Consider the following scenario. You are designing a web interface to an online banking system, where

input typed into a web form is used to insert entries into a database. In order to simplify the issue, let’s

make the following assumptions when you are building your first prototype:

1. Input is captured from the terminal using a Scanner object, rather than from a web form.

2. You are going to start off with a piece of code that transfers money from your account to someone

else’s account — we’ll call the person who the money is transferred to the beneficiary.

3. The values captured by the Scanner are used to build an SQL statement.

Here is some sample code for doing this:

import java.util.Scanner ;

public class InsertRow {

public static void main(String[] args) { String name; int amount;

Scanner input = new Scanner(System.in); System.out.print("Please enter beneficiary’s name and amount to transfer"); name = input.next(); amount = input.nextInt(); String sql = "INSERT\nINTO Transfers (beneficiary, amount)\nVALUES (" + name + " System.out.println(sql); } }

Consider possible incorrect inputs that a user might give, either by mistake or intentionally. How robust

is this code likely to be, in your judgement?

Exercise 3: Testing a Banking System, 1

You have the task of implementing a bank account class Account which has methods balance() for

getting the current balance; withdraw(int w) for withdrawing money; and deposit(int d) for

depositing money. You have decided that you are first going to write a TestAccount with a main()

method for testing your code. Once you have your testing code, you will write the implementation and

check that it meets the tests. Sketch out what your TestAccount class will look like.

public class SumOddsEvens { public static void main( String [] args ) { int sumEven = 0; int sumOdd = 0; for ( int i = 0; i < args.length; i++ ) { int temp = Integer.parseInt(args[i]); if ( temp % 2 == 0 ) { sumEven += temp; }

else { sumOdd += temp;}} System.out.println("The sum of the even numbers is: " + sumEven); System.out.println("The sum of the odd numbers is: " + sumOdd); } }

Exercise 2: Array Manipulation

Suppose you have a partially-filled array values, where the index of the last value is given by the

variable valuesSize. For example, you might have values be [3, 3, 55, 5, 2, 7, 22,

0, 0, 0] where valuesSize is 6. Explain what the following two snippets of code are intended to

achieve.

for ( int i = pos; i < valuesSize; i++) { values[i] = values[i + 1]; } valuesSize--;

if (valuesSize < values.length) { for ( int i = valuesSize; i > pos; i--) { values[i] = values[i - 1]; } values[pos] = elem; valuesSize++; }

Exercise 3: Common Members

You are designing the DoItAll content management system (CMS), which allows users to upload files

of different formats over the web and then make them available on their web pages. You are going to

design a set of classes to associate with these file formats, and instances of these various classes will

possess various methods which determine how they behave when uploaded. Many of these methods

are listed below. On the basis of shared behaviour, consider what abstractions you could make, using

inheritance and / or interfaces. Feel free to add some more methods or more file formats! Draw a UML

diagram of the relationships you come up with.

PDF document:

getTitle() setTitle(String)

showRelated() getFileSize() addAnnotation(String)

HTML document:

getTitle() setTitle(String) showRelated() getFileSize() compress(Float) // arg provides the compression ratio convertToPDF()

Word document:

getTitle() setTitle(String) showRelated() getFileSize() compress(Float) convertToPDF() convertToHTML()

JPEG image:

getTitle() setTitle(String) getFileSize() makeThumbnail() getResolution()

SVG image:

getTitle() setTitle(String) getFileSize() makeThumbnail() getDTD() addEventHandler(Action)

MP3 audio file:

getTitle() setTitle(String) getFileSize() getBitRate() getPlayTime()

WMA audio file:

getTitle() setTitle(String) getFileSize() getBitRate()

5 Indices and tables

  • Index