Last Java programming questions

  • Thread starter Thread starter JasonJo
  • Start date Start date
  • Tags Tags
    Java Programming
AI Thread Summary
The discussion focuses on creating a Java program that prompts the user for five integers between 0 and 9 and generates all possible permutations of those integers using a method called "permutations." Participants suggest using five nested loops to explore all combinations, storing the integers in an array, and addressing how to handle duplicate permutations. There is also a challenge to modify the program to eliminate duplicates in the output. Insights are shared on managing array indices and the potential use of a second array for temporary storage during permutation generation. The conversation emphasizes the importance of understanding the logic behind permutations in programming.
JasonJo
Messages
425
Reaction score
2
Your task is to create a Java program with the name that asks the user for 5 integers in the main method that are between 0 and 9, inclusive. You should then write a method called “permutations” which accepts these 5 integers and prints all possible permutations for those numbers. (Hint: You will need 5 nested loops and an array to do this. Store the 5 numbers in an array inside the method and use the 5 loops to generate all possible array position combinations). Next, after this method is called, generate all possible permutations for (9 – 1st number), (9 – 2nd number), (9 – 3rd number), (9 – 4th number), and (9 – 5th number). For example, if the first time you entered the numbers (5, 6, 7, 8, 9), the second time the method is called the numbers will be (4, 3, 2, 1, 0). It is okay for this assignment if there are duplicate permutations that are printed.

And the next part is do the same problem but make sure there are no duplicate permutations.

i don't really know any discrete math, so any insight to this problem would be great.

i tried to conceptualize it in terms of switching indices of an array, but i don't quite know how to formulate it.
 
Physics news on Phys.org
First, place the first element. This is your outer loop and you can place the outer element in five different positions: 0, 1, 2, 3, or 4. The outer loop counter can represent the current position of the first element.

Then, place the second element. The second element can have four different positions because when you come to the position where you already have the first element, you have to skip it for the second element.

Proceed similarly for the other elements.
 
Actually, that method pretty much requires that you use a second array to store the temporary permutation as you go. If you want to just print it immediately without storing it again, then you can modify the method so that the x'th loop counter refers to the array index of the element to go at the x'th location.
 
I like BicycleTree's suggestion, unless you're required to use nested loops for the learning value. Then again, you can use nested loops to print out a list of permutations of the letters a,b,c,d,e and then copy them into your program, having it store values for each letter. Perhaps you'll get extra credit for making it run faster :)
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'Calculation of Tensile Forces in Piston-Type Water-Lifting Devices at Elevated Locations'
Figure 1 Overall Structure Diagram Figure 2: Top view of the piston when it is cylindrical A circular opening is created at a height of 5 meters above the water surface. Inside this opening is a sleeve-type piston with a cross-sectional area of 1 square meter. The piston is pulled to the right at a constant speed. The pulling force is(Figure 2): F = ρshg = 1000 × 1 × 5 × 10 = 50,000 N. Figure 3: Modifying the structure to incorporate a fixed internal piston When I modify the piston...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...

Similar threads

Replies
7
Views
3K
Replies
4
Views
5K
Replies
21
Views
3K
Replies
16
Views
2K
Replies
13
Views
4K
Back
Top