You are on page 1of 3

Lasse Myllärinen Assignment 1 (3)

26.1.2022
Haaga-Helia University of Applied Sciences
Programming 1

Written assignment 2

Control structures

2.1 What is wrong in the code below? int i = 0; while (1 < 10) { System.out.println(i); }

Value one going to be always less than 10 so the loop never stops and
also the variable created above are not involved to the loop at all, so one
idea would make a for-loop by expression i < 10 and put the varaible i to
increase the value, so when the value becomes 10, the loop stop iterating.

2.2 What is the standard way to write a clean while loop?

int i = 0;
while (i < 10) {
System.out.print(”Enter an integer: ”);
int inNum = input.nextLine();
i = inNum;
}

2.3 By default, the program crashes when the code line below is executed. int age = Integer.par-
seInt("ten years"); Explain how we can prevent the program to crash when the code line above is exe-
cuted. Assume that we do not modify the code line. Methods

We can put the code above inside try, catch block.The catch clause would
include numberFormat exception type. If the exception occurs, the catch
block would take the control of running. It would be determined in the catch
block an instructions how to proceed/handle the exception.

Or then we would give the String value ”ten years” initially in form ”10” and
declare an integer type of a string, which would get the value ”years”..

2.4 What does passing parameters by value mean?

Passing parameter by value actually means that; The copy of the value
can be passed to a method as a parameter value. The method is called by
doing a method call/invoke. It is possible to pass by calling; a
primitive value and reference to object type of copies.

2.5 How does passing arrays as parameters differ from passing ints as parameters?

When passing arrays as parameters (you pass the copy of the reference of
an array), it is giving a possibility to access and change the original values
(in the array) for the method that is called. Instead of, when passing primiti-
vive type of values like an int., the called method cannot change the value
of an original variable, becouse it only gets the copy of a value of the va-
riable.
Lasse Myllärinen Assignment 2 (3)

26.1.2022
Haaga-Helia University of Applied Sciences
Programming 1

2.6 What is the purpose of the keyword void?

A keyword void determines that the method do not return anything. The
keyword void is in the method definition clause. If the method is wanted to
be returning something, then it is needed to put the datatype instead of the
keyword void in the definition clause.

2.7 What does method overloading mean?

The method overloading is; declaring two methods by same name/identi-


fier in one class with different parameter list. In this case when a program
is compiled, the java compiler chooses the relevent method based by the
matching of the parameter lists of the method and caller.

2.8 What are the two given rules of thumb for limiting the size of a method?

1. a code of one method should be able to fit in one a4 paper, if that is not
happening, you should consider to chop the method into two or more
extra methods and to make the code print neatly, you should not do too
long statements.
2. If in a method the number of inner control statements ex. loops, conditi-
onals goes over three and the inner/nested statements are not very
short and simple, then should write a new private method and moving
the innermost and possible more nested control statements/structures
to the new method.

2.9 What does 'method library class' mean? How does a method library class differ from a 'program
class'? What kind of benefit we can get by creating and using 'method library classes'?

Method library class is a ready made class in Java Class Library. These
kind of classes are for general use and because they consists methods
and code which are needed often, it is using of them saves time and ma-
king less the code needed to be written. This kind of classes you can use
in your program classes which you have made yourself.

2.10 In your program, what are the two possible ways to use a class from another namespace

You can import from different namespace ( = package = scope) classes


by; using import directive ex. import java.text.DecimalFormat; or refer to
the class with its namespace ex. java.text.DecimalFormat twoDecimal =
new java.text.DecimalFormat(”0.00”);
Lasse Myllärinen Assignment 3 (3)

26.1.2022
Haaga-Helia University of Applied Sciences
Programming 1

References Silpiö Kari, Moodle learning material, 2021, Eclipse quickstart, HH Moodle,
viewed 27.1.2022 <https://hhmoodle.haaga-helia.fi -> programming 1 ->
Course material>

You might also like