Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Tutorial 7 Solutions, Exercises of Engineering

Solution: Static methods may be called without instantiating an object. Therefore, an instance variable may not have an assigned value when a static method is ...

Typology: Exercises

2022/2023

Uploaded on 02/28/2023

laskhminaran
laskhminaran 🇺🇸

4.7

(6)

224 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tutorial Questions Semester 1, 2020
Tutorial 7 Solutions
You will be expected to engage with the tutor and discuss solutions to the problems below.
1. Explain why a static method cannot refer to an instance variable.
Solution:
Static methods may be called without instantiating an object. Therefore, an instance variable may not
have an assigned value when a static method is called, so a static method cannot reference it.
2. Explain why method overloading is useful.
Solution:
Method overloading is useful because it allows multiple methods to have the same name as long as they
have different signatures (method name and parameter list [types, number, order]). This allows for more
flexibility in choosing identifiers for methods, since the alternative would require the programmer to have
different method identifiers for every method that took in a different set of parameters.
3. Method Overloading
i. Write a method called average that accepts two integer parameters and returns their
average as a floating point value.
Solution:
pu b li c do u bl e av e ra g e ( i nt nu m1 , i nt nu m 2 ) {
re tu rn ( n um 1 + num2 ) / 2.0;
}
ii. Overload the average method of Exercise 3.i such that if three integers are provided as
parameters, the method returns the average of all three.
Solution:
pu bl ic do ub le av er ag e ( in t nu m1 , i nt n um2 , in t n um3 ) {
re tu rn ( n um 1 + num2 + nu m3 ) / 3. 0;
}
iii. Overload the average method of Exercise 3.i to accept four integer parameters and
return their average.
Solution:
pu bl ic do ub le av er ag e ( in t nu m1 , i nt n um2 , in t nu m3 , i nt nu m4 ) {
re tu rn ( n um 1 + num2 + nu m3 + n um 4 ) / 4.0 ;
}
iv. Write a method called multiConcat that takes a string parameter called text and an
integer parameter called count. Return a string that consists of text concatenated with
itself count times. For example, if the parameter values are "hi" and 4, the return
value is "hihihihi". Return the original string if the integer parameter is less than 2.
COMP1102/8702 Computer Programming 1, CHM & LJS
College of Science & Engineering, Flinders University
34
pf3

Partial preview of the text

Download Tutorial 7 Solutions and more Exercises Engineering in PDF only on Docsity!

Tutorial 7 Solutions

You will be expected to engage with the tutor and discuss solutions to the problems below.

1. Explain why a static method cannot refer to an instance variable.

Solution:

Static methods may be called without instantiating an object. Therefore, an instance variable may not

have an assigned value when a static method is called, so a static method cannot reference it.

2. Explain why method overloading is useful.

Solution:

Method overloading is useful because it allows multiple methods to have the same name as long as they

have different signatures (method name and parameter list [types, number, order]). This allows for more

flexibility in choosing identifiers for methods, since the alternative would require the programmer to have

different method identifiers for every method that took in a different set of parameters.

3. Method Overloading

i. Write a method called average that accepts two integer parameters and returns their

average as a floating point value.

Solution:

public double average ( int num1 , int num2 ) { return ( num1 + num2 ) / 2.0; }

ii. Overload the average method of Exercise 3.i such that if three integers are provided as

parameters, the method returns the average of all three.

Solution:

public double average ( int num1 , int num2 , int num3 ) { return ( num1 + num2 + num3 ) / 3.0; }

iii. Overload the average method of Exercise 3.i to accept four integer parameters and

return their average.

Solution:

public double average ( int num1 , int num2 , int num3 , int num4 ) { return ( num1 + num2 + num3 + num4 ) / 4.0; }

iv. Write a method called multiConcat that takes a string parameter called text and an

integer parameter called count. Return a string that consists of text concatenated with

itself count times. For example, if the parameter values are "hi" and 4 , the return

value is "hihihihi". Return the original string if the integer parameter is less than 2.

COMP1102/8702 Computer Programming 1, CHM & LJS

Solution:

public String multiConcat ( String text , int count ) { String result = text ; if ( count > 1) for ( int i = 2; i <= count ; i ++) result += text ; return result ; }

v. Overload the multiConcat method from Exercise 3.iv such that if the count parameter

is not provided, the method returns the string concatenated with itself. For example,

if the parameter is "test", the return value is "testtest"

Solution:

public String multiConcat ( String text ) { return text + text ; }

4. Interfaces

i. Can a class implement two interfaces that each contains the same method signature?

Explain.

Solution:

Yes. The class which implements an interface provides method implementations for each of the

abstract methods defined in the interface. In satisfying the requirements for a method of one

interface, the class would simultaneously satisfy the requirements for a method with the same

signature in the other interface.

ii. Create an interface called Visible that includes two methods:

makeVisible and makeInvisible. Both methods should take no parameters and should

return a boolean result. Describe how a class might implement this interface.

Solution:

public interface Visible { public boolean makeVisible (); public boolean makeInvisible (); }

A class implementing Visible would include an implements clause in the class header, such as:

public class ControlPanel implements Visible

The class would contain, among other things, two methods with signatures that match those specified

in the interface.

iii. Imagine a game in which some game elements can be broken by the player and others

cannot. Create an interface called Breakable that has a method called break that

takes no parameters and another method called broken that returns a boolean result

indicating whether the object is currently broken.

Solution:

public interface Breakable { public void break (); public boolean broken (); }

iv. Write a Java interface called Priority that includes two methods: setPriority and

getPriority. The interface should define a way to establish numeric priority among a

COMP1102/8702 Computer Programming 1, CHM & LJS