Homework Assignment 2 (100 + 10 Points.) : Deadline: Sat. Jul. 1st, 2023, 9:00 AM

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

American University of Armenia, CSE

CS120 Intro to OOP Sections A


Summer 2023
Homework Assignment 2 (100 + 10 points.)
Deadline: Sat. Jul. 1st, 2023, 9:00 AM

● Please upload your submissions as zip files containing .java source files to
Moodle.

● Organize your solutions such that each problem comes in its own class and
name your classes accordingly, e.g. P1, P2 or One, Two, etc.

● Instances of academic dishonesty will result in the forfeiture of the entire


submission.

1. (10 points) Write a Java program that reads a user given integer in the range of 1 to
8 and outputs both of its ordinal forms in the following manner:

Input: 2
Output: Second
2nd

2. (15 points) Write a Java program that prints out isosceles right triangles of side n
with the following arrangement, with parameter n being a positive number.

Example for n = 5.

# ##### ##### #
## #### #### ##
### ### ### ###
#### ## ## ####
##### # # #####

Note how each line contains a portion of all four triangles.

The solution must not rely on arrays, matrices, etc. Loops and print statements are
perfectly sufficient for achieving this effect.
3. (15 points) Write a java program that takes an integer and reverses it horizontally.

Input: 2305
Output: 5032

For full points, the type of the result must be int and your solution must solely rely
on arithmetic operations and must not use Strings, arrays, etc.

4. (10 points) Write iterative and recursive versions of a Java method that takes a string
and produces its reverse.

String reverse(String s)

5. (15 points) Write two versions of a Java program that inputs and processes the
exam scores of a class of n students, according to the given example. Exams scores
are floating point numbers within the range of 0 to 20. An error message should
inform the user about out-of-range inputs and prompt from re-entry. The program
then outputs the following information:

● The highest and the lowest scores and their indices


● The average score in the class
● Score difference between the highest and the lowest scores (range)

The first (FiveA) version of the solution relies on arrays.

The second version (FiveB) performs the same task without the use of arrays.

This example illustrates the interaction of the user with the program.

Enter the number of students: 5


Enter exam score (1/5): 16
Enter exam score (2/5): 14
Enter exam score (3/5): 20
Enter exam score (4/5): 18
Enter exam score (5/5): 20

The highest score is 20, at position 4


The lowest score is 14, at position 1
The average score is 17.6
The range is 6.

In case of duplicate values, both the old one and the new one are acceptable.

BONUS. (10 points) Rewrite both programs such that they work without knowing n in
advance. Print instructions for the users and tell them how they should use the
program.
6. (15 points) Write a Java method that takes a natural number n and returns its prime
factors as an array. For example, for argument 60 your method should produce the
array {2, 2, 3, 5}.

You may define and use additional utility methods, for example a method with the
following heading that adds an element to an array of integers:

int[] appendToArray(int[], int)


void printArray(int[])

7. (20 points) Write a Java method that takes an integer n (1 ≤ n ≤ 30) and returns an
n×n matrix filled with natural numbers from 1 to n2 arranged in a spiral order. For
example, the matrices for n = 3 and n = 4 would be:

9 8 7 16 15 14 13
2 1 6 5 4 3 12
3 4 5 6 1 2 11
7 8 9 10

You might also like