COSC 236
Assignment 2
Due date: Thursday, 03/26/2020
Make sure you use
proper style when you name variables/constants and align statements. Do not
forget to put your name in the top comments of each of your programs. The
format of your programs is very important when learning a programming language
and from now on, bad style will be penalized with up to 50%.
For each program, you
will turn in the printouts with the source file and the output window (a
must!). You have to staple all pages together. You should also be prepared to
make a demo of your programs if you are asked so.
1. Write a Java program that first
reads a positive integer from the user- let's call it howMany. Then the
program reads howMany
pairs of integers - let's call them n1 and n2.
For each pair, the program determines if the first component in the pair is a
multiple of the second component, in other words, if n1
is a multiple of n2. For example if howMany is 5, your program will read 5
pairs of integers, and for each pair (n1,
n2) it will decide if n1
is a multiple of n2.
Sample output:
How many numbers in
input? -4
ERROR! Should be
positive. REENTER: 5
Enter a pair of
positive integers: 10 3
10 is NOT a multiple
of 3.
Enter a pair of
positive integers: -1 7
ERROR! Both should be
positive. REENTER: 15 2
15 is NOT a multiple
of 2.
Enter a pair of
positive integers: 81 3
81 is a multiple of 3.
Enter a pair of
positive integers: 121 11
121 is a multiple of
11.
Enter a pair of
positive integers: 12 -7
ERROR! Both should be
positive. REENTER: 256 16
256 is a multiple of
16.
2. Write a Java
program that takes input 3 floating-point numbers and sends output the maximum
and minimum value. Find the most efficient solution. The processing will
take place in a sentinel controlled loop, like in the following sample output:
Sample output:
Min/Max problem for
3 floating-point numbers
============================================
Do you want to start
(Y/N): y
Enter 3 float values:
1.2 2.3 3.4
You entered 1.2, 2.3, 3.4
Min = 1.2
Max = 3.4
Do you want to
continue (Y/N): y
Enter 3 float values:
2.3 1.2 4.5
You entered 2.3, 1.2, 4.5
Min = 1.2
Max = 4.5
Do you want to
continue (Y/N): y
Enter 3 float values:
4.5 3.4 1.2
You entered 4.5, 3.4, 1.2
Min = 1.2
Max = 4.5
Do you want to continue (Y/N): n
3. Write a Java program that will
input integer values from the user. The user will enter a sequence of integer
values in a loop that will go for maximum 10 iterations (declare a class
constant for 10) but it will stop if the user is entering a negative
value. The program computes the sum and the average of all positive numbers in
the sequence.
HINT: You will need to implement a
loop that is both count-controlled (has up to 10 iterations) and
event-controlled (stops earlier if a negative value is entered).
OUTPUT
(3 SAMPLES):
SAMPLE
#1
Enter a number,
negative to STOP: 1
Enter a number,
negative to STOP: 2
Enter a number,
negative to STOP: 3
Enter a number,
negative to STOP: 4
Enter a number,
negative to STOP: 5
Enter a number,
negative to STOP: 6
Enter a number,
negative to STOP: -8
===============================
You entered 6 positive
numbers.
The sum = 21
The average = 3.50
SAMPLE
#2
Enter a number,
negative to STOP: -1
ERROR! Division by 0.
No input.
SAMPLE
#3
Enter a number,
negative to STOP: 1
Enter a number,
negative to STOP: 2
Enter a number,
negative to STOP: 3
Enter a number, negative
to STOP: 4
Enter a number,
negative to STOP: 5
Enter a number,
negative to STOP: 6
Enter a number,
negative to STOP: 7
Enter a number,
negative to STOP: 8
Enter a number,
negative to STOP: 9
Enter a number,
negative to STOP: 10
================================
You entered 10
positive numbers.
The sum = 55
The average = 5.50
4. Use nested for
loops to draw boxes of "*"s with a diagonal of "0"s.
The number of rows and columns are equal and they should be input from the user
(valid range: 5 to 21).
Sample output:
Drawing program
Do you want to start (Y/N)?
y
How many rows/columns
(5-21)? 7
0******
*0*****
**0****
***0***
****0**
*****0*
******0
Do you want to
continue (Y/N)? Y
How many rows/columns
(5-21)? 55
ERROR! OUT OF RANGE (5
- 21). REENTER: 5
0****
*0***
**0**
***0*
****0
Do you want to continue(Y/N)? N
Note
on Academic Honesty and Collaboration: This should be your own work. Do not ask the TA or CIS tutors to
do the assignment for you. Do not allow your work to be used by others, you
will get the same penalty: 0 for the assignment and I will report you to
Academic Standards and Student Conduct Committees.
Warning: If someone cheats by using
your work, you will also be penalized.