Find Us On:

# assignment java

tsega111

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 9 81 is a multiple of 9. 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. Try to 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 Attachments
Tags
Clarifications

## Java - Assignments - Solution

dickersonka

Attached is the source including comments for the how many, minimum maximum, positive number, and box assignments. Attachments
JavaAssignments.zip (2 K)
Preview
you want to start (Y/N): "); //Get input String cont = input.next(); //Continue looping, case insensitive compare while(cont.toUpperCase().equals("Y")){ //Prompt for float values System.out.print("Enter 3 float values: "); //Input float values float f1 = input.nextFloat(); float f2 = input.nextFloat();

Preview
Positive { public static void main(String[] args){ //Input Scanner input = new Scanner(System.in); //Prompt first time System.out.print("Enter a number, negative to STOP: "); //Input, Count, and sum int num = 0; int count = 0; int sum = 0;

Preview
* if(col == zeroPos){ System.out.print("0"); } else { System.out.print("*"); } } //increment zero position zeroPos++; //line break System.out.println(); } System.out.println(); //prompt to continue System.out.print("Do you want to continue (Y/N)? "); cont = input.nextLine();

Preview
//Check if both inputs are positive bothPositive = (n1 >= 0) && (n2 >= 0); if(!bothPositive){ //Display error System.out.print("ERROR! Both should be positive. REENTER: "); } else { //Check if n1 is multiple of n2 boolean isMultiple = ((n1 % n2)

Screenshots