Write a java code about the following problem: Young Mirko threw matches all ove
ID: 3913169 • Letter: W
Question
Write a java code about the following problem: Young Mirko threw matches all over the floor of his room.His mom did not like that and ordered him to put all the matches in a box. Mirko soon noticed that not all of the matches on the floor fit in the box, so he decided to take the matches that don’t fit and throw them in the neighbour’s garbage, where his mom (hopefully) won’t find them.
Help Mirko determine which of the matches fit in the box his mom gave him. A match fits in the box if its entire length can lie on the bottom of the box. Mirko examines the matches one by one.
Input The first line of input contains an integer N (1?N?50 ), the number of matches on the floor, and two integers W and H , the dimensions of the box (1?W?100 , 1?H?100 ).
Each of the following N lines contains a single integer between 1 and 1000 (inclusive), the length of one match.
Output For each match, in the order they were given in the input, output on a separate line “DA” if the match fits in the box or “NE” if it does not.
Sample Input 1 Sample Output 1 5 3 4 3 4 5 6 7 DA DA DA NE NE Sample Input 2 Sample Output 2 2 12 17 21 20 NE DA
Write a java code about the following problem: Young Mirko threw matches all over the floor of his room.
His mom did not like that and ordered him to put all the matches in a box. Mirko soon noticed that not all of the matches on the floor fit in the box, so he decided to take the matches that don’t fit and throw them in the neighbour’s garbage, where his mom (hopefully) won’t find them.
Help Mirko determine which of the matches fit in the box his mom gave him. A match fits in the box if its entire length can lie on the bottom of the box. Mirko examines the matches one by one.
Input The first line of input contains an integer N (1?N?50 ), the number of matches on the floor, and two integers W and H , the dimensions of the box (1?W?100 , 1?H?100 ).
Each of the following N lines contains a single integer between 1 and 1000 (inclusive), the length of one match.
Output For each match, in the order they were given in the input, output on a separate line “DA” if the match fits in the box or “NE” if it does not.
Sample Input 1 Sample Output 1 5 3 4 3 4 5 6 7 DA DA DA NE NE Sample Input 2 Sample Output 2 2 12 17 21 20 NE DA
Young Mirko threw matches all over the floor of his room.
His mom did not like that and ordered him to put all the matches in a box. Mirko soon noticed that not all of the matches on the floor fit in the box, so he decided to take the matches that don’t fit and throw them in the neighbour’s garbage, where his mom (hopefully) won’t find them.
Help Mirko determine which of the matches fit in the box his mom gave him. A match fits in the box if its entire length can lie on the bottom of the box. Mirko examines the matches one by one.
Input The first line of input contains an integer N (1?N?50 ), the number of matches on the floor, and two integers W and H , the dimensions of the box (1?W?100 , 1?H?100 ).
Each of the following N lines contains a single integer between 1 and 1000 (inclusive), the length of one match.
Output For each match, in the order they were given in the input, output on a separate line “DA” if the match fits in the box or “NE” if it does not.
Sample Input 1 Sample Output 1 5 3 4 3 4 5 6 7 DA DA DA NE NE Sample Input 2 Sample Output 2 2 12 17 21 20 NE DA Young Mirko threw matches all over the floor of his room.
His mom did not like that and ordered him to put all the matches in a box. Mirko soon noticed that not all of the matches on the floor fit in the box, so he decided to take the matches that don’t fit and throw them in the neighbour’s garbage, where his mom (hopefully) won’t find them.
Help Mirko determine which of the matches fit in the box his mom gave him. A match fits in the box if its entire length can lie on the bottom of the box. Mirko examines the matches one by one.
Input The first line of input contains an integer N (1?N?50 ), the number of matches on the floor, and two integers W and H , the dimensions of the box (1?W?100 , 1?H?100 ).
Each of the following N lines contains a single integer between 1 and 1000 (inclusive), the length of one match.
Output For each match, in the order they were given in the input, output on a separate line “DA” if the match fits in the box or “NE” if it does not.
Sample Input 1 Sample Output 1 5 3 4 3 4 5 6 7 DA DA DA NE NE Sample Input 2 Sample Output 2 2 12 17 21 20 NE DA
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Matches.java
import java.util.Scanner;
public class Matches {
public static void main(String[] args) {
/**
* initializing scanner to read input from console, if you want, replace
* 'System.in' with a File object to read the input from a file
*/
Scanner scanner = new Scanner(System.in);
int N, W, H; // number of matches, width and height
// reading N, W and H
N = scanner.nextInt();
W = scanner.nextInt();
H = scanner.nextInt();
/**
* Finding the maximum match length that can be fit at the bottom of the
* box. In a rectangular box, the maximum length would be its diagonal.
* The diagonal can be found by applying pythagoras theorem, which is
* hypotenuse^2 = base^2 + altitude^2, where hypotenuse is the diagonal,
* base and altitude are width and height respectively
*/
double maxLength = Math.sqrt(W * W + H * H);
int matches[] = new int[N];//defining an array to store N matches lengths
for (int i = 0; i < N; i++) {
//reading matches lengths
matches[i] = scanner.nextInt();
}
//looping through all matches
for (int i = 0; i < N; i++) {
//checking if length is under maxLength
if (matches[i] <= maxLength) {
//possible
System.out.println("DA");
} else {
//not possible
System.out.println("NE");
}
}
}
}
/*Sample run output1 */
5 3 4
3
4
5
6
7
DA
DA
DA
NE
NE
/*Sample run output2 */
2 12 17
21
20
NE
DA
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.